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>
20 #ifndef SOFIA_SIP_GCC_4_8_PATCH_APLLIED
21 #warning ********************************************************
22 #warning Please apply the sofia-sip-gcc-4.8.patch !
23 #warning If this issue is already fixed, just remove this check.
24 #warning ********************************************************
30 unsigned char flip[256];
32 int any_sip_interface = 0;
34 //pthread_mutex_t mutex_msg;
35 su_home_t sip_home[1];
37 #define REGISTER_STATE_UNREGISTERED 1
38 #define REGISTER_STATE_REGISTERING 2
39 #define REGISTER_STATE_REGISTERED 3
40 #define REGISTER_STATE_FAILED 4
42 #define STUN_RETRY_TIMER 10, 0
43 #define REGISTER_RETRY_TIMER 10, 0
45 #define STUN_STATE_UNRESOLVED 1
46 #define STUN_STATE_RESOLVING 2
47 #define STUN_STATE_RESOLVED 3
48 #define STUN_STATE_FAILED 4
50 #define RTP_PORT_BASE 30000
51 #define RTP_PORT_MAX 39998
54 char interface_name[64];
56 char remote_peer[128];
58 char register_user[128];
59 char register_host[128];
60 nua_handle_t *register_handle;
61 struct lcr_timer register_retry_timer;
62 struct lcr_timer register_option_timer;
63 int register_interval;
66 char auth_password[128];
72 char stun_server[128];
73 stun_handle_t *stun_handle;
74 su_socket_t stun_socket;
75 struct lcr_timer stun_retry_timer;
78 unsigned short rtp_port_from;
79 unsigned short rtp_port_to;
80 unsigned short next_rtp_port;
84 static int delete_event(struct lcr_work *work, void *instance, int index);
85 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index);
86 static int load_timer(struct lcr_timer *timer, void *instance, int index);
91 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
94 if (interface->rtp_bridge)
96 p_s_sip_inst = interface->sip_inst;
97 memset(&p_s_delete, 0, sizeof(p_s_delete));
98 add_work(&p_s_delete, delete_event, this, 0);
101 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
102 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
103 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
104 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
105 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
106 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
107 p_s_rtp_ip_local = 0;
108 p_s_rtp_ip_remote = 0;
109 p_s_rtp_port_local = 0;
110 p_s_rtp_port_remote = 0;
115 p_s_rtp_tx_action = 0;
116 p_s_rtp_is_connected = 0;
118 /* create option timer */
119 memset(&p_s_invite_option_timer, 0, sizeof(p_s_invite_option_timer));
120 add_timer(&p_s_invite_option_timer, invite_option_timer, this, 0);
121 p_s_invite_direction = 0;
124 memset(&p_s_load_timer, 0, sizeof(p_s_load_timer));
125 add_timer(&p_s_load_timer, load_timer, this, 0);
128 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
130 FATAL("No SIP instance for interface\n");
139 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
141 del_timer(&p_s_invite_option_timer);
142 del_timer(&p_s_load_timer);
143 del_work(&p_s_delete);
148 static const char *media_type2name(uint8_t media_type) {
149 switch (media_type) {
150 case MEDIA_TYPE_ULAW:
152 case MEDIA_TYPE_ALAW:
156 case MEDIA_TYPE_GSM_HR:
158 case MEDIA_TYPE_GSM_EFR:
167 static void sip_trace_header(class Psip *sip, const char *interface_name, const char *message, int direction)
169 struct interface *interface = NULL;
172 interface = getinterfacebyname(interface_name);
174 /* init trace with given values */
177 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
178 sip?sip->p_dialinginfo.id:NULL,
189 /* according to RFC 3550 */
191 #if __BYTE_ORDER == __LITTLE_ENDIAN
192 uint8_t csrc_count:4,
196 uint8_t payload_type:7,
198 #elif __BYTE_ORDER == __BIG_ENDIAN
209 } __attribute__((packed));
214 } __attribute__((packed));
216 #define RTP_VERSION 2
218 #define PAYLOAD_TYPE_ULAW 0
219 #define PAYLOAD_TYPE_ALAW 8
220 #define PAYLOAD_TYPE_GSM 3
222 /* decode an rtp frame */
223 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
225 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
226 struct rtp_x_hdr *rtpxh;
230 unsigned char *from, *to;
234 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
237 if (rtph->version != RTP_VERSION) {
238 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
241 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
242 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
243 if (payload_len < 0) {
244 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
245 "csrc count = %d)\n", len, rtph->csrc_count);
248 if (rtph->extension) {
249 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
250 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
251 "extension header\n");
254 rtpxh = (struct rtp_x_hdr *)payload;
255 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
257 payload_len -= x_len;
258 if (payload_len < 0) {
259 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
260 "extension header exceeds frame length\n");
265 if (payload_len < 0) {
266 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
270 payload_len -= payload[payload_len - 1];
271 if (payload_len < 0) {
272 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
273 "greater than payload\n");
278 switch (rtph->payload_type) {
280 we only support alaw and ulaw!
281 case RTP_PT_GSM_FULL:
282 if (payload_len != 33) {
283 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
284 "payload length != 33 (len = %d)\n",
290 if (payload_len != 31) {
291 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
292 "payload length != 31 (len = %d)\n",
297 case RTP_PT_GSM_HALF:
298 if (payload_len != 14) {
299 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
300 "payload length != 14 (len = %d)\n",
306 case PAYLOAD_TYPE_ALAW:
307 if (options.law != 'a') {
308 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
312 case PAYLOAD_TYPE_ULAW:
313 if (options.law == 'a') {
314 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
319 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
320 "type %d\n", rtph->payload_type);
324 if (payload_len <= 0) {
325 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
331 psip->record(payload, payload_len, 0); // from down
333 psip->tap(payload, payload_len, 0); // from down
338 if (psip->p_echotest) {
339 /* echo rtp data we just received */
340 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
344 *to++ = flip[*from++];
346 psip->dov_rx(payload, payload_len);
347 psip->bridge_tx(payload, payload_len);
352 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
354 class Psip *psip = (class Psip *) instance;
356 unsigned char buffer[256];
359 if ((what & LCR_FD_READ)) {
360 len = read(fd->fd, &buffer, sizeof(buffer));
362 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
363 // psip->rtp_close();
364 // psip->rtp_shutdown();
367 if (psip->p_s_rtp_is_connected)
368 rc = rtp_decode(psip, buffer, len);
374 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
376 // class Psip *psip = (class Psip *) instance;
378 unsigned char buffer[256];
380 if ((what & LCR_FD_READ)) {
381 len = read(fd->fd, &buffer, sizeof(buffer));
383 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
384 // psip->rtp_close();
385 // psip->rtp_shutdown();
388 PDEBUG(DEBUG_SIP, "rtcp!\n");
394 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
397 socklen_t alen = sizeof(*sin_local);
399 sin_local->sin_family = AF_INET;
400 sin_local->sin_addr.s_addr = htonl(ip);
401 sin_local->sin_port = htons(port);
403 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
407 /* retrieve the address we actually bound to, in case we
408 * passed INADDR_ANY as IP address */
409 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
412 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
415 socklen_t alen = sizeof(*sin_local);
417 sin_remote->sin_family = AF_INET;
418 sin_remote->sin_addr.s_addr = htonl(ip);
419 sin_remote->sin_port = htons(port);
421 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
423 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
427 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
430 int Psip::rtp_open(void)
432 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
434 // struct in_addr ia;
436 unsigned short start_port;
439 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
445 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
447 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
453 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
456 ip = htonl(INADDR_ANY);
458 start_port = inst->next_rtp_port;
460 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, inst->next_rtp_port);
464 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, inst->next_rtp_port + 1);
466 p_s_rtp_port_local = inst->next_rtp_port;
467 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
470 /* reopen rtp socket and try again with next udp port */
471 unregister_fd(&p_s_rtp_fd);
472 close(p_s_rtp_fd.fd);
474 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
480 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
483 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
484 if (inst->next_rtp_port == start_port)
486 /* we must use rc2, in order to preserve rc */
489 PDEBUG(DEBUG_SIP, "failed to find port\n");
493 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
494 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
495 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
497 return p_s_rtp_port_local;
500 int Psip::rtp_connect(void)
505 ia.s_addr = htonl(p_s_rtp_ip_remote);
506 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
508 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);
512 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);
516 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
517 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
518 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
519 p_s_rtp_is_connected = 1;
523 void Psip::rtp_close(void)
525 if (p_s_rtp_fd.fd > 0) {
526 unregister_fd(&p_s_rtp_fd);
527 close(p_s_rtp_fd.fd);
530 if (p_s_rtcp_fd.fd > 0) {
531 unregister_fd(&p_s_rtcp_fd);
532 close(p_s_rtcp_fd.fd);
535 if (p_s_rtp_is_connected) {
536 PDEBUG(DEBUG_SIP, "rtp closed\n");
537 p_s_rtp_is_connected = 0;
542 void tv_difference(struct timeval *diff, const struct timeval *from,
543 const struct timeval *__to)
545 struct timeval _to = *__to, *to = &_to;
547 if (to->tv_usec < from->tv_usec) {
549 to->tv_usec += 1000000;
552 diff->tv_usec = to->tv_usec - from->tv_usec;
553 diff->tv_sec = to->tv_sec - from->tv_sec;
556 /* encode and send a rtp frame */
557 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
559 struct rtp_hdr *rtph;
561 int duration; /* in samples */
562 unsigned char buffer[256];
566 record(data, len, 1); // from up
568 tap(data, len, 1); // from up
570 if (!p_s_rtp_is_connected) {
575 if (!p_s_rtp_tx_action) {
576 /* initialize sequences */
577 p_s_rtp_tx_action = 1;
578 p_s_rtp_tx_ssrc = rand();
579 p_s_rtp_tx_sequence = random();
580 p_s_rtp_tx_timestamp = random();
581 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
584 switch (payload_type) {
586 we only support alaw and ulaw!
587 case RTP_PT_GSM_FULL:
595 case RTP_PT_GSM_HALF:
600 case PAYLOAD_TYPE_ALAW:
601 case PAYLOAD_TYPE_ULAW:
606 PERROR("unsupported message type %d\n", payload_type);
612 struct timeval tv, tv_diff;
613 long int usec_diff, frame_diff;
615 gettimeofday(&tv, NULL);
616 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
617 p_s_rtp_tx_last_tv = tv;
619 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
620 frame_diff = (usec_diff / 20000);
622 if (abs(frame_diff) > 1) {
623 long int frame_diff_excess = frame_diff - 1;
625 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
626 p_s_rtp_tx_sequence += frame_diff_excess;
627 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
632 rtph = (struct rtp_hdr *) buffer;
633 rtph->version = RTP_VERSION;
636 rtph->csrc_count = 0;
638 rtph->payload_type = payload_type;
639 rtph->sequence = htons(p_s_rtp_tx_sequence++);
640 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
641 p_s_rtp_tx_timestamp += duration;
642 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
643 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
645 if (p_s_rtp_fd.fd > 0) {
646 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
647 if (len != sizeof(struct rtp_hdr) + payload_len) {
648 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
658 /* receive from remote */
659 int Psip::bridge_rx(unsigned char *data, int len)
663 /* don't bridge, if tones are provided */
664 if (p_tone_name[0] || p_dov_tx)
670 if ((ret = Port::bridge_rx(data, len)))
673 /* write to rx buffer */
675 p_s_rxdata[p_s_rxpos++] = flip[*data++];
676 if (p_s_rxpos == 160) {
679 /* transmit data via rtp */
680 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
687 /* taken from freeswitch */
688 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
689 static int status2cause(int status)
693 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
699 return 21; //SWITCH_CAUSE_CALL_REJECTED;
701 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
704 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
707 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
709 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
718 return 127; //SWITCH_CAUSE_INTERWORKING;
720 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
725 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
728 return 17; //SWITCH_CAUSE_USER_BUSY;
730 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
733 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
735 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
737 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
741 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
744 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
746 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
748 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
752 static int cause2status(int cause, int location, const char **st)
758 s = 404; *st = sip_404_Not_found;
761 s = 404; *st = sip_404_Not_found;
764 s = 404; *st = sip_404_Not_found;
767 s = 486; *st = sip_486_Busy_here;
770 s = 408; *st = sip_408_Request_timeout;
773 s = 480; *st = sip_480_Temporarily_unavailable;
776 s = 480; *st = sip_480_Temporarily_unavailable;
779 if (location == LOCATION_USER) {
780 s = 603; *st = sip_603_Decline;
782 s = 403; *st = sip_403_Forbidden;
786 //s = 301; *st = sip_301_Moved_permanently;
787 s = 410; *st = sip_410_Gone;
790 s = 410; *st = sip_410_Gone;
793 s = 404; *st = sip_404_Not_found;
796 s = 502; *st = sip_502_Bad_gateway;
799 s = 484; *st = sip_484_Address_incomplete;
802 s = 501; *st = sip_501_Not_implemented;
805 s = 480; *st = sip_480_Temporarily_unavailable;
808 s = 503; *st = sip_503_Service_unavailable;
811 s = 503; *st = sip_503_Service_unavailable;
814 s = 503; *st = sip_503_Service_unavailable;
817 s = 503; *st = sip_503_Service_unavailable;
820 s = 503; *st = sip_503_Service_unavailable;
823 s = 403; *st = sip_403_Forbidden;
826 s = 403; *st = sip_403_Forbidden;
829 s = 503; *st = sip_503_Service_unavailable;
832 s = 488; *st = sip_488_Not_acceptable;
835 s = 501; *st = sip_501_Not_implemented;
838 s = 488; *st = sip_488_Not_acceptable;
841 s = 501; *st = sip_501_Not_implemented;
844 s = 403; *st = sip_403_Forbidden;
847 s = 503; *st = sip_503_Service_unavailable;
850 s = 504; *st = sip_504_Gateway_time_out;
853 s = 500; *st = sip_500_Internal_server_error;
856 s = 500; *st = sip_500_Internal_server_error;
859 s = 468; *st = sip_486_Busy_here;
865 /* use STUN ip, or return the ip without change */
866 unsigned int Psip::get_local_ip(unsigned int ip)
868 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
870 if (inst->public_ip[0]) {
871 PDEBUG(DEBUG_SIP, "RTP local IP is replaced by STUN ip %s\n", inst->public_ip);
872 inet_pton(AF_INET, inst->public_ip, &ip);
879 * endpoint sends messages to the SIP port
882 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
884 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
887 struct lcr_msg *message;
889 unsigned char payload_type;
891 if (param->connectinfo.rtpinfo.port) {
892 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
894 media_type = param->connectinfo.rtpinfo.media_types[0];
895 payload_type = param->connectinfo.rtpinfo.payload_types[0];
896 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
897 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
898 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
899 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
900 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
902 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
903 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
904 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
905 /* open local RTP peer (if not bridging) */
906 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
907 nua_cancel(p_s_handle, TAG_END());
908 nua_handle_destroy(p_s_handle);
910 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
911 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
913 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
914 message->param.disconnectinfo.cause = 41;
915 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
916 message_put(message);
917 new_state(PORT_STATE_RELEASE);
918 trigger_work(&p_s_delete);
923 memset(&ia, 0, sizeof(ia));
924 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
927 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
931 "m=audio %d RTP/AVP %d\r\n"
932 "a=rtpmap:%d %s/8000\r\n"
933 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
934 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
937 * If this response causes corrupt messages, like SDP body inside or
938 * before header, check if the sofia-sip-gcc-4.8.patch was applied.
939 * If it is still corrupted, try to disable optimization when compiling
942 nua_respond(p_s_handle, SIP_200_OK,
943 NUTAG_MEDIA_ENABLE(0),
944 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
945 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
947 new_state(PORT_STATE_CONNECT);
948 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
949 add_trace("respond", "value", "200 OK");
950 add_trace("reason", NULL, "call connected");
951 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
952 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
953 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
959 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
961 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
962 struct lcr_msg *message;
963 char cause_str[128] = "";
964 int cause = param->disconnectinfo.cause;
965 int location = param->disconnectinfo.cause;
967 const char *status_text;
969 if (cause > 0 && cause <= 127) {
970 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
974 case PORT_STATE_OUT_SETUP:
975 case PORT_STATE_OUT_PROCEEDING:
976 case PORT_STATE_OUT_ALERTING:
977 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
978 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
980 add_trace("cause", "value", "%d", cause);
982 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
984 case PORT_STATE_IN_SETUP:
985 case PORT_STATE_IN_PROCEEDING:
986 case PORT_STATE_IN_ALERTING:
987 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
988 status = cause2status(cause, location, &status_text);
989 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
991 add_trace("cause", "value", "%d", cause);
992 add_trace("respond", "value", "%d %s", status, status_text);
994 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
995 nua_handle_destroy(p_s_handle);
997 trigger_work(&p_s_delete);
1000 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1001 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1003 add_trace("cause", "value", "%d", cause);
1005 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1008 if (message_id == MESSAGE_DISCONNECT) {
1009 while(p_epointlist) {
1010 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1011 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1012 message->param.disconnectinfo.location = LOCATION_BEYOND;
1013 message_put(message);
1015 free_epointlist(p_epointlist);
1019 new_state(PORT_STATE_RELEASE);
1024 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1026 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1027 char from[128] = "";
1029 char contact[128] = "";
1030 const char *local = inst->local_peer;
1032 const char *remote = inst->remote_peer;
1033 char sdp_str[512], pt_str[32];
1035 struct epoint_list *epointlist;
1036 sip_cseq_t *cseq = NULL;
1037 struct lcr_msg *message;
1038 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1039 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1041 unsigned char *payload_types;
1045 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1047 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1048 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1049 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1050 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1051 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1053 if (param->setup.rtpinfo.port) {
1054 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1056 media_types = param->setup.rtpinfo.media_types;
1057 payload_types = param->setup.rtpinfo.payload_types;
1058 payloads = param->setup.rtpinfo.payloads;
1059 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1060 p_s_rtp_port_local = param->setup.rtpinfo.port;
1061 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1062 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1064 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1066 media_types = &lcr_media;
1067 payload_types = &lcr_payload;
1070 /* open local RTP peer (if not bridging) */
1071 if (rtp_open() < 0) {
1072 PERROR("Failed to open RTP sockets\n");
1073 /* send release message to endpoit */
1074 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1075 message->param.disconnectinfo.cause = 41;
1076 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1077 message_put(message);
1078 new_state(PORT_STATE_RELEASE);
1079 trigger_work(&p_s_delete);
1082 if (!p_s_rtp_ip_local) {
1085 /* extract IP from local peer */
1086 SCPY(local_ip, local);
1087 p = strchr(local_ip, ':');
1090 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1091 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1092 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1096 memset(&ia, 0, sizeof(ia));
1097 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1098 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1100 PERROR("Failed to create handle\n");
1101 /* send release message to endpoit */
1102 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1103 message->param.disconnectinfo.cause = 41;
1104 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1105 message_put(message);
1106 new_state(PORT_STATE_RELEASE);
1107 trigger_work(&p_s_delete);
1110 /* apply handle to trace */
1111 sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1112 add_trace("handle", "new", "0x%x", p_s_handle);
1117 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1121 "m=audio %d RTP/AVP"
1122 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1123 for (i = 0; i < payloads; i++) {
1124 SPRINT(pt_str, " %d", payload_types[i]);
1125 SCAT(sdp_str, pt_str);
1127 SCAT(sdp_str, "\r\n");
1128 for (i = 0; i < payloads; i++) {
1129 SPRINT(pt_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1130 SCAT(sdp_str, pt_str);
1132 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1134 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1135 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1136 if (inst->public_ip[0])
1137 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1139 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1140 add_trace("from", "uri", "%s", from);
1141 add_trace("to", "uri", "%s", to);
1142 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1143 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1144 for (i = 0; i < payloads; i++)
1145 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1148 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1150 nua_invite(p_s_handle,
1151 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1152 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1153 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1154 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1155 NUTAG_MEDIA_ENABLE(0),
1156 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1157 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1158 new_state(PORT_STATE_OUT_SETUP);
1160 p_s_invite_direction = DIRECTION_OUT;
1163 PDEBUG(DEBUG_SIP, "do overlap\n");
1164 new_state(PORT_STATE_OUT_OVERLAP);
1165 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1166 message_put(message);
1168 PDEBUG(DEBUG_SIP, "do proceeding\n");
1169 new_state(PORT_STATE_OUT_PROCEEDING);
1170 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1171 message_put(message);
1174 /* attach only if not already */
1175 epointlist = p_epointlist;
1177 if (epointlist->epoint_id == epoint_id)
1179 epointlist = epointlist->next;
1182 epointlist_new(epoint_id);
1187 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1190 // struct in_addr ia;
1192 switch (param->notifyinfo.notify) {
1193 case INFO_NOTIFY_REMOTE_HOLD:
1197 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1199 "c=IN IP4 0.0.0.0\r\n"
1202 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1203 nua_info(p_s_handle,
1204 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1205 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1206 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1207 NUTAG_MEDIA_ENABLE(0),
1208 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1209 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1212 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1214 memset(&ia, 0, sizeof(ia));
1215 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1218 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1222 "m=audio %d RTP/AVP %d\r\n"
1223 "a=rtpmap:%d %s/8000\r\n"
1224 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, p_s_rtp_payload_type, p_s_rtp_payload_type, media_type2name(p_s_rtp_media_type));
1225 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1226 nua_info(p_s_handle,
1227 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1228 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1229 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1230 NUTAG_MEDIA_ENABLE(0),
1231 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1232 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1240 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1244 /* prepare DTMF info payload */
1250 /* start invite to handle DTMF */
1251 nua_info(p_s_handle,
1252 NUTAG_MEDIA_ENABLE(0),
1253 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1254 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1259 /* NOTE: incomplete and not working */
1260 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1264 /* prepare DTMF info payload */
1268 , param->information.id);
1270 /* start invite to handle DTMF */
1271 nua_info(p_s_handle,
1272 NUTAG_MEDIA_ENABLE(0),
1273 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1274 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1279 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1281 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1283 if (Port::message_epoint(epoint_id, message_id, param))
1286 switch(message_id) {
1287 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1288 if (p_state != PORT_STATE_IN_SETUP
1289 && p_state != PORT_STATE_IN_PROCEEDING)
1291 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1292 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1293 add_trace("respond", "value", "180 Ringing");
1295 new_state(PORT_STATE_IN_ALERTING);
1298 case MESSAGE_CONNECT: /* call is connected on LCR side */
1299 if (p_state != PORT_STATE_IN_SETUP
1300 && p_state != PORT_STATE_IN_PROCEEDING
1301 && p_state != PORT_STATE_IN_ALERTING)
1303 message_connect(epoint_id, message_id, param);
1306 case MESSAGE_DISCONNECT: /* call has been disconnected */
1307 case MESSAGE_RELEASE: /* call has been released */
1308 message_release(epoint_id, message_id, param);
1311 case MESSAGE_SETUP: /* dial-out command received from epoint */
1312 message_setup(epoint_id, message_id, param);
1315 case MESSAGE_INFORMATION: /* overlap dialing */
1316 if (p_state != PORT_STATE_OUT_OVERLAP)
1318 message_information(epoint_id, message_id, param);
1321 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1322 if (p_state == PORT_STATE_CONNECT)
1323 message_dtmf(epoint_id, message_id, param);
1324 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1325 if (p_state == PORT_STATE_CONNECT)
1326 message_notify(epoint_id, message_id, param);
1330 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1336 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)
1340 if (!sip->sip_payload) {
1341 PDEBUG(DEBUG_SIP, "no payload given\n");
1345 sdp_parser_t *parser;
1348 sdp_attribute_t *attr;
1350 sdp_connection_t *conn;
1352 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1354 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1358 if (!(sdp = sdp_session(parser))) {
1359 sdp_parser_free(parser);
1362 for (m = sdp->sdp_media; m; m = m->m_next) {
1363 if (m->m_proto != sdp_proto_rtp)
1365 if (m->m_type != sdp_media_audio)
1367 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1369 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1370 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1372 if (m->m_connections) {
1373 conn = m->m_connections;
1374 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1375 inet_pton(AF_INET, conn->c_address, ip);
1376 *ip = ntohl(p_s_rtp_ip_remote);
1378 char *p = sip->sip_payload->pl_data;
1381 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1382 p = strstr(p, "c=IN IP4 ");
1384 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1385 sdp_parser_free(parser);
1389 if ((p = strchr(addr, '\n'))) *p = '\0';
1390 if ((p = strchr(addr, '\r'))) *p = '\0';
1391 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1392 inet_pton(AF_INET, addr, ip);
1393 *ip = ntohl(p_s_rtp_ip_remote);
1395 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1398 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1399 /* append to payload list, if there is space */
1400 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1401 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1402 media_type = MEDIA_TYPE_ALAW;
1403 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1404 media_type = MEDIA_TYPE_ULAW;
1405 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1406 media_type = MEDIA_TYPE_GSM;
1407 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1408 media_type = MEDIA_TYPE_GSM_EFR;
1409 else if (!strcmp(map->rm_encoding, "AMR"))
1410 media_type = MEDIA_TYPE_AMR;
1411 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1412 media_type = MEDIA_TYPE_GSM_HR;
1413 if (media_type && *payloads <= max_payloads) {
1414 *payload_types++ = map->rm_pt;
1415 *media_types++ = media_type;
1421 sdp_parser_free(parser);
1426 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 tagss[])
1428 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1429 const char *from = "", *to = "", *name = "";
1432 class Endpoint *epoint;
1433 struct lcr_msg *message;
1434 struct interface *interface;
1435 int media_types[32];
1436 uint8_t payload_types[32];
1440 interface = getinterfacebyname(inst->interface_name);
1442 PERROR("Cannot find interface %s.\n", inst->interface_name);
1446 if (sip->sip_from) {
1447 if (sip->sip_from->a_url)
1448 from = sip->sip_from->a_url->url_user;
1449 if (sip->sip_from->a_display) {
1450 name = sip->sip_from->a_display;
1451 if (!strncmp(name, "\"IMSI", 5)) {
1452 strncpy(imsi, name + 5, 15);
1459 if (sip->sip_to->a_url)
1460 to = sip->sip_to->a_url->url_user;
1462 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1464 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1465 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1467 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1468 if (!p_s_rtp_bridge) {
1471 /* check if supported payload type exists */
1472 for (i = 0; i < payloads; i++) {
1473 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1476 if (i == payloads) {
1477 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1485 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1487 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1488 nua_handle_destroy(nh);
1490 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1492 add_trace("respond", "value", "415 Unsupported Media");
1494 add_trace("respond", "value", "400 Bad Request");
1495 add_trace("reason", NULL, "offered codec does not match");
1497 new_state(PORT_STATE_RELEASE);
1498 trigger_work(&p_s_delete);
1502 /* open local RTP peer (if not bridging) */
1503 if (!p_s_rtp_bridge && rtp_open() < 0) {
1504 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1505 nua_handle_destroy(nh);
1507 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1508 add_trace("respond", "value", "500 Internal Server Error");
1509 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1511 new_state(PORT_STATE_RELEASE);
1512 trigger_work(&p_s_delete);
1517 sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1518 add_trace("handle", "new", "0x%x", nh);
1522 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1523 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1524 /* caller information */
1526 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1527 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1528 add_trace("calling", "present", "unavailable");
1530 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1531 add_trace("calling", "present", "allowed");
1532 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1533 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1534 SCPY(p_callerinfo.id, from);
1535 add_trace("calling", "number", "%s", from);
1536 SCPY(p_callerinfo.name, name);
1538 add_trace("calling", "name", "%s", name);
1539 SCPY(p_callerinfo.imsi, imsi);
1541 add_trace("calling", "imsi", "%s", imsi);
1543 SCPY(p_callerinfo.interface, inst->interface_name);
1544 /* dialing information */
1546 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1547 SCAT(p_dialinginfo.id, to);
1548 add_trace("dialing", "number", "%s", to);
1551 /* bearer capability */
1552 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1553 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1554 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1555 add_trace("bearer", "capa", "speech");
1556 add_trace("bearer", "mode", "circuit");
1557 /* if packet mode works some day, see dss1.cpp for conditions */
1558 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1562 /* create endpoint */
1564 FATAL("Incoming call but already got an endpoint.\n");
1565 if (!(epoint = new Endpoint(p_serial, 0)))
1566 FATAL("No memory for Endpoint instance\n");
1567 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1568 epointlist_new(epoint->ep_serial);
1570 #ifdef NUTAG_AUTO100
1571 /* send trying (proceeding) */
1572 nua_respond(nh, SIP_100_TRYING, TAG_END());
1573 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1574 add_trace("respond", "value", "100 Trying");
1578 new_state(PORT_STATE_IN_PROCEEDING);
1580 /* send setup message to endpoit */
1581 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1582 message->param.setup.port_type = p_type;
1583 // message->param.setup.dtmf = 0;
1584 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1585 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1586 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1587 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1588 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1589 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1590 if (p_s_rtp_bridge) {
1593 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1594 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1595 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1596 /* add codecs to setup message */
1597 for (i = 0; i < payloads; i++) {
1598 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1599 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1600 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1603 message->param.setup.rtpinfo.payloads = i;
1605 message_put(message);
1607 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer\n");
1608 /* start option timer */
1609 if (inst->options_interval)
1610 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1611 p_s_invite_direction = DIRECTION_IN;
1613 /* send progress, if tones are available and if we don't bridge */
1614 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1617 unsigned char payload_type;
1619 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1620 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1621 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1622 /* open local RTP peer (if not bridging) */
1623 if (rtp_connect() < 0) {
1624 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1625 nua_handle_destroy(nh);
1627 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1628 add_trace("respond", "value", "500 Internal Server Error");
1629 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1631 new_state(PORT_STATE_RELEASE);
1632 trigger_work(&p_s_delete);
1633 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1634 message->param.disconnectinfo.cause = 41;
1635 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1636 message_put(message);
1637 new_state(PORT_STATE_RELEASE);
1638 trigger_work(&p_s_delete);
1642 memset(&ia, 0, sizeof(ia));
1643 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1646 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1650 "m=audio %d RTP/AVP %d\r\n"
1651 "a=rtpmap:%d %s/8000\r\n"
1652 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
1653 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1655 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1656 NUTAG_MEDIA_ENABLE(0),
1657 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1658 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1659 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1660 add_trace("respond", "value", "183 SESSION PROGRESS");
1661 add_trace("reason", NULL, "audio available");
1662 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1663 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1664 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1669 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 tagss[])
1671 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1672 struct lcr_msg *message;
1675 PDEBUG(DEBUG_SIP, "bye received\n");
1677 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
1678 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1679 cause = atoi(sip->sip_reason->re_cause);
1680 add_trace("cause", "value", "%d", cause);
1684 // let stack do bye automaticall, since it will not accept our response for some reason
1685 // nua_respond(nh, SIP_200_OK, TAG_END());
1686 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1687 add_trace("respond", "value", "200 OK");
1689 // nua_handle_destroy(nh);
1694 while(p_epointlist) {
1695 /* send setup message to endpoit */
1696 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1697 message->param.disconnectinfo.cause = cause ? : 16;
1698 message->param.disconnectinfo.location = LOCATION_BEYOND;
1699 message_put(message);
1701 free_epointlist(p_epointlist);
1703 new_state(PORT_STATE_RELEASE);
1704 trigger_work(&p_s_delete);
1707 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 tagss[])
1709 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1710 struct lcr_msg *message;
1712 PDEBUG(DEBUG_SIP, "cancel received\n");
1714 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
1717 nua_handle_destroy(nh);
1722 while(p_epointlist) {
1723 /* send setup message to endpoit */
1724 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1725 message->param.disconnectinfo.cause = 16;
1726 message->param.disconnectinfo.location = LOCATION_BEYOND;
1727 message_put(message);
1729 free_epointlist(p_epointlist);
1731 new_state(PORT_STATE_RELEASE);
1732 trigger_work(&p_s_delete);
1735 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 tagss[])
1737 PDEBUG(DEBUG_SIP, "bye response received\n");
1739 nua_handle_destroy(nh);
1744 trigger_work(&p_s_delete);
1747 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 tagss[])
1749 PDEBUG(DEBUG_SIP, "cancel response received\n");
1751 nua_handle_destroy(nh);
1756 trigger_work(&p_s_delete);
1759 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 tagss[])
1761 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1762 struct lcr_msg *message;
1763 int cause = 0, location = 0;
1764 int media_types[32];
1765 uint8_t payload_types[32];
1768 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1770 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1771 add_trace("respond", "value", "%d", status);
1775 if (status == 183 || (status >= 200 && status <= 299)) {
1778 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1779 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1783 else if (!p_s_rtp_bridge) {
1784 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1785 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1792 nua_cancel(nh, TAG_END());
1793 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1794 add_trace("reason", NULL, "accepted codec does not match");
1797 location = LOCATION_PRIVATE_LOCAL;
1798 goto release_with_cause;
1801 /* connect to remote RTP (if not bridging) */
1802 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1803 nua_cancel(nh, TAG_END());
1804 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1805 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1808 location = LOCATION_PRIVATE_LOCAL;
1809 goto release_with_cause;
1813 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer\n");
1814 /* start option timer */
1815 if (inst->options_interval)
1816 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1821 PDEBUG(DEBUG_SIP, "do proceeding\n");
1822 new_state(PORT_STATE_OUT_PROCEEDING);
1823 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1824 message_put(message);
1828 PDEBUG(DEBUG_SIP, "do alerting\n");
1829 new_state(PORT_STATE_OUT_ALERTING);
1830 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1831 message_put(message);
1834 PDEBUG(DEBUG_SIP, "do progress\n");
1835 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1836 message->param.progressinfo.progress = 8;
1837 message->param.progressinfo.location = 10;
1838 if (p_s_rtp_bridge) {
1839 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1840 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1841 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1842 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1843 message->param.progressinfo.rtpinfo.payloads = 1;
1845 message_put(message);
1849 PDEBUG(DEBUG_SIP, "do connect\n");
1850 nua_ack(nh, TAG_END());
1851 new_state(PORT_STATE_CONNECT);
1852 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1853 if (p_s_rtp_bridge) {
1854 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1855 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1856 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1857 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1858 message->param.connectinfo.rtpinfo.payloads = 1;
1860 message_put(message);
1863 if (status >= 200 && status <= 299)
1865 if (status < 100 || status > 199)
1867 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1872 cause = status2cause(status);
1873 location = LOCATION_BEYOND;
1876 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1878 while(p_epointlist) {
1879 /* send setup message to endpoit */
1880 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1881 message->param.disconnectinfo.cause = cause;
1882 message->param.disconnectinfo.location = location;
1883 message_put(message);
1885 free_epointlist(p_epointlist);
1888 new_state(PORT_STATE_RELEASE);
1892 trigger_work(&p_s_delete);
1895 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 tagss[])
1897 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1898 int cause = 0, location = 0;
1899 struct lcr_msg *message;
1901 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
1903 if (status >= 200 && status <= 299) {
1904 PDEBUG(DEBUG_SIP, "options ok, scheduling timer\n");
1905 /* restart option timer */
1906 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1910 nua_handle_destroy(nh);
1915 cause = status2cause(status);
1916 location = LOCATION_BEYOND;
1918 while(p_epointlist) {
1919 /* send setup message to endpoit */
1920 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1921 message->param.disconnectinfo.cause = cause;
1922 message->param.disconnectinfo.location = location;
1923 message_put(message);
1925 free_epointlist(p_epointlist);
1927 new_state(PORT_STATE_RELEASE);
1928 trigger_work(&p_s_delete);
1931 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 tagss[])
1933 sip_www_authenticate_t const *authenticate = NULL;
1934 char const *realm = NULL;
1935 char const *scheme = NULL;
1938 char authentication[256] = "";
1939 PDEBUG(DEBUG_SIP, "challenge order received\n");
1941 if (!inst->auth_user[0]) {
1942 PDEBUG(DEBUG_SIP, "No credentials available\n");
1943 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1944 add_trace("error", NULL, "There are no credentials given for interface");
1949 if (sip->sip_www_authenticate) {
1950 authenticate = sip->sip_www_authenticate;
1951 } else if (sip->sip_proxy_authenticate) {
1952 authenticate = sip->sip_proxy_authenticate;
1954 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1955 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1956 add_trace("error", NULL, "Authentication method unknwon");
1961 scheme = (char const *) authenticate->au_scheme;
1962 if (authenticate->au_params) {
1963 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1964 if ((realm = strstr(cur, "realm="))) {
1971 if (!scheme || !realm) {
1972 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1973 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1974 add_trace("error", NULL, "Authentication header has no realm or scheme");
1979 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1980 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1982 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1983 add_trace("scheme", NULL, "%s", scheme);
1984 add_trace("realm", NULL, "%s", realm);
1985 add_trace("user", NULL, "%s", inst->auth_user);
1986 add_trace("pass", NULL, "%s", inst->auth_password);
1989 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1994 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[])
1996 struct sip_inst *inst = (struct sip_inst *) magic;
1998 class Psip *psip = NULL;
2001 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2005 if (inst->register_handle == nh) {
2006 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_OUT);
2007 add_trace("value", NULL, "%d", status);
2008 add_trace("phrase", NULL, "%s", phrase);
2014 /* if not registered, become registered and start register interval timer */
2015 if (inst->register_state != REGISTER_STATE_REGISTERED) {
2016 if (inst->register_interval)
2017 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
2018 inst->register_state = REGISTER_STATE_REGISTERED;
2020 /* start option timer */
2021 if (inst->options_interval)
2022 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
2026 PDEBUG(DEBUG_SIP, "Register challenge received\n");
2027 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
2032 if (status >= 200 && status <= 299)
2037 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
2038 inst->register_state = REGISTER_STATE_FAILED;
2039 nua_handle_destroy(inst->register_handle);
2040 /* stop option timer */
2041 unsched_timer(&inst->register_option_timer);
2042 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
2043 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
2044 inst->register_handle = NULL;
2050 /* create or find port instance */
2051 if (event == nua_i_invite)
2054 struct interface *interface = interface_first;
2056 /* create call instance */
2057 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2059 if (!strcmp(interface->name, inst->interface_name))
2061 interface = interface->next;
2064 PERROR("Cannot find interface %s.\n", inst->interface_name);
2067 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2068 FATAL("Cannot create Port instance.\n");
2072 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2073 psip = (class Psip *)port;
2074 if (psip->p_s_handle == nh) {
2082 PERROR("no SIP Port found for handel %p\n", nh);
2083 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2084 nua_handle_destroy(nh);
2088 sip_trace_header(psip, inst->interface_name, "STATUS", DIRECTION_OUT);
2089 add_trace("value", NULL, "%d", status);
2090 add_trace("phrase", NULL, "%s", phrase);
2096 rc = challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
2102 case nua_r_set_params:
2103 PDEBUG(DEBUG_SIP, "setparam response\n");
2106 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2109 PDEBUG(DEBUG_SIP, "error received\n");
2112 PDEBUG(DEBUG_SIP, "state change received\n");
2114 case nua_i_register:
2115 PDEBUG(DEBUG_SIP, "register received\n");
2118 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2121 PDEBUG(DEBUG_SIP, "ack received\n");
2124 PDEBUG(DEBUG_SIP, "active received\n");
2127 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2130 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2133 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2136 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2139 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2141 case nua_i_terminated:
2142 PDEBUG(DEBUG_SIP, "terminated received\n");
2145 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2149 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)
2151 struct sip_inst *inst = (struct sip_inst *) magic;
2155 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2158 case stun_discovery_done:
2159 addrlen = sizeof(sa);
2160 memset(&sa, 0, addrlen);
2161 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2162 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2165 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2166 inst->stun_state = STUN_STATE_RESOLVED;
2167 /* start timer for next stun request with inst->stun_interval */
2168 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2169 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2170 add_trace("ip", "addr", "%s", inst->public_ip);
2175 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2176 inst->stun_state = STUN_STATE_FAILED;
2177 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2178 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2179 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2184 /* received shutdown due to termination of RTP */
2185 void Psip::rtp_shutdown(void)
2187 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2188 struct lcr_msg *message;
2190 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2192 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2195 nua_handle_destroy(p_s_handle);
2198 while(p_epointlist) {
2199 /* send setup message to endpoit */
2200 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2201 message->param.disconnectinfo.cause = 16;
2202 message->param.disconnectinfo.location = LOCATION_BEYOND;
2203 message_put(message);
2205 free_epointlist(p_epointlist);
2207 new_state(PORT_STATE_RELEASE);
2208 trigger_work(&p_s_delete);
2211 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2213 class Psip *psip = (class Psip *)instance;
2214 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2216 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2219 nua_options(psip->p_s_handle,
2225 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2227 struct sip_inst *inst = (struct sip_inst *)instance;
2229 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2230 inst->stun_state = STUN_STATE_UNRESOLVED;
2235 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2237 struct sip_inst *inst = (struct sip_inst *)instance;
2239 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2240 /* if we have a handle, destroy it and becom unregistered, so registration is
2242 if (inst->register_handle) {
2243 /* stop option timer */
2244 unsched_timer(&inst->register_option_timer);
2245 nua_handle_destroy(inst->register_handle);
2246 inst->register_handle = NULL;
2248 inst->register_state = REGISTER_STATE_UNREGISTERED;
2253 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2255 struct sip_inst *inst = (struct sip_inst *)instance;
2256 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2259 nua_options(inst->register_handle,
2265 int sip_init_inst(struct interface *interface)
2267 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2270 interface->sip_inst = inst;
2271 SCPY(inst->interface_name, interface->name);
2272 SCPY(inst->local_peer, interface->sip_local_peer);
2273 SCPY(inst->remote_peer, interface->sip_remote_peer);
2274 if (interface->sip_register) {
2275 inst->register_state = REGISTER_STATE_UNREGISTERED;
2276 SCPY(inst->register_user, interface->sip_register_user);
2277 SCPY(inst->register_host, interface->sip_register_host);
2279 SCPY(inst->auth_user, interface->sip_auth_user);
2280 SCPY(inst->auth_password, interface->sip_auth_password);
2281 inst->register_interval = interface->sip_register_interval;
2282 inst->options_interval = interface->sip_options_interval;
2284 inst->rtp_port_from = interface->rtp_port_from;
2285 inst->rtp_port_to = interface->rtp_port_to;
2286 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2287 inst->rtp_port_from = RTP_PORT_BASE;
2288 inst->rtp_port_to = RTP_PORT_MAX;
2290 inst->next_rtp_port = inst->rtp_port_from;
2293 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2294 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2295 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2296 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2297 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2298 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2300 /* init root object */
2301 inst->root = su_root_create(inst);
2303 PERROR("Failed to create SIP root\n");
2304 sip_exit_inst(interface);
2308 SPRINT(local, "sip:%s",inst->local_peer);
2309 if (!strchr(inst->local_peer, ':'))
2310 SCAT(local, ":5060");
2311 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2313 PERROR("Failed to create SIP stack object\n");
2314 sip_exit_inst(interface);
2317 nua_set_params(inst->nua,
2318 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2319 NUTAG_APPL_METHOD("INVITE"),
2320 NUTAG_APPL_METHOD("ACK"),
2321 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2322 NUTAG_APPL_METHOD("CANCEL"),
2323 NUTAG_APPL_METHOD("OPTIONS"),
2324 NUTAG_APPL_METHOD("NOTIFY"),
2325 NUTAG_APPL_METHOD("INFO"),
2327 #ifdef NUTAG_AUTO100
2331 NUTAG_AUTOANSWER(0),
2334 SCPY(inst->public_ip, interface->sip_public_ip);
2335 if (interface->sip_stun_server[0]) {
2337 inst->stun_root = su_root_create(inst);
2338 if (!inst->stun_root) {
2339 PERROR("Failed to create STUN root\n");
2340 sip_exit_inst(interface);
2345 SCPY(inst->stun_server, interface->sip_stun_server);
2346 inst->stun_interval = interface->sip_stun_interval;
2347 inst->stun_handle = stun_handle_init(inst->root,
2348 STUNTAG_SERVER(inst->stun_server),
2350 if (!inst->stun_handle) {
2351 PERROR("Failed to create STUN handle\n");
2352 sip_exit_inst(interface);
2355 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2356 if (inst->stun_socket < 0) {
2357 PERROR("Failed to create STUN socket\n");
2358 sip_exit_inst(interface);
2361 inst->stun_state = STUN_STATE_UNRESOLVED;
2364 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2366 any_sip_interface = 1;
2371 void sip_exit_inst(struct interface *interface)
2373 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2377 del_timer(&inst->stun_retry_timer);
2378 del_timer(&inst->register_retry_timer);
2379 del_timer(&inst->register_option_timer);
2380 if (inst->stun_socket)
2381 su_close(inst->stun_socket);
2382 if (inst->stun_handle)
2383 stun_handle_destroy(inst->stun_handle);
2384 if (inst->register_handle)
2385 nua_handle_destroy(inst->register_handle);
2387 su_root_destroy(inst->root);
2389 nua_destroy(inst->nua);
2390 FREE(inst, sizeof(*inst));
2391 interface->sip_inst = NULL;
2393 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2395 /* check if there is any other SIP interface left */
2396 interface = interface_first;
2398 if (interface->sip_inst)
2400 interface = interface->next;
2403 any_sip_interface = 0;
2406 extern su_log_t su_log_default[];
2407 extern su_log_t nua_log[];
2408 //extern su_log_t soa_log[];
2414 /* init SOFIA lib */
2416 su_home_init(sip_home);
2418 if (options.deb & DEBUG_SIP) {
2419 su_log_set_level(su_log_default, 9);
2420 su_log_set_level(nua_log, 9);
2421 //su_log_set_level(soa_log, 9);
2424 for (i = 0; i < 256; i++)
2425 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);
2427 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2434 su_home_deinit(sip_home);
2437 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2440 static void sip_handle_stun(struct sip_inst *inst)
2444 switch (inst->stun_state) {
2445 case STUN_STATE_UNRESOLVED:
2446 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2447 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2448 STUNTAG_SOCKET(inst->stun_socket),
2449 STUNTAG_REGISTER_EVENTS(1),
2452 PERROR("Failed to call stun_bind()\n");
2453 inst->stun_state = STUN_STATE_FAILED;
2456 inst->stun_state = STUN_STATE_RESOLVING;
2457 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2458 add_trace("server", "addr", "%s", inst->stun_server);
2464 static void sip_handle_register(struct sip_inst *inst)
2466 char from[128] = "";
2468 char contact[128] = "";
2470 switch (inst->register_state) {
2471 case REGISTER_STATE_UNREGISTERED:
2472 /* wait for resoved stun */
2473 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2476 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2477 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2478 if (!inst->register_handle) {
2479 PERROR("Failed to create handle\n");
2480 inst->register_state = REGISTER_STATE_FAILED;
2483 /* apply handle to trace */
2484 sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2485 add_trace("handle", "new", "0x%x", inst->register_handle);
2488 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2489 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2490 if (inst->public_ip[0])
2491 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2493 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2494 add_trace("from", "uri", "%s", from);
2495 add_trace("to", "uri", "%s", to);
2498 nua_register(inst->register_handle,
2499 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2500 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2501 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2504 inst->register_state = REGISTER_STATE_REGISTERING;
2511 void sip_handle(void)
2513 struct interface *interface = interface_first;
2514 struct sip_inst *inst;
2517 if (interface->sip_inst) {
2518 inst = (struct sip_inst *) interface->sip_inst;
2519 su_root_step(inst->root, 0);
2520 sip_handle_stun(inst);
2521 sip_handle_register(inst);
2523 interface = interface->next;
2527 /* deletes when back in event loop */
2528 static int delete_event(struct lcr_work *work, void *instance, int index)
2530 class Psip *psip = (class Psip *)instance;
2539 * generate audio, if no data is received from bridge
2542 void Psip::set_tone(const char *dir, const char *tone)
2544 Port::set_tone(dir, tone);
2549 void Psip::update_load(void)
2551 /* don't trigger load event if event already active */
2552 if (p_s_load_timer.active)
2555 /* don't start timer if ... */
2556 if (!p_tone_name[0] && !p_dov_tx)
2559 p_s_next_tv_sec = 0;
2560 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2563 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2565 class Psip *psip = (class Psip *)instance;
2567 /* stop timer if ... */
2568 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2576 #define SEND_SIP_LEN 160
2578 void Psip::load_tx(void)
2581 struct timeval current_time;
2582 int tosend = SEND_SIP_LEN, i;
2583 unsigned char buf[SEND_SIP_LEN], *p = buf;
2586 gettimeofday(¤t_time, NULL);
2587 if (!p_s_next_tv_sec) {
2588 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2589 p_s_next_tv_sec = current_time.tv_sec;
2590 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2591 if (p_s_next_tv_usec >= 1000000) {
2592 p_s_next_tv_usec -= 1000000;
2595 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
2597 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2598 + (current_time.tv_usec - p_s_next_tv_usec);
2599 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2600 /* if clock drifts too much, set next timeout event to current timer + 160 */
2602 p_s_next_tv_sec = current_time.tv_sec;
2603 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2604 if (p_s_next_tv_usec >= 1000000) {
2605 p_s_next_tv_usec -= 1000000;
2609 /* if diff is positive, it took too long, so next timeout will be earlier */
2610 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2611 if (p_s_next_tv_usec >= 1000000) {
2612 p_s_next_tv_usec -= 1000000;
2616 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
2620 if (p_tone_name[0]) {
2621 tosend -= read_audio(p, tosend);
2624 tosend -= dov_tx(p, tosend);
2627 PERROR("buffer is not completely filled\n");
2632 for (i = 0; i < SEND_SIP_LEN; i++) {
2636 /* transmit data via rtp */
2637 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);