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_rtp_fd, 0, sizeof(p_s_rtp_fd));
107 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
108 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
109 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
110 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
111 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
112 p_s_rtp_ip_local = 0;
113 p_s_rtp_ip_remote = 0;
114 p_s_rtp_port_local = 0;
115 p_s_rtp_port_remote = 0;
120 p_s_rtp_tx_action = 0;
121 p_s_rtp_is_connected = 0;
123 /* create option timer */
124 memset(&p_s_invite_option_timer, 0, sizeof(p_s_invite_option_timer));
125 add_timer(&p_s_invite_option_timer, invite_option_timer, this, 0);
126 p_s_invite_direction = 0;
129 memset(&p_s_load_timer, 0, sizeof(p_s_load_timer));
130 add_timer(&p_s_load_timer, load_timer, this, 0);
133 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
135 FATAL("No SIP instance for interface\n");
144 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
146 del_timer(&p_s_invite_option_timer);
147 del_timer(&p_s_load_timer);
148 del_work(&p_s_delete);
153 static const char *media_type2name(uint8_t media_type) {
154 switch (media_type) {
155 case MEDIA_TYPE_ULAW:
157 case MEDIA_TYPE_ALAW:
161 case MEDIA_TYPE_GSM_HR:
163 case MEDIA_TYPE_GSM_EFR:
172 static void sip_trace_header(class Psip *sip, const char *interface_name, const char *message, int direction)
174 struct interface *interface = NULL;
177 interface = getinterfacebyname(interface_name);
179 /* init trace with given values */
182 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
183 sip?sip->p_dialinginfo.id:NULL,
194 /* according to RFC 3550 */
196 #if __BYTE_ORDER == __LITTLE_ENDIAN
197 uint8_t csrc_count:4,
201 uint8_t payload_type:7,
203 #elif __BYTE_ORDER == __BIG_ENDIAN
214 } __attribute__((packed));
219 } __attribute__((packed));
221 #define RTP_VERSION 2
223 #define PAYLOAD_TYPE_ULAW 0
224 #define PAYLOAD_TYPE_ALAW 8
225 #define PAYLOAD_TYPE_GSM 3
227 /* decode an rtp frame */
228 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
230 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
231 struct rtp_x_hdr *rtpxh;
235 unsigned char *from, *to;
239 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
242 if (rtph->version != RTP_VERSION) {
243 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
246 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
247 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
248 if (payload_len < 0) {
249 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
250 "csrc count = %d)\n", len, rtph->csrc_count);
253 if (rtph->extension) {
254 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
255 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
256 "extension header\n");
259 rtpxh = (struct rtp_x_hdr *)payload;
260 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
262 payload_len -= x_len;
263 if (payload_len < 0) {
264 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
265 "extension header exceeds frame length\n");
270 if (payload_len < 0) {
271 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
275 payload_len -= payload[payload_len - 1];
276 if (payload_len < 0) {
277 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
278 "greater than payload\n");
283 switch (rtph->payload_type) {
285 we only support alaw and ulaw!
286 case RTP_PT_GSM_FULL:
287 if (payload_len != 33) {
288 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
289 "payload length != 33 (len = %d)\n",
295 if (payload_len != 31) {
296 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
297 "payload length != 31 (len = %d)\n",
302 case RTP_PT_GSM_HALF:
303 if (payload_len != 14) {
304 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
305 "payload length != 14 (len = %d)\n",
311 case PAYLOAD_TYPE_ALAW:
312 if (options.law != 'a') {
313 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
317 case PAYLOAD_TYPE_ULAW:
318 if (options.law == 'a') {
319 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
324 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
325 "type %d\n", rtph->payload_type);
329 if (payload_len <= 0) {
330 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
336 psip->record(payload, payload_len, 0); // from down
338 psip->tap(payload, payload_len, 0); // from down
343 if (psip->p_echotest) {
344 /* echo rtp data we just received */
345 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
349 *to++ = flip[*from++];
351 psip->dov_rx(payload, payload_len);
352 psip->bridge_tx(payload, payload_len);
357 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
359 class Psip *psip = (class Psip *) instance;
361 unsigned char buffer[256];
364 if ((what & LCR_FD_READ)) {
365 len = read(fd->fd, &buffer, sizeof(buffer));
367 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
368 // psip->rtp_close();
369 // psip->rtp_shutdown();
372 if (psip->p_s_rtp_is_connected)
373 rc = rtp_decode(psip, buffer, len);
379 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
381 // class Psip *psip = (class Psip *) instance;
383 unsigned char buffer[256];
385 if ((what & LCR_FD_READ)) {
386 len = read(fd->fd, &buffer, sizeof(buffer));
388 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
389 // psip->rtp_close();
390 // psip->rtp_shutdown();
393 PDEBUG(DEBUG_SIP, "rtcp!\n");
399 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
402 socklen_t alen = sizeof(*sin_local);
404 sin_local->sin_family = AF_INET;
405 sin_local->sin_addr.s_addr = htonl(ip);
406 sin_local->sin_port = htons(port);
408 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
412 /* retrieve the address we actually bound to, in case we
413 * passed INADDR_ANY as IP address */
414 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
417 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
420 socklen_t alen = sizeof(*sin_local);
422 sin_remote->sin_family = AF_INET;
423 sin_remote->sin_addr.s_addr = htonl(ip);
424 sin_remote->sin_port = htons(port);
426 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
428 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
432 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
435 int Psip::rtp_open(void)
437 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
439 // struct in_addr ia;
441 unsigned short start_port;
443 PDEBUG(DEBUG_SIP, "rtp_open\n");
446 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
452 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
454 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
460 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
463 ip = htonl(INADDR_ANY);
464 start_port = inst->next_rtp_port;
466 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, inst->next_rtp_port);
470 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, inst->next_rtp_port + 1);
472 p_s_rtp_port_local = inst->next_rtp_port;
473 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
476 /* reopen rtp socket and try again with next udp port */
477 unregister_fd(&p_s_rtp_fd);
478 close(p_s_rtp_fd.fd);
480 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
486 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
489 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
490 if (inst->next_rtp_port == start_port)
492 /* we must use rc2, in order to preserve rc */
495 PDEBUG(DEBUG_SIP, "failed to find port\n");
499 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
500 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
501 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
503 return p_s_rtp_port_local;
506 int Psip::rtp_connect(void)
511 ia.s_addr = htonl(p_s_rtp_ip_remote);
512 if (p_s_rtp_is_connected)
513 PDEBUG(DEBUG_SIP, "reconnecting existing RTP connection to new/same destination\n");
514 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
516 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);
520 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);
524 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
525 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
526 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
527 p_s_rtp_is_connected = 1;
531 void Psip::rtp_close(void)
533 if (p_s_rtp_fd.fd > 0) {
534 unregister_fd(&p_s_rtp_fd);
535 close(p_s_rtp_fd.fd);
538 if (p_s_rtcp_fd.fd > 0) {
539 unregister_fd(&p_s_rtcp_fd);
540 close(p_s_rtcp_fd.fd);
543 if (p_s_rtp_is_connected) {
544 PDEBUG(DEBUG_SIP, "rtp closed\n");
545 p_s_rtp_is_connected = 0;
550 void tv_difference(struct timeval *diff, const struct timeval *from,
551 const struct timeval *__to)
553 struct timeval _to = *__to, *to = &_to;
555 if (to->tv_usec < from->tv_usec) {
557 to->tv_usec += 1000000;
560 diff->tv_usec = to->tv_usec - from->tv_usec;
561 diff->tv_sec = to->tv_sec - from->tv_sec;
564 /* encode and send a rtp frame */
565 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
567 struct rtp_hdr *rtph;
569 int duration; /* in samples */
570 unsigned char buffer[256];
574 record(data, len, 1); // from up
576 tap(data, len, 1); // from up
578 if (!p_s_rtp_is_connected) {
583 if (!p_s_rtp_tx_action) {
584 /* initialize sequences */
585 p_s_rtp_tx_action = 1;
586 p_s_rtp_tx_ssrc = rand();
587 p_s_rtp_tx_sequence = random();
588 p_s_rtp_tx_timestamp = random();
589 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
592 switch (payload_type) {
594 we only support alaw and ulaw!
595 case RTP_PT_GSM_FULL:
603 case RTP_PT_GSM_HALF:
608 case PAYLOAD_TYPE_ALAW:
609 case PAYLOAD_TYPE_ULAW:
614 PERROR("unsupported message type %d\n", payload_type);
620 struct timeval tv, tv_diff;
621 long int usec_diff, frame_diff;
623 gettimeofday(&tv, NULL);
624 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
625 p_s_rtp_tx_last_tv = tv;
627 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
628 frame_diff = (usec_diff / 20000);
630 if (abs(frame_diff) > 1) {
631 long int frame_diff_excess = frame_diff - 1;
633 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
634 p_s_rtp_tx_sequence += frame_diff_excess;
635 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
640 rtph = (struct rtp_hdr *) buffer;
641 rtph->version = RTP_VERSION;
644 rtph->csrc_count = 0;
646 rtph->payload_type = payload_type;
647 rtph->sequence = htons(p_s_rtp_tx_sequence++);
648 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
649 p_s_rtp_tx_timestamp += duration;
650 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
651 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
653 if (p_s_rtp_fd.fd > 0) {
654 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
655 if (len != sizeof(struct rtp_hdr) + payload_len) {
656 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
666 /* receive from remote */
667 int Psip::bridge_rx(unsigned char *data, int len)
671 /* don't bridge, if tones are provided */
672 if (p_tone_name[0] || p_dov_tx)
678 if ((ret = Port::bridge_rx(data, len)))
681 /* write to rx buffer */
683 p_s_rxdata[p_s_rxpos++] = flip[*data++];
684 if (p_s_rxpos == 160) {
687 /* transmit data via rtp */
688 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
695 /* taken from freeswitch */
696 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
697 static int status2cause(int status)
701 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
707 return 21; //SWITCH_CAUSE_CALL_REJECTED;
709 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
712 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
715 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
717 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
726 return 127; //SWITCH_CAUSE_INTERWORKING;
728 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
733 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
736 return 17; //SWITCH_CAUSE_USER_BUSY;
738 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
741 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
743 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
745 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
749 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
752 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
754 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
756 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
760 static int cause2status(int cause, int location, const char **st)
766 s = 404; *st = sip_404_Not_found;
769 s = 404; *st = sip_404_Not_found;
772 s = 404; *st = sip_404_Not_found;
775 s = 486; *st = sip_486_Busy_here;
778 s = 408; *st = sip_408_Request_timeout;
781 s = 480; *st = sip_480_Temporarily_unavailable;
784 s = 480; *st = sip_480_Temporarily_unavailable;
787 if (location == LOCATION_USER) {
788 s = 603; *st = sip_603_Decline;
790 s = 403; *st = sip_403_Forbidden;
794 //s = 301; *st = sip_301_Moved_permanently;
795 s = 410; *st = sip_410_Gone;
798 s = 410; *st = sip_410_Gone;
801 s = 404; *st = sip_404_Not_found;
804 s = 502; *st = sip_502_Bad_gateway;
807 s = 484; *st = sip_484_Address_incomplete;
810 s = 501; *st = sip_501_Not_implemented;
813 s = 480; *st = sip_480_Temporarily_unavailable;
816 s = 503; *st = sip_503_Service_unavailable;
819 s = 503; *st = sip_503_Service_unavailable;
822 s = 503; *st = sip_503_Service_unavailable;
825 s = 503; *st = sip_503_Service_unavailable;
828 s = 503; *st = sip_503_Service_unavailable;
831 s = 403; *st = sip_403_Forbidden;
834 s = 403; *st = sip_403_Forbidden;
837 s = 503; *st = sip_503_Service_unavailable;
840 s = 488; *st = sip_488_Not_acceptable;
843 s = 501; *st = sip_501_Not_implemented;
846 s = 488; *st = sip_488_Not_acceptable;
849 s = 501; *st = sip_501_Not_implemented;
852 s = 403; *st = sip_403_Forbidden;
855 s = 503; *st = sip_503_Service_unavailable;
858 s = 504; *st = sip_504_Gateway_time_out;
861 s = 500; *st = sip_500_Internal_server_error;
864 s = 500; *st = sip_500_Internal_server_error;
867 s = 468; *st = sip_486_Busy_here;
873 /* use STUN ip, or return the ip without change */
874 unsigned int Psip::get_local_ip(unsigned int ip)
876 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
878 if (inst->public_ip[0]) {
879 PDEBUG(DEBUG_SIP, "RTP local IP is replaced by STUN ip %s\n", inst->public_ip);
880 inet_pton(AF_INET, inst->public_ip, &ip);
886 /* some simple nonce generator */
887 static void generate_nonce(char *result)
889 UPRINT(result, "%08x", (unsigned int)random());
891 UPRINT(result, "%08x", (unsigned int)random());
893 UPRINT(result, "%08x", (unsigned int)random());
895 UPRINT(result, "%08x", (unsigned int)random());
898 /* check authorization */
899 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)
902 *auth_text = "Internal Server Error";
904 char *username = NULL;
911 char *response = NULL;
916 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];
919 if (!check_nonce || !check_nonce[0] || !authorization || !authorization->au_params) {
920 if (!strcmp(regstr, "REGISTER")) {
921 *auth_text = "Unauthorized";
924 *auth_text = "Proxy Authentication Required";
930 /* parse header (stolen from freeswitch) */
931 for (indexnum = 0; (cur = authorization->au_params[indexnum]); indexnum++) {
932 char *var, *val, *p, *work;
933 var = val = work = NULL;
934 if ((work = strdup(cur))) {
936 if ((val = strchr(var, '='))) {
938 while (*val == '"') {
941 if ((p = strchr(val, '"'))) {
945 PDEBUG(DEBUG_SIP, "Found in Auth header: %s = %s\n", var, val);
946 if (!strcasecmp(var, "username")) {
947 username = strdup(val);
948 } else if (!strcasecmp(var, "realm")) {
950 } else if (!strcasecmp(var, "nonce")) {
952 } else if (!strcasecmp(var, "uri")) {
954 } else if (!strcasecmp(var, "qop")) {
956 } else if (!strcasecmp(var, "cnonce")) {
957 cnonce = strdup(val);
958 } else if (!strcasecmp(var, "response")) {
959 response = strdup(val);
960 } else if (!strcasecmp(var, "nc")) {
969 if (!username || !realm || !nonce || ! uri || !response) {
970 *auth_text = "Authorization header incomplete";
975 if (!!strcmp(username, check_user)) {
976 *auth_text = "Authorization Username Missmatch";
980 if (!!strcmp(realm, check_realm)) {
981 *auth_text = "Authorization Realm Missmatch";
985 if (!!strcmp(nonce, check_nonce)) {
986 *auth_text = "Authorization Nonce Missmatch";
992 SPRINT(temp, "%s:%s:%s", check_user, realm, check_pass);
993 PDEBUG(DEBUG_SIP, "First hash: %s\n", temp);
994 su_md5_init(&md5_ctx);
995 su_md5_strupdate(&md5_ctx, temp);
996 su_md5_hexdigest(&md5_ctx, first_digest);
997 su_md5_deinit(&md5_ctx);
999 SPRINT(temp, "%s:%s", regstr, uri);
1000 PDEBUG(DEBUG_SIP, "Second hash: %s\n", temp);
1001 su_md5_init(&md5_ctx);
1002 su_md5_strupdate(&md5_ctx, temp);
1003 su_md5_hexdigest(&md5_ctx, second_digest);
1004 su_md5_deinit(&md5_ctx);
1006 if (nc && cnonce && qop)
1007 SPRINT(temp, "%s:%s:%s:%s:%s:%s", first_digest, nonce, nc, cnonce, qop, second_digest);
1009 SPRINT(temp, "%s:%s:%s", first_digest, nonce, second_digest);
1010 PDEBUG(DEBUG_SIP, "Third hash: %s\n", temp);
1011 su_md5_init(&md5_ctx);
1012 su_md5_strupdate(&md5_ctx, temp);
1013 su_md5_hexdigest(&md5_ctx, third_digest);
1014 su_md5_deinit(&md5_ctx);
1016 if (!!strcmp(response, third_digest)) {
1017 *auth_text = "Authorization Failed";
1022 *auth_text = "Authorization Success";
1039 * endpoint sends messages to the SIP port
1042 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
1044 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1045 const char *sdp_str = NULL;
1046 struct lcr_msg *message;
1047 struct interface *interface;
1049 unsigned char payload_type;
1051 interface = getinterfacebyname(inst->interface_name);
1053 PERROR("Cannot find interface %s.\n", inst->interface_name);
1057 if (param->connectinfo.rtpinfo.port) {
1058 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1060 media_type = param->connectinfo.rtpinfo.media_types[0];
1061 payload_type = param->connectinfo.rtpinfo.payload_types[0];
1062 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
1063 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
1064 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
1065 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1066 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1068 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1069 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1070 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1071 /* open local RTP peer (if not bridging) */
1072 if (rtp_connect() < 0) {
1073 nua_cancel(p_s_handle, TAG_END());
1074 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", p_s_handle);
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 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", p_s_handle);
1156 nua_handle_destroy(p_s_handle);
1158 trigger_work(&p_s_delete);
1161 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1162 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1164 add_trace("cause", "value", "%d", cause);
1166 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1169 if (message_id == MESSAGE_DISCONNECT) {
1170 while(p_epointlist) {
1171 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1172 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1173 message->param.disconnectinfo.location = LOCATION_BEYOND;
1174 message_put(message);
1176 free_epointlist(p_epointlist);
1180 new_state(PORT_STATE_RELEASE);
1185 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1187 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1188 char from[128] = "";
1189 char asserted_id[128] = "", asserted_msg[256] = "";
1191 char contact[128] = "";
1192 const char *local = inst->local_peer;
1194 const char *remote = inst->remote_peer;
1195 const char *sdp_str = NULL;
1196 struct epoint_list *epointlist;
1197 sip_cseq_t *cseq = NULL;
1198 struct lcr_msg *message;
1199 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1200 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1202 unsigned char *payload_types;
1207 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1208 add_trace("failed", "reason", "No remote peer set or no peer has registered to us.");
1210 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1211 message->param.disconnectinfo.cause = 27;
1212 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1213 message_put(message);
1214 new_state(PORT_STATE_RELEASE);
1215 trigger_work(&p_s_delete);
1219 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1221 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1222 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1223 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1224 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1225 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1227 if (param->setup.rtpinfo.port) {
1228 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1230 media_types = param->setup.rtpinfo.media_types;
1231 payload_types = param->setup.rtpinfo.payload_types;
1232 payloads = param->setup.rtpinfo.payloads;
1233 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1234 p_s_rtp_port_local = param->setup.rtpinfo.port;
1235 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1236 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1238 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1240 media_types = &lcr_media;
1241 payload_types = &lcr_payload;
1244 /* open local RTP peer (if not bridging) */
1245 if (rtp_open() < 0) {
1246 PERROR("Failed to open RTP sockets\n");
1247 /* send release message to endpoit */
1248 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1249 message->param.disconnectinfo.cause = 41;
1250 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1251 message_put(message);
1252 new_state(PORT_STATE_RELEASE);
1253 trigger_work(&p_s_delete);
1256 if (!p_s_rtp_ip_local) {
1259 /* extract IP from local peer */
1260 SCPY(local_ip, local);
1261 p = strchr(local_ip, ':');
1264 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1265 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1266 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1270 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1272 PERROR("Failed to create handle\n");
1273 /* send release message to endpoit */
1274 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1275 message->param.disconnectinfo.cause = 41;
1276 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1277 message_put(message);
1278 new_state(PORT_STATE_RELEASE);
1279 trigger_work(&p_s_delete);
1282 /* apply handle to trace */
1283 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1284 // add_trace("handle", "new", "0x%x", p_s_handle);
1287 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, payloads, payload_types, media_types);
1288 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1290 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1291 // SPRINT(from, "\"%s\" <sip:%s@%s>", /*p_callerinfo.id*/ "4946448519988", p_callerinfo.id, remote);
1292 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1293 if (inst->asserted_id[0]) {
1294 SPRINT(asserted_id, "sip:%s@%s", inst->asserted_id, remote);
1295 SPRINT(asserted_msg, "P-Asserted-Identity: <%s>", asserted_id);
1297 if (inst->public_ip[0]) {
1299 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1300 p = strchr(inst->local_peer, ':');
1305 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1306 add_trace("from", "uri", "%s", from);
1307 add_trace("to", "uri", "%s", to);
1309 add_trace("assert-id", "uri", "%s", asserted_id);
1311 memset(&ia, 0, sizeof(ia));
1312 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1313 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1314 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1315 for (i = 0; i < payloads; i++)
1316 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1319 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1321 nua_invite(p_s_handle,
1322 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1323 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1324 TAG_IF(asserted_msg[0], SIPTAG_HEADER_STR(asserted_msg)),
1325 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1326 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1327 NUTAG_MEDIA_ENABLE(0),
1328 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1329 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1330 new_state(PORT_STATE_OUT_SETUP);
1332 p_s_invite_direction = DIRECTION_OUT;
1335 PDEBUG(DEBUG_SIP, "do overlap\n");
1336 new_state(PORT_STATE_OUT_OVERLAP);
1337 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1338 message_put(message);
1340 PDEBUG(DEBUG_SIP, "do proceeding\n");
1341 new_state(PORT_STATE_OUT_PROCEEDING);
1342 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1343 message_put(message);
1346 /* attach only if not already */
1347 epointlist = p_epointlist;
1349 if (epointlist->epoint_id == epoint_id)
1351 epointlist = epointlist->next;
1354 epointlist_new(epoint_id);
1359 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1362 // struct in_addr ia;
1364 switch (param->notifyinfo.notify) {
1365 case INFO_NOTIFY_REMOTE_HOLD:
1367 sdp_str = generate_sdp(0, 0, 0, NULL, NULL);
1370 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1372 "c=IN IP4 0.0.0.0\r\n"
1375 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1376 nua_info(p_s_handle,
1377 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1378 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1379 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1380 NUTAG_MEDIA_ENABLE(0),
1381 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1382 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1385 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1387 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1388 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1389 nua_info(p_s_handle,
1390 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1391 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1392 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1393 NUTAG_MEDIA_ENABLE(0),
1394 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1395 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1403 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1407 /* prepare DTMF info payload */
1413 /* start invite to handle DTMF */
1414 nua_info(p_s_handle,
1415 NUTAG_MEDIA_ENABLE(0),
1416 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1417 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1422 /* NOTE: incomplete and not working */
1423 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1427 /* prepare DTMF info payload */
1431 , param->information.id);
1433 /* start invite to handle DTMF */
1434 nua_info(p_s_handle,
1435 NUTAG_MEDIA_ENABLE(0),
1436 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1437 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1442 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1444 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1446 if (Port::message_epoint(epoint_id, message_id, param))
1449 switch(message_id) {
1450 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1451 if (p_state != PORT_STATE_IN_SETUP
1452 && p_state != PORT_STATE_IN_PROCEEDING)
1454 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1455 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1456 add_trace("respond", "value", "180 Ringing");
1458 new_state(PORT_STATE_IN_ALERTING);
1461 case MESSAGE_CONNECT: /* call is connected on LCR side */
1462 if (p_state != PORT_STATE_IN_SETUP
1463 && p_state != PORT_STATE_IN_PROCEEDING
1464 && p_state != PORT_STATE_IN_ALERTING)
1466 message_connect(epoint_id, message_id, param);
1469 case MESSAGE_DISCONNECT: /* call has been disconnected */
1470 case MESSAGE_RELEASE: /* call has been released */
1471 message_release(epoint_id, message_id, param);
1474 case MESSAGE_SETUP: /* dial-out command received from epoint */
1475 message_setup(epoint_id, message_id, param);
1478 case MESSAGE_INFORMATION: /* overlap dialing */
1479 if (p_state != PORT_STATE_OUT_OVERLAP)
1481 message_information(epoint_id, message_id, param);
1484 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1485 if (p_state == PORT_STATE_CONNECT)
1486 message_dtmf(epoint_id, message_id, param);
1487 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1488 if (p_state == PORT_STATE_CONNECT)
1489 message_notify(epoint_id, message_id, param);
1493 PDEBUG(DEBUG_SIP, "PORT(%s) SIP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1499 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)
1503 if (!sip->sip_payload) {
1504 PDEBUG(DEBUG_SIP, "no payload given\n");
1508 sdp_parser_t *parser;
1511 sdp_attribute_t *attr;
1513 sdp_connection_t *conn;
1515 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1517 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1521 if (!(sdp = sdp_session(parser))) {
1522 sdp_parser_free(parser);
1525 for (m = sdp->sdp_media; m; m = m->m_next) {
1526 if (m->m_proto != sdp_proto_rtp)
1528 if (m->m_type != sdp_media_audio)
1530 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1532 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1533 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1535 if (m->m_connections) {
1536 conn = m->m_connections;
1537 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1538 inet_pton(AF_INET, conn->c_address, ip);
1539 *ip = ntohl(p_s_rtp_ip_remote);
1541 char *p = sip->sip_payload->pl_data;
1544 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1545 p = strstr(p, "c=IN IP4 ");
1547 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1548 sdp_parser_free(parser);
1552 if ((p = strchr(addr, '\n'))) *p = '\0';
1553 if ((p = strchr(addr, '\r'))) *p = '\0';
1554 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1555 inet_pton(AF_INET, addr, ip);
1556 *ip = ntohl(p_s_rtp_ip_remote);
1558 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1561 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1562 /* append to payload list, if there is space */
1563 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1564 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1565 media_type = MEDIA_TYPE_ALAW;
1566 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1567 media_type = MEDIA_TYPE_ULAW;
1568 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1569 media_type = MEDIA_TYPE_GSM;
1570 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1571 media_type = MEDIA_TYPE_GSM_EFR;
1572 else if (!strcmp(map->rm_encoding, "AMR"))
1573 media_type = MEDIA_TYPE_AMR;
1574 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1575 media_type = MEDIA_TYPE_GSM_HR;
1576 if (media_type && *payloads <= max_payloads) {
1577 *payload_types++ = map->rm_pt;
1578 *media_types++ = media_type;
1584 sdp_parser_free(parser);
1589 const char *Psip::generate_sdp(unsigned int rtp_ip_local, unsigned short rtp_port_local, int payloads, unsigned char *payload_types, int *media_types)
1592 static char sdp_str[256], sub_str[128];
1595 memset(&ia, 0, sizeof(ia));
1596 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1599 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1602 "t=0 0\r\n", inet_ntoa(ia), inet_ntoa(ia));
1604 SPRINT(sub_str, "m=audio %d RTP/AVP", p_s_rtp_port_local);
1605 SCAT(sdp_str, sub_str);
1606 for (i = 0; i < payloads; i++) {
1607 SPRINT(sub_str, " %d", payload_types[i]);
1608 SCAT(sdp_str, sub_str);
1610 SCAT(sdp_str, "\r\n");
1611 for (i = 0; i < payloads; i++) {
1612 SPRINT(sub_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1613 SCAT(sdp_str, sub_str);
1620 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[])
1622 sip_www_authenticate_t const *authenticate = NULL;
1623 char const *realm = NULL;
1624 char const *scheme = NULL;
1627 char authentication[256] = "";
1628 PDEBUG(DEBUG_SIP, "challenge order received\n");
1630 if (!inst->auth_user[0]) {
1631 PDEBUG(DEBUG_SIP, "No credentials available\n");
1632 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1633 add_trace("error", NULL, "There are no credentials given for interface");
1638 if (sip->sip_www_authenticate) {
1639 authenticate = sip->sip_www_authenticate;
1640 } else if (sip->sip_proxy_authenticate) {
1641 authenticate = sip->sip_proxy_authenticate;
1643 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1644 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1645 add_trace("error", NULL, "Authentication method unknwon");
1650 scheme = (char const *) authenticate->au_scheme;
1651 if (authenticate->au_params) {
1652 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1653 if ((realm = strstr(cur, "realm="))) {
1660 if (!scheme || !realm) {
1661 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1662 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1663 add_trace("error", NULL, "Authentication header has no realm or scheme");
1668 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1669 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1671 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1672 add_trace("scheme", NULL, "%s", scheme);
1673 add_trace("realm", NULL, "%s", realm);
1674 add_trace("user", NULL, "%s", inst->auth_user);
1675 add_trace("pass", NULL, "%s", inst->auth_password);
1678 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1683 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[])
1685 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1686 nua_saved_event_t saved[1];
1687 nua_save_event(nua, saved);
1688 nua_event_data_t const *data = nua_event_data(saved);
1690 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_IN);
1693 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1694 add_trace("respond", "value", "200 OK");
1697 nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
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 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
1726 nua_handle_destroy(nh);
1727 inst->register_handle = NULL;
1731 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1732 add_trace("contact", "uri", "%s", uri);
1735 sip_trace_header(NULL, inst->interface_name, "Authorization", DIRECTION_IN);
1736 if (inst->auth_realm[0]) {
1737 authorization = sip->sip_authorization;
1738 status = check_authorization(authorization, "REGISTER", inst->auth_user, inst->auth_password, inst->auth_realm, inst->auth_nonce, &auth_text);
1739 if (status == 401) {
1740 if (!inst->auth_nonce[0])
1741 generate_nonce(inst->auth_nonce);
1742 SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, inst->auth_nonce);
1746 auth_text = "Authentication not required";
1748 add_trace("result", NULL, "%s", auth_text);
1751 if (status == 200) {
1752 SCPY(inst->remote_peer, uri);
1755 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1756 add_trace("respond", "value", "%d", status);
1757 add_trace("reason", NULL, "peer registers");
1760 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());
1761 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
1762 nua_handle_destroy(nh);
1763 inst->register_handle = NULL;
1766 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[])
1770 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_IN);
1771 add_trace("value", NULL, "%d", status);
1772 add_trace("phrase", NULL, "%s", phrase);
1778 /* if not registered, become registered and start register interval timer */
1779 if (inst->register_state != REGISTER_STATE_REGISTERED) {
1780 if (inst->register_interval)
1781 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
1782 inst->register_state = REGISTER_STATE_REGISTERED;
1784 /* start option timer */
1785 if (inst->options_interval) {
1786 PDEBUG(DEBUG_SIP, "register ok, scheduling option timer with %d seconds\n", inst->options_interval);
1787 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1792 PDEBUG(DEBUG_SIP, "Register challenge received\n");
1793 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
1798 if (status >= 200 && status <= 299)
1803 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
1804 inst->register_state = REGISTER_STATE_FAILED;
1805 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
1806 nua_handle_destroy(nh);
1807 inst->register_handle = NULL;
1808 /* stop option timer */
1809 unsched_timer(&inst->register_option_timer);
1810 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1811 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1815 static void r_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[])
1817 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
1819 // if (status >= 200 && status <= 299) {
1820 PDEBUG(DEBUG_SIP, "options ok, scheduling option timer with %d seconds\n", inst->options_interval);
1821 /* restart option timer */
1822 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1826 PDEBUG(DEBUG_SIP, "Register options failed, starting register timer\n");
1827 inst->register_state = REGISTER_STATE_FAILED;
1828 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
1829 nua_handle_destroy(nh);
1830 inst->register_handle = NULL;
1831 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1832 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1835 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[])
1837 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1838 const char *from = "", *to = "", *name = "";
1841 class Endpoint *epoint;
1842 struct lcr_msg *message;
1843 struct interface *interface;
1844 const char *sdp_str = NULL;
1845 int media_types[32];
1846 uint8_t payload_types[32];
1848 unsigned char payload_type;
1850 sip_authorization_t const *authorization;
1851 const char *auth_text = NULL;
1852 char auth_str[256] = "";
1854 interface = getinterfacebyname(inst->interface_name);
1856 PERROR("Cannot find interface %s.\n", inst->interface_name);
1860 if (sip->sip_from) {
1861 if (sip->sip_from->a_url)
1862 from = sip->sip_from->a_url->url_user;
1863 if (sip->sip_from->a_display) {
1864 name = sip->sip_from->a_display;
1865 if (!strncmp(name, "\"IMSI", 5)) {
1866 strncpy(imsi, name + 5, 15);
1873 if (sip->sip_to->a_url)
1874 to = sip->sip_to->a_url->url_user;
1876 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1878 sip_trace_header(this, inst->interface_name, "Authorization", DIRECTION_IN);
1879 if (inst->auth_realm[0] && p_state == PORT_STATE_IDLE) {
1880 /* only authenticate remote, if we have a realm set and we don't have re-invite */
1881 authorization = sip->sip_proxy_authorization;
1882 status = check_authorization(authorization, "INVITE", inst->auth_user, inst->auth_password, inst->auth_realm, inst->auth_nonce, &auth_text);
1883 if (status == 407) {
1884 if (!inst->auth_nonce[0])
1885 generate_nonce(inst->auth_nonce);
1886 SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, inst->auth_nonce);
1890 auth_text = "Authentication not required";
1892 add_trace("result", NULL, "%s", auth_text);
1895 if (status == 200) {
1897 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1900 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1901 add_trace("respond", "value", "%d", status);
1902 add_trace("reason", NULL, "peer invited");
1905 nua_respond(nh, status, auth_text, SIPTAG_CONTACT(sip->sip_contact), TAG_IF(auth_str[0], SIPTAG_PROXY_AUTHENTICATE_STR(auth_str)), TAG_END());
1906 new_state(PORT_STATE_RELEASE);
1907 trigger_work(&p_s_delete);
1911 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1912 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1914 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1915 if (!p_s_rtp_bridge) {
1918 /* check if supported payload type exists */
1919 for (i = 0; i < payloads; i++) {
1920 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1923 if (i == payloads) {
1924 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1932 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1934 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1935 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
1936 nua_handle_destroy(nh);
1938 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1940 add_trace("respond", "value", "415 Unsupported Media");
1942 add_trace("respond", "value", "400 Bad Request");
1943 add_trace("reason", NULL, "offered codec does not match");
1945 if (p_state != PORT_STATE_IDLE) {
1946 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1947 message->param.disconnectinfo.cause = 41;
1948 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1949 message_put(message);
1951 new_state(PORT_STATE_RELEASE);
1952 trigger_work(&p_s_delete);
1956 /* handle re-invite */
1957 if (p_state != PORT_STATE_IDLE) {
1958 sip_trace_header(this, inst->interface_name, "RE-INVITE", DIRECTION_IN);
1960 if (p_s_rtp_bridge) {
1961 PDEBUG(DEBUG_SIP, "RE-INVITE not implemented for RTP forwarding\n");
1962 nua_respond(nh, SIP_501_NOT_IMPLEMENTED, TAG_END());
1963 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1964 add_trace("respond", "value", "501 NOT IMPLEMENTED");
1965 add_trace("reason", NULL, "RE-INVITE not implemented for RTP forwarding");
1968 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1969 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1970 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1971 if (rtp_connect() < 0) {
1974 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1975 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1976 nua_respond(p_s_handle, SIP_200_OK,
1977 NUTAG_MEDIA_ENABLE(0),
1978 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1979 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1984 /* open local RTP peer (if not bridging) */
1985 if (!p_s_rtp_bridge && rtp_open() < 0) {
1986 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1987 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
1988 nua_handle_destroy(nh);
1990 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1991 add_trace("respond", "value", "500 Internal Server Error");
1992 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1994 new_state(PORT_STATE_RELEASE);
1995 trigger_work(&p_s_delete);
2000 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
2001 // add_trace("handle", "new", "0x%x", nh);
2006 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
2007 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
2008 /* caller information */
2010 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
2011 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
2012 add_trace("calling", "present", "unavailable");
2014 p_callerinfo.present = INFO_PRESENT_ALLOWED;
2015 add_trace("calling", "present", "allowed");
2016 p_callerinfo.screen = INFO_SCREEN_NETWORK;
2017 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
2018 SCPY(p_callerinfo.id, from);
2019 add_trace("calling", "number", "%s", from);
2020 SCPY(p_callerinfo.name, name);
2022 add_trace("calling", "name", "%s", name);
2023 SCPY(p_callerinfo.imsi, imsi);
2025 add_trace("calling", "imsi", "%s", imsi);
2027 SCPY(p_callerinfo.interface, inst->interface_name);
2028 /* dialing information */
2030 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
2031 SCAT(p_dialinginfo.id, to);
2032 add_trace("dialing", "number", "%s", to);
2035 /* bearer capability */
2036 p_capainfo.bearer_capa = INFO_BC_SPEECH;
2037 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
2038 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
2039 add_trace("bearer", "capa", "speech");
2040 add_trace("bearer", "mode", "circuit");
2041 /* if packet mode works some day, see dss1.cpp for conditions */
2042 p_capainfo.source_mode = B_MODE_TRANSPARENT;
2046 /* create endpoint */
2048 FATAL("Incoming call but already got an endpoint.\n");
2049 if (!(epoint = new Endpoint(p_serial, 0)))
2050 FATAL("No memory for Endpoint instance\n");
2051 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
2052 epointlist_new(epoint->ep_serial);
2054 #ifdef NUTAG_AUTO100
2055 /* send trying (proceeding) */
2056 nua_respond(nh, SIP_100_TRYING, TAG_END());
2057 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2058 add_trace("respond", "value", "100 Trying");
2062 new_state(PORT_STATE_IN_PROCEEDING);
2064 /* send setup message to endpoit */
2065 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
2066 message->param.setup.port_type = p_type;
2067 // message->param.setup.dtmf = 0;
2068 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
2069 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
2070 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
2071 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
2072 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
2073 // message->param.setup.useruser.protocol = mncc->useruser.proto;
2074 if (p_s_rtp_bridge) {
2077 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
2078 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
2079 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
2080 /* add codecs to setup message */
2081 for (i = 0; i < payloads; i++) {
2082 message->param.setup.rtpinfo.media_types[i] = media_types[i];
2083 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
2084 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
2087 message->param.setup.rtpinfo.payloads = i;
2089 message_put(message);
2091 /* start option timer */
2092 if (inst->options_interval) {
2093 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer with %d seconds\n", inst->options_interval);
2094 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2097 p_s_invite_direction = DIRECTION_IN;
2099 /* send progress, if tones are available and if we don't bridge */
2100 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
2101 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
2102 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
2103 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
2104 /* open local RTP peer (if not bridging) */
2105 if (rtp_connect() < 0) {
2107 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2108 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2109 nua_handle_destroy(nh);
2111 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2112 add_trace("respond", "value", "500 Internal Server Error");
2113 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
2115 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2116 message->param.disconnectinfo.cause = 41;
2117 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2118 message_put(message);
2119 new_state(PORT_STATE_RELEASE);
2120 trigger_work(&p_s_delete);
2124 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
2125 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
2127 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
2128 NUTAG_MEDIA_ENABLE(0),
2129 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
2130 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
2131 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2132 add_trace("respond", "value", "183 SESSION PROGRESS");
2133 add_trace("reason", NULL, "audio available");
2135 memset(&ia, 0, sizeof(ia));
2136 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
2137 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
2138 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
2139 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
2144 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[])
2146 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2148 PDEBUG(DEBUG_SIP, "options received\n");
2150 sip_trace_header(this, inst->interface_name, "OPTIONS", DIRECTION_IN);
2153 nua_respond(nh, SIP_200_OK, TAG_END());
2156 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[])
2158 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2159 struct lcr_msg *message;
2162 PDEBUG(DEBUG_SIP, "bye received\n");
2164 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
2165 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
2166 cause = atoi(sip->sip_reason->re_cause);
2167 add_trace("cause", "value", "%d", cause);
2171 // let stack do bye automaticall, since it will not accept our response for some reason
2172 // nua_respond(nh, SIP_200_OK, TAG_END());
2173 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2174 add_trace("respond", "value", "200 OK");
2176 PDEBUG(DEBUG_SIP, "just remove nua_handle without destruction %x\n", nh);
2177 // nua_handle_destroy(nh);
2182 while(p_epointlist) {
2183 /* send setup message to endpoit */
2184 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2185 message->param.disconnectinfo.cause = cause ? : 16;
2186 message->param.disconnectinfo.location = LOCATION_BEYOND;
2187 message_put(message);
2189 free_epointlist(p_epointlist);
2191 new_state(PORT_STATE_RELEASE);
2192 trigger_work(&p_s_delete);
2195 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[])
2197 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2198 struct lcr_msg *message;
2200 PDEBUG(DEBUG_SIP, "cancel received\n");
2202 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
2205 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2206 nua_handle_destroy(nh);
2211 while(p_epointlist) {
2212 /* send setup message to endpoit */
2213 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2214 message->param.disconnectinfo.cause = 16;
2215 message->param.disconnectinfo.location = LOCATION_BEYOND;
2216 message_put(message);
2218 free_epointlist(p_epointlist);
2220 new_state(PORT_STATE_RELEASE);
2221 trigger_work(&p_s_delete);
2224 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[])
2226 PDEBUG(DEBUG_SIP, "bye response received\n");
2228 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2229 nua_handle_destroy(nh);
2234 trigger_work(&p_s_delete);
2237 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[])
2239 PDEBUG(DEBUG_SIP, "cancel response received\n");
2241 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2242 nua_handle_destroy(nh);
2247 trigger_work(&p_s_delete);
2250 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[])
2252 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2253 struct lcr_msg *message;
2254 int cause = 0, location = 0;
2255 int media_types[32];
2256 uint8_t payload_types[32];
2259 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
2261 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2262 add_trace("respond", "value", "%d", status);
2265 if (status == 401 || status == 407) {
2266 PDEBUG(DEBUG_SIP, "Invite challenge received\n");
2267 challenge(inst, this, status, phrase, nua, magic, nh, hmagic, sip, tags);
2272 if (status == 183 || (status >= 200 && status <= 299)) {
2275 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
2276 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
2280 else if (!p_s_rtp_bridge) {
2281 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
2282 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
2289 nua_cancel(nh, TAG_END());
2290 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2291 add_trace("reason", NULL, "accepted codec does not match");
2294 location = LOCATION_PRIVATE_LOCAL;
2295 goto release_with_cause;
2298 /* connect to remote RTP (if not bridging) */
2299 if (!p_s_rtp_bridge && rtp_connect() < 0) {
2300 nua_cancel(nh, TAG_END());
2301 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2302 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
2305 location = LOCATION_PRIVATE_LOCAL;
2306 goto release_with_cause;
2310 /* start option timer */
2311 if (inst->options_interval) {
2312 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer with %d seconds\n", inst->options_interval);
2313 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2319 PDEBUG(DEBUG_SIP, "do proceeding\n");
2320 new_state(PORT_STATE_OUT_PROCEEDING);
2321 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
2322 message_put(message);
2326 PDEBUG(DEBUG_SIP, "do alerting\n");
2327 new_state(PORT_STATE_OUT_ALERTING);
2328 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
2329 message_put(message);
2332 PDEBUG(DEBUG_SIP, "do progress\n");
2333 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
2334 message->param.progressinfo.progress = 8;
2335 message->param.progressinfo.location = 10;
2336 if (p_s_rtp_bridge) {
2337 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2338 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
2339 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
2340 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
2341 message->param.progressinfo.rtpinfo.payloads = 1;
2343 message_put(message);
2347 PDEBUG(DEBUG_SIP, "do connect\n");
2348 nua_ack(nh, TAG_END());
2349 new_state(PORT_STATE_CONNECT);
2350 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
2351 if (p_s_rtp_bridge) {
2352 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2353 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
2354 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
2355 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
2356 message->param.connectinfo.rtpinfo.payloads = 1;
2358 message_put(message);
2361 if (status >= 200 && status <= 299)
2363 if (status < 100 || status > 199)
2365 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
2370 cause = status2cause(status);
2371 location = LOCATION_BEYOND;
2374 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
2376 while(p_epointlist) {
2377 /* send setup message to endpoit */
2378 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2379 message->param.disconnectinfo.cause = cause;
2380 message->param.disconnectinfo.location = location;
2381 message_put(message);
2383 free_epointlist(p_epointlist);
2386 new_state(PORT_STATE_RELEASE);
2390 trigger_work(&p_s_delete);
2393 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[])
2395 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2396 int cause = 0, location = 0;
2397 struct lcr_msg *message;
2399 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
2401 if (status >= 200 && status <= 299) {
2402 PDEBUG(DEBUG_SIP, "options ok, scheduling option timer with %d seconds\n", inst->options_interval);
2403 /* restart option timer */
2404 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2408 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2409 nua_handle_destroy(nh);
2414 cause = status2cause(status);
2415 location = LOCATION_BEYOND;
2417 while(p_epointlist) {
2418 /* send setup message to endpoit */
2419 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2420 message->param.disconnectinfo.cause = cause;
2421 message->param.disconnectinfo.location = location;
2422 message_put(message);
2424 free_epointlist(p_epointlist);
2426 new_state(PORT_STATE_RELEASE);
2427 trigger_work(&p_s_delete);
2430 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[])
2432 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2434 PDEBUG(DEBUG_SIP, "state change received\n");
2435 sip_trace_header(this, inst->interface_name, "STATUS", DIRECTION_OUT);
2436 add_trace("value", NULL, "%d", status);
2437 add_trace("phrase", NULL, "%s", phrase);
2441 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[])
2443 struct sip_inst *inst = (struct sip_inst *) magic;
2445 class Psip *psip = NULL;
2447 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2451 /* hunt for existing handles */
2454 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2455 psip = (class Psip *)port;
2456 if (psip->p_s_handle == nh) {
2457 PDEBUG(DEBUG_SIP, "Event found for port %s\n", psip->p_name);
2470 i_options(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2471 if (inst->register_handle != nh) {
2472 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2473 nua_handle_destroy(nh);
2480 r_options(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2484 case nua_i_register:
2485 if (!psip && !inst->register_handle) {
2486 PDEBUG(DEBUG_SIP, "New register instance\n");
2487 inst->register_handle = nh;
2490 i_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2494 case nua_r_register:
2496 r_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2503 struct interface *interface = interface_first;
2505 PDEBUG(DEBUG_SIP, "New psip instance\n");
2507 /* create call instance */
2508 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2510 if (!strcmp(interface->name, inst->interface_name))
2512 interface = interface->next;
2515 FATAL("Cannot find interface %s.\n", inst->interface_name);
2516 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2517 FATAL("Cannot create Port instance.\n");
2520 case nua_i_outbound:
2521 PDEBUG(DEBUG_SIP, "Outbound status\n");
2527 /* handle port process */
2529 if (nh != inst->register_handle) {
2530 PERROR("no SIP Port found for handel %p\n", nh);
2531 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2532 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", nh);
2533 nua_handle_destroy(nh);
2539 case nua_r_set_params:
2540 PDEBUG(DEBUG_SIP, "setparam response\n");
2543 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2546 PDEBUG(DEBUG_SIP, "error received\n");
2549 psip->i_state(status, phrase, nua, magic, nh, hmagic, sip, tags);
2552 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2555 PDEBUG(DEBUG_SIP, "ack received\n");
2558 PDEBUG(DEBUG_SIP, "active received\n");
2561 psip->i_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2564 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2567 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2570 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2573 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2576 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2578 case nua_i_terminated:
2579 PDEBUG(DEBUG_SIP, "terminated received\n");
2582 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2586 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)
2588 struct sip_inst *inst = (struct sip_inst *) magic;
2592 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2595 case stun_discovery_done:
2596 addrlen = sizeof(sa);
2597 memset(&sa, 0, addrlen);
2598 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2599 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2602 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2603 inst->stun_state = STUN_STATE_RESOLVED;
2604 /* start timer for next stun request with inst->stun_interval */
2605 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2606 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2607 add_trace("ip", "addr", "%s", inst->public_ip);
2612 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2613 inst->stun_state = STUN_STATE_FAILED;
2614 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2615 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2616 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2621 /* received shutdown due to termination of RTP */
2622 void Psip::rtp_shutdown(void)
2624 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2625 struct lcr_msg *message;
2627 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2629 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2632 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", p_s_handle);
2633 nua_handle_destroy(p_s_handle);
2636 while(p_epointlist) {
2637 /* send setup message to endpoit */
2638 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2639 message->param.disconnectinfo.cause = 16;
2640 message->param.disconnectinfo.location = LOCATION_BEYOND;
2641 message_put(message);
2643 free_epointlist(p_epointlist);
2645 new_state(PORT_STATE_RELEASE);
2646 trigger_work(&p_s_delete);
2649 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2651 class Psip *psip = (class Psip *)instance;
2652 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2654 PDEBUG(DEBUG_SIP, "invite options timer fired\n");
2656 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2659 nua_options(psip->p_s_handle,
2665 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2667 struct sip_inst *inst = (struct sip_inst *)instance;
2669 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2670 inst->stun_state = STUN_STATE_UNRESOLVED;
2675 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2677 struct sip_inst *inst = (struct sip_inst *)instance;
2679 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2680 /* if we have a handle, destroy it and becom unregistered, so registration is
2682 if (inst->register_handle) {
2683 /* stop option timer */
2684 unsched_timer(&inst->register_option_timer);
2685 PDEBUG(DEBUG_SIP, "nua_handle_destroy %x\n", inst->register_handle);
2686 nua_handle_destroy(inst->register_handle);
2687 inst->register_handle = NULL;
2689 inst->register_state = REGISTER_STATE_UNREGISTERED;
2694 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2696 struct sip_inst *inst = (struct sip_inst *)instance;
2698 PDEBUG(DEBUG_SIP, "register options timer fired\n");
2700 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2703 nua_options(inst->register_handle,
2709 int sip_init_inst(struct interface *interface)
2711 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2714 interface->sip_inst = inst;
2715 SCPY(inst->interface_name, interface->name);
2716 SCPY(inst->local_peer, interface->sip_local_peer);
2717 SCPY(inst->remote_peer, interface->sip_remote_peer);
2718 if (!inst->remote_peer[0])
2719 inst->allow_register = 1;
2720 SCPY(inst->asserted_id, interface->sip_asserted_id);
2721 if (interface->sip_register) {
2722 inst->register_state = REGISTER_STATE_UNREGISTERED;
2723 SCPY(inst->register_user, interface->sip_register_user);
2724 SCPY(inst->register_host, interface->sip_register_host);
2726 SCPY(inst->auth_user, interface->sip_auth_user);
2727 SCPY(inst->auth_password, interface->sip_auth_password);
2728 SCPY(inst->auth_realm, interface->sip_auth_realm);
2729 inst->register_interval = interface->sip_register_interval;
2730 inst->options_interval = interface->sip_options_interval;
2732 inst->rtp_port_from = interface->rtp_port_from;
2733 inst->rtp_port_to = interface->rtp_port_to;
2734 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2735 inst->rtp_port_from = RTP_PORT_BASE;
2736 inst->rtp_port_to = RTP_PORT_MAX;
2738 inst->next_rtp_port = inst->rtp_port_from;
2741 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2742 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2743 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2744 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2745 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2746 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2748 /* init root object */
2749 inst->root = su_root_create(inst);
2751 PERROR("Failed to create SIP root\n");
2752 sip_exit_inst(interface);
2756 SPRINT(local, "sip:%s",inst->local_peer);
2757 if (!strchr(inst->local_peer, ':'))
2758 SCAT(local, ":5060");
2759 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2761 PERROR("Failed to create SIP stack object\n");
2762 sip_exit_inst(interface);
2765 nua_set_params(inst->nua,
2766 SIPTAG_ALLOW_STR("REGISTER,INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2767 NUTAG_APPL_METHOD("REGISTER"),
2768 NUTAG_APPL_METHOD("INVITE"),
2769 NUTAG_APPL_METHOD("ACK"),
2770 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2771 NUTAG_APPL_METHOD("CANCEL"),
2772 NUTAG_APPL_METHOD("OPTIONS"),
2773 NUTAG_APPL_METHOD("NOTIFY"),
2774 NUTAG_APPL_METHOD("INFO"),
2776 #ifdef NUTAG_AUTO100
2780 NUTAG_AUTOANSWER(0),
2783 SCPY(inst->public_ip, interface->sip_public_ip);
2784 if (interface->sip_stun_server[0]) {
2785 SCPY(inst->stun_server, interface->sip_stun_server);
2786 inst->stun_interval = interface->sip_stun_interval;
2787 inst->stun_handle = stun_handle_init(inst->root,
2788 STUNTAG_SERVER(inst->stun_server),
2790 if (!inst->stun_handle) {
2791 PERROR("Failed to create STUN handle\n");
2792 sip_exit_inst(interface);
2795 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2796 if (inst->stun_socket < 0) {
2797 PERROR("Failed to create STUN socket\n");
2798 sip_exit_inst(interface);
2801 inst->stun_state = STUN_STATE_UNRESOLVED;
2804 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2806 any_sip_interface = 1;
2811 void sip_exit_inst(struct interface *interface)
2813 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2817 del_timer(&inst->stun_retry_timer);
2818 del_timer(&inst->register_retry_timer);
2819 del_timer(&inst->register_option_timer);
2820 if (inst->stun_socket)
2821 su_close(inst->stun_socket);
2822 if (inst->stun_handle)
2823 stun_handle_destroy(inst->stun_handle);
2824 if (inst->register_handle)
2825 nua_handle_destroy(inst->register_handle);
2827 su_root_destroy(inst->root);
2829 nua_destroy(inst->nua);
2830 FREE(inst, sizeof(*inst));
2831 interface->sip_inst = NULL;
2833 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2835 /* check if there is any other SIP interface left */
2836 interface = interface_first;
2838 if (interface->sip_inst)
2840 interface = interface->next;
2843 any_sip_interface = 0;
2846 extern su_log_t su_log_default[];
2847 extern su_log_t nua_log[];
2853 /* init SOFIA lib */
2855 su_home_init(sip_home);
2857 if (options.deb & DEBUG_SIP) {
2858 su_log_set_level(su_log_default, 9);
2859 su_log_set_level(nua_log, 9);
2860 //su_log_set_level(soa_log, 9);
2863 for (i = 0; i < 256; i++)
2864 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);
2866 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2873 su_home_deinit(sip_home);
2876 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2879 static void sip_handle_stun(struct sip_inst *inst)
2883 switch (inst->stun_state) {
2884 case STUN_STATE_UNRESOLVED:
2885 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2886 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2887 STUNTAG_SOCKET(inst->stun_socket),
2888 STUNTAG_REGISTER_EVENTS(1),
2891 PERROR("Failed to call stun_bind()\n");
2892 inst->stun_state = STUN_STATE_FAILED;
2895 inst->stun_state = STUN_STATE_RESOLVING;
2896 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2897 add_trace("server", "addr", "%s", inst->stun_server);
2903 static void sip_handle_register(struct sip_inst *inst)
2905 char from[128] = "";
2907 char contact[128] = "";
2908 char expires[128] = "";
2910 switch (inst->register_state) {
2911 case REGISTER_STATE_UNREGISTERED:
2912 /* wait for resoved stun */
2913 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2916 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2917 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2918 if (!inst->register_handle) {
2919 PERROR("Failed to create handle\n");
2920 inst->register_state = REGISTER_STATE_FAILED;
2923 /* apply handle to trace */
2924 // sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2925 // add_trace("handle", "new", "0x%x", inst->register_handle);
2928 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2929 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2930 if (inst->public_ip[0]) {
2932 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2933 p = strchr(inst->local_peer, ':');
2938 if (inst->register_interval) {
2939 SPRINT(expires, "%d", inst->register_interval + 60);
2942 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2943 add_trace("from", "uri", "%s", from);
2944 add_trace("to", "uri", "%s", to);
2946 add_trace("expires", NULL, "%s", expires);
2949 nua_register(inst->register_handle,
2950 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2951 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2952 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2953 TAG_IF(expires[0], SIPTAG_EXPIRES_STR(expires)),
2956 inst->register_state = REGISTER_STATE_REGISTERING;
2963 void sip_handle(void)
2965 struct interface *interface = interface_first;
2966 struct sip_inst *inst;
2969 if (interface->sip_inst) {
2970 inst = (struct sip_inst *) interface->sip_inst;
2971 su_root_step(inst->root, 0);
2972 sip_handle_stun(inst);
2973 sip_handle_register(inst);
2975 interface = interface->next;
2979 /* deletes when back in event loop */
2980 static int delete_event(struct lcr_work *work, void *instance, int index)
2982 class Psip *psip = (class Psip *)instance;
2991 * generate audio, if no data is received from bridge
2994 void Psip::set_tone(const char *dir, const char *tone)
2996 Port::set_tone(dir, tone);
3001 void Psip::update_load(void)
3003 /* don't trigger load event if event already active */
3004 if (p_s_load_timer.active)
3007 /* don't start timer if ... */
3008 if (!p_tone_name[0] && !p_dov_tx)
3011 p_s_next_tv_sec = 0;
3012 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
3015 static int load_timer(struct lcr_timer *timer, void *instance, int index)
3017 class Psip *psip = (class Psip *)instance;
3019 /* stop timer if ... */
3020 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
3028 #define SEND_SIP_LEN 160
3030 void Psip::load_tx(void)
3033 struct timeval current_time;
3034 int tosend = SEND_SIP_LEN, i;
3035 unsigned char buf[SEND_SIP_LEN], *p = buf;
3038 gettimeofday(¤t_time, NULL);
3039 if (!p_s_next_tv_sec) {
3040 /* if timer expired the first time, set next expected timeout 160 samples in advance */
3041 p_s_next_tv_sec = current_time.tv_sec;
3042 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
3043 if (p_s_next_tv_usec >= 1000000) {
3044 p_s_next_tv_usec -= 1000000;
3047 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
3049 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
3050 + (current_time.tv_usec - p_s_next_tv_usec);
3051 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
3052 /* if clock drifts too much, set next timeout event to current timer + 160 */
3054 p_s_next_tv_sec = current_time.tv_sec;
3055 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
3056 if (p_s_next_tv_usec >= 1000000) {
3057 p_s_next_tv_usec -= 1000000;
3061 /* if diff is positive, it took too long, so next timeout will be earlier */
3062 p_s_next_tv_usec += SEND_SIP_LEN * 125;
3063 if (p_s_next_tv_usec >= 1000000) {
3064 p_s_next_tv_usec -= 1000000;
3068 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
3072 if (p_tone_name[0]) {
3073 tosend -= read_audio(p, tosend);
3076 tosend -= dov_tx(p, tosend);
3079 PERROR("buffer is not completely filled\n");
3084 for (i = 0; i < SEND_SIP_LEN; i++) {
3088 /* transmit data via rtp */
3089 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);