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;
67 nua_handle_t *options_handle;
70 char auth_password[128];
78 char stun_server[128];
79 stun_handle_t *stun_handle;
80 su_socket_t stun_socket;
81 struct lcr_timer stun_retry_timer;
84 unsigned short rtp_port_from;
85 unsigned short rtp_port_to;
86 unsigned short next_rtp_port;
90 static int delete_event(struct lcr_work *work, void *instance, int index);
91 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index);
92 static int load_timer(struct lcr_timer *timer, void *instance, int index);
97 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
100 if (interface->rtp_bridge)
102 p_s_sip_inst = interface->sip_inst;
103 memset(&p_s_delete, 0, sizeof(p_s_delete));
104 add_work(&p_s_delete, delete_event, this, 0);
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, "Second 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(from, "\"%s\" <sip:%s@%s>", /*p_callerinfo.id*/ "4946448519988", p_callerinfo.id, remote);
1291 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1292 if (inst->asserted_id[0]) {
1293 SPRINT(asserted_id, "sip:%s@%s", inst->asserted_id, remote);
1294 SPRINT(asserted_msg, "P-Asserted-Identity: <%s>", asserted_id);
1296 if (inst->public_ip[0]) {
1298 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1299 p = strchr(inst->local_peer, ':');
1304 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1305 add_trace("from", "uri", "%s", from);
1306 add_trace("to", "uri", "%s", to);
1308 add_trace("assert-id", "uri", "%s", asserted_id);
1310 memset(&ia, 0, sizeof(ia));
1311 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1312 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1313 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1314 for (i = 0; i < payloads; i++)
1315 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1318 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1320 nua_invite(p_s_handle,
1321 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1322 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1323 TAG_IF(asserted_msg[0], SIPTAG_HEADER_STR(asserted_msg)),
1324 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1325 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1326 NUTAG_MEDIA_ENABLE(0),
1327 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1328 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1329 new_state(PORT_STATE_OUT_SETUP);
1331 p_s_invite_direction = DIRECTION_OUT;
1334 PDEBUG(DEBUG_SIP, "do overlap\n");
1335 new_state(PORT_STATE_OUT_OVERLAP);
1336 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1337 message_put(message);
1339 PDEBUG(DEBUG_SIP, "do proceeding\n");
1340 new_state(PORT_STATE_OUT_PROCEEDING);
1341 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1342 message_put(message);
1345 /* attach only if not already */
1346 epointlist = p_epointlist;
1348 if (epointlist->epoint_id == epoint_id)
1350 epointlist = epointlist->next;
1353 epointlist_new(epoint_id);
1358 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1361 // struct in_addr ia;
1363 switch (param->notifyinfo.notify) {
1364 case INFO_NOTIFY_REMOTE_HOLD:
1366 sdp_str = generate_sdp(0, 0, 0, NULL, NULL);
1369 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1371 "c=IN IP4 0.0.0.0\r\n"
1374 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1375 nua_info(p_s_handle,
1376 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1377 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1378 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1379 NUTAG_MEDIA_ENABLE(0),
1380 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1381 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1384 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1386 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1387 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1388 nua_info(p_s_handle,
1389 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1390 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1391 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1392 NUTAG_MEDIA_ENABLE(0),
1393 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1394 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1402 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1406 /* prepare DTMF info payload */
1412 /* start invite to handle DTMF */
1413 nua_info(p_s_handle,
1414 NUTAG_MEDIA_ENABLE(0),
1415 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1416 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1421 /* NOTE: incomplete and not working */
1422 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1426 /* prepare DTMF info payload */
1430 , param->information.id);
1432 /* start invite to handle DTMF */
1433 nua_info(p_s_handle,
1434 NUTAG_MEDIA_ENABLE(0),
1435 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1436 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1441 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1443 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1445 if (Port::message_epoint(epoint_id, message_id, param))
1448 switch(message_id) {
1449 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1450 if (p_state != PORT_STATE_IN_SETUP
1451 && p_state != PORT_STATE_IN_PROCEEDING)
1453 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1454 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1455 add_trace("respond", "value", "180 Ringing");
1457 new_state(PORT_STATE_IN_ALERTING);
1460 case MESSAGE_CONNECT: /* call is connected on LCR side */
1461 if (p_state != PORT_STATE_IN_SETUP
1462 && p_state != PORT_STATE_IN_PROCEEDING
1463 && p_state != PORT_STATE_IN_ALERTING)
1465 message_connect(epoint_id, message_id, param);
1468 case MESSAGE_DISCONNECT: /* call has been disconnected */
1469 case MESSAGE_RELEASE: /* call has been released */
1470 message_release(epoint_id, message_id, param);
1473 case MESSAGE_SETUP: /* dial-out command received from epoint */
1474 message_setup(epoint_id, message_id, param);
1477 case MESSAGE_INFORMATION: /* overlap dialing */
1478 if (p_state != PORT_STATE_OUT_OVERLAP)
1480 message_information(epoint_id, message_id, param);
1483 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1484 if (p_state == PORT_STATE_CONNECT)
1485 message_dtmf(epoint_id, message_id, param);
1486 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1487 if (p_state == PORT_STATE_CONNECT)
1488 message_notify(epoint_id, message_id, param);
1492 PDEBUG(DEBUG_SIP, "PORT(%s) SIP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1498 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)
1502 if (!sip->sip_payload) {
1503 PDEBUG(DEBUG_SIP, "no payload given\n");
1507 sdp_parser_t *parser;
1510 sdp_attribute_t *attr;
1512 sdp_connection_t *conn;
1514 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1516 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1520 if (!(sdp = sdp_session(parser))) {
1521 sdp_parser_free(parser);
1524 for (m = sdp->sdp_media; m; m = m->m_next) {
1525 if (m->m_proto != sdp_proto_rtp)
1527 if (m->m_type != sdp_media_audio)
1529 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1531 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1532 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1534 if (m->m_connections) {
1535 conn = m->m_connections;
1536 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1537 inet_pton(AF_INET, conn->c_address, ip);
1538 *ip = ntohl(p_s_rtp_ip_remote);
1540 char *p = sip->sip_payload->pl_data;
1543 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1544 p = strstr(p, "c=IN IP4 ");
1546 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1547 sdp_parser_free(parser);
1551 if ((p = strchr(addr, '\n'))) *p = '\0';
1552 if ((p = strchr(addr, '\r'))) *p = '\0';
1553 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1554 inet_pton(AF_INET, addr, ip);
1555 *ip = ntohl(p_s_rtp_ip_remote);
1557 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1560 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1561 /* append to payload list, if there is space */
1562 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1563 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1564 media_type = MEDIA_TYPE_ALAW;
1565 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1566 media_type = MEDIA_TYPE_ULAW;
1567 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1568 media_type = MEDIA_TYPE_GSM;
1569 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1570 media_type = MEDIA_TYPE_GSM_EFR;
1571 else if (!strcmp(map->rm_encoding, "AMR"))
1572 media_type = MEDIA_TYPE_AMR;
1573 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1574 media_type = MEDIA_TYPE_GSM_HR;
1575 if (media_type && *payloads <= max_payloads) {
1576 *payload_types++ = map->rm_pt;
1577 *media_types++ = media_type;
1583 sdp_parser_free(parser);
1588 const char *Psip::generate_sdp(unsigned int rtp_ip_local, unsigned short rtp_port_local, int payloads, unsigned char *payload_types, int *media_types)
1591 static char sdp_str[256], sub_str[128];
1594 memset(&ia, 0, sizeof(ia));
1595 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1598 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1601 "t=0 0\r\n", inet_ntoa(ia), inet_ntoa(ia));
1603 SPRINT(sub_str, "m=audio %d RTP/AVP", p_s_rtp_port_local);
1604 SCAT(sdp_str, sub_str);
1605 for (i = 0; i < payloads; i++) {
1606 SPRINT(sub_str, " %d", payload_types[i]);
1607 SCAT(sdp_str, sub_str);
1609 SCAT(sdp_str, "\r\n");
1610 for (i = 0; i < payloads; i++) {
1611 SPRINT(sub_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1612 SCAT(sdp_str, sub_str);
1619 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[])
1621 sip_www_authenticate_t const *authenticate = NULL;
1622 char const *realm = NULL;
1623 char const *scheme = NULL;
1626 char authentication[256] = "";
1627 PDEBUG(DEBUG_SIP, "challenge order received\n");
1629 if (!inst->auth_user[0]) {
1630 PDEBUG(DEBUG_SIP, "No credentials available\n");
1631 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1632 add_trace("error", NULL, "There are no credentials given for interface");
1637 if (sip->sip_www_authenticate) {
1638 authenticate = sip->sip_www_authenticate;
1639 } else if (sip->sip_proxy_authenticate) {
1640 authenticate = sip->sip_proxy_authenticate;
1642 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1643 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1644 add_trace("error", NULL, "Authentication method unknwon");
1649 scheme = (char const *) authenticate->au_scheme;
1650 if (authenticate->au_params) {
1651 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1652 if ((realm = strstr(cur, "realm="))) {
1659 if (!scheme || !realm) {
1660 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1661 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1662 add_trace("error", NULL, "Authentication header has no realm or scheme");
1667 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1668 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1670 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1671 add_trace("scheme", NULL, "%s", scheme);
1672 add_trace("realm", NULL, "%s", realm);
1673 add_trace("user", NULL, "%s", inst->auth_user);
1674 add_trace("pass", NULL, "%s", inst->auth_password);
1677 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1682 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[])
1684 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1685 nua_saved_event_t saved[1];
1686 nua_save_event(nua, saved);
1687 nua_event_data_t const *data = nua_event_data(saved);
1689 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_IN);
1692 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1693 add_trace("respond", "value", "200 OK");
1696 nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1697 nua_handle_destroy(nh);
1698 inst->options_handle = NULL;
1701 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[])
1703 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1704 nua_saved_event_t saved[1];
1705 sip_contact_t const *contact = NULL;
1706 contact = sip->sip_contact;
1707 nua_save_event(nua, saved);
1708 nua_event_data_t const *data = nua_event_data(saved);
1709 sip_authorization_t const *authorization;
1711 const char *auth_text = NULL;
1712 char auth_str[256] = "";
1714 if (contact->m_url->url_host)
1715 SCPY(uri, contact->m_url->url_host);
1716 if (contact->m_url->url_port && contact->m_url->url_port[0]) {
1718 SCAT(uri, contact->m_url->url_port);
1721 if (!inst->allow_register) {
1722 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1723 add_trace("error", NULL, "forbidden, because we don't accept registration");
1725 nua_respond(nh, SIP_403_FORBIDDEN, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
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 nua_handle_destroy(nh);
1762 inst->register_handle = NULL;
1765 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[])
1769 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_IN);
1770 add_trace("value", NULL, "%d", status);
1771 add_trace("phrase", NULL, "%s", phrase);
1777 /* if not registered, become registered and start register interval timer */
1778 if (inst->register_state != REGISTER_STATE_REGISTERED) {
1779 if (inst->register_interval)
1780 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
1781 inst->register_state = REGISTER_STATE_REGISTERED;
1783 /* start option timer */
1784 if (inst->options_interval)
1785 PDEBUG(DEBUG_SIP, "register ok, scheduling option timer with %d seconds\n", inst->options_interval);
1786 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1790 PDEBUG(DEBUG_SIP, "Register challenge received\n");
1791 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
1796 if (status >= 200 && status <= 299)
1801 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
1802 inst->register_state = REGISTER_STATE_FAILED;
1803 nua_handle_destroy(nh);
1804 inst->register_handle = NULL;
1805 /* stop option timer */
1806 unsched_timer(&inst->register_option_timer);
1807 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1808 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1812 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[])
1814 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1815 const char *from = "", *to = "", *name = "";
1818 class Endpoint *epoint;
1819 struct lcr_msg *message;
1820 struct interface *interface;
1821 const char *sdp_str = NULL;
1822 int media_types[32];
1823 uint8_t payload_types[32];
1825 unsigned char payload_type;
1827 sip_authorization_t const *authorization;
1828 const char *auth_text = NULL;
1829 char auth_str[256] = "";
1831 interface = getinterfacebyname(inst->interface_name);
1833 PERROR("Cannot find interface %s.\n", inst->interface_name);
1837 if (sip->sip_from) {
1838 if (sip->sip_from->a_url)
1839 from = sip->sip_from->a_url->url_user;
1840 if (sip->sip_from->a_display) {
1841 name = sip->sip_from->a_display;
1842 if (!strncmp(name, "\"IMSI", 5)) {
1843 strncpy(imsi, name + 5, 15);
1850 if (sip->sip_to->a_url)
1851 to = sip->sip_to->a_url->url_user;
1853 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1855 sip_trace_header(this, inst->interface_name, "Authorization", DIRECTION_IN);
1856 if (inst->auth_realm[0] && p_state == PORT_STATE_IDLE) {
1857 /* only authenticate remote, if we have a realm set and we don't have re-invite */
1858 authorization = sip->sip_proxy_authorization;
1859 status = check_authorization(authorization, "INVITE", inst->auth_user, inst->auth_password, inst->auth_realm, inst->auth_nonce, &auth_text);
1860 if (status == 407) {
1861 if (!inst->auth_nonce[0])
1862 generate_nonce(inst->auth_nonce);
1863 SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, inst->auth_nonce);
1867 auth_text = "Authentication not required";
1869 add_trace("result", NULL, "%s", auth_text);
1872 if (status == 200) {
1874 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1877 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1878 add_trace("respond", "value", "%d", status);
1879 add_trace("reason", NULL, "peer invited");
1882 nua_respond(nh, status, auth_text, SIPTAG_CONTACT(sip->sip_contact), TAG_IF(auth_str[0], SIPTAG_PROXY_AUTHENTICATE_STR(auth_str)), TAG_END());
1883 new_state(PORT_STATE_RELEASE);
1884 trigger_work(&p_s_delete);
1888 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1889 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1891 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1892 if (!p_s_rtp_bridge) {
1895 /* check if supported payload type exists */
1896 for (i = 0; i < payloads; i++) {
1897 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1900 if (i == payloads) {
1901 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1909 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1911 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1912 nua_handle_destroy(nh);
1914 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1916 add_trace("respond", "value", "415 Unsupported Media");
1918 add_trace("respond", "value", "400 Bad Request");
1919 add_trace("reason", NULL, "offered codec does not match");
1921 if (p_state != PORT_STATE_IDLE) {
1922 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1923 message->param.disconnectinfo.cause = 41;
1924 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1925 message_put(message);
1927 new_state(PORT_STATE_RELEASE);
1928 trigger_work(&p_s_delete);
1932 /* handle re-invite */
1933 if (p_state != PORT_STATE_IDLE) {
1934 sip_trace_header(this, inst->interface_name, "RE-INVITE", DIRECTION_IN);
1936 if (p_s_rtp_bridge) {
1937 PDEBUG(DEBUG_SIP, "RE-INVITE not implemented for RTP forwarding\n");
1938 nua_respond(nh, SIP_501_NOT_IMPLEMENTED, TAG_END());
1939 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1940 add_trace("respond", "value", "501 NOT IMPLEMENTED");
1941 add_trace("reason", NULL, "RE-INVITE not implemented for RTP forwarding");
1944 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1945 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1946 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1947 if (rtp_connect() < 0) {
1950 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1951 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1952 nua_respond(p_s_handle, SIP_200_OK,
1953 NUTAG_MEDIA_ENABLE(0),
1954 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1955 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1960 /* open local RTP peer (if not bridging) */
1961 if (!p_s_rtp_bridge && rtp_open() < 0) {
1962 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1963 nua_handle_destroy(nh);
1965 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1966 add_trace("respond", "value", "500 Internal Server Error");
1967 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1969 new_state(PORT_STATE_RELEASE);
1970 trigger_work(&p_s_delete);
1975 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1976 // add_trace("handle", "new", "0x%x", nh);
1981 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1982 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1983 /* caller information */
1985 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1986 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1987 add_trace("calling", "present", "unavailable");
1989 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1990 add_trace("calling", "present", "allowed");
1991 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1992 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1993 SCPY(p_callerinfo.id, from);
1994 add_trace("calling", "number", "%s", from);
1995 SCPY(p_callerinfo.name, name);
1997 add_trace("calling", "name", "%s", name);
1998 SCPY(p_callerinfo.imsi, imsi);
2000 add_trace("calling", "imsi", "%s", imsi);
2002 SCPY(p_callerinfo.interface, inst->interface_name);
2003 /* dialing information */
2005 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
2006 SCAT(p_dialinginfo.id, to);
2007 add_trace("dialing", "number", "%s", to);
2010 /* bearer capability */
2011 p_capainfo.bearer_capa = INFO_BC_SPEECH;
2012 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
2013 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
2014 add_trace("bearer", "capa", "speech");
2015 add_trace("bearer", "mode", "circuit");
2016 /* if packet mode works some day, see dss1.cpp for conditions */
2017 p_capainfo.source_mode = B_MODE_TRANSPARENT;
2021 /* create endpoint */
2023 FATAL("Incoming call but already got an endpoint.\n");
2024 if (!(epoint = new Endpoint(p_serial, 0)))
2025 FATAL("No memory for Endpoint instance\n");
2026 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
2027 epointlist_new(epoint->ep_serial);
2029 #ifdef NUTAG_AUTO100
2030 /* send trying (proceeding) */
2031 nua_respond(nh, SIP_100_TRYING, TAG_END());
2032 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2033 add_trace("respond", "value", "100 Trying");
2037 new_state(PORT_STATE_IN_PROCEEDING);
2039 /* send setup message to endpoit */
2040 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
2041 message->param.setup.port_type = p_type;
2042 // message->param.setup.dtmf = 0;
2043 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
2044 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
2045 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
2046 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
2047 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
2048 // message->param.setup.useruser.protocol = mncc->useruser.proto;
2049 if (p_s_rtp_bridge) {
2052 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
2053 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
2054 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
2055 /* add codecs to setup message */
2056 for (i = 0; i < payloads; i++) {
2057 message->param.setup.rtpinfo.media_types[i] = media_types[i];
2058 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
2059 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
2062 message->param.setup.rtpinfo.payloads = i;
2064 message_put(message);
2066 /* start option timer */
2067 if (inst->options_interval) {
2068 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer with %d seconds\n", inst->options_interval);
2069 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2072 p_s_invite_direction = DIRECTION_IN;
2074 /* send progress, if tones are available and if we don't bridge */
2075 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
2076 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
2077 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
2078 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
2079 /* open local RTP peer (if not bridging) */
2080 if (rtp_connect() < 0) {
2082 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2083 nua_handle_destroy(nh);
2085 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2086 add_trace("respond", "value", "500 Internal Server Error");
2087 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
2089 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
2090 message->param.disconnectinfo.cause = 41;
2091 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
2092 message_put(message);
2093 new_state(PORT_STATE_RELEASE);
2094 trigger_work(&p_s_delete);
2098 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
2099 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
2101 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
2102 NUTAG_MEDIA_ENABLE(0),
2103 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
2104 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
2105 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2106 add_trace("respond", "value", "183 SESSION PROGRESS");
2107 add_trace("reason", NULL, "audio available");
2109 memset(&ia, 0, sizeof(ia));
2110 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
2111 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
2112 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
2113 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
2118 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[])
2120 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2122 PDEBUG(DEBUG_SIP, "options received\n");
2124 sip_trace_header(this, inst->interface_name, "OPTIONS", DIRECTION_IN);
2127 nua_respond(nh, SIP_200_OK, TAG_END());
2130 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[])
2132 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2133 struct lcr_msg *message;
2136 PDEBUG(DEBUG_SIP, "bye received\n");
2138 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
2139 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
2140 cause = atoi(sip->sip_reason->re_cause);
2141 add_trace("cause", "value", "%d", cause);
2145 // let stack do bye automaticall, since it will not accept our response for some reason
2146 // nua_respond(nh, SIP_200_OK, TAG_END());
2147 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2148 add_trace("respond", "value", "200 OK");
2150 // nua_handle_destroy(nh);
2155 while(p_epointlist) {
2156 /* send setup message to endpoit */
2157 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2158 message->param.disconnectinfo.cause = cause ? : 16;
2159 message->param.disconnectinfo.location = LOCATION_BEYOND;
2160 message_put(message);
2162 free_epointlist(p_epointlist);
2164 new_state(PORT_STATE_RELEASE);
2165 trigger_work(&p_s_delete);
2168 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[])
2170 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2171 struct lcr_msg *message;
2173 PDEBUG(DEBUG_SIP, "cancel received\n");
2175 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
2178 nua_handle_destroy(nh);
2183 while(p_epointlist) {
2184 /* send setup message to endpoit */
2185 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2186 message->param.disconnectinfo.cause = 16;
2187 message->param.disconnectinfo.location = LOCATION_BEYOND;
2188 message_put(message);
2190 free_epointlist(p_epointlist);
2192 new_state(PORT_STATE_RELEASE);
2193 trigger_work(&p_s_delete);
2196 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[])
2198 PDEBUG(DEBUG_SIP, "bye response received\n");
2200 nua_handle_destroy(nh);
2205 trigger_work(&p_s_delete);
2208 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[])
2210 PDEBUG(DEBUG_SIP, "cancel response received\n");
2212 nua_handle_destroy(nh);
2217 trigger_work(&p_s_delete);
2220 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[])
2222 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2223 struct lcr_msg *message;
2224 int cause = 0, location = 0;
2225 int media_types[32];
2226 uint8_t payload_types[32];
2229 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
2231 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2232 add_trace("respond", "value", "%d", status);
2235 if (status == 401 || status == 407) {
2236 PDEBUG(DEBUG_SIP, "Invite challenge received\n");
2237 challenge(inst, this, status, phrase, nua, magic, nh, hmagic, sip, tags);
2242 if (status == 183 || (status >= 200 && status <= 299)) {
2245 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
2246 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
2250 else if (!p_s_rtp_bridge) {
2251 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
2252 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
2259 nua_cancel(nh, TAG_END());
2260 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2261 add_trace("reason", NULL, "accepted codec does not match");
2264 location = LOCATION_PRIVATE_LOCAL;
2265 goto release_with_cause;
2268 /* connect to remote RTP (if not bridging) */
2269 if (!p_s_rtp_bridge && rtp_connect() < 0) {
2270 nua_cancel(nh, TAG_END());
2271 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2272 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
2275 location = LOCATION_PRIVATE_LOCAL;
2276 goto release_with_cause;
2280 /* start option timer */
2281 if (inst->options_interval) {
2282 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer with %d seconds\n", inst->options_interval);
2283 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2289 PDEBUG(DEBUG_SIP, "do proceeding\n");
2290 new_state(PORT_STATE_OUT_PROCEEDING);
2291 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
2292 message_put(message);
2296 PDEBUG(DEBUG_SIP, "do alerting\n");
2297 new_state(PORT_STATE_OUT_ALERTING);
2298 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
2299 message_put(message);
2302 PDEBUG(DEBUG_SIP, "do progress\n");
2303 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
2304 message->param.progressinfo.progress = 8;
2305 message->param.progressinfo.location = 10;
2306 if (p_s_rtp_bridge) {
2307 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2308 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
2309 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
2310 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
2311 message->param.progressinfo.rtpinfo.payloads = 1;
2313 message_put(message);
2317 PDEBUG(DEBUG_SIP, "do connect\n");
2318 nua_ack(nh, TAG_END());
2319 new_state(PORT_STATE_CONNECT);
2320 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
2321 if (p_s_rtp_bridge) {
2322 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2323 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
2324 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
2325 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
2326 message->param.connectinfo.rtpinfo.payloads = 1;
2328 message_put(message);
2331 if (status >= 200 && status <= 299)
2333 if (status < 100 || status > 199)
2335 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
2340 cause = status2cause(status);
2341 location = LOCATION_BEYOND;
2344 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
2346 while(p_epointlist) {
2347 /* send setup message to endpoit */
2348 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2349 message->param.disconnectinfo.cause = cause;
2350 message->param.disconnectinfo.location = location;
2351 message_put(message);
2353 free_epointlist(p_epointlist);
2356 new_state(PORT_STATE_RELEASE);
2360 trigger_work(&p_s_delete);
2363 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[])
2365 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2366 int cause = 0, location = 0;
2367 struct lcr_msg *message;
2369 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
2371 if (status >= 200 && status <= 299) {
2372 PDEBUG(DEBUG_SIP, "options ok, scheduling option timer with %d seconds\n", inst->options_interval);
2373 /* restart option timer */
2374 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2378 nua_handle_destroy(nh);
2383 cause = status2cause(status);
2384 location = LOCATION_BEYOND;
2386 while(p_epointlist) {
2387 /* send setup message to endpoit */
2388 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2389 message->param.disconnectinfo.cause = cause;
2390 message->param.disconnectinfo.location = location;
2391 message_put(message);
2393 free_epointlist(p_epointlist);
2395 new_state(PORT_STATE_RELEASE);
2396 trigger_work(&p_s_delete);
2399 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[])
2401 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2403 PDEBUG(DEBUG_SIP, "state change received\n");
2404 sip_trace_header(this, inst->interface_name, "STATUS", DIRECTION_OUT);
2405 add_trace("value", NULL, "%d", status);
2406 add_trace("phrase", NULL, "%s", phrase);
2410 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[])
2412 struct sip_inst *inst = (struct sip_inst *) magic;
2414 class Psip *psip = NULL;
2416 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2420 /* hunt for existing handles */
2423 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2424 psip = (class Psip *)port;
2425 if (psip->p_s_handle == nh) {
2426 PDEBUG(DEBUG_SIP, "Event found for port %s\n", psip->p_name);
2438 if (!psip && !inst->options_handle) {
2439 PDEBUG(DEBUG_SIP, "New options instance\n");
2440 inst->options_handle = nh;
2443 i_options(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2447 case nua_i_register:
2448 if (!inst->register_handle) {
2449 PDEBUG(DEBUG_SIP, "New register instance\n");
2450 inst->register_handle = nh;
2452 i_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2454 case nua_r_register:
2456 r_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2463 struct interface *interface = interface_first;
2465 PDEBUG(DEBUG_SIP, "New psip instance\n");
2467 /* create call instance */
2468 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2470 if (!strcmp(interface->name, inst->interface_name))
2472 interface = interface->next;
2475 FATAL("Cannot find interface %s.\n", inst->interface_name);
2476 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2477 FATAL("Cannot create Port instance.\n");
2481 if (!psip && inst->register_handle != nh) {
2482 PDEBUG(DEBUG_SIP, "Destroying unknown instance\n");
2483 nua_handle_destroy(nh);
2488 /* handle port process */
2490 PERROR("no SIP Port found for handel %p\n", nh);
2491 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2492 nua_handle_destroy(nh);
2497 case nua_r_set_params:
2498 PDEBUG(DEBUG_SIP, "setparam response\n");
2501 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2504 PDEBUG(DEBUG_SIP, "error received\n");
2507 psip->i_state(status, phrase, nua, magic, nh, hmagic, sip, tags);
2510 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2513 PDEBUG(DEBUG_SIP, "ack received\n");
2516 PDEBUG(DEBUG_SIP, "active received\n");
2519 psip->i_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2522 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2525 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2528 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2531 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2534 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2536 case nua_i_terminated:
2537 PDEBUG(DEBUG_SIP, "terminated received\n");
2540 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2544 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)
2546 struct sip_inst *inst = (struct sip_inst *) magic;
2550 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2553 case stun_discovery_done:
2554 addrlen = sizeof(sa);
2555 memset(&sa, 0, addrlen);
2556 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2557 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2560 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2561 inst->stun_state = STUN_STATE_RESOLVED;
2562 /* start timer for next stun request with inst->stun_interval */
2563 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2564 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2565 add_trace("ip", "addr", "%s", inst->public_ip);
2570 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2571 inst->stun_state = STUN_STATE_FAILED;
2572 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2573 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2574 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2579 /* received shutdown due to termination of RTP */
2580 void Psip::rtp_shutdown(void)
2582 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2583 struct lcr_msg *message;
2585 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2587 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2590 nua_handle_destroy(p_s_handle);
2593 while(p_epointlist) {
2594 /* send setup message to endpoit */
2595 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2596 message->param.disconnectinfo.cause = 16;
2597 message->param.disconnectinfo.location = LOCATION_BEYOND;
2598 message_put(message);
2600 free_epointlist(p_epointlist);
2602 new_state(PORT_STATE_RELEASE);
2603 trigger_work(&p_s_delete);
2606 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2608 class Psip *psip = (class Psip *)instance;
2609 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2611 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2614 nua_options(psip->p_s_handle,
2620 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2622 struct sip_inst *inst = (struct sip_inst *)instance;
2624 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2625 inst->stun_state = STUN_STATE_UNRESOLVED;
2630 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2632 struct sip_inst *inst = (struct sip_inst *)instance;
2634 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2635 /* if we have a handle, destroy it and becom unregistered, so registration is
2637 if (inst->register_handle) {
2638 /* stop option timer */
2639 unsched_timer(&inst->register_option_timer);
2640 nua_handle_destroy(inst->register_handle);
2641 inst->register_handle = NULL;
2643 inst->register_state = REGISTER_STATE_UNREGISTERED;
2648 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2650 struct sip_inst *inst = (struct sip_inst *)instance;
2651 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2654 nua_options(inst->register_handle,
2660 int sip_init_inst(struct interface *interface)
2662 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2665 interface->sip_inst = inst;
2666 SCPY(inst->interface_name, interface->name);
2667 SCPY(inst->local_peer, interface->sip_local_peer);
2668 SCPY(inst->remote_peer, interface->sip_remote_peer);
2669 if (!inst->remote_peer[0])
2670 inst->allow_register = 1;
2671 SCPY(inst->asserted_id, interface->sip_asserted_id);
2672 if (interface->sip_register) {
2673 inst->register_state = REGISTER_STATE_UNREGISTERED;
2674 SCPY(inst->register_user, interface->sip_register_user);
2675 SCPY(inst->register_host, interface->sip_register_host);
2677 SCPY(inst->auth_user, interface->sip_auth_user);
2678 SCPY(inst->auth_password, interface->sip_auth_password);
2679 SCPY(inst->auth_realm, interface->sip_auth_realm);
2680 inst->register_interval = interface->sip_register_interval;
2681 inst->options_interval = interface->sip_options_interval;
2683 inst->rtp_port_from = interface->rtp_port_from;
2684 inst->rtp_port_to = interface->rtp_port_to;
2685 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2686 inst->rtp_port_from = RTP_PORT_BASE;
2687 inst->rtp_port_to = RTP_PORT_MAX;
2689 inst->next_rtp_port = inst->rtp_port_from;
2692 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2693 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2694 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2695 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2696 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2697 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2699 /* init root object */
2700 inst->root = su_root_create(inst);
2702 PERROR("Failed to create SIP root\n");
2703 sip_exit_inst(interface);
2707 SPRINT(local, "sip:%s",inst->local_peer);
2708 if (!strchr(inst->local_peer, ':'))
2709 SCAT(local, ":5060");
2710 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2712 PERROR("Failed to create SIP stack object\n");
2713 sip_exit_inst(interface);
2716 nua_set_params(inst->nua,
2717 SIPTAG_ALLOW_STR("REGISTER,INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2718 NUTAG_APPL_METHOD("REGISTER"),
2719 NUTAG_APPL_METHOD("INVITE"),
2720 NUTAG_APPL_METHOD("ACK"),
2721 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2722 NUTAG_APPL_METHOD("CANCEL"),
2723 NUTAG_APPL_METHOD("OPTIONS"),
2724 NUTAG_APPL_METHOD("NOTIFY"),
2725 NUTAG_APPL_METHOD("INFO"),
2727 #ifdef NUTAG_AUTO100
2731 NUTAG_AUTOANSWER(0),
2734 SCPY(inst->public_ip, interface->sip_public_ip);
2735 if (interface->sip_stun_server[0]) {
2736 SCPY(inst->stun_server, interface->sip_stun_server);
2737 inst->stun_interval = interface->sip_stun_interval;
2738 inst->stun_handle = stun_handle_init(inst->root,
2739 STUNTAG_SERVER(inst->stun_server),
2741 if (!inst->stun_handle) {
2742 PERROR("Failed to create STUN handle\n");
2743 sip_exit_inst(interface);
2746 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2747 if (inst->stun_socket < 0) {
2748 PERROR("Failed to create STUN socket\n");
2749 sip_exit_inst(interface);
2752 inst->stun_state = STUN_STATE_UNRESOLVED;
2755 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2757 any_sip_interface = 1;
2762 void sip_exit_inst(struct interface *interface)
2764 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2768 del_timer(&inst->stun_retry_timer);
2769 del_timer(&inst->register_retry_timer);
2770 del_timer(&inst->register_option_timer);
2771 if (inst->stun_socket)
2772 su_close(inst->stun_socket);
2773 if (inst->stun_handle)
2774 stun_handle_destroy(inst->stun_handle);
2775 if (inst->register_handle)
2776 nua_handle_destroy(inst->register_handle);
2777 if (inst->options_handle)
2778 nua_handle_destroy(inst->options_handle);
2780 su_root_destroy(inst->root);
2782 nua_destroy(inst->nua);
2783 FREE(inst, sizeof(*inst));
2784 interface->sip_inst = NULL;
2786 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2788 /* check if there is any other SIP interface left */
2789 interface = interface_first;
2791 if (interface->sip_inst)
2793 interface = interface->next;
2796 any_sip_interface = 0;
2799 extern su_log_t su_log_default[];
2800 extern su_log_t nua_log[];
2806 /* init SOFIA lib */
2808 su_home_init(sip_home);
2810 if (options.deb & DEBUG_SIP) {
2811 su_log_set_level(su_log_default, 9);
2812 su_log_set_level(nua_log, 9);
2813 //su_log_set_level(soa_log, 9);
2816 for (i = 0; i < 256; i++)
2817 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);
2819 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2826 su_home_deinit(sip_home);
2829 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2832 static void sip_handle_stun(struct sip_inst *inst)
2836 switch (inst->stun_state) {
2837 case STUN_STATE_UNRESOLVED:
2838 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2839 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2840 STUNTAG_SOCKET(inst->stun_socket),
2841 STUNTAG_REGISTER_EVENTS(1),
2844 PERROR("Failed to call stun_bind()\n");
2845 inst->stun_state = STUN_STATE_FAILED;
2848 inst->stun_state = STUN_STATE_RESOLVING;
2849 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2850 add_trace("server", "addr", "%s", inst->stun_server);
2856 static void sip_handle_register(struct sip_inst *inst)
2858 char from[128] = "";
2860 char contact[128] = "";
2861 char expires[128] = "";
2863 switch (inst->register_state) {
2864 case REGISTER_STATE_UNREGISTERED:
2865 /* wait for resoved stun */
2866 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2869 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2870 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2871 if (!inst->register_handle) {
2872 PERROR("Failed to create handle\n");
2873 inst->register_state = REGISTER_STATE_FAILED;
2876 /* apply handle to trace */
2877 // sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2878 // add_trace("handle", "new", "0x%x", inst->register_handle);
2881 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2882 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2883 if (inst->public_ip[0]) {
2885 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2886 p = strchr(inst->local_peer, ':');
2891 if (inst->register_interval) {
2892 SPRINT(expires, "%d", inst->register_interval + 60);
2895 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2896 add_trace("from", "uri", "%s", from);
2897 add_trace("to", "uri", "%s", to);
2899 add_trace("expires", NULL, "%s", expires);
2902 nua_register(inst->register_handle,
2903 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2904 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2905 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2906 TAG_IF(expires[0], SIPTAG_EXPIRES_STR(expires)),
2909 inst->register_state = REGISTER_STATE_REGISTERING;
2916 void sip_handle(void)
2918 struct interface *interface = interface_first;
2919 struct sip_inst *inst;
2922 if (interface->sip_inst) {
2923 inst = (struct sip_inst *) interface->sip_inst;
2924 su_root_step(inst->root, 0);
2925 sip_handle_stun(inst);
2926 sip_handle_register(inst);
2928 interface = interface->next;
2932 /* deletes when back in event loop */
2933 static int delete_event(struct lcr_work *work, void *instance, int index)
2935 class Psip *psip = (class Psip *)instance;
2944 * generate audio, if no data is received from bridge
2947 void Psip::set_tone(const char *dir, const char *tone)
2949 Port::set_tone(dir, tone);
2954 void Psip::update_load(void)
2956 /* don't trigger load event if event already active */
2957 if (p_s_load_timer.active)
2960 /* don't start timer if ... */
2961 if (!p_tone_name[0] && !p_dov_tx)
2964 p_s_next_tv_sec = 0;
2965 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2968 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2970 class Psip *psip = (class Psip *)instance;
2972 /* stop timer if ... */
2973 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2981 #define SEND_SIP_LEN 160
2983 void Psip::load_tx(void)
2986 struct timeval current_time;
2987 int tosend = SEND_SIP_LEN, i;
2988 unsigned char buf[SEND_SIP_LEN], *p = buf;
2991 gettimeofday(¤t_time, NULL);
2992 if (!p_s_next_tv_sec) {
2993 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2994 p_s_next_tv_sec = current_time.tv_sec;
2995 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2996 if (p_s_next_tv_usec >= 1000000) {
2997 p_s_next_tv_usec -= 1000000;
3000 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
3002 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
3003 + (current_time.tv_usec - p_s_next_tv_usec);
3004 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
3005 /* if clock drifts too much, set next timeout event to current timer + 160 */
3007 p_s_next_tv_sec = current_time.tv_sec;
3008 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
3009 if (p_s_next_tv_usec >= 1000000) {
3010 p_s_next_tv_usec -= 1000000;
3014 /* if diff is positive, it took too long, so next timeout will be earlier */
3015 p_s_next_tv_usec += SEND_SIP_LEN * 125;
3016 if (p_s_next_tv_usec >= 1000000) {
3017 p_s_next_tv_usec -= 1000000;
3021 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
3025 if (p_tone_name[0]) {
3026 tosend -= read_audio(p, tosend);
3029 tosend -= dov_tx(p, tosend);
3032 PERROR("buffer is not completely filled\n");
3037 for (i = 0; i < SEND_SIP_LEN; i++) {
3041 /* transmit data via rtp */
3042 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);