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;
65 char auth_password[128];
70 char stun_server[128];
71 stun_handle_t *stun_handle;
72 su_socket_t stun_socket;
73 char stun_ipaddr[128];
74 struct lcr_timer stun_retry_timer;
77 unsigned short rtp_port_from;
78 unsigned short rtp_port_to;
79 unsigned short next_rtp_port;
83 static int delete_event(struct lcr_work *work, void *instance, int index);
84 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index);
85 static int load_timer(struct lcr_timer *timer, void *instance, int index);
90 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
93 if (interface->rtp_bridge)
95 p_s_sip_inst = interface->sip_inst;
96 memset(&p_s_delete, 0, sizeof(p_s_delete));
97 add_work(&p_s_delete, delete_event, this, 0);
100 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
101 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
102 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
103 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
104 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
105 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
106 p_s_rtp_ip_local = 0;
107 p_s_rtp_ip_remote = 0;
108 p_s_rtp_port_local = 0;
109 p_s_rtp_port_remote = 0;
114 p_s_rtp_tx_action = 0;
115 p_s_rtp_is_connected = 0;
117 /* create option timer */
118 memset(&p_s_invite_option_timer, 0, sizeof(p_s_invite_option_timer));
119 add_timer(&p_s_invite_option_timer, invite_option_timer, this, 0);
120 p_s_invite_direction = 0;
123 memset(&p_s_load_timer, 0, sizeof(p_s_load_timer));
124 add_timer(&p_s_load_timer, load_timer, this, 0);
127 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
129 FATAL("No SIP instance for interface\n");
138 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
140 del_timer(&p_s_invite_option_timer);
141 del_timer(&p_s_load_timer);
142 del_work(&p_s_delete);
147 static const char *media_type2name(uint8_t media_type) {
148 switch (media_type) {
149 case MEDIA_TYPE_ULAW:
151 case MEDIA_TYPE_ALAW:
155 case MEDIA_TYPE_GSM_HR:
157 case MEDIA_TYPE_GSM_EFR:
166 static void sip_trace_header(class Psip *sip, const char *interface_name, const char *message, int direction)
168 struct interface *interface = NULL;
171 interface = getinterfacebyname(interface_name);
173 /* init trace with given values */
176 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
177 sip?sip->p_dialinginfo.id:NULL,
188 /* according to RFC 3550 */
190 #if __BYTE_ORDER == __LITTLE_ENDIAN
191 uint8_t csrc_count:4,
195 uint8_t payload_type:7,
197 #elif __BYTE_ORDER == __BIG_ENDIAN
208 } __attribute__((packed));
213 } __attribute__((packed));
215 #define RTP_VERSION 2
217 #define PAYLOAD_TYPE_ULAW 0
218 #define PAYLOAD_TYPE_ALAW 8
219 #define PAYLOAD_TYPE_GSM 3
221 /* decode an rtp frame */
222 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
224 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
225 struct rtp_x_hdr *rtpxh;
229 unsigned char *from, *to;
233 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
236 if (rtph->version != RTP_VERSION) {
237 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
240 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
241 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
242 if (payload_len < 0) {
243 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
244 "csrc count = %d)\n", len, rtph->csrc_count);
247 if (rtph->extension) {
248 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
249 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
250 "extension header\n");
253 rtpxh = (struct rtp_x_hdr *)payload;
254 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
256 payload_len -= x_len;
257 if (payload_len < 0) {
258 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
259 "extension header exceeds frame length\n");
264 if (payload_len < 0) {
265 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
269 payload_len -= payload[payload_len - 1];
270 if (payload_len < 0) {
271 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
272 "greater than payload\n");
277 switch (rtph->payload_type) {
279 we only support alaw and ulaw!
280 case RTP_PT_GSM_FULL:
281 if (payload_len != 33) {
282 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
283 "payload length != 33 (len = %d)\n",
289 if (payload_len != 31) {
290 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
291 "payload length != 31 (len = %d)\n",
296 case RTP_PT_GSM_HALF:
297 if (payload_len != 14) {
298 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
299 "payload length != 14 (len = %d)\n",
305 case PAYLOAD_TYPE_ALAW:
306 if (options.law != 'a') {
307 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
311 case PAYLOAD_TYPE_ULAW:
312 if (options.law == 'a') {
313 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
318 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
319 "type %d\n", rtph->payload_type);
323 if (payload_len <= 0) {
324 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
330 psip->record(payload, payload_len, 0); // from down
332 psip->tap(payload, payload_len, 0); // from down
337 if (psip->p_echotest) {
338 /* echo rtp data we just received */
339 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
343 *to++ = flip[*from++];
345 psip->dov_rx(payload, payload_len);
346 psip->bridge_tx(payload, payload_len);
351 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
353 class Psip *psip = (class Psip *) instance;
355 unsigned char buffer[256];
358 if ((what & LCR_FD_READ)) {
359 len = read(fd->fd, &buffer, sizeof(buffer));
361 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
362 // psip->rtp_close();
363 // psip->rtp_shutdown();
366 if (psip->p_s_rtp_is_connected)
367 rc = rtp_decode(psip, buffer, len);
373 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
375 // class Psip *psip = (class Psip *) instance;
377 unsigned char buffer[256];
379 if ((what & LCR_FD_READ)) {
380 len = read(fd->fd, &buffer, sizeof(buffer));
382 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
383 // psip->rtp_close();
384 // psip->rtp_shutdown();
387 PDEBUG(DEBUG_SIP, "rtcp!\n");
393 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
396 socklen_t alen = sizeof(*sin_local);
398 sin_local->sin_family = AF_INET;
399 sin_local->sin_addr.s_addr = htonl(ip);
400 sin_local->sin_port = htons(port);
402 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
406 /* retrieve the address we actually bound to, in case we
407 * passed INADDR_ANY as IP address */
408 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
411 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
414 socklen_t alen = sizeof(*sin_local);
416 sin_remote->sin_family = AF_INET;
417 sin_remote->sin_addr.s_addr = htonl(ip);
418 sin_remote->sin_port = htons(port);
420 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
422 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
426 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
429 int Psip::rtp_open(void)
431 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
433 // struct in_addr ia;
435 unsigned short start_port;
438 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
444 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
446 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
452 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
455 ip = htonl(INADDR_ANY);
457 start_port = inst->next_rtp_port;
459 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, inst->next_rtp_port);
463 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, inst->next_rtp_port + 1);
465 p_s_rtp_port_local = inst->next_rtp_port;
466 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
469 /* reopen rtp socket and try again with next udp port */
470 unregister_fd(&p_s_rtp_fd);
471 close(p_s_rtp_fd.fd);
473 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
479 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
482 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
483 if (inst->next_rtp_port == start_port)
485 /* we must use rc2, in order to preserve rc */
488 PDEBUG(DEBUG_SIP, "failed to find port\n");
492 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
493 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
494 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
496 return p_s_rtp_port_local;
499 int Psip::rtp_connect(void)
504 ia.s_addr = htonl(p_s_rtp_ip_remote);
505 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
507 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);
511 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);
515 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
516 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
517 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
518 p_s_rtp_is_connected = 1;
522 void Psip::rtp_close(void)
524 if (p_s_rtp_fd.fd > 0) {
525 unregister_fd(&p_s_rtp_fd);
526 close(p_s_rtp_fd.fd);
529 if (p_s_rtcp_fd.fd > 0) {
530 unregister_fd(&p_s_rtcp_fd);
531 close(p_s_rtcp_fd.fd);
534 if (p_s_rtp_is_connected) {
535 PDEBUG(DEBUG_SIP, "rtp closed\n");
536 p_s_rtp_is_connected = 0;
541 void tv_difference(struct timeval *diff, const struct timeval *from,
542 const struct timeval *__to)
544 struct timeval _to = *__to, *to = &_to;
546 if (to->tv_usec < from->tv_usec) {
548 to->tv_usec += 1000000;
551 diff->tv_usec = to->tv_usec - from->tv_usec;
552 diff->tv_sec = to->tv_sec - from->tv_sec;
555 /* encode and send a rtp frame */
556 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
558 struct rtp_hdr *rtph;
560 int duration; /* in samples */
561 unsigned char buffer[256];
565 record(data, len, 1); // from up
567 tap(data, len, 1); // from up
569 if (!p_s_rtp_is_connected) {
574 if (!p_s_rtp_tx_action) {
575 /* initialize sequences */
576 p_s_rtp_tx_action = 1;
577 p_s_rtp_tx_ssrc = rand();
578 p_s_rtp_tx_sequence = random();
579 p_s_rtp_tx_timestamp = random();
580 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
583 switch (payload_type) {
585 we only support alaw and ulaw!
586 case RTP_PT_GSM_FULL:
594 case RTP_PT_GSM_HALF:
599 case PAYLOAD_TYPE_ALAW:
600 case PAYLOAD_TYPE_ULAW:
605 PERROR("unsupported message type %d\n", payload_type);
611 struct timeval tv, tv_diff;
612 long int usec_diff, frame_diff;
614 gettimeofday(&tv, NULL);
615 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
616 p_s_rtp_tx_last_tv = tv;
618 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
619 frame_diff = (usec_diff / 20000);
621 if (abs(frame_diff) > 1) {
622 long int frame_diff_excess = frame_diff - 1;
624 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
625 p_s_rtp_tx_sequence += frame_diff_excess;
626 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
631 rtph = (struct rtp_hdr *) buffer;
632 rtph->version = RTP_VERSION;
635 rtph->csrc_count = 0;
637 rtph->payload_type = payload_type;
638 rtph->sequence = htons(p_s_rtp_tx_sequence++);
639 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
640 p_s_rtp_tx_timestamp += duration;
641 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
642 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
644 if (p_s_rtp_fd.fd > 0) {
645 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
646 if (len != sizeof(struct rtp_hdr) + payload_len) {
647 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
657 /* receive from remote */
658 int Psip::bridge_rx(unsigned char *data, int len)
662 /* don't bridge, if tones are provided */
663 if (p_tone_name[0] || p_dov_tx)
669 if ((ret = Port::bridge_rx(data, len)))
672 /* write to rx buffer */
674 p_s_rxdata[p_s_rxpos++] = flip[*data++];
675 if (p_s_rxpos == 160) {
678 /* transmit data via rtp */
679 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
686 /* taken from freeswitch */
687 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
688 static int status2cause(int status)
692 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
698 return 21; //SWITCH_CAUSE_CALL_REJECTED;
700 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
703 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
706 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
708 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
717 return 127; //SWITCH_CAUSE_INTERWORKING;
719 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
724 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
727 return 17; //SWITCH_CAUSE_USER_BUSY;
729 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
732 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
734 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
736 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
740 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
743 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
745 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
747 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
751 static int cause2status(int cause, int location, const char **st)
757 s = 404; *st = sip_404_Not_found;
760 s = 404; *st = sip_404_Not_found;
763 s = 404; *st = sip_404_Not_found;
766 s = 486; *st = sip_486_Busy_here;
769 s = 408; *st = sip_408_Request_timeout;
772 s = 480; *st = sip_480_Temporarily_unavailable;
775 s = 480; *st = sip_480_Temporarily_unavailable;
778 if (location == LOCATION_USER) {
779 s = 603; *st = sip_603_Decline;
781 s = 403; *st = sip_403_Forbidden;
785 //s = 301; *st = sip_301_Moved_permanently;
786 s = 410; *st = sip_410_Gone;
789 s = 410; *st = sip_410_Gone;
792 s = 404; *st = sip_404_Not_found;
795 s = 502; *st = sip_502_Bad_gateway;
798 s = 484; *st = sip_484_Address_incomplete;
801 s = 501; *st = sip_501_Not_implemented;
804 s = 480; *st = sip_480_Temporarily_unavailable;
807 s = 503; *st = sip_503_Service_unavailable;
810 s = 503; *st = sip_503_Service_unavailable;
813 s = 503; *st = sip_503_Service_unavailable;
816 s = 503; *st = sip_503_Service_unavailable;
819 s = 503; *st = sip_503_Service_unavailable;
822 s = 403; *st = sip_403_Forbidden;
825 s = 403; *st = sip_403_Forbidden;
828 s = 503; *st = sip_503_Service_unavailable;
831 s = 488; *st = sip_488_Not_acceptable;
834 s = 501; *st = sip_501_Not_implemented;
837 s = 488; *st = sip_488_Not_acceptable;
840 s = 501; *st = sip_501_Not_implemented;
843 s = 403; *st = sip_403_Forbidden;
846 s = 503; *st = sip_503_Service_unavailable;
849 s = 504; *st = sip_504_Gateway_time_out;
852 s = 500; *st = sip_500_Internal_server_error;
855 s = 500; *st = sip_500_Internal_server_error;
858 s = 468; *st = sip_486_Busy_here;
864 /* use STUN ip, or return the ip without change */
865 unsigned int Psip::get_local_ip(unsigned int ip)
867 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
869 if (inst->stun_handle && inst->stun_ipaddr[0]) {
870 PDEBUG(DEBUG_SIP, "RTP local IP is replaced by STUN ip %s\n", inst->stun_ipaddr);
871 inet_pton(AF_INET, inst->stun_ipaddr, &ip);
878 * endpoint sends messages to the SIP port
881 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
883 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
886 struct lcr_msg *message;
888 unsigned char payload_type;
890 if (param->connectinfo.rtpinfo.port) {
891 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
893 media_type = param->connectinfo.rtpinfo.media_types[0];
894 payload_type = param->connectinfo.rtpinfo.payload_types[0];
895 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
896 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
897 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
898 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
899 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
901 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
902 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
903 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
904 /* open local RTP peer (if not bridging) */
905 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
906 nua_cancel(p_s_handle, TAG_END());
907 nua_handle_destroy(p_s_handle);
909 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
910 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
912 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
913 message->param.disconnectinfo.cause = 41;
914 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
915 message_put(message);
916 new_state(PORT_STATE_RELEASE);
917 trigger_work(&p_s_delete);
922 memset(&ia, 0, sizeof(ia));
923 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
926 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
930 "m=audio %d RTP/AVP %d\r\n"
931 "a=rtpmap:%d %s/8000\r\n"
932 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
933 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
936 * If this response causes corrupt messages, like SDP body inside or
937 * before header, check if the sofia-sip-gcc-4.8.patch was applied.
938 * If it is still corrupted, try to disable optimization when compiling
941 nua_respond(p_s_handle, SIP_200_OK,
942 NUTAG_MEDIA_ENABLE(0),
943 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
944 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
946 new_state(PORT_STATE_CONNECT);
947 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
948 add_trace("respond", "value", "200 OK");
949 add_trace("reason", NULL, "call connected");
950 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
951 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
952 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
958 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
960 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
961 struct lcr_msg *message;
962 char cause_str[128] = "";
963 int cause = param->disconnectinfo.cause;
964 int location = param->disconnectinfo.cause;
966 const char *status_text;
968 if (cause > 0 && cause <= 127) {
969 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
973 case PORT_STATE_OUT_SETUP:
974 case PORT_STATE_OUT_PROCEEDING:
975 case PORT_STATE_OUT_ALERTING:
976 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
977 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
979 add_trace("cause", "value", "%d", cause);
981 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
983 case PORT_STATE_IN_SETUP:
984 case PORT_STATE_IN_PROCEEDING:
985 case PORT_STATE_IN_ALERTING:
986 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
987 status = cause2status(cause, location, &status_text);
988 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
990 add_trace("cause", "value", "%d", cause);
991 add_trace("respond", "value", "%d %s", status, status_text);
993 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
994 nua_handle_destroy(p_s_handle);
996 trigger_work(&p_s_delete);
999 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1000 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1002 add_trace("cause", "value", "%d", cause);
1004 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1007 if (message_id == MESSAGE_DISCONNECT) {
1008 while(p_epointlist) {
1009 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1010 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1011 message->param.disconnectinfo.location = LOCATION_BEYOND;
1012 message_put(message);
1014 free_epointlist(p_epointlist);
1018 new_state(PORT_STATE_RELEASE);
1023 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1025 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1026 char from[128] = "";
1028 char contact[128] = "";
1029 const char *local = inst->local_peer;
1031 const char *remote = inst->remote_peer;
1032 char sdp_str[512], pt_str[32];
1034 struct epoint_list *epointlist;
1035 sip_cseq_t *cseq = NULL;
1036 struct lcr_msg *message;
1037 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1038 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1040 unsigned char *payload_types;
1044 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1046 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1047 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1048 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1049 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1050 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1052 if (param->setup.rtpinfo.port) {
1053 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1055 media_types = param->setup.rtpinfo.media_types;
1056 payload_types = param->setup.rtpinfo.payload_types;
1057 payloads = param->setup.rtpinfo.payloads;
1058 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1059 p_s_rtp_port_local = param->setup.rtpinfo.port;
1060 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1061 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1063 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1065 media_types = &lcr_media;
1066 payload_types = &lcr_payload;
1069 /* open local RTP peer (if not bridging) */
1070 if (rtp_open() < 0) {
1071 PERROR("Failed to open RTP sockets\n");
1072 /* send release message to endpoit */
1073 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1074 message->param.disconnectinfo.cause = 41;
1075 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1076 message_put(message);
1077 new_state(PORT_STATE_RELEASE);
1078 trigger_work(&p_s_delete);
1081 if (!p_s_rtp_ip_local) {
1084 /* extract IP from local peer */
1085 SCPY(local_ip, local);
1086 p = strchr(local_ip, ':');
1089 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1090 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1091 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1095 memset(&ia, 0, sizeof(ia));
1096 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1097 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1099 PERROR("Failed to create handle\n");
1100 /* send release message to endpoit */
1101 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1102 message->param.disconnectinfo.cause = 41;
1103 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1104 message_put(message);
1105 new_state(PORT_STATE_RELEASE);
1106 trigger_work(&p_s_delete);
1109 /* apply handle to trace */
1110 sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1111 add_trace("handle", "new", "0x%x", p_s_handle);
1116 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1120 "m=audio %d RTP/AVP"
1121 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1122 for (i = 0; i < payloads; i++) {
1123 SPRINT(pt_str, " %d", payload_types[i]);
1124 SCAT(sdp_str, pt_str);
1126 SCAT(sdp_str, "\r\n");
1127 for (i = 0; i < payloads; i++) {
1128 SPRINT(pt_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1129 SCAT(sdp_str, pt_str);
1131 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1133 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1134 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1135 if (inst->stun_handle && inst->stun_ipaddr[0])
1136 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->stun_ipaddr);
1138 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1139 add_trace("from", "uri", "%s", from);
1140 add_trace("to", "uri", "%s", to);
1141 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1142 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1143 for (i = 0; i < payloads; i++)
1144 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1147 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1149 nua_invite(p_s_handle,
1150 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1151 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1152 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1153 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1154 NUTAG_MEDIA_ENABLE(0),
1155 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1156 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1157 new_state(PORT_STATE_OUT_SETUP);
1159 p_s_invite_direction = DIRECTION_OUT;
1162 PDEBUG(DEBUG_SIP, "do overlap\n");
1163 new_state(PORT_STATE_OUT_OVERLAP);
1164 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1165 message_put(message);
1167 PDEBUG(DEBUG_SIP, "do proceeding\n");
1168 new_state(PORT_STATE_OUT_PROCEEDING);
1169 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1170 message_put(message);
1173 /* attach only if not already */
1174 epointlist = p_epointlist;
1176 if (epointlist->epoint_id == epoint_id)
1178 epointlist = epointlist->next;
1181 epointlist_new(epoint_id);
1186 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1189 // struct in_addr ia;
1191 switch (param->notifyinfo.notify) {
1192 case INFO_NOTIFY_REMOTE_HOLD:
1196 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1198 "c=IN IP4 0.0.0.0\r\n"
1201 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1202 nua_info(p_s_handle,
1203 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1204 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1205 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1206 NUTAG_MEDIA_ENABLE(0),
1207 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1208 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1211 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1213 memset(&ia, 0, sizeof(ia));
1214 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1217 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1221 "m=audio %d RTP/AVP %d\r\n"
1222 "a=rtpmap:%d %s/8000\r\n"
1223 , 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));
1224 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1225 nua_info(p_s_handle,
1226 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1227 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1228 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1229 NUTAG_MEDIA_ENABLE(0),
1230 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1231 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1239 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1243 /* prepare DTMF info payload */
1249 /* start invite to handle DTMF */
1250 nua_info(p_s_handle,
1251 NUTAG_MEDIA_ENABLE(0),
1252 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1253 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1258 /* NOTE: incomplete and not working */
1259 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1263 /* prepare DTMF info payload */
1267 , param->information.id);
1269 /* start invite to handle DTMF */
1270 nua_info(p_s_handle,
1271 NUTAG_MEDIA_ENABLE(0),
1272 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1273 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1278 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1280 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1282 if (Port::message_epoint(epoint_id, message_id, param))
1285 switch(message_id) {
1286 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1287 if (p_state != PORT_STATE_IN_SETUP
1288 && p_state != PORT_STATE_IN_PROCEEDING)
1290 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1291 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1292 add_trace("respond", "value", "180 Ringing");
1294 new_state(PORT_STATE_IN_ALERTING);
1297 case MESSAGE_CONNECT: /* call is connected on LCR side */
1298 if (p_state != PORT_STATE_IN_SETUP
1299 && p_state != PORT_STATE_IN_PROCEEDING
1300 && p_state != PORT_STATE_IN_ALERTING)
1302 message_connect(epoint_id, message_id, param);
1305 case MESSAGE_DISCONNECT: /* call has been disconnected */
1306 case MESSAGE_RELEASE: /* call has been released */
1307 message_release(epoint_id, message_id, param);
1310 case MESSAGE_SETUP: /* dial-out command received from epoint */
1311 message_setup(epoint_id, message_id, param);
1314 case MESSAGE_INFORMATION: /* overlap dialing */
1315 if (p_state != PORT_STATE_OUT_OVERLAP)
1317 message_information(epoint_id, message_id, param);
1320 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1321 if (p_state == PORT_STATE_CONNECT)
1322 message_dtmf(epoint_id, message_id, param);
1323 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1324 if (p_state == PORT_STATE_CONNECT)
1325 message_notify(epoint_id, message_id, param);
1329 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1335 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)
1339 if (!sip->sip_payload) {
1340 PDEBUG(DEBUG_SIP, "no payload given\n");
1344 sdp_parser_t *parser;
1347 sdp_attribute_t *attr;
1349 sdp_connection_t *conn;
1351 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1353 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1357 if (!(sdp = sdp_session(parser))) {
1358 sdp_parser_free(parser);
1361 for (m = sdp->sdp_media; m; m = m->m_next) {
1362 if (m->m_proto != sdp_proto_rtp)
1364 if (m->m_type != sdp_media_audio)
1366 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1368 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1369 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1371 if (m->m_connections) {
1372 conn = m->m_connections;
1373 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1374 inet_pton(AF_INET, conn->c_address, ip);
1375 *ip = ntohl(p_s_rtp_ip_remote);
1377 char *p = sip->sip_payload->pl_data;
1380 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1381 p = strstr(p, "c=IN IP4 ");
1383 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1384 sdp_parser_free(parser);
1388 if ((p = strchr(addr, '\n'))) *p = '\0';
1389 if ((p = strchr(addr, '\r'))) *p = '\0';
1390 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1391 inet_pton(AF_INET, addr, ip);
1392 *ip = ntohl(p_s_rtp_ip_remote);
1394 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1397 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1398 /* append to payload list, if there is space */
1399 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1400 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1401 media_type = MEDIA_TYPE_ALAW;
1402 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1403 media_type = MEDIA_TYPE_ULAW;
1404 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1405 media_type = MEDIA_TYPE_GSM;
1406 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1407 media_type = MEDIA_TYPE_GSM_EFR;
1408 else if (!strcmp(map->rm_encoding, "AMR"))
1409 media_type = MEDIA_TYPE_AMR;
1410 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1411 media_type = MEDIA_TYPE_GSM_HR;
1412 if (media_type && *payloads <= max_payloads) {
1413 *payload_types++ = map->rm_pt;
1414 *media_types++ = media_type;
1420 sdp_parser_free(parser);
1425 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[])
1427 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1428 const char *from = "", *to = "", *name = "";
1431 class Endpoint *epoint;
1432 struct lcr_msg *message;
1433 struct interface *interface;
1434 int media_types[32];
1435 uint8_t payload_types[32];
1439 interface = getinterfacebyname(inst->interface_name);
1441 PERROR("Cannot find interface %s.\n", inst->interface_name);
1445 if (sip->sip_from) {
1446 if (sip->sip_from->a_url)
1447 from = sip->sip_from->a_url->url_user;
1448 if (sip->sip_from->a_display) {
1449 name = sip->sip_from->a_display;
1450 if (!strncmp(name, "\"IMSI", 5)) {
1451 strncpy(imsi, name + 5, 15);
1458 if (sip->sip_to->a_url)
1459 to = sip->sip_to->a_url->url_user;
1461 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1463 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1464 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1466 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1467 if (!p_s_rtp_bridge) {
1470 /* check if supported payload type exists */
1471 for (i = 0; i < payloads; i++) {
1472 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1475 if (i == payloads) {
1476 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1484 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1486 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1487 nua_handle_destroy(nh);
1489 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1491 add_trace("respond", "value", "415 Unsupported Media");
1493 add_trace("respond", "value", "400 Bad Request");
1494 add_trace("reason", NULL, "offered codec does not match");
1496 new_state(PORT_STATE_RELEASE);
1497 trigger_work(&p_s_delete);
1501 /* open local RTP peer (if not bridging) */
1502 if (!p_s_rtp_bridge && rtp_open() < 0) {
1503 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1504 nua_handle_destroy(nh);
1506 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1507 add_trace("respond", "value", "500 Internal Server Error");
1508 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1510 new_state(PORT_STATE_RELEASE);
1511 trigger_work(&p_s_delete);
1516 sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1517 add_trace("handle", "new", "0x%x", nh);
1521 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1522 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1523 /* caller information */
1525 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1526 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1527 add_trace("calling", "present", "unavailable");
1529 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1530 add_trace("calling", "present", "allowed");
1531 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1532 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1533 SCPY(p_callerinfo.id, from);
1534 add_trace("calling", "number", "%s", from);
1535 SCPY(p_callerinfo.name, name);
1537 add_trace("calling", "name", "%s", name);
1538 SCPY(p_callerinfo.imsi, imsi);
1540 add_trace("calling", "imsi", "%s", imsi);
1542 SCPY(p_callerinfo.interface, inst->interface_name);
1543 /* dialing information */
1545 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1546 SCAT(p_dialinginfo.id, to);
1547 add_trace("dialing", "number", "%s", to);
1550 /* bearer capability */
1551 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1552 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1553 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1554 add_trace("bearer", "capa", "speech");
1555 add_trace("bearer", "mode", "circuit");
1556 /* if packet mode works some day, see dss1.cpp for conditions */
1557 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1561 /* create endpoint */
1563 FATAL("Incoming call but already got an endpoint.\n");
1564 if (!(epoint = new Endpoint(p_serial, 0)))
1565 FATAL("No memory for Endpoint instance\n");
1566 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1567 epointlist_new(epoint->ep_serial);
1569 #ifdef NUTAG_AUTO100
1570 /* send trying (proceeding) */
1571 nua_respond(nh, SIP_100_TRYING, TAG_END());
1572 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1573 add_trace("respond", "value", "100 Trying");
1577 new_state(PORT_STATE_IN_PROCEEDING);
1579 /* send setup message to endpoit */
1580 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1581 message->param.setup.port_type = p_type;
1582 // message->param.setup.dtmf = 0;
1583 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1584 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1585 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1586 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1587 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1588 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1589 if (p_s_rtp_bridge) {
1592 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1593 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1594 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1595 /* add codecs to setup message */
1596 for (i = 0; i < payloads; i++) {
1597 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1598 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1599 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1602 message->param.setup.rtpinfo.payloads = i;
1604 message_put(message);
1606 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer\n");
1607 if (inst->options_timeout)
1608 schedule_timer(&p_s_invite_option_timer, inst->options_timeout, 0);
1609 p_s_invite_direction = DIRECTION_IN;
1611 /* send progress, if tones are available and if we don't bridge */
1612 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1615 unsigned char payload_type;
1617 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1618 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1619 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1620 /* open local RTP peer (if not bridging) */
1621 if (rtp_connect() < 0) {
1622 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1623 nua_handle_destroy(nh);
1625 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1626 add_trace("respond", "value", "500 Internal Server Error");
1627 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1629 new_state(PORT_STATE_RELEASE);
1630 trigger_work(&p_s_delete);
1631 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1632 message->param.disconnectinfo.cause = 41;
1633 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1634 message_put(message);
1635 new_state(PORT_STATE_RELEASE);
1636 trigger_work(&p_s_delete);
1640 memset(&ia, 0, sizeof(ia));
1641 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1644 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1648 "m=audio %d RTP/AVP %d\r\n"
1649 "a=rtpmap:%d %s/8000\r\n"
1650 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
1651 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1653 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1654 NUTAG_MEDIA_ENABLE(0),
1655 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1656 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1657 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1658 add_trace("respond", "value", "183 SESSION PROGRESS");
1659 add_trace("reason", NULL, "audio available");
1660 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1661 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1662 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1667 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[])
1669 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1670 struct lcr_msg *message;
1673 PDEBUG(DEBUG_SIP, "bye received\n");
1675 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
1676 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1677 cause = atoi(sip->sip_reason->re_cause);
1678 add_trace("cause", "value", "%d", cause);
1682 // let stack do bye automaticall, since it will not accept our response for some reason
1683 // nua_respond(nh, SIP_200_OK, TAG_END());
1684 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1685 add_trace("respond", "value", "200 OK");
1687 // nua_handle_destroy(nh);
1692 while(p_epointlist) {
1693 /* send setup message to endpoit */
1694 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1695 message->param.disconnectinfo.cause = cause ? : 16;
1696 message->param.disconnectinfo.location = LOCATION_BEYOND;
1697 message_put(message);
1699 free_epointlist(p_epointlist);
1701 new_state(PORT_STATE_RELEASE);
1702 trigger_work(&p_s_delete);
1705 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[])
1707 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1708 struct lcr_msg *message;
1710 PDEBUG(DEBUG_SIP, "cancel received\n");
1712 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
1715 nua_handle_destroy(nh);
1720 while(p_epointlist) {
1721 /* send setup message to endpoit */
1722 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1723 message->param.disconnectinfo.cause = 16;
1724 message->param.disconnectinfo.location = LOCATION_BEYOND;
1725 message_put(message);
1727 free_epointlist(p_epointlist);
1729 new_state(PORT_STATE_RELEASE);
1730 trigger_work(&p_s_delete);
1733 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[])
1735 PDEBUG(DEBUG_SIP, "bye response received\n");
1737 nua_handle_destroy(nh);
1742 trigger_work(&p_s_delete);
1745 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[])
1747 PDEBUG(DEBUG_SIP, "cancel response received\n");
1749 nua_handle_destroy(nh);
1754 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 if (inst->options_timeout)
1815 schedule_timer(&p_s_invite_option_timer, inst->options_timeout, 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);
1848 if (status < 100 || status > 199)
1850 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1856 if (status >= 200 && status <= 299) {
1857 PDEBUG(DEBUG_SIP, "do connect\n");
1858 nua_ack(nh, TAG_END());
1859 new_state(PORT_STATE_CONNECT);
1860 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1861 if (p_s_rtp_bridge) {
1862 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1863 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1864 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1865 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1866 message->param.connectinfo.rtpinfo.payloads = 1;
1868 message_put(message);
1871 cause = status2cause(status);
1872 location = LOCATION_BEYOND;
1875 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1877 while(p_epointlist) {
1878 /* send setup message to endpoit */
1879 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1880 message->param.disconnectinfo.cause = cause;
1881 message->param.disconnectinfo.location = location;
1882 message_put(message);
1884 free_epointlist(p_epointlist);
1887 new_state(PORT_STATE_RELEASE);
1891 trigger_work(&p_s_delete);
1894 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[])
1896 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1897 int cause = 0, location = 0;
1898 struct lcr_msg *message;
1900 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
1902 if (status == 200) {
1903 PDEBUG(DEBUG_SIP, "options ok, scheduling timer\n");
1904 schedule_timer(&p_s_invite_option_timer, inst->options_timeout, 0);
1908 nua_handle_destroy(nh);
1913 cause = status2cause(status);
1914 location = LOCATION_BEYOND;
1916 while(p_epointlist) {
1917 /* send setup message to endpoit */
1918 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1919 message->param.disconnectinfo.cause = cause;
1920 message->param.disconnectinfo.location = location;
1921 message_put(message);
1923 free_epointlist(p_epointlist);
1925 new_state(PORT_STATE_RELEASE);
1926 trigger_work(&p_s_delete);
1929 static void 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[])
1931 sip_www_authenticate_t const *authenticate = NULL;
1932 char const *realm = NULL;
1933 char const *scheme = NULL;
1936 char authentication[256] = "";
1938 PDEBUG(DEBUG_SIP, "challenge order received\n");
1940 if (sip->sip_www_authenticate) {
1941 authenticate = sip->sip_www_authenticate;
1942 } else if (sip->sip_proxy_authenticate) {
1943 authenticate = sip->sip_proxy_authenticate;
1945 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1946 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1947 add_trace("error", NULL, "Authentication method unknwon");
1952 scheme = (char const *) authenticate->au_scheme;
1953 if (authenticate->au_params) {
1954 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1955 if ((realm = strstr(cur, "realm="))) {
1962 if (!scheme || !realm) {
1963 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1964 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1965 add_trace("error", NULL, "Authentication header has no realm or scheme");
1970 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1971 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1973 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1974 add_trace("scheme", NULL, "%s", scheme);
1975 add_trace("realm", NULL, "%s", realm);
1976 add_trace("user", NULL, "%s", inst->auth_user);
1977 add_trace("pass", NULL, "%s", inst->auth_password);
1980 #warning hier als option
1981 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1984 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[])
1986 struct sip_inst *inst = (struct sip_inst *) magic;
1988 class Psip *psip = NULL;
1990 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
1994 if (inst->register_handle == nh) {
1995 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_OUT);
1996 add_trace("value", NULL, "%d", status);
1997 add_trace("phrase", NULL, "%s", phrase);
2002 inst->register_state = REGISTER_STATE_REGISTERED;
2003 if (inst->options_timeout)
2004 schedule_timer(&inst->register_option_timer, inst->options_timeout, 0);
2008 PDEBUG(DEBUG_SIP, "Register challenge received\n");
2009 challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
2014 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
2015 inst->register_state = REGISTER_STATE_FAILED;
2016 nua_handle_destroy(inst->register_handle);
2017 unsched_timer(&inst->register_option_timer);
2018 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
2019 inst->register_handle = NULL;
2025 /* create or find port instance */
2026 if (event == nua_i_invite)
2029 struct interface *interface = interface_first;
2031 /* create call instance */
2032 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2034 if (!strcmp(interface->name, inst->interface_name))
2036 interface = interface->next;
2039 PERROR("Cannot find interface %s.\n", inst->interface_name);
2042 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2043 FATAL("Cannot create Port instance.\n");
2047 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2048 psip = (class Psip *)port;
2049 if (psip->p_s_handle == nh) {
2057 PERROR("no SIP Port found for handel %p\n", nh);
2058 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2059 nua_handle_destroy(nh);
2063 sip_trace_header(psip, inst->interface_name, "STATUS", DIRECTION_OUT);
2064 add_trace("value", NULL, "%d", status);
2065 add_trace("phrase", NULL, "%s", phrase);
2071 challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
2076 case nua_r_set_params:
2077 PDEBUG(DEBUG_SIP, "setparam response\n");
2080 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2083 PDEBUG(DEBUG_SIP, "error received\n");
2086 PDEBUG(DEBUG_SIP, "state change received\n");
2088 case nua_i_register:
2089 PDEBUG(DEBUG_SIP, "register received\n");
2092 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2095 PDEBUG(DEBUG_SIP, "ack received\n");
2098 PDEBUG(DEBUG_SIP, "active received\n");
2101 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2104 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2107 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2110 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2113 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2115 case nua_i_terminated:
2116 PDEBUG(DEBUG_SIP, "terminated received\n");
2119 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2123 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)
2125 struct sip_inst *inst = (struct sip_inst *) magic;
2129 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2132 case stun_discovery_done:
2133 addrlen = sizeof(sa);
2134 memset(&sa, 0, addrlen);
2135 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2136 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2139 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->stun_ipaddr, sizeof(inst->stun_ipaddr));
2140 inst->stun_state = STUN_STATE_RESOLVED;
2141 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2142 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2143 add_trace("ip", "addr", "%s", inst->stun_ipaddr);
2148 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2149 inst->stun_state = STUN_STATE_FAILED;
2150 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2151 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2156 /* received shutdown due to termination of RTP */
2157 void Psip::rtp_shutdown(void)
2159 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2160 struct lcr_msg *message;
2162 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2164 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2167 nua_handle_destroy(p_s_handle);
2170 while(p_epointlist) {
2171 /* send setup message to endpoit */
2172 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2173 message->param.disconnectinfo.cause = 16;
2174 message->param.disconnectinfo.location = LOCATION_BEYOND;
2175 message_put(message);
2177 free_epointlist(p_epointlist);
2179 new_state(PORT_STATE_RELEASE);
2180 trigger_work(&p_s_delete);
2183 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2185 class Psip *psip = (class Psip *)instance;
2186 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2188 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2191 nua_options(psip->p_s_handle,
2194 // schedule_timer(&psip->p_s_invite_option_timer, inst->options_timeout, 0);
2199 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2201 struct sip_inst *inst = (struct sip_inst *)instance;
2203 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2204 inst->stun_state = STUN_STATE_UNRESOLVED;
2209 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2211 struct sip_inst *inst = (struct sip_inst *)instance;
2213 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2214 inst->register_state = REGISTER_STATE_UNREGISTERED;
2219 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2221 struct sip_inst *inst = (struct sip_inst *)instance;
2222 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2225 nua_options(inst->register_handle,
2228 // schedule_timer(&inst->register_option_timer, inst->options_timeout, 0);
2233 int sip_init_inst(struct interface *interface)
2235 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2238 interface->sip_inst = inst;
2239 SCPY(inst->interface_name, interface->name);
2240 SCPY(inst->local_peer, interface->sip_local_peer);
2241 SCPY(inst->remote_peer, interface->sip_remote_peer);
2242 if (interface->sip_register) {
2243 inst->register_state = REGISTER_STATE_UNREGISTERED;
2244 SCPY(inst->register_user, interface->sip_register_user);
2245 SCPY(inst->register_host, interface->sip_register_host);
2247 SCPY(inst->auth_user, interface->sip_auth_user);
2248 SCPY(inst->auth_password, interface->sip_auth_password);
2249 inst->options_timeout = interface->sip_options_timer;
2251 inst->rtp_port_from = interface->rtp_port_from;
2252 inst->rtp_port_to = interface->rtp_port_to;
2253 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2254 inst->rtp_port_from = RTP_PORT_BASE;
2255 inst->rtp_port_to = RTP_PORT_MAX;
2257 inst->next_rtp_port = inst->rtp_port_from;
2260 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2261 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2262 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2263 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2264 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2265 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2267 /* init root object */
2268 inst->root = su_root_create(inst);
2270 PERROR("Failed to create SIP root\n");
2271 sip_exit_inst(interface);
2275 SPRINT(local, "sip:%s",inst->local_peer);
2276 if (!strchr(inst->local_peer, ':'))
2277 SCAT(local, ":5060");
2278 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2280 PERROR("Failed to create SIP stack object\n");
2281 sip_exit_inst(interface);
2284 nua_set_params(inst->nua,
2285 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2286 NUTAG_APPL_METHOD("INVITE"),
2287 NUTAG_APPL_METHOD("ACK"),
2288 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2289 NUTAG_APPL_METHOD("CANCEL"),
2290 NUTAG_APPL_METHOD("OPTIONS"),
2291 NUTAG_APPL_METHOD("NOTIFY"),
2292 NUTAG_APPL_METHOD("INFO"),
2294 #ifdef NUTAG_AUTO100
2298 NUTAG_AUTOANSWER(0),
2301 if (interface->sip_stun_server[0]) {
2303 inst->stun_root = su_root_create(inst);
2304 if (!inst->stun_root) {
2305 PERROR("Failed to create STUN root\n");
2306 sip_exit_inst(interface);
2311 SCPY(inst->stun_server, interface->sip_stun_server);
2312 inst->stun_interval = interface->sip_stun_interval;
2313 inst->stun_handle = stun_handle_init(inst->root,
2314 STUNTAG_SERVER(inst->stun_server),
2316 if (!inst->stun_handle) {
2317 PERROR("Failed to create STUN handle\n");
2318 sip_exit_inst(interface);
2321 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2322 if (inst->stun_socket < 0) {
2323 PERROR("Failed to create STUN socket\n");
2324 sip_exit_inst(interface);
2327 inst->stun_state = STUN_STATE_UNRESOLVED;
2330 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2332 any_sip_interface = 1;
2337 void sip_exit_inst(struct interface *interface)
2339 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2343 del_timer(&inst->stun_retry_timer);
2344 del_timer(&inst->register_retry_timer);
2345 del_timer(&inst->register_option_timer);
2346 if (inst->stun_socket)
2347 su_close(inst->stun_socket);
2348 if (inst->stun_handle)
2349 stun_handle_destroy(inst->stun_handle);
2350 if (inst->register_handle)
2351 nua_handle_destroy(inst->register_handle);
2353 su_root_destroy(inst->root);
2355 nua_destroy(inst->nua);
2356 FREE(inst, sizeof(*inst));
2357 interface->sip_inst = NULL;
2359 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2361 /* check if there is any other SIP interface left */
2362 interface = interface_first;
2364 if (interface->sip_inst)
2366 interface = interface->next;
2369 any_sip_interface = 0;
2372 extern su_log_t su_log_default[];
2373 extern su_log_t nua_log[];
2374 //extern su_log_t soa_log[];
2380 /* init SOFIA lib */
2382 su_home_init(sip_home);
2384 if (options.deb & DEBUG_SIP) {
2385 su_log_set_level(su_log_default, 9);
2386 su_log_set_level(nua_log, 9);
2387 //su_log_set_level(soa_log, 9);
2390 for (i = 0; i < 256; i++)
2391 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);
2393 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2400 su_home_deinit(sip_home);
2403 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2406 static void sip_handle_stun(struct sip_inst *inst)
2410 switch (inst->stun_state) {
2411 case STUN_STATE_UNRESOLVED:
2412 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2413 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2414 STUNTAG_SOCKET(inst->stun_socket),
2415 STUNTAG_REGISTER_EVENTS(1),
2418 PERROR("Failed to call stun_bind()\n");
2419 inst->stun_state = STUN_STATE_FAILED;
2422 inst->stun_state = STUN_STATE_RESOLVING;
2423 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2424 add_trace("server", "addr", "%s", inst->stun_server);
2430 static void sip_handle_register(struct sip_inst *inst)
2432 char from[128] = "";
2434 char contact[128] = "";
2436 switch (inst->register_state) {
2437 case REGISTER_STATE_UNREGISTERED:
2438 /* wait for resoved stun */
2439 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2442 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2443 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2444 if (!inst->register_handle) {
2445 PERROR("Failed to create handle\n");
2446 inst->register_state = REGISTER_STATE_FAILED;
2449 /* apply handle to trace */
2450 sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2451 add_trace("handle", "new", "0x%x", inst->register_handle);
2454 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2455 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2456 if (inst->stun_handle && inst->stun_ipaddr[0])
2457 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->stun_ipaddr);
2459 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2460 add_trace("from", "uri", "%s", from);
2461 add_trace("to", "uri", "%s", to);
2464 nua_register(inst->register_handle,
2465 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2466 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2467 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2470 inst->register_state = REGISTER_STATE_REGISTERING;
2477 void sip_handle(void)
2479 struct interface *interface = interface_first;
2480 struct sip_inst *inst;
2483 if (interface->sip_inst) {
2484 inst = (struct sip_inst *) interface->sip_inst;
2485 su_root_step(inst->root, 0);
2486 sip_handle_stun(inst);
2487 sip_handle_register(inst);
2489 interface = interface->next;
2493 /* deletes when back in event loop */
2494 static int delete_event(struct lcr_work *work, void *instance, int index)
2496 class Psip *psip = (class Psip *)instance;
2505 * generate audio, if no data is received from bridge
2508 void Psip::set_tone(const char *dir, const char *tone)
2510 Port::set_tone(dir, tone);
2515 void Psip::update_load(void)
2517 /* don't trigger load event if event already active */
2518 if (p_s_load_timer.active)
2521 /* don't start timer if ... */
2522 if (!p_tone_name[0] && !p_dov_tx)
2525 p_s_next_tv_sec = 0;
2526 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2529 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2531 class Psip *psip = (class Psip *)instance;
2533 /* stop timer if ... */
2534 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2542 #define SEND_SIP_LEN 160
2544 void Psip::load_tx(void)
2547 struct timeval current_time;
2548 int tosend = SEND_SIP_LEN, i;
2549 unsigned char buf[SEND_SIP_LEN], *p = buf;
2552 gettimeofday(¤t_time, NULL);
2553 if (!p_s_next_tv_sec) {
2554 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2555 p_s_next_tv_sec = current_time.tv_sec;
2556 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2557 if (p_s_next_tv_usec >= 1000000) {
2558 p_s_next_tv_usec -= 1000000;
2561 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
2563 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2564 + (current_time.tv_usec - p_s_next_tv_usec);
2565 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2566 /* if clock drifts too much, set next timeout event to current timer + 160 */
2568 p_s_next_tv_sec = current_time.tv_sec;
2569 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2570 if (p_s_next_tv_usec >= 1000000) {
2571 p_s_next_tv_usec -= 1000000;
2575 /* if diff is positive, it took too long, so next timeout will be earlier */
2576 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2577 if (p_s_next_tv_usec >= 1000000) {
2578 p_s_next_tv_usec -= 1000000;
2582 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
2586 if (p_tone_name[0]) {
2587 tosend -= read_audio(p, tosend);
2590 tosend -= dov_tx(p, tosend);
2593 PERROR("buffer is not completely filled\n");
2598 for (i = 0; i < SEND_SIP_LEN; i++) {
2602 /* transmit data via rtp */
2603 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);