1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #include <sofia-sip/sip_status.h>
14 #include <sofia-sip/su_log.h>
15 #include <sofia-sip/sdp.h>
16 #include <sofia-sip/sip_header.h>
17 #include <sofia-sip/stun.h>
18 #include <sofia-sip/stun_tag.h>
19 #include <sofia-sip/su_md5.h>
21 #ifndef SOFIA_SIP_GCC_4_8_PATCH_APLLIED
22 #warning ********************************************************
23 #warning Please apply the sofia-sip-gcc-4.8.patch !
24 #warning If this issue is already fixed, just remove this check.
25 #warning ********************************************************
31 unsigned char flip[256];
33 int any_sip_interface = 0;
35 //pthread_mutex_t mutex_msg;
36 su_home_t sip_home[1];
38 #define REGISTER_STATE_UNREGISTERED 1
39 #define REGISTER_STATE_REGISTERING 2
40 #define REGISTER_STATE_REGISTERED 3
41 #define REGISTER_STATE_FAILED 4
43 #define STUN_RETRY_TIMER 10, 0
44 #define REGISTER_RETRY_TIMER 10, 0
46 #define STUN_STATE_UNRESOLVED 1
47 #define STUN_STATE_RESOLVING 2
48 #define STUN_STATE_RESOLVED 3
49 #define STUN_STATE_FAILED 4
51 #define RTP_PORT_BASE 30000
52 #define RTP_PORT_MAX 39998
55 char interface_name[64];
57 char remote_peer[128];
58 char asserted_id[128];
61 char register_user[128];
62 char register_host[128];
63 nua_handle_t *register_handle;
64 struct lcr_timer register_retry_timer;
65 struct lcr_timer register_option_timer;
66 int register_interval;
69 char auth_password[128];
77 char stun_server[128];
78 stun_handle_t *stun_handle;
79 su_socket_t stun_socket;
80 struct lcr_timer stun_retry_timer;
83 unsigned short rtp_port_from;
84 unsigned short rtp_port_to;
85 unsigned short next_rtp_port;
89 static int delete_event(struct lcr_work *work, void *instance, int index);
90 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index);
91 static int load_timer(struct lcr_timer *timer, void *instance, int index);
96 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
99 if (interface->rtp_bridge)
101 p_s_sip_inst = interface->sip_inst;
102 memset(&p_s_delete, 0, sizeof(p_s_delete));
103 add_work(&p_s_delete, delete_event, this, 0);
106 memset(&p_s_auth_nonce, 0, sizeof(p_s_auth_nonce));
107 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
108 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
109 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
110 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
111 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
112 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
113 p_s_rtp_ip_local = 0;
114 p_s_rtp_ip_remote = 0;
115 p_s_rtp_port_local = 0;
116 p_s_rtp_port_remote = 0;
121 p_s_rtp_tx_action = 0;
122 p_s_rtp_is_connected = 0;
124 /* create option timer */
125 memset(&p_s_invite_option_timer, 0, sizeof(p_s_invite_option_timer));
126 add_timer(&p_s_invite_option_timer, invite_option_timer, this, 0);
127 p_s_invite_direction = 0;
130 memset(&p_s_load_timer, 0, sizeof(p_s_load_timer));
131 add_timer(&p_s_load_timer, load_timer, this, 0);
134 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
136 FATAL("No SIP instance for interface\n");
145 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
147 del_timer(&p_s_invite_option_timer);
148 del_timer(&p_s_load_timer);
149 del_work(&p_s_delete);
154 static const char *media_type2name(uint8_t media_type) {
155 switch (media_type) {
156 case MEDIA_TYPE_ULAW:
158 case MEDIA_TYPE_ALAW:
162 case MEDIA_TYPE_GSM_HR:
164 case MEDIA_TYPE_GSM_EFR:
173 static void sip_trace_header(class Psip *sip, const char *interface_name, const char *message, int direction)
175 struct interface *interface = NULL;
178 interface = getinterfacebyname(interface_name);
180 /* init trace with given values */
183 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
184 sip?sip->p_dialinginfo.id:NULL,
195 /* according to RFC 3550 */
197 #if __BYTE_ORDER == __LITTLE_ENDIAN
198 uint8_t csrc_count:4,
202 uint8_t payload_type:7,
204 #elif __BYTE_ORDER == __BIG_ENDIAN
215 } __attribute__((packed));
220 } __attribute__((packed));
222 #define RTP_VERSION 2
224 #define PAYLOAD_TYPE_ULAW 0
225 #define PAYLOAD_TYPE_ALAW 8
226 #define PAYLOAD_TYPE_GSM 3
228 /* decode an rtp frame */
229 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
231 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
232 struct rtp_x_hdr *rtpxh;
236 unsigned char *from, *to;
240 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
243 if (rtph->version != RTP_VERSION) {
244 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
247 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
248 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
249 if (payload_len < 0) {
250 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
251 "csrc count = %d)\n", len, rtph->csrc_count);
254 if (rtph->extension) {
255 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
256 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
257 "extension header\n");
260 rtpxh = (struct rtp_x_hdr *)payload;
261 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
263 payload_len -= x_len;
264 if (payload_len < 0) {
265 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
266 "extension header exceeds frame length\n");
271 if (payload_len < 0) {
272 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
276 payload_len -= payload[payload_len - 1];
277 if (payload_len < 0) {
278 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
279 "greater than payload\n");
284 switch (rtph->payload_type) {
286 we only support alaw and ulaw!
287 case RTP_PT_GSM_FULL:
288 if (payload_len != 33) {
289 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
290 "payload length != 33 (len = %d)\n",
296 if (payload_len != 31) {
297 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
298 "payload length != 31 (len = %d)\n",
303 case RTP_PT_GSM_HALF:
304 if (payload_len != 14) {
305 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
306 "payload length != 14 (len = %d)\n",
312 case PAYLOAD_TYPE_ALAW:
313 if (options.law != 'a') {
314 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
318 case PAYLOAD_TYPE_ULAW:
319 if (options.law == 'a') {
320 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
325 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
326 "type %d\n", rtph->payload_type);
330 if (payload_len <= 0) {
331 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
337 psip->record(payload, payload_len, 0); // from down
339 psip->tap(payload, payload_len, 0); // from down
344 if (psip->p_echotest) {
345 /* echo rtp data we just received */
346 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
350 *to++ = flip[*from++];
352 psip->dov_rx(payload, payload_len);
353 psip->bridge_tx(payload, payload_len);
358 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
360 class Psip *psip = (class Psip *) instance;
362 unsigned char buffer[256];
365 if ((what & LCR_FD_READ)) {
366 len = read(fd->fd, &buffer, sizeof(buffer));
368 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
369 // psip->rtp_close();
370 // psip->rtp_shutdown();
373 if (psip->p_s_rtp_is_connected)
374 rc = rtp_decode(psip, buffer, len);
380 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
382 // class Psip *psip = (class Psip *) instance;
384 unsigned char buffer[256];
386 if ((what & LCR_FD_READ)) {
387 len = read(fd->fd, &buffer, sizeof(buffer));
389 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
390 // psip->rtp_close();
391 // psip->rtp_shutdown();
394 PDEBUG(DEBUG_SIP, "rtcp!\n");
400 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
403 socklen_t alen = sizeof(*sin_local);
405 sin_local->sin_family = AF_INET;
406 sin_local->sin_addr.s_addr = htonl(ip);
407 sin_local->sin_port = htons(port);
409 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
413 /* retrieve the address we actually bound to, in case we
414 * passed INADDR_ANY as IP address */
415 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
418 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
421 socklen_t alen = sizeof(*sin_local);
423 sin_remote->sin_family = AF_INET;
424 sin_remote->sin_addr.s_addr = htonl(ip);
425 sin_remote->sin_port = htons(port);
427 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
429 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
433 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
436 int Psip::rtp_open(void)
438 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
440 // struct in_addr ia;
442 unsigned short start_port;
444 PDEBUG(DEBUG_SIP, "rtp_open\n");
447 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
453 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
455 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
461 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
464 ip = htonl(INADDR_ANY);
465 start_port = inst->next_rtp_port;
467 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, inst->next_rtp_port);
471 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, inst->next_rtp_port + 1);
473 p_s_rtp_port_local = inst->next_rtp_port;
474 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
477 /* reopen rtp socket and try again with next udp port */
478 unregister_fd(&p_s_rtp_fd);
479 close(p_s_rtp_fd.fd);
481 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
487 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
490 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
491 if (inst->next_rtp_port == start_port)
493 /* we must use rc2, in order to preserve rc */
496 PDEBUG(DEBUG_SIP, "failed to find port\n");
500 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
501 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
502 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
504 return p_s_rtp_port_local;
507 int Psip::rtp_connect(void)
512 ia.s_addr = htonl(p_s_rtp_ip_remote);
513 if (p_s_rtp_is_connected)
514 PDEBUG(DEBUG_SIP, "reconnecting existing RTP connection to new/same destination\n");
515 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
517 rc = rtp_sub_socket_connect(p_s_rtp_fd.fd, &p_s_rtp_sin_local, &p_s_rtp_sin_remote, p_s_rtp_ip_remote, p_s_rtp_port_remote);
521 rc = rtp_sub_socket_connect(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, &p_s_rtcp_sin_remote, p_s_rtp_ip_remote, p_s_rtp_port_remote + 1);
525 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
526 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
527 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
528 p_s_rtp_is_connected = 1;
532 void Psip::rtp_close(void)
534 if (p_s_rtp_fd.fd > 0) {
535 unregister_fd(&p_s_rtp_fd);
536 close(p_s_rtp_fd.fd);
539 if (p_s_rtcp_fd.fd > 0) {
540 unregister_fd(&p_s_rtcp_fd);
541 close(p_s_rtcp_fd.fd);
544 if (p_s_rtp_is_connected) {
545 PDEBUG(DEBUG_SIP, "rtp closed\n");
546 p_s_rtp_is_connected = 0;
551 void tv_difference(struct timeval *diff, const struct timeval *from,
552 const struct timeval *__to)
554 struct timeval _to = *__to, *to = &_to;
556 if (to->tv_usec < from->tv_usec) {
558 to->tv_usec += 1000000;
561 diff->tv_usec = to->tv_usec - from->tv_usec;
562 diff->tv_sec = to->tv_sec - from->tv_sec;
565 /* encode and send a rtp frame */
566 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
568 struct rtp_hdr *rtph;
570 int duration; /* in samples */
571 unsigned char buffer[256];
575 record(data, len, 1); // from up
577 tap(data, len, 1); // from up
579 if (!p_s_rtp_is_connected) {
584 if (!p_s_rtp_tx_action) {
585 /* initialize sequences */
586 p_s_rtp_tx_action = 1;
587 p_s_rtp_tx_ssrc = rand();
588 p_s_rtp_tx_sequence = random();
589 p_s_rtp_tx_timestamp = random();
590 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
593 switch (payload_type) {
595 we only support alaw and ulaw!
596 case RTP_PT_GSM_FULL:
604 case RTP_PT_GSM_HALF:
609 case PAYLOAD_TYPE_ALAW:
610 case PAYLOAD_TYPE_ULAW:
615 PERROR("unsupported message type %d\n", payload_type);
621 struct timeval tv, tv_diff;
622 long int usec_diff, frame_diff;
624 gettimeofday(&tv, NULL);
625 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
626 p_s_rtp_tx_last_tv = tv;
628 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
629 frame_diff = (usec_diff / 20000);
631 if (abs(frame_diff) > 1) {
632 long int frame_diff_excess = frame_diff - 1;
634 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
635 p_s_rtp_tx_sequence += frame_diff_excess;
636 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
641 rtph = (struct rtp_hdr *) buffer;
642 rtph->version = RTP_VERSION;
645 rtph->csrc_count = 0;
647 rtph->payload_type = payload_type;
648 rtph->sequence = htons(p_s_rtp_tx_sequence++);
649 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
650 p_s_rtp_tx_timestamp += duration;
651 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
652 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
654 if (p_s_rtp_fd.fd > 0) {
655 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
656 if (len != sizeof(struct rtp_hdr) + payload_len) {
657 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
667 /* receive from remote */
668 int Psip::bridge_rx(unsigned char *data, int len)
672 /* don't bridge, if tones are provided */
673 if (p_tone_name[0] || p_dov_tx)
679 if ((ret = Port::bridge_rx(data, len)))
682 /* write to rx buffer */
684 p_s_rxdata[p_s_rxpos++] = flip[*data++];
685 if (p_s_rxpos == 160) {
688 /* transmit data via rtp */
689 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
696 /* taken from freeswitch */
697 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
698 static int status2cause(int status)
702 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
708 return 21; //SWITCH_CAUSE_CALL_REJECTED;
710 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
713 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
716 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
718 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
727 return 127; //SWITCH_CAUSE_INTERWORKING;
729 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
734 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
737 return 17; //SWITCH_CAUSE_USER_BUSY;
739 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
742 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
744 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
746 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
750 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
753 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
755 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
757 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
761 static int cause2status(int cause, int location, const char **st)
767 s = 404; *st = sip_404_Not_found;
770 s = 404; *st = sip_404_Not_found;
773 s = 404; *st = sip_404_Not_found;
776 s = 486; *st = sip_486_Busy_here;
779 s = 408; *st = sip_408_Request_timeout;
782 s = 480; *st = sip_480_Temporarily_unavailable;
785 s = 480; *st = sip_480_Temporarily_unavailable;
788 if (location == LOCATION_USER) {
789 s = 603; *st = sip_603_Decline;
791 s = 403; *st = sip_403_Forbidden;
795 //s = 301; *st = sip_301_Moved_permanently;
796 s = 410; *st = sip_410_Gone;
799 s = 410; *st = sip_410_Gone;
802 s = 404; *st = sip_404_Not_found;
805 s = 502; *st = sip_502_Bad_gateway;
808 s = 484; *st = sip_484_Address_incomplete;
811 s = 501; *st = sip_501_Not_implemented;
814 s = 480; *st = sip_480_Temporarily_unavailable;
817 s = 503; *st = sip_503_Service_unavailable;
820 s = 503; *st = sip_503_Service_unavailable;
823 s = 503; *st = sip_503_Service_unavailable;
826 s = 503; *st = sip_503_Service_unavailable;
829 s = 503; *st = sip_503_Service_unavailable;
832 s = 403; *st = sip_403_Forbidden;
835 s = 403; *st = sip_403_Forbidden;
838 s = 503; *st = sip_503_Service_unavailable;
841 s = 488; *st = sip_488_Not_acceptable;
844 s = 501; *st = sip_501_Not_implemented;
847 s = 488; *st = sip_488_Not_acceptable;
850 s = 501; *st = sip_501_Not_implemented;
853 s = 403; *st = sip_403_Forbidden;
856 s = 503; *st = sip_503_Service_unavailable;
859 s = 504; *st = sip_504_Gateway_time_out;
862 s = 500; *st = sip_500_Internal_server_error;
865 s = 500; *st = sip_500_Internal_server_error;
868 s = 468; *st = sip_486_Busy_here;
874 /* use STUN ip, or return the ip without change */
875 unsigned int Psip::get_local_ip(unsigned int ip)
877 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
879 if (inst->public_ip[0]) {
880 PDEBUG(DEBUG_SIP, "RTP local IP is replaced by STUN ip %s\n", inst->public_ip);
881 inet_pton(AF_INET, inst->public_ip, &ip);
887 /* some simple nonce generator */
888 static void generate_nonce(char *result)
890 UPRINT(result, "%08x", (unsigned int)random());
892 UPRINT(result, "%08x", (unsigned int)random());
894 UPRINT(result, "%08x", (unsigned int)random());
896 UPRINT(result, "%08x", (unsigned int)random());
899 /* check authorization */
900 static int check_authorization(sip_authorization_t const *authorization, const char *regstr, const char *check_user, const char *check_pass, const char *check_realm, const char *check_nonce, const char **auth_text)
903 *auth_text = "Internal Server Error";
905 char *username = NULL;
912 char *response = NULL;
917 char temp[256], first_digest[2 * SU_MD5_DIGEST_SIZE + 1], second_digest[2 * SU_MD5_DIGEST_SIZE + 1], third_digest[2 * SU_MD5_DIGEST_SIZE + 1];
920 if (!check_nonce || !check_nonce[0] || !authorization || !authorization->au_params) {
921 if (!strcmp(regstr, "REGISTER")) {
922 *auth_text = "Unauthorized";
925 *auth_text = "Proxy Authentication Required";
931 /* parse header (stolen from freeswitch) */
932 for (indexnum = 0; (cur = authorization->au_params[indexnum]); indexnum++) {
933 char *var, *val, *p, *work;
934 var = val = work = NULL;
935 if ((work = strdup(cur))) {
937 if ((val = strchr(var, '='))) {
939 while (*val == '"') {
942 if ((p = strchr(val, '"'))) {
946 PDEBUG(DEBUG_SIP, "Found in Auth header: %s = %s\n", var, val);
947 if (!strcasecmp(var, "username")) {
948 username = strdup(val);
949 } else if (!strcasecmp(var, "realm")) {
951 } else if (!strcasecmp(var, "nonce")) {
953 } else if (!strcasecmp(var, "uri")) {
955 } else if (!strcasecmp(var, "qop")) {
957 } else if (!strcasecmp(var, "cnonce")) {
958 cnonce = strdup(val);
959 } else if (!strcasecmp(var, "response")) {
960 response = strdup(val);
961 } else if (!strcasecmp(var, "nc")) {
970 if (!username || !realm || !nonce || ! uri || !response) {
971 *auth_text = "Authorization header incomplete";
976 if (!!strcmp(username, check_user)) {
977 *auth_text = "Authorization Username Missmatch";
981 if (!!strcmp(realm, check_realm)) {
982 *auth_text = "Authorization Realm Missmatch";
986 if (!!strcmp(nonce, check_nonce)) {
987 *auth_text = "Authorization Nonce Missmatch";
993 SPRINT(temp, "%s:%s:%s", check_user, realm, check_pass);
994 PDEBUG(DEBUG_SIP, "First hash: %s\n", temp);
995 su_md5_init(&md5_ctx);
996 su_md5_strupdate(&md5_ctx, temp);
997 su_md5_hexdigest(&md5_ctx, first_digest);
998 su_md5_deinit(&md5_ctx);
1000 SPRINT(temp, "%s:%s", regstr, uri);
1001 PDEBUG(DEBUG_SIP, "First hash: %s\n", temp);
1002 su_md5_init(&md5_ctx);
1003 su_md5_strupdate(&md5_ctx, temp);
1004 su_md5_hexdigest(&md5_ctx, second_digest);
1005 su_md5_deinit(&md5_ctx);
1007 if (nc && cnonce && qop)
1008 SPRINT(temp, "%s:%s:%s:%s:%s:%s", first_digest, nonce, nc, cnonce, qop, second_digest);
1010 SPRINT(temp, "%s:%s:%s", first_digest, nonce, second_digest);
1011 PDEBUG(DEBUG_SIP, "Third hash: %s\n", temp);
1012 su_md5_init(&md5_ctx);
1013 su_md5_strupdate(&md5_ctx, temp);
1014 su_md5_hexdigest(&md5_ctx, third_digest);
1015 su_md5_deinit(&md5_ctx);
1017 if (!!strcmp(response, third_digest)) {
1018 *auth_text = "Authorization Failed";
1023 *auth_text = "Authorization Success";
1040 * endpoint sends messages to the SIP port
1043 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1045 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1046 const char *sdp_str = NULL;
1047 struct lcr_msg *message;
1048 struct interface *interface;
1050 unsigned char payload_type;
1052 interface = getinterfacebyname(inst->interface_name);
1054 PERROR("Cannot find interface %s.\n", inst->interface_name);
1058 if (param->connectinfo.rtpinfo.port) {
1059 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1061 media_type = param->connectinfo.rtpinfo.media_types[0];
1062 payload_type = param->connectinfo.rtpinfo.payload_types[0];
1063 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
1064 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
1065 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
1066 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1067 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1069 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1070 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1071 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1072 /* open local RTP peer (if not bridging) */
1073 if (rtp_connect() < 0) {
1074 nua_cancel(p_s_handle, TAG_END());
1075 nua_handle_destroy(p_s_handle);
1077 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1078 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1080 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1081 message->param.disconnectinfo.cause = 41;
1082 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1083 message_put(message);
1084 new_state(PORT_STATE_RELEASE);
1085 trigger_work(&p_s_delete);
1090 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1091 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1094 * If this response causes corrupt messages, like SDP body inside or
1095 * before header, check if the sofia-sip-gcc-4.8.patch was applied.
1096 * If it is still corrupted, try to disable optimization when compiling
1099 nua_respond(p_s_handle, SIP_200_OK,
1100 NUTAG_MEDIA_ENABLE(0),
1101 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1102 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1104 new_state(PORT_STATE_CONNECT);
1105 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1106 add_trace("respond", "value", "200 OK");
1107 add_trace("reason", NULL, "call connected");
1109 memset(&ia, 0, sizeof(ia));
1110 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1111 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1112 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1113 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1119 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
1121 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1122 struct lcr_msg *message;
1123 char cause_str[128] = "";
1124 int cause = param->disconnectinfo.cause;
1125 int location = param->disconnectinfo.cause;
1127 const char *status_text;
1129 if (cause > 0 && cause <= 127) {
1130 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
1134 case PORT_STATE_OUT_SETUP:
1135 case PORT_STATE_OUT_PROCEEDING:
1136 case PORT_STATE_OUT_ALERTING:
1137 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
1138 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1140 add_trace("cause", "value", "%d", cause);
1142 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1144 case PORT_STATE_IN_SETUP:
1145 case PORT_STATE_IN_PROCEEDING:
1146 case PORT_STATE_IN_ALERTING:
1147 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
1148 status = cause2status(cause, location, &status_text);
1149 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1151 add_trace("cause", "value", "%d", cause);
1152 add_trace("respond", "value", "%d %s", status, status_text);
1154 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1155 nua_handle_destroy(p_s_handle);
1157 trigger_work(&p_s_delete);
1160 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1161 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1163 add_trace("cause", "value", "%d", cause);
1165 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1168 if (message_id == MESSAGE_DISCONNECT) {
1169 while(p_epointlist) {
1170 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1171 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1172 message->param.disconnectinfo.location = LOCATION_BEYOND;
1173 message_put(message);
1175 free_epointlist(p_epointlist);
1179 new_state(PORT_STATE_RELEASE);
1184 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1186 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1187 char from[128] = "";
1188 char asserted_id[128] = "", asserted_msg[256] = "";
1190 char contact[128] = "";
1191 const char *local = inst->local_peer;
1193 const char *remote = inst->remote_peer;
1194 const char *sdp_str = NULL;
1195 struct epoint_list *epointlist;
1196 sip_cseq_t *cseq = NULL;
1197 struct lcr_msg *message;
1198 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1199 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1201 unsigned char *payload_types;
1206 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1207 add_trace("failed", "reason", "No remote peer set or no peer has registered to us.");
1209 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1210 message->param.disconnectinfo.cause = 27;
1211 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1212 message_put(message);
1213 new_state(PORT_STATE_RELEASE);
1214 trigger_work(&p_s_delete);
1218 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1220 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1221 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1222 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1223 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1224 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1226 if (param->setup.rtpinfo.port) {
1227 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1229 media_types = param->setup.rtpinfo.media_types;
1230 payload_types = param->setup.rtpinfo.payload_types;
1231 payloads = param->setup.rtpinfo.payloads;
1232 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1233 p_s_rtp_port_local = param->setup.rtpinfo.port;
1234 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1235 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1237 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1239 media_types = &lcr_media;
1240 payload_types = &lcr_payload;
1243 /* open local RTP peer (if not bridging) */
1244 if (rtp_open() < 0) {
1245 PERROR("Failed to open RTP sockets\n");
1246 /* send release message to endpoit */
1247 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1248 message->param.disconnectinfo.cause = 41;
1249 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1250 message_put(message);
1251 new_state(PORT_STATE_RELEASE);
1252 trigger_work(&p_s_delete);
1255 if (!p_s_rtp_ip_local) {
1258 /* extract IP from local peer */
1259 SCPY(local_ip, local);
1260 p = strchr(local_ip, ':');
1263 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1264 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1265 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1269 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1271 PERROR("Failed to create handle\n");
1272 /* send release message to endpoit */
1273 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1274 message->param.disconnectinfo.cause = 41;
1275 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1276 message_put(message);
1277 new_state(PORT_STATE_RELEASE);
1278 trigger_work(&p_s_delete);
1281 /* apply handle to trace */
1282 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1283 // add_trace("handle", "new", "0x%x", p_s_handle);
1286 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, payloads, payload_types, media_types);
1287 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1289 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1290 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1291 if (inst->asserted_id[0]) {
1292 SPRINT(asserted_id, "sip:%s@%s", inst->asserted_id, remote);
1293 SPRINT(asserted_msg, "P-Asserted-Identity: <%s>", asserted_id);
1295 if (inst->public_ip[0]) {
1297 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1298 p = strchr(inst->local_peer, ':');
1303 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1304 add_trace("from", "uri", "%s", from);
1305 add_trace("to", "uri", "%s", to);
1307 add_trace("assert-id", "uri", "%s", asserted_id);
1309 memset(&ia, 0, sizeof(ia));
1310 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1311 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1312 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1313 for (i = 0; i < payloads; i++)
1314 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1317 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1319 nua_invite(p_s_handle,
1320 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1321 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1322 TAG_IF(asserted_msg[0], SIPTAG_HEADER_STR(asserted_msg)),
1323 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1324 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1325 NUTAG_MEDIA_ENABLE(0),
1326 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1327 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1328 new_state(PORT_STATE_OUT_SETUP);
1330 p_s_invite_direction = DIRECTION_OUT;
1333 PDEBUG(DEBUG_SIP, "do overlap\n");
1334 new_state(PORT_STATE_OUT_OVERLAP);
1335 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1336 message_put(message);
1338 PDEBUG(DEBUG_SIP, "do proceeding\n");
1339 new_state(PORT_STATE_OUT_PROCEEDING);
1340 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1341 message_put(message);
1344 /* attach only if not already */
1345 epointlist = p_epointlist;
1347 if (epointlist->epoint_id == epoint_id)
1349 epointlist = epointlist->next;
1352 epointlist_new(epoint_id);
1357 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1360 // struct in_addr ia;
1362 switch (param->notifyinfo.notify) {
1363 case INFO_NOTIFY_REMOTE_HOLD:
1365 sdp_str = generate_sdp(0, 0, 0, NULL, NULL);
1368 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1370 "c=IN IP4 0.0.0.0\r\n"
1373 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1374 nua_info(p_s_handle,
1375 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1376 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1377 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1378 NUTAG_MEDIA_ENABLE(0),
1379 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1380 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1383 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1385 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1386 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1387 nua_info(p_s_handle,
1388 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1389 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1390 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1391 NUTAG_MEDIA_ENABLE(0),
1392 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1393 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1401 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1405 /* prepare DTMF info payload */
1411 /* start invite to handle DTMF */
1412 nua_info(p_s_handle,
1413 NUTAG_MEDIA_ENABLE(0),
1414 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1415 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1420 /* NOTE: incomplete and not working */
1421 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1425 /* prepare DTMF info payload */
1429 , param->information.id);
1431 /* start invite to handle DTMF */
1432 nua_info(p_s_handle,
1433 NUTAG_MEDIA_ENABLE(0),
1434 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1435 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1440 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1442 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1444 if (Port::message_epoint(epoint_id, message_id, param))
1447 switch(message_id) {
1448 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1449 if (p_state != PORT_STATE_IN_SETUP
1450 && p_state != PORT_STATE_IN_PROCEEDING)
1452 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1453 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1454 add_trace("respond", "value", "180 Ringing");
1456 new_state(PORT_STATE_IN_ALERTING);
1459 case MESSAGE_CONNECT: /* call is connected on LCR side */
1460 if (p_state != PORT_STATE_IN_SETUP
1461 && p_state != PORT_STATE_IN_PROCEEDING
1462 && p_state != PORT_STATE_IN_ALERTING)
1464 message_connect(epoint_id, message_id, param);
1467 case MESSAGE_DISCONNECT: /* call has been disconnected */
1468 case MESSAGE_RELEASE: /* call has been released */
1469 message_release(epoint_id, message_id, param);
1472 case MESSAGE_SETUP: /* dial-out command received from epoint */
1473 message_setup(epoint_id, message_id, param);
1476 case MESSAGE_INFORMATION: /* overlap dialing */
1477 if (p_state != PORT_STATE_OUT_OVERLAP)
1479 message_information(epoint_id, message_id, param);
1482 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1483 if (p_state == PORT_STATE_CONNECT)
1484 message_dtmf(epoint_id, message_id, param);
1485 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1486 if (p_state == PORT_STATE_CONNECT)
1487 message_notify(epoint_id, message_id, param);
1491 PDEBUG(DEBUG_SIP, "PORT(%s) SIP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1497 int Psip::parse_sdp(sip_t const *sip, unsigned int *ip, unsigned short *port, uint8_t *payload_types, int *media_types, int *payloads, int max_payloads)
1501 if (!sip->sip_payload) {
1502 PDEBUG(DEBUG_SIP, "no payload given\n");
1506 sdp_parser_t *parser;
1509 sdp_attribute_t *attr;
1511 sdp_connection_t *conn;
1513 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1515 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1519 if (!(sdp = sdp_session(parser))) {
1520 sdp_parser_free(parser);
1523 for (m = sdp->sdp_media; m; m = m->m_next) {
1524 if (m->m_proto != sdp_proto_rtp)
1526 if (m->m_type != sdp_media_audio)
1528 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1530 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1531 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1533 if (m->m_connections) {
1534 conn = m->m_connections;
1535 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1536 inet_pton(AF_INET, conn->c_address, ip);
1537 *ip = ntohl(p_s_rtp_ip_remote);
1539 char *p = sip->sip_payload->pl_data;
1542 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1543 p = strstr(p, "c=IN IP4 ");
1545 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1546 sdp_parser_free(parser);
1550 if ((p = strchr(addr, '\n'))) *p = '\0';
1551 if ((p = strchr(addr, '\r'))) *p = '\0';
1552 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1553 inet_pton(AF_INET, addr, ip);
1554 *ip = ntohl(p_s_rtp_ip_remote);
1556 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1559 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1560 /* append to payload list, if there is space */
1561 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1562 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1563 media_type = MEDIA_TYPE_ALAW;
1564 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1565 media_type = MEDIA_TYPE_ULAW;
1566 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1567 media_type = MEDIA_TYPE_GSM;
1568 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1569 media_type = MEDIA_TYPE_GSM_EFR;
1570 else if (!strcmp(map->rm_encoding, "AMR"))
1571 media_type = MEDIA_TYPE_AMR;
1572 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1573 media_type = MEDIA_TYPE_GSM_HR;
1574 if (media_type && *payloads <= max_payloads) {
1575 *payload_types++ = map->rm_pt;
1576 *media_types++ = media_type;
1582 sdp_parser_free(parser);
1587 const char *Psip::generate_sdp(unsigned int rtp_ip_local, unsigned short rtp_port_local, int payloads, unsigned char *payload_types, int *media_types)
1590 static char sdp_str[256], sub_str[128];
1593 memset(&ia, 0, sizeof(ia));
1594 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1597 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1600 "t=0 0\r\n", inet_ntoa(ia), inet_ntoa(ia));
1602 SPRINT(sub_str, "m=audio %d RTP/AVP", p_s_rtp_port_local);
1603 SCAT(sdp_str, sub_str);
1604 for (i = 0; i < payloads; i++) {
1605 SPRINT(sub_str, " %d", payload_types[i]);
1606 SCAT(sdp_str, sub_str);
1608 SCAT(sdp_str, "\r\n");
1609 for (i = 0; i < payloads; i++) {
1610 SPRINT(sub_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1611 SCAT(sdp_str, sub_str);
1618 static int challenge(struct sip_inst *inst, class Psip *psip, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1620 sip_www_authenticate_t const *authenticate = NULL;
1621 char const *realm = NULL;
1622 char const *scheme = NULL;
1625 char authentication[256] = "";
1626 PDEBUG(DEBUG_SIP, "challenge order received\n");
1628 if (!inst->auth_user[0]) {
1629 PDEBUG(DEBUG_SIP, "No credentials available\n");
1630 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1631 add_trace("error", NULL, "There are no credentials given for interface");
1636 if (sip->sip_www_authenticate) {
1637 authenticate = sip->sip_www_authenticate;
1638 } else if (sip->sip_proxy_authenticate) {
1639 authenticate = sip->sip_proxy_authenticate;
1641 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1642 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1643 add_trace("error", NULL, "Authentication method unknwon");
1648 scheme = (char const *) authenticate->au_scheme;
1649 if (authenticate->au_params) {
1650 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1651 if ((realm = strstr(cur, "realm="))) {
1658 if (!scheme || !realm) {
1659 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1660 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1661 add_trace("error", NULL, "Authentication header has no realm or scheme");
1666 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1667 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1669 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1670 add_trace("scheme", NULL, "%s", scheme);
1671 add_trace("realm", NULL, "%s", realm);
1672 add_trace("user", NULL, "%s", inst->auth_user);
1673 add_trace("pass", NULL, "%s", inst->auth_password);
1676 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1681 static void i_options(struct sip_inst *inst, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1683 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1684 nua_saved_event_t saved[1];
1685 nua_save_event(nua, saved);
1686 nua_event_data_t const *data = nua_event_data(saved);
1688 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_IN);
1691 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1692 add_trace("respond", "value", "200 OK");
1695 nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1696 nua_handle_destroy(nh);
1697 inst->register_handle = NULL;
1700 static void i_register(struct sip_inst *inst, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1702 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1703 nua_saved_event_t saved[1];
1704 sip_contact_t const *contact = NULL;
1705 contact = sip->sip_contact;
1706 nua_save_event(nua, saved);
1707 nua_event_data_t const *data = nua_event_data(saved);
1708 sip_authorization_t const *authorization;
1710 const char *auth_text = NULL;
1711 char auth_str[256] = "";
1713 if (contact->m_url->url_host)
1714 SCPY(uri, contact->m_url->url_host);
1715 if (contact->m_url->url_port && contact->m_url->url_port[0]) {
1717 SCAT(uri, contact->m_url->url_port);
1720 if (!inst->allow_register) {
1721 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1722 add_trace("error", NULL, "forbidden, because we don't accept registration");
1724 nua_respond(nh, SIP_403_FORBIDDEN, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1725 nua_handle_destroy(nh);
1726 inst->register_handle = NULL;
1730 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1731 add_trace("contact", "uri", "%s", uri);
1734 sip_trace_header(NULL, inst->interface_name, "Authorization", DIRECTION_IN);
1735 if (inst->auth_realm[0]) {
1736 authorization = sip->sip_authorization;
1737 status = check_authorization(authorization, "REGISTER", inst->auth_user, inst->auth_password, inst->auth_realm, inst->auth_nonce, &auth_text);
1738 if (status == 401) {
1739 generate_nonce(inst->auth_nonce);
1740 SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, inst->auth_nonce);
1744 auth_text = "Authentication not required";
1746 add_trace("result", NULL, "%s", auth_text);
1749 if (status == 200) {
1750 SCPY(inst->remote_peer, uri);
1751 inst->auth_nonce[0] = '\0';
1754 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1755 add_trace("respond", "value", "%d", status);
1756 add_trace("reason", NULL, "peer registers");
1759 nua_respond(nh, status, auth_text, SIPTAG_CONTACT(sip->sip_contact), NUTAG_WITH_THIS_MSG(data->e_msg), TAG_IF(auth_str[0], SIPTAG_WWW_AUTHENTICATE_STR(auth_str)), TAG_END());
1760 nua_handle_destroy(nh);
1761 inst->register_handle = NULL;
1764 static void r_register(struct sip_inst *inst, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1768 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_IN);
1769 add_trace("value", NULL, "%d", status);
1770 add_trace("phrase", NULL, "%s", phrase);
1776 /* if not registered, become registered and start register interval timer */
1777 if (inst->register_state != REGISTER_STATE_REGISTERED) {
1778 if (inst->register_interval)
1779 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
1780 inst->register_state = REGISTER_STATE_REGISTERED;
1782 /* start option timer */
1783 if (inst->options_interval)
1784 PDEBUG(DEBUG_SIP, "register ok, scheduling option timer with %d seconds\n", inst->options_interval);
1785 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1789 PDEBUG(DEBUG_SIP, "Register challenge received\n");
1790 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
1795 if (status >= 200 && status <= 299)
1800 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
1801 inst->register_state = REGISTER_STATE_FAILED;
1802 nua_handle_destroy(nh);
1803 inst->register_handle = NULL;
1804 /* stop option timer */
1805 unsched_timer(&inst->register_option_timer);
1806 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1807 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1811 void Psip::i_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1813 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1814 const char *from = "", *to = "", *name = "";
1817 class Endpoint *epoint;
1818 struct lcr_msg *message;
1819 struct interface *interface;
1820 const char *sdp_str = NULL;
1821 int media_types[32];
1822 uint8_t payload_types[32];
1824 unsigned char payload_type;
1826 sip_authorization_t const *authorization;
1827 const char *auth_text = NULL;
1828 char auth_str[256] = "";
1830 interface = getinterfacebyname(inst->interface_name);
1832 PERROR("Cannot find interface %s.\n", inst->interface_name);
1836 if (sip->sip_from) {
1837 if (sip->sip_from->a_url)
1838 from = sip->sip_from->a_url->url_user;
1839 if (sip->sip_from->a_display) {
1840 name = sip->sip_from->a_display;
1841 if (!strncmp(name, "\"IMSI", 5)) {
1842 strncpy(imsi, name + 5, 15);
1849 if (sip->sip_to->a_url)
1850 to = sip->sip_to->a_url->url_user;
1852 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1854 sip_trace_header(this, inst->interface_name, "Authorization", DIRECTION_IN);
1855 if (inst->auth_realm[0]) {
1856 authorization = sip->sip_proxy_authorization;
1857 status = check_authorization(authorization, "INVITE", inst->auth_user, inst->auth_password, inst->auth_realm, p_s_auth_nonce, &auth_text);
1858 if (status == 401) {
1859 generate_nonce(p_s_auth_nonce);
1860 SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, p_s_auth_nonce);
1864 auth_text = "Authentication not required";
1866 add_trace("result", NULL, "%s", auth_text);
1869 if (status == 200) {
1870 p_s_auth_nonce[0] = '\0';
1873 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1874 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1876 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1877 if (!p_s_rtp_bridge) {
1880 /* check if supported payload type exists */
1881 for (i = 0; i < payloads; i++) {
1882 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1885 if (i == payloads) {
1886 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1894 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1896 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1897 nua_handle_destroy(nh);
1899 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1901 add_trace("respond", "value", "415 Unsupported Media");
1903 add_trace("respond", "value", "400 Bad Request");
1904 add_trace("reason", NULL, "offered codec does not match");
1906 if (p_state != PORT_STATE_IDLE) {
1907 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1908 message->param.disconnectinfo.cause = 41;
1909 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1910 message_put(message);
1912 new_state(PORT_STATE_RELEASE);
1913 trigger_work(&p_s_delete);
1917 /* handle re-invite */
1918 if (p_state != PORT_STATE_IDLE) {
1919 sip_trace_header(this, inst->interface_name, "RE-INVITE", DIRECTION_IN);
1921 if (p_s_rtp_bridge) {
1922 PDEBUG(DEBUG_SIP, "RE-INVITE not implemented for RTP forwarding\n");
1923 nua_respond(nh, SIP_501_NOT_IMPLEMENTED, TAG_END());
1924 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1925 add_trace("respond", "value", "501 NOT IMPLEMENTED");
1926 add_trace("reason", NULL, "RE-INVITE not implemented for RTP forwarding");
1929 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1930 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1931 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1932 if (rtp_connect() < 0) {
1935 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1936 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1937 nua_respond(p_s_handle, SIP_200_OK,
1938 NUTAG_MEDIA_ENABLE(0),
1939 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1940 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1945 /* open local RTP peer (if not bridging) */
1946 if (!p_s_rtp_bridge && rtp_open() < 0) {
1947 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1948 nua_handle_destroy(nh);
1950 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1951 add_trace("respond", "value", "500 Internal Server Error");
1952 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1954 new_state(PORT_STATE_RELEASE);
1955 trigger_work(&p_s_delete);
1960 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1961 // add_trace("handle", "new", "0x%x", nh);
1966 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1967 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1968 /* caller information */
1970 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1971 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1972 add_trace("calling", "present", "unavailable");
1974 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1975 add_trace("calling", "present", "allowed");
1976 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1977 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1978 SCPY(p_callerinfo.id, from);
1979 add_trace("calling", "number", "%s", from);
1980 SCPY(p_callerinfo.name, name);
1982 add_trace("calling", "name", "%s", name);
1983 SCPY(p_callerinfo.imsi, imsi);
1985 add_trace("calling", "imsi", "%s", imsi);
1987 SCPY(p_callerinfo.interface, inst->interface_name);
1988 /* dialing information */
1990 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1991 SCAT(p_dialinginfo.id, to);
1992 add_trace("dialing", "number", "%s", to);
1995 /* bearer capability */
1996 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1997 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1998 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1999 add_trace("bearer", "capa", "speech");
2000 add_trace("bearer", "mode", "circuit");
2001 /* if packet mode works some day, see dss1.cpp for conditions */
2002 p_capainfo.source_mode = B_MODE_TRANSPARENT;
2006 /* create endpoint */
2008 FATAL("Incoming call but already got an endpoint.\n");
2009 if (!(epoint = new Endpoint(p_serial, 0)))
2010 FATAL("No memory for Endpoint instance\n");
2011 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
2012 epointlist_new(epoint->ep_serial);
2014 #ifdef NUTAG_AUTO100
2015 /* send trying (proceeding) */
2016 nua_respond(nh, SIP_100_TRYING, TAG_END());
2017 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2018 add_trace("respond", "value", "100 Trying");
2022 new_state(PORT_STATE_IN_PROCEEDING);
2024 /* send setup message to endpoit */
2025 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
2026 message->param.setup.port_type = p_type;
2027 // message->param.setup.dtmf = 0;
2028 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
2029 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
2030 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
2031 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
2032 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
2033 // message->param.setup.useruser.protocol = mncc->useruser.proto;
2034 if (p_s_rtp_bridge) {
2037 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
2038 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
2039 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
2040 /* add codecs to setup message */
2041 for (i = 0; i < payloads; i++) {
2042 message->param.setup.rtpinfo.media_types[i] = media_types[i];
2043 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
2044 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
2047 message->param.setup.rtpinfo.payloads = i;
2049 message_put(message);
2051 /* start option timer */
2052 if (inst->options_interval) {
2053 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer with %d seconds\n", inst->options_interval);
2054 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2057 p_s_invite_direction = DIRECTION_IN;
2059 /* send progress, if tones are available and if we don't bridge */
2060 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
2061 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
2062 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
2063 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
2064 /* open local RTP peer (if not bridging) */
2065 if (rtp_connect() < 0) {
2067 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2068 nua_handle_destroy(nh);
2070 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2071 add_trace("respond", "value", "500 Internal Server Error");
2072 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
2074 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2075 message->param.disconnectinfo.cause = 41;
2076 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2077 message_put(message);
2078 new_state(PORT_STATE_RELEASE);
2079 trigger_work(&p_s_delete);
2083 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
2084 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
2086 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
2087 NUTAG_MEDIA_ENABLE(0),
2088 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
2089 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
2090 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2091 add_trace("respond", "value", "183 SESSION PROGRESS");
2092 add_trace("reason", NULL, "audio available");
2094 memset(&ia, 0, sizeof(ia));
2095 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
2096 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
2097 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
2098 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
2103 void Psip::i_options(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2105 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2107 PDEBUG(DEBUG_SIP, "options received\n");
2109 sip_trace_header(this, inst->interface_name, "OPTIONS", DIRECTION_IN);
2112 nua_respond(nh, SIP_200_OK, TAG_END());
2115 void Psip::i_bye(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2117 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2118 struct lcr_msg *message;
2121 PDEBUG(DEBUG_SIP, "bye received\n");
2123 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
2124 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
2125 cause = atoi(sip->sip_reason->re_cause);
2126 add_trace("cause", "value", "%d", cause);
2130 // let stack do bye automaticall, since it will not accept our response for some reason
2131 // nua_respond(nh, SIP_200_OK, TAG_END());
2132 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2133 add_trace("respond", "value", "200 OK");
2135 // nua_handle_destroy(nh);
2140 while(p_epointlist) {
2141 /* send setup message to endpoit */
2142 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2143 message->param.disconnectinfo.cause = cause ? : 16;
2144 message->param.disconnectinfo.location = LOCATION_BEYOND;
2145 message_put(message);
2147 free_epointlist(p_epointlist);
2149 new_state(PORT_STATE_RELEASE);
2150 trigger_work(&p_s_delete);
2153 void Psip::i_cancel(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2155 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2156 struct lcr_msg *message;
2158 PDEBUG(DEBUG_SIP, "cancel received\n");
2160 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
2163 nua_handle_destroy(nh);
2168 while(p_epointlist) {
2169 /* send setup message to endpoit */
2170 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2171 message->param.disconnectinfo.cause = 16;
2172 message->param.disconnectinfo.location = LOCATION_BEYOND;
2173 message_put(message);
2175 free_epointlist(p_epointlist);
2177 new_state(PORT_STATE_RELEASE);
2178 trigger_work(&p_s_delete);
2181 void Psip::r_bye(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2183 PDEBUG(DEBUG_SIP, "bye response received\n");
2185 nua_handle_destroy(nh);
2190 trigger_work(&p_s_delete);
2193 void Psip::r_cancel(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2195 PDEBUG(DEBUG_SIP, "cancel response received\n");
2197 nua_handle_destroy(nh);
2202 trigger_work(&p_s_delete);
2205 void Psip::r_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2207 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2208 struct lcr_msg *message;
2209 int cause = 0, location = 0;
2210 int media_types[32];
2211 uint8_t payload_types[32];
2214 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
2216 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2217 add_trace("respond", "value", "%d", status);
2221 if (status == 183 || (status >= 200 && status <= 299)) {
2224 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
2225 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
2229 else if (!p_s_rtp_bridge) {
2230 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
2231 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
2238 nua_cancel(nh, TAG_END());
2239 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2240 add_trace("reason", NULL, "accepted codec does not match");
2243 location = LOCATION_PRIVATE_LOCAL;
2244 goto release_with_cause;
2247 /* connect to remote RTP (if not bridging) */
2248 if (!p_s_rtp_bridge && rtp_connect() < 0) {
2249 nua_cancel(nh, TAG_END());
2250 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2251 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
2254 location = LOCATION_PRIVATE_LOCAL;
2255 goto release_with_cause;
2259 /* start option timer */
2260 if (inst->options_interval) {
2261 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer with %d seconds\n", inst->options_interval);
2262 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2268 PDEBUG(DEBUG_SIP, "do proceeding\n");
2269 new_state(PORT_STATE_OUT_PROCEEDING);
2270 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
2271 message_put(message);
2275 PDEBUG(DEBUG_SIP, "do alerting\n");
2276 new_state(PORT_STATE_OUT_ALERTING);
2277 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
2278 message_put(message);
2281 PDEBUG(DEBUG_SIP, "do progress\n");
2282 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
2283 message->param.progressinfo.progress = 8;
2284 message->param.progressinfo.location = 10;
2285 if (p_s_rtp_bridge) {
2286 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2287 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
2288 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
2289 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
2290 message->param.progressinfo.rtpinfo.payloads = 1;
2292 message_put(message);
2296 PDEBUG(DEBUG_SIP, "do connect\n");
2297 nua_ack(nh, TAG_END());
2298 new_state(PORT_STATE_CONNECT);
2299 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
2300 if (p_s_rtp_bridge) {
2301 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2302 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
2303 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
2304 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
2305 message->param.connectinfo.rtpinfo.payloads = 1;
2307 message_put(message);
2310 if (status >= 200 && status <= 299)
2312 if (status < 100 || status > 199)
2314 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
2319 cause = status2cause(status);
2320 location = LOCATION_BEYOND;
2323 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
2325 while(p_epointlist) {
2326 /* send setup message to endpoit */
2327 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2328 message->param.disconnectinfo.cause = cause;
2329 message->param.disconnectinfo.location = location;
2330 message_put(message);
2332 free_epointlist(p_epointlist);
2335 new_state(PORT_STATE_RELEASE);
2339 trigger_work(&p_s_delete);
2342 void Psip::r_options(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2344 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2345 int cause = 0, location = 0;
2346 struct lcr_msg *message;
2348 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
2350 if (status >= 200 && status <= 299) {
2351 PDEBUG(DEBUG_SIP, "options ok, scheduling option timer with %d seconds\n", inst->options_interval);
2352 /* restart option timer */
2353 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2357 nua_handle_destroy(nh);
2362 cause = status2cause(status);
2363 location = LOCATION_BEYOND;
2365 while(p_epointlist) {
2366 /* send setup message to endpoit */
2367 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2368 message->param.disconnectinfo.cause = cause;
2369 message->param.disconnectinfo.location = location;
2370 message_put(message);
2372 free_epointlist(p_epointlist);
2374 new_state(PORT_STATE_RELEASE);
2375 trigger_work(&p_s_delete);
2378 void Psip::i_state(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2380 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2382 PDEBUG(DEBUG_SIP, "state change received\n");
2383 sip_trace_header(this, inst->interface_name, "STATUS", DIRECTION_OUT);
2384 add_trace("value", NULL, "%d", status);
2385 add_trace("phrase", NULL, "%s", phrase);
2389 static void sip_callback(nua_event_t event, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2391 struct sip_inst *inst = (struct sip_inst *) magic;
2393 class Psip *psip = NULL;
2396 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2400 /* hunt for existing handles */
2403 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2404 psip = (class Psip *)port;
2405 if (psip->p_s_handle == nh) {
2406 PDEBUG(DEBUG_SIP, "Event found for port %s\n", psip->p_name);
2418 if (!inst->register_handle) {
2419 PDEBUG(DEBUG_SIP, "New options instance\n");
2420 inst->register_handle = nh;
2423 case nua_i_register:
2424 if (!inst->register_handle) {
2425 PDEBUG(DEBUG_SIP, "New register instance\n");
2426 inst->register_handle = nh;
2432 struct interface *interface = interface_first;
2434 PDEBUG(DEBUG_SIP, "New psip instance\n");
2436 /* create call instance */
2437 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2439 if (!strcmp(interface->name, inst->interface_name))
2441 interface = interface->next;
2444 FATAL("Cannot find interface %s.\n", inst->interface_name);
2445 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2446 FATAL("Cannot create Port instance.\n");
2450 if (!psip && !inst->register_handle) {
2451 PDEBUG(DEBUG_SIP, "Destroying unknown instance\n");
2452 nua_handle_destroy(nh);
2457 /* handle register process */
2458 if (inst->register_handle == nh) {
2461 i_options(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2463 case nua_i_register:
2464 i_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2466 case nua_r_register:
2467 r_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2470 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2475 /* handle port process */
2477 PERROR("no SIP Port found for handel %p\n", nh);
2478 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2479 nua_handle_destroy(nh);
2486 rc = challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
2492 case nua_r_set_params:
2493 PDEBUG(DEBUG_SIP, "setparam response\n");
2496 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2499 PDEBUG(DEBUG_SIP, "error received\n");
2502 psip->i_state(status, phrase, nua, magic, nh, hmagic, sip, tags);
2505 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2508 PDEBUG(DEBUG_SIP, "ack received\n");
2511 PDEBUG(DEBUG_SIP, "active received\n");
2514 psip->i_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2517 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2520 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2523 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2526 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2529 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2531 case nua_i_terminated:
2532 PDEBUG(DEBUG_SIP, "terminated received\n");
2535 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2539 static void stun_bind_cb(stun_discovery_magic_t *magic, stun_handle_t *sh, stun_discovery_t *sd, stun_action_t action, stun_state_t event)
2541 struct sip_inst *inst = (struct sip_inst *) magic;
2545 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2548 case stun_discovery_done:
2549 addrlen = sizeof(sa);
2550 memset(&sa, 0, addrlen);
2551 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2552 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2555 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2556 inst->stun_state = STUN_STATE_RESOLVED;
2557 /* start timer for next stun request with inst->stun_interval */
2558 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2559 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2560 add_trace("ip", "addr", "%s", inst->public_ip);
2565 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2566 inst->stun_state = STUN_STATE_FAILED;
2567 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2568 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2569 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2574 /* received shutdown due to termination of RTP */
2575 void Psip::rtp_shutdown(void)
2577 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2578 struct lcr_msg *message;
2580 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2582 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2585 nua_handle_destroy(p_s_handle);
2588 while(p_epointlist) {
2589 /* send setup message to endpoit */
2590 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2591 message->param.disconnectinfo.cause = 16;
2592 message->param.disconnectinfo.location = LOCATION_BEYOND;
2593 message_put(message);
2595 free_epointlist(p_epointlist);
2597 new_state(PORT_STATE_RELEASE);
2598 trigger_work(&p_s_delete);
2601 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2603 class Psip *psip = (class Psip *)instance;
2604 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2606 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2609 nua_options(psip->p_s_handle,
2615 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2617 struct sip_inst *inst = (struct sip_inst *)instance;
2619 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2620 inst->stun_state = STUN_STATE_UNRESOLVED;
2625 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2627 struct sip_inst *inst = (struct sip_inst *)instance;
2629 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2630 /* if we have a handle, destroy it and becom unregistered, so registration is
2632 if (inst->register_handle) {
2633 /* stop option timer */
2634 unsched_timer(&inst->register_option_timer);
2635 nua_handle_destroy(inst->register_handle);
2636 inst->register_handle = NULL;
2638 inst->register_state = REGISTER_STATE_UNREGISTERED;
2643 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2645 struct sip_inst *inst = (struct sip_inst *)instance;
2646 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2649 nua_options(inst->register_handle,
2655 int sip_init_inst(struct interface *interface)
2657 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2660 interface->sip_inst = inst;
2661 SCPY(inst->interface_name, interface->name);
2662 SCPY(inst->local_peer, interface->sip_local_peer);
2663 SCPY(inst->remote_peer, interface->sip_remote_peer);
2664 if (!inst->remote_peer[0])
2665 inst->allow_register = 1;
2666 SCPY(inst->asserted_id, interface->sip_asserted_id);
2667 if (interface->sip_register) {
2668 inst->register_state = REGISTER_STATE_UNREGISTERED;
2669 SCPY(inst->register_user, interface->sip_register_user);
2670 SCPY(inst->register_host, interface->sip_register_host);
2672 SCPY(inst->auth_user, interface->sip_auth_user);
2673 SCPY(inst->auth_password, interface->sip_auth_password);
2674 SCPY(inst->auth_realm, interface->sip_auth_realm);
2675 inst->register_interval = interface->sip_register_interval;
2676 inst->options_interval = interface->sip_options_interval;
2678 inst->rtp_port_from = interface->rtp_port_from;
2679 inst->rtp_port_to = interface->rtp_port_to;
2680 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2681 inst->rtp_port_from = RTP_PORT_BASE;
2682 inst->rtp_port_to = RTP_PORT_MAX;
2684 inst->next_rtp_port = inst->rtp_port_from;
2687 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2688 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2689 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2690 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2691 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2692 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2694 /* init root object */
2695 inst->root = su_root_create(inst);
2697 PERROR("Failed to create SIP root\n");
2698 sip_exit_inst(interface);
2702 SPRINT(local, "sip:%s",inst->local_peer);
2703 if (!strchr(inst->local_peer, ':'))
2704 SCAT(local, ":5060");
2705 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2707 PERROR("Failed to create SIP stack object\n");
2708 sip_exit_inst(interface);
2711 nua_set_params(inst->nua,
2712 SIPTAG_ALLOW_STR("REGISTER,INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2713 NUTAG_APPL_METHOD("REGISTER"),
2714 NUTAG_APPL_METHOD("INVITE"),
2715 NUTAG_APPL_METHOD("ACK"),
2716 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2717 NUTAG_APPL_METHOD("CANCEL"),
2718 NUTAG_APPL_METHOD("OPTIONS"),
2719 NUTAG_APPL_METHOD("NOTIFY"),
2720 NUTAG_APPL_METHOD("INFO"),
2722 #ifdef NUTAG_AUTO100
2726 NUTAG_AUTOANSWER(0),
2729 SCPY(inst->public_ip, interface->sip_public_ip);
2730 if (interface->sip_stun_server[0]) {
2731 SCPY(inst->stun_server, interface->sip_stun_server);
2732 inst->stun_interval = interface->sip_stun_interval;
2733 inst->stun_handle = stun_handle_init(inst->root,
2734 STUNTAG_SERVER(inst->stun_server),
2736 if (!inst->stun_handle) {
2737 PERROR("Failed to create STUN handle\n");
2738 sip_exit_inst(interface);
2741 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2742 if (inst->stun_socket < 0) {
2743 PERROR("Failed to create STUN socket\n");
2744 sip_exit_inst(interface);
2747 inst->stun_state = STUN_STATE_UNRESOLVED;
2750 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2752 any_sip_interface = 1;
2757 void sip_exit_inst(struct interface *interface)
2759 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2763 del_timer(&inst->stun_retry_timer);
2764 del_timer(&inst->register_retry_timer);
2765 del_timer(&inst->register_option_timer);
2766 if (inst->stun_socket)
2767 su_close(inst->stun_socket);
2768 if (inst->stun_handle)
2769 stun_handle_destroy(inst->stun_handle);
2770 if (inst->register_handle)
2771 nua_handle_destroy(inst->register_handle);
2773 su_root_destroy(inst->root);
2775 nua_destroy(inst->nua);
2776 FREE(inst, sizeof(*inst));
2777 interface->sip_inst = NULL;
2779 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2781 /* check if there is any other SIP interface left */
2782 interface = interface_first;
2784 if (interface->sip_inst)
2786 interface = interface->next;
2789 any_sip_interface = 0;
2792 extern su_log_t su_log_default[];
2793 extern su_log_t nua_log[];
2799 /* init SOFIA lib */
2801 su_home_init(sip_home);
2803 if (options.deb & DEBUG_SIP) {
2804 su_log_set_level(su_log_default, 9);
2805 su_log_set_level(nua_log, 9);
2806 //su_log_set_level(soa_log, 9);
2809 for (i = 0; i < 256; i++)
2810 flip[i] = ((i & 1) << 7) + ((i & 2) << 5) + ((i & 4) << 3) + ((i & 8) << 1) + ((i & 16) >> 1) + ((i & 32) >> 3) + ((i & 64) >> 5) + ((i & 128) >> 7);
2812 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2819 su_home_deinit(sip_home);
2822 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2825 static void sip_handle_stun(struct sip_inst *inst)
2829 switch (inst->stun_state) {
2830 case STUN_STATE_UNRESOLVED:
2831 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2832 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2833 STUNTAG_SOCKET(inst->stun_socket),
2834 STUNTAG_REGISTER_EVENTS(1),
2837 PERROR("Failed to call stun_bind()\n");
2838 inst->stun_state = STUN_STATE_FAILED;
2841 inst->stun_state = STUN_STATE_RESOLVING;
2842 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2843 add_trace("server", "addr", "%s", inst->stun_server);
2849 static void sip_handle_register(struct sip_inst *inst)
2851 char from[128] = "";
2853 char contact[128] = "";
2855 switch (inst->register_state) {
2856 case REGISTER_STATE_UNREGISTERED:
2857 /* wait for resoved stun */
2858 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2861 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2862 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2863 if (!inst->register_handle) {
2864 PERROR("Failed to create handle\n");
2865 inst->register_state = REGISTER_STATE_FAILED;
2868 /* apply handle to trace */
2869 // sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2870 // add_trace("handle", "new", "0x%x", inst->register_handle);
2873 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2874 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2875 if (inst->public_ip[0]) {
2877 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2878 p = strchr(inst->local_peer, ':');
2883 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2884 add_trace("from", "uri", "%s", from);
2885 add_trace("to", "uri", "%s", to);
2888 nua_register(inst->register_handle,
2889 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2890 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2891 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2894 inst->register_state = REGISTER_STATE_REGISTERING;
2901 void sip_handle(void)
2903 struct interface *interface = interface_first;
2904 struct sip_inst *inst;
2907 if (interface->sip_inst) {
2908 inst = (struct sip_inst *) interface->sip_inst;
2909 su_root_step(inst->root, 0);
2910 sip_handle_stun(inst);
2911 sip_handle_register(inst);
2913 interface = interface->next;
2917 /* deletes when back in event loop */
2918 static int delete_event(struct lcr_work *work, void *instance, int index)
2920 class Psip *psip = (class Psip *)instance;
2929 * generate audio, if no data is received from bridge
2932 void Psip::set_tone(const char *dir, const char *tone)
2934 Port::set_tone(dir, tone);
2939 void Psip::update_load(void)
2941 /* don't trigger load event if event already active */
2942 if (p_s_load_timer.active)
2945 /* don't start timer if ... */
2946 if (!p_tone_name[0] && !p_dov_tx)
2949 p_s_next_tv_sec = 0;
2950 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2953 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2955 class Psip *psip = (class Psip *)instance;
2957 /* stop timer if ... */
2958 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2966 #define SEND_SIP_LEN 160
2968 void Psip::load_tx(void)
2971 struct timeval current_time;
2972 int tosend = SEND_SIP_LEN, i;
2973 unsigned char buf[SEND_SIP_LEN], *p = buf;
2976 gettimeofday(¤t_time, NULL);
2977 if (!p_s_next_tv_sec) {
2978 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2979 p_s_next_tv_sec = current_time.tv_sec;
2980 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2981 if (p_s_next_tv_usec >= 1000000) {
2982 p_s_next_tv_usec -= 1000000;
2985 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
2987 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2988 + (current_time.tv_usec - p_s_next_tv_usec);
2989 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2990 /* if clock drifts too much, set next timeout event to current timer + 160 */
2992 p_s_next_tv_sec = current_time.tv_sec;
2993 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2994 if (p_s_next_tv_usec >= 1000000) {
2995 p_s_next_tv_usec -= 1000000;
2999 /* if diff is positive, it took too long, so next timeout will be earlier */
3000 p_s_next_tv_usec += SEND_SIP_LEN * 125;
3001 if (p_s_next_tv_usec >= 1000000) {
3002 p_s_next_tv_usec -= 1000000;
3006 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
3010 if (p_tone_name[0]) {
3011 tosend -= read_audio(p, tosend);
3014 tosend -= dov_tx(p, tosend);
3017 PERROR("buffer is not completely filled\n");
3022 for (i = 0; i < SEND_SIP_LEN; i++) {
3026 /* transmit data via rtp */
3027 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);