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];
57 char asserted_id[128];
59 char register_user[128];
60 char register_host[128];
61 nua_handle_t *register_handle;
62 struct lcr_timer register_retry_timer;
63 struct lcr_timer register_option_timer;
64 int register_interval;
67 char auth_password[128];
73 char stun_server[128];
74 stun_handle_t *stun_handle;
75 su_socket_t stun_socket;
76 struct lcr_timer stun_retry_timer;
79 unsigned short rtp_port_from;
80 unsigned short rtp_port_to;
81 unsigned short next_rtp_port;
85 static int delete_event(struct lcr_work *work, void *instance, int index);
86 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index);
87 static int load_timer(struct lcr_timer *timer, void *instance, int index);
92 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
95 if (interface->rtp_bridge)
97 p_s_sip_inst = interface->sip_inst;
98 memset(&p_s_delete, 0, sizeof(p_s_delete));
99 add_work(&p_s_delete, delete_event, this, 0);
102 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
103 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
104 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
105 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
106 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
107 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
108 p_s_rtp_ip_local = 0;
109 p_s_rtp_ip_remote = 0;
110 p_s_rtp_port_local = 0;
111 p_s_rtp_port_remote = 0;
116 p_s_rtp_tx_action = 0;
117 p_s_rtp_is_connected = 0;
119 /* create option timer */
120 memset(&p_s_invite_option_timer, 0, sizeof(p_s_invite_option_timer));
121 add_timer(&p_s_invite_option_timer, invite_option_timer, this, 0);
122 p_s_invite_direction = 0;
125 memset(&p_s_load_timer, 0, sizeof(p_s_load_timer));
126 add_timer(&p_s_load_timer, load_timer, this, 0);
129 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
131 FATAL("No SIP instance for interface\n");
140 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
142 del_timer(&p_s_invite_option_timer);
143 del_timer(&p_s_load_timer);
144 del_work(&p_s_delete);
149 static const char *media_type2name(uint8_t media_type) {
150 switch (media_type) {
151 case MEDIA_TYPE_ULAW:
153 case MEDIA_TYPE_ALAW:
157 case MEDIA_TYPE_GSM_HR:
159 case MEDIA_TYPE_GSM_EFR:
168 static void sip_trace_header(class Psip *sip, const char *interface_name, const char *message, int direction)
170 struct interface *interface = NULL;
173 interface = getinterfacebyname(interface_name);
175 /* init trace with given values */
178 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
179 sip?sip->p_dialinginfo.id:NULL,
190 /* according to RFC 3550 */
192 #if __BYTE_ORDER == __LITTLE_ENDIAN
193 uint8_t csrc_count:4,
197 uint8_t payload_type:7,
199 #elif __BYTE_ORDER == __BIG_ENDIAN
210 } __attribute__((packed));
215 } __attribute__((packed));
217 #define RTP_VERSION 2
219 #define PAYLOAD_TYPE_ULAW 0
220 #define PAYLOAD_TYPE_ALAW 8
221 #define PAYLOAD_TYPE_GSM 3
223 /* decode an rtp frame */
224 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
226 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
227 struct rtp_x_hdr *rtpxh;
231 unsigned char *from, *to;
235 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
238 if (rtph->version != RTP_VERSION) {
239 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
242 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
243 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
244 if (payload_len < 0) {
245 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
246 "csrc count = %d)\n", len, rtph->csrc_count);
249 if (rtph->extension) {
250 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
251 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
252 "extension header\n");
255 rtpxh = (struct rtp_x_hdr *)payload;
256 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
258 payload_len -= x_len;
259 if (payload_len < 0) {
260 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
261 "extension header exceeds frame length\n");
266 if (payload_len < 0) {
267 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
271 payload_len -= payload[payload_len - 1];
272 if (payload_len < 0) {
273 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
274 "greater than payload\n");
279 switch (rtph->payload_type) {
281 we only support alaw and ulaw!
282 case RTP_PT_GSM_FULL:
283 if (payload_len != 33) {
284 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
285 "payload length != 33 (len = %d)\n",
291 if (payload_len != 31) {
292 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
293 "payload length != 31 (len = %d)\n",
298 case RTP_PT_GSM_HALF:
299 if (payload_len != 14) {
300 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
301 "payload length != 14 (len = %d)\n",
307 case PAYLOAD_TYPE_ALAW:
308 if (options.law != 'a') {
309 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
313 case PAYLOAD_TYPE_ULAW:
314 if (options.law == 'a') {
315 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
320 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
321 "type %d\n", rtph->payload_type);
325 if (payload_len <= 0) {
326 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
332 psip->record(payload, payload_len, 0); // from down
334 psip->tap(payload, payload_len, 0); // from down
339 if (psip->p_echotest) {
340 /* echo rtp data we just received */
341 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
345 *to++ = flip[*from++];
347 psip->dov_rx(payload, payload_len);
348 psip->bridge_tx(payload, payload_len);
353 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
355 class Psip *psip = (class Psip *) instance;
357 unsigned char buffer[256];
360 if ((what & LCR_FD_READ)) {
361 len = read(fd->fd, &buffer, sizeof(buffer));
363 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
364 // psip->rtp_close();
365 // psip->rtp_shutdown();
368 if (psip->p_s_rtp_is_connected)
369 rc = rtp_decode(psip, buffer, len);
375 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
377 // class Psip *psip = (class Psip *) instance;
379 unsigned char buffer[256];
381 if ((what & LCR_FD_READ)) {
382 len = read(fd->fd, &buffer, sizeof(buffer));
384 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
385 // psip->rtp_close();
386 // psip->rtp_shutdown();
389 PDEBUG(DEBUG_SIP, "rtcp!\n");
395 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
398 socklen_t alen = sizeof(*sin_local);
400 sin_local->sin_family = AF_INET;
401 sin_local->sin_addr.s_addr = htonl(ip);
402 sin_local->sin_port = htons(port);
404 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
408 /* retrieve the address we actually bound to, in case we
409 * passed INADDR_ANY as IP address */
410 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
413 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
416 socklen_t alen = sizeof(*sin_local);
418 sin_remote->sin_family = AF_INET;
419 sin_remote->sin_addr.s_addr = htonl(ip);
420 sin_remote->sin_port = htons(port);
422 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
424 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
428 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
431 int Psip::rtp_open(void)
433 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
435 // struct in_addr ia;
437 unsigned short start_port;
440 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
446 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
448 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
454 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
457 ip = htonl(INADDR_ANY);
459 start_port = inst->next_rtp_port;
461 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, inst->next_rtp_port);
465 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, inst->next_rtp_port + 1);
467 p_s_rtp_port_local = inst->next_rtp_port;
468 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
471 /* reopen rtp socket and try again with next udp port */
472 unregister_fd(&p_s_rtp_fd);
473 close(p_s_rtp_fd.fd);
475 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
481 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
484 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
485 if (inst->next_rtp_port == start_port)
487 /* we must use rc2, in order to preserve rc */
490 PDEBUG(DEBUG_SIP, "failed to find port\n");
494 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
495 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
496 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
498 return p_s_rtp_port_local;
501 int Psip::rtp_connect(void)
506 ia.s_addr = htonl(p_s_rtp_ip_remote);
507 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
509 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);
513 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);
517 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
518 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
519 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
520 p_s_rtp_is_connected = 1;
524 void Psip::rtp_close(void)
526 if (p_s_rtp_fd.fd > 0) {
527 unregister_fd(&p_s_rtp_fd);
528 close(p_s_rtp_fd.fd);
531 if (p_s_rtcp_fd.fd > 0) {
532 unregister_fd(&p_s_rtcp_fd);
533 close(p_s_rtcp_fd.fd);
536 if (p_s_rtp_is_connected) {
537 PDEBUG(DEBUG_SIP, "rtp closed\n");
538 p_s_rtp_is_connected = 0;
543 void tv_difference(struct timeval *diff, const struct timeval *from,
544 const struct timeval *__to)
546 struct timeval _to = *__to, *to = &_to;
548 if (to->tv_usec < from->tv_usec) {
550 to->tv_usec += 1000000;
553 diff->tv_usec = to->tv_usec - from->tv_usec;
554 diff->tv_sec = to->tv_sec - from->tv_sec;
557 /* encode and send a rtp frame */
558 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
560 struct rtp_hdr *rtph;
562 int duration; /* in samples */
563 unsigned char buffer[256];
567 record(data, len, 1); // from up
569 tap(data, len, 1); // from up
571 if (!p_s_rtp_is_connected) {
576 if (!p_s_rtp_tx_action) {
577 /* initialize sequences */
578 p_s_rtp_tx_action = 1;
579 p_s_rtp_tx_ssrc = rand();
580 p_s_rtp_tx_sequence = random();
581 p_s_rtp_tx_timestamp = random();
582 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
585 switch (payload_type) {
587 we only support alaw and ulaw!
588 case RTP_PT_GSM_FULL:
596 case RTP_PT_GSM_HALF:
601 case PAYLOAD_TYPE_ALAW:
602 case PAYLOAD_TYPE_ULAW:
607 PERROR("unsupported message type %d\n", payload_type);
613 struct timeval tv, tv_diff;
614 long int usec_diff, frame_diff;
616 gettimeofday(&tv, NULL);
617 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
618 p_s_rtp_tx_last_tv = tv;
620 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
621 frame_diff = (usec_diff / 20000);
623 if (abs(frame_diff) > 1) {
624 long int frame_diff_excess = frame_diff - 1;
626 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
627 p_s_rtp_tx_sequence += frame_diff_excess;
628 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
633 rtph = (struct rtp_hdr *) buffer;
634 rtph->version = RTP_VERSION;
637 rtph->csrc_count = 0;
639 rtph->payload_type = payload_type;
640 rtph->sequence = htons(p_s_rtp_tx_sequence++);
641 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
642 p_s_rtp_tx_timestamp += duration;
643 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
644 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
646 if (p_s_rtp_fd.fd > 0) {
647 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
648 if (len != sizeof(struct rtp_hdr) + payload_len) {
649 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
659 /* receive from remote */
660 int Psip::bridge_rx(unsigned char *data, int len)
664 /* don't bridge, if tones are provided */
665 if (p_tone_name[0] || p_dov_tx)
671 if ((ret = Port::bridge_rx(data, len)))
674 /* write to rx buffer */
676 p_s_rxdata[p_s_rxpos++] = flip[*data++];
677 if (p_s_rxpos == 160) {
680 /* transmit data via rtp */
681 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
688 /* taken from freeswitch */
689 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
690 static int status2cause(int status)
694 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
700 return 21; //SWITCH_CAUSE_CALL_REJECTED;
702 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
705 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
708 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
710 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
719 return 127; //SWITCH_CAUSE_INTERWORKING;
721 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
726 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
729 return 17; //SWITCH_CAUSE_USER_BUSY;
731 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
734 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
736 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
738 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
742 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
745 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
747 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
749 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
753 static int cause2status(int cause, int location, const char **st)
759 s = 404; *st = sip_404_Not_found;
762 s = 404; *st = sip_404_Not_found;
765 s = 404; *st = sip_404_Not_found;
768 s = 486; *st = sip_486_Busy_here;
771 s = 408; *st = sip_408_Request_timeout;
774 s = 480; *st = sip_480_Temporarily_unavailable;
777 s = 480; *st = sip_480_Temporarily_unavailable;
780 if (location == LOCATION_USER) {
781 s = 603; *st = sip_603_Decline;
783 s = 403; *st = sip_403_Forbidden;
787 //s = 301; *st = sip_301_Moved_permanently;
788 s = 410; *st = sip_410_Gone;
791 s = 410; *st = sip_410_Gone;
794 s = 404; *st = sip_404_Not_found;
797 s = 502; *st = sip_502_Bad_gateway;
800 s = 484; *st = sip_484_Address_incomplete;
803 s = 501; *st = sip_501_Not_implemented;
806 s = 480; *st = sip_480_Temporarily_unavailable;
809 s = 503; *st = sip_503_Service_unavailable;
812 s = 503; *st = sip_503_Service_unavailable;
815 s = 503; *st = sip_503_Service_unavailable;
818 s = 503; *st = sip_503_Service_unavailable;
821 s = 503; *st = sip_503_Service_unavailable;
824 s = 403; *st = sip_403_Forbidden;
827 s = 403; *st = sip_403_Forbidden;
830 s = 503; *st = sip_503_Service_unavailable;
833 s = 488; *st = sip_488_Not_acceptable;
836 s = 501; *st = sip_501_Not_implemented;
839 s = 488; *st = sip_488_Not_acceptable;
842 s = 501; *st = sip_501_Not_implemented;
845 s = 403; *st = sip_403_Forbidden;
848 s = 503; *st = sip_503_Service_unavailable;
851 s = 504; *st = sip_504_Gateway_time_out;
854 s = 500; *st = sip_500_Internal_server_error;
857 s = 500; *st = sip_500_Internal_server_error;
860 s = 468; *st = sip_486_Busy_here;
866 /* use STUN ip, or return the ip without change */
867 unsigned int Psip::get_local_ip(unsigned int ip)
869 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
871 if (inst->public_ip[0]) {
872 PDEBUG(DEBUG_SIP, "RTP local IP is replaced by STUN ip %s\n", inst->public_ip);
873 inet_pton(AF_INET, inst->public_ip, &ip);
880 * endpoint sends messages to the SIP port
883 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
885 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
886 char sdp_str[256] = "";
888 struct lcr_msg *message;
889 struct interface *interface;
891 unsigned char payload_type;
893 interface = getinterfacebyname(inst->interface_name);
895 PERROR("Cannot find interface %s.\n", inst->interface_name);
899 if (param->connectinfo.rtpinfo.port) {
900 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
902 media_type = param->connectinfo.rtpinfo.media_types[0];
903 payload_type = param->connectinfo.rtpinfo.payload_types[0];
904 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
905 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
906 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
907 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
908 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
910 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
911 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
912 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
913 /* open local RTP peer (if not bridging) */
914 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
915 nua_cancel(p_s_handle, TAG_END());
916 nua_handle_destroy(p_s_handle);
918 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
919 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
921 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
922 message->param.disconnectinfo.cause = 41;
923 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
924 message_put(message);
925 new_state(PORT_STATE_RELEASE);
926 trigger_work(&p_s_delete);
931 memset(&ia, 0, sizeof(ia));
932 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
933 if (p_s_rtp_bridge || interface->is_tones != IS_YES) {
936 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
940 "m=audio %d RTP/AVP %d\r\n"
941 "a=rtpmap:%d %s/8000\r\n"
942 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
943 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
947 * If this response causes corrupt messages, like SDP body inside or
948 * before header, check if the sofia-sip-gcc-4.8.patch was applied.
949 * If it is still corrupted, try to disable optimization when compiling
952 nua_respond(p_s_handle, SIP_200_OK,
953 NUTAG_MEDIA_ENABLE(0),
954 TAG_IF(sdp_str[0], SIPTAG_CONTENT_TYPE_STR("application/sdp")),
955 TAG_IF(sdp_str[0], SIPTAG_PAYLOAD_STR(sdp_str)),
958 new_state(PORT_STATE_CONNECT);
959 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
960 add_trace("respond", "value", "200 OK");
961 add_trace("reason", NULL, "call connected");
963 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
964 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
965 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
972 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
974 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
975 struct lcr_msg *message;
976 char cause_str[128] = "";
977 int cause = param->disconnectinfo.cause;
978 int location = param->disconnectinfo.cause;
980 const char *status_text;
982 if (cause > 0 && cause <= 127) {
983 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
987 case PORT_STATE_OUT_SETUP:
988 case PORT_STATE_OUT_PROCEEDING:
989 case PORT_STATE_OUT_ALERTING:
990 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
991 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
993 add_trace("cause", "value", "%d", cause);
995 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
997 case PORT_STATE_IN_SETUP:
998 case PORT_STATE_IN_PROCEEDING:
999 case PORT_STATE_IN_ALERTING:
1000 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
1001 status = cause2status(cause, location, &status_text);
1002 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1004 add_trace("cause", "value", "%d", cause);
1005 add_trace("respond", "value", "%d %s", status, status_text);
1007 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1008 nua_handle_destroy(p_s_handle);
1010 trigger_work(&p_s_delete);
1013 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1014 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1016 add_trace("cause", "value", "%d", cause);
1018 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1021 if (message_id == MESSAGE_DISCONNECT) {
1022 while(p_epointlist) {
1023 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1024 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1025 message->param.disconnectinfo.location = LOCATION_BEYOND;
1026 message_put(message);
1028 free_epointlist(p_epointlist);
1032 new_state(PORT_STATE_RELEASE);
1037 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1039 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1040 char from[128] = "";
1041 char asserted_id[128] = "", asserted_msg[256] = "";
1043 char contact[128] = "";
1044 const char *local = inst->local_peer;
1046 const char *remote = inst->remote_peer;
1047 char sdp_str[512], pt_str[32];
1049 struct epoint_list *epointlist;
1050 sip_cseq_t *cseq = NULL;
1051 struct lcr_msg *message;
1052 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1053 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1055 unsigned char *payload_types;
1060 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1061 add_trace("failed", "reason", "No remote peer set or no peer has registered to us.");
1063 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1064 message->param.disconnectinfo.cause = 27;
1065 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1066 message_put(message);
1067 new_state(PORT_STATE_RELEASE);
1068 trigger_work(&p_s_delete);
1072 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1074 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1075 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1076 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1077 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1078 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1080 if (param->setup.rtpinfo.port) {
1081 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1083 media_types = param->setup.rtpinfo.media_types;
1084 payload_types = param->setup.rtpinfo.payload_types;
1085 payloads = param->setup.rtpinfo.payloads;
1086 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1087 p_s_rtp_port_local = param->setup.rtpinfo.port;
1088 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1089 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1091 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1093 media_types = &lcr_media;
1094 payload_types = &lcr_payload;
1097 /* open local RTP peer (if not bridging) */
1098 if (rtp_open() < 0) {
1099 PERROR("Failed to open RTP sockets\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 if (!p_s_rtp_ip_local) {
1112 /* extract IP from local peer */
1113 SCPY(local_ip, local);
1114 p = strchr(local_ip, ':');
1117 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1118 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1119 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1123 memset(&ia, 0, sizeof(ia));
1124 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1125 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1127 PERROR("Failed to create handle\n");
1128 /* send release message to endpoit */
1129 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1130 message->param.disconnectinfo.cause = 41;
1131 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1132 message_put(message);
1133 new_state(PORT_STATE_RELEASE);
1134 trigger_work(&p_s_delete);
1137 /* apply handle to trace */
1138 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1139 // add_trace("handle", "new", "0x%x", p_s_handle);
1144 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1148 "m=audio %d RTP/AVP"
1149 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1150 for (i = 0; i < payloads; i++) {
1151 SPRINT(pt_str, " %d", payload_types[i]);
1152 SCAT(sdp_str, pt_str);
1154 SCAT(sdp_str, "\r\n");
1155 for (i = 0; i < payloads; i++) {
1156 SPRINT(pt_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1157 SCAT(sdp_str, pt_str);
1159 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1161 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1162 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1163 if (inst->asserted_id[0]) {
1164 SPRINT(asserted_id, "sip:%s@%s", inst->asserted_id, remote);
1165 SPRINT(asserted_msg, "P-Asserted-Identity: <%s>", asserted_id);
1167 if (inst->public_ip[0])
1168 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1170 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1171 add_trace("from", "uri", "%s", from);
1172 add_trace("to", "uri", "%s", to);
1174 add_trace("assert-id", "uri", "%s", asserted_id);
1175 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1176 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1177 for (i = 0; i < payloads; i++)
1178 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1181 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1183 nua_invite(p_s_handle,
1184 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1185 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1186 TAG_IF(asserted_msg[0], SIPTAG_HEADER_STR(asserted_msg)),
1187 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1188 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1189 NUTAG_MEDIA_ENABLE(0),
1190 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1191 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1192 new_state(PORT_STATE_OUT_SETUP);
1194 p_s_invite_direction = DIRECTION_OUT;
1197 PDEBUG(DEBUG_SIP, "do overlap\n");
1198 new_state(PORT_STATE_OUT_OVERLAP);
1199 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1200 message_put(message);
1202 PDEBUG(DEBUG_SIP, "do proceeding\n");
1203 new_state(PORT_STATE_OUT_PROCEEDING);
1204 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1205 message_put(message);
1208 /* attach only if not already */
1209 epointlist = p_epointlist;
1211 if (epointlist->epoint_id == epoint_id)
1213 epointlist = epointlist->next;
1216 epointlist_new(epoint_id);
1221 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1224 // struct in_addr ia;
1226 switch (param->notifyinfo.notify) {
1227 case INFO_NOTIFY_REMOTE_HOLD:
1231 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1233 "c=IN IP4 0.0.0.0\r\n"
1236 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1237 nua_info(p_s_handle,
1238 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1239 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1240 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1241 NUTAG_MEDIA_ENABLE(0),
1242 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1243 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1246 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1248 memset(&ia, 0, sizeof(ia));
1249 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1252 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1256 "m=audio %d RTP/AVP %d\r\n"
1257 "a=rtpmap:%d %s/8000\r\n"
1258 , 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));
1259 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1260 nua_info(p_s_handle,
1261 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1262 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1263 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1264 NUTAG_MEDIA_ENABLE(0),
1265 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1266 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1274 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1278 /* prepare DTMF info payload */
1284 /* start invite to handle DTMF */
1285 nua_info(p_s_handle,
1286 NUTAG_MEDIA_ENABLE(0),
1287 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1288 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1293 /* NOTE: incomplete and not working */
1294 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1298 /* prepare DTMF info payload */
1302 , param->information.id);
1304 /* start invite to handle DTMF */
1305 nua_info(p_s_handle,
1306 NUTAG_MEDIA_ENABLE(0),
1307 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1308 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1313 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1315 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1317 if (Port::message_epoint(epoint_id, message_id, param))
1320 switch(message_id) {
1321 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1322 if (p_state != PORT_STATE_IN_SETUP
1323 && p_state != PORT_STATE_IN_PROCEEDING)
1325 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1326 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1327 add_trace("respond", "value", "180 Ringing");
1329 new_state(PORT_STATE_IN_ALERTING);
1332 case MESSAGE_CONNECT: /* call is connected on LCR side */
1333 if (p_state != PORT_STATE_IN_SETUP
1334 && p_state != PORT_STATE_IN_PROCEEDING
1335 && p_state != PORT_STATE_IN_ALERTING)
1337 message_connect(epoint_id, message_id, param);
1340 case MESSAGE_DISCONNECT: /* call has been disconnected */
1341 case MESSAGE_RELEASE: /* call has been released */
1342 message_release(epoint_id, message_id, param);
1345 case MESSAGE_SETUP: /* dial-out command received from epoint */
1346 message_setup(epoint_id, message_id, param);
1349 case MESSAGE_INFORMATION: /* overlap dialing */
1350 if (p_state != PORT_STATE_OUT_OVERLAP)
1352 message_information(epoint_id, message_id, param);
1355 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1356 if (p_state == PORT_STATE_CONNECT)
1357 message_dtmf(epoint_id, message_id, param);
1358 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1359 if (p_state == PORT_STATE_CONNECT)
1360 message_notify(epoint_id, message_id, param);
1364 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1370 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)
1374 if (!sip->sip_payload) {
1375 PDEBUG(DEBUG_SIP, "no payload given\n");
1379 sdp_parser_t *parser;
1382 sdp_attribute_t *attr;
1384 sdp_connection_t *conn;
1386 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1388 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1392 if (!(sdp = sdp_session(parser))) {
1393 sdp_parser_free(parser);
1396 for (m = sdp->sdp_media; m; m = m->m_next) {
1397 if (m->m_proto != sdp_proto_rtp)
1399 if (m->m_type != sdp_media_audio)
1401 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1403 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1404 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1406 if (m->m_connections) {
1407 conn = m->m_connections;
1408 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1409 inet_pton(AF_INET, conn->c_address, ip);
1410 *ip = ntohl(p_s_rtp_ip_remote);
1412 char *p = sip->sip_payload->pl_data;
1415 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1416 p = strstr(p, "c=IN IP4 ");
1418 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1419 sdp_parser_free(parser);
1423 if ((p = strchr(addr, '\n'))) *p = '\0';
1424 if ((p = strchr(addr, '\r'))) *p = '\0';
1425 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1426 inet_pton(AF_INET, addr, ip);
1427 *ip = ntohl(p_s_rtp_ip_remote);
1429 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1432 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1433 /* append to payload list, if there is space */
1434 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1435 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1436 media_type = MEDIA_TYPE_ALAW;
1437 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1438 media_type = MEDIA_TYPE_ULAW;
1439 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1440 media_type = MEDIA_TYPE_GSM;
1441 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1442 media_type = MEDIA_TYPE_GSM_EFR;
1443 else if (!strcmp(map->rm_encoding, "AMR"))
1444 media_type = MEDIA_TYPE_AMR;
1445 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1446 media_type = MEDIA_TYPE_GSM_HR;
1447 if (media_type && *payloads <= max_payloads) {
1448 *payload_types++ = map->rm_pt;
1449 *media_types++ = media_type;
1455 sdp_parser_free(parser);
1460 static int challenge(struct sip_inst *inst, class Psip *psip, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1462 sip_www_authenticate_t const *authenticate = NULL;
1463 char const *realm = NULL;
1464 char const *scheme = NULL;
1467 char authentication[256] = "";
1468 PDEBUG(DEBUG_SIP, "challenge order received\n");
1470 if (!inst->auth_user[0]) {
1471 PDEBUG(DEBUG_SIP, "No credentials available\n");
1472 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1473 add_trace("error", NULL, "There are no credentials given for interface");
1478 if (sip->sip_www_authenticate) {
1479 authenticate = sip->sip_www_authenticate;
1480 } else if (sip->sip_proxy_authenticate) {
1481 authenticate = sip->sip_proxy_authenticate;
1483 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1484 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1485 add_trace("error", NULL, "Authentication method unknwon");
1490 scheme = (char const *) authenticate->au_scheme;
1491 if (authenticate->au_params) {
1492 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1493 if ((realm = strstr(cur, "realm="))) {
1500 if (!scheme || !realm) {
1501 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1502 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1503 add_trace("error", NULL, "Authentication header has no realm or scheme");
1508 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1509 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1511 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1512 add_trace("scheme", NULL, "%s", scheme);
1513 add_trace("realm", NULL, "%s", realm);
1514 add_trace("user", NULL, "%s", inst->auth_user);
1515 add_trace("pass", NULL, "%s", inst->auth_password);
1518 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1523 static void i_register(struct sip_inst *inst, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1525 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1526 nua_saved_event_t saved[1];
1527 sip_contact_t const *contact = NULL;
1528 contact = sip->sip_contact;
1529 nua_save_event(nua, saved);
1530 nua_event_data_t const *data = nua_event_data(saved);
1532 if (contact->m_url->url_host)
1533 SCPY(inst->remote_peer, contact->m_url->url_host);
1534 if (contact->m_url->url_port && contact->m_url->url_port[0]) {
1535 SCAT(inst->remote_peer, ":");
1536 SCAT(inst->remote_peer, contact->m_url->url_port);
1539 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1540 add_trace("contact", "uri", "%s", inst->remote_peer);
1543 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1544 add_trace("respond", "value", "200 OK");
1545 add_trace("reason", NULL, "peer registered");
1548 nua_respond(nh, SIP_200_OK, SIPTAG_CONTACT(sip->sip_contact), NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1549 nua_handle_destroy(nh);
1550 inst->register_handle = NULL;
1553 static void r_register(struct sip_inst *inst, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1557 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_IN);
1558 add_trace("value", NULL, "%d", status);
1559 add_trace("phrase", NULL, "%s", phrase);
1565 /* if not registered, become registered and start register interval timer */
1566 if (inst->register_state != REGISTER_STATE_REGISTERED) {
1567 if (inst->register_interval)
1568 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
1569 inst->register_state = REGISTER_STATE_REGISTERED;
1571 /* start option timer */
1572 if (inst->options_interval)
1573 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1577 PDEBUG(DEBUG_SIP, "Register challenge received\n");
1578 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
1583 if (status >= 200 && status <= 299)
1588 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
1589 inst->register_state = REGISTER_STATE_FAILED;
1590 nua_handle_destroy(nh);
1591 inst->register_handle = NULL;
1592 /* stop option timer */
1593 unsched_timer(&inst->register_option_timer);
1594 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1595 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1599 void Psip::i_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1601 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1602 const char *from = "", *to = "", *name = "";
1605 class Endpoint *epoint;
1606 struct lcr_msg *message;
1607 struct interface *interface;
1608 int media_types[32];
1609 uint8_t payload_types[32];
1613 interface = getinterfacebyname(inst->interface_name);
1615 PERROR("Cannot find interface %s.\n", inst->interface_name);
1619 if (p_state != PORT_STATE_IDLE) {
1620 sip_trace_header(this, inst->interface_name, "RE-INVITE", DIRECTION_IN);
1622 nua_respond(p_s_handle, SIP_200_OK,
1623 NUTAG_MEDIA_ENABLE(0),
1628 if (sip->sip_from) {
1629 if (sip->sip_from->a_url)
1630 from = sip->sip_from->a_url->url_user;
1631 if (sip->sip_from->a_display) {
1632 name = sip->sip_from->a_display;
1633 if (!strncmp(name, "\"IMSI", 5)) {
1634 strncpy(imsi, name + 5, 15);
1641 if (sip->sip_to->a_url)
1642 to = sip->sip_to->a_url->url_user;
1644 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1646 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1647 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1649 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1650 if (!p_s_rtp_bridge) {
1653 /* check if supported payload type exists */
1654 for (i = 0; i < payloads; i++) {
1655 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1658 if (i == payloads) {
1659 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1667 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1669 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1670 nua_handle_destroy(nh);
1672 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1674 add_trace("respond", "value", "415 Unsupported Media");
1676 add_trace("respond", "value", "400 Bad Request");
1677 add_trace("reason", NULL, "offered codec does not match");
1679 new_state(PORT_STATE_RELEASE);
1680 trigger_work(&p_s_delete);
1684 /* open local RTP peer (if not bridging) */
1685 if (!p_s_rtp_bridge && rtp_open() < 0) {
1686 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1687 nua_handle_destroy(nh);
1689 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1690 add_trace("respond", "value", "500 Internal Server Error");
1691 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1693 new_state(PORT_STATE_RELEASE);
1694 trigger_work(&p_s_delete);
1699 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1700 // add_trace("handle", "new", "0x%x", nh);
1705 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1706 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1707 /* caller information */
1709 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1710 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1711 add_trace("calling", "present", "unavailable");
1713 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1714 add_trace("calling", "present", "allowed");
1715 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1716 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1717 SCPY(p_callerinfo.id, from);
1718 add_trace("calling", "number", "%s", from);
1719 SCPY(p_callerinfo.name, name);
1721 add_trace("calling", "name", "%s", name);
1722 SCPY(p_callerinfo.imsi, imsi);
1724 add_trace("calling", "imsi", "%s", imsi);
1726 SCPY(p_callerinfo.interface, inst->interface_name);
1727 /* dialing information */
1729 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1730 SCAT(p_dialinginfo.id, to);
1731 add_trace("dialing", "number", "%s", to);
1734 /* bearer capability */
1735 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1736 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1737 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1738 add_trace("bearer", "capa", "speech");
1739 add_trace("bearer", "mode", "circuit");
1740 /* if packet mode works some day, see dss1.cpp for conditions */
1741 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1745 /* create endpoint */
1747 FATAL("Incoming call but already got an endpoint.\n");
1748 if (!(epoint = new Endpoint(p_serial, 0)))
1749 FATAL("No memory for Endpoint instance\n");
1750 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1751 epointlist_new(epoint->ep_serial);
1753 #ifdef NUTAG_AUTO100
1754 /* send trying (proceeding) */
1755 nua_respond(nh, SIP_100_TRYING, TAG_END());
1756 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1757 add_trace("respond", "value", "100 Trying");
1761 new_state(PORT_STATE_IN_PROCEEDING);
1763 /* send setup message to endpoit */
1764 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1765 message->param.setup.port_type = p_type;
1766 // message->param.setup.dtmf = 0;
1767 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1768 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1769 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1770 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1771 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1772 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1773 if (p_s_rtp_bridge) {
1776 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1777 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1778 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1779 /* add codecs to setup message */
1780 for (i = 0; i < payloads; i++) {
1781 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1782 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1783 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1786 message->param.setup.rtpinfo.payloads = i;
1788 message_put(message);
1790 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer\n");
1791 /* start option timer */
1792 if (inst->options_interval)
1793 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1794 p_s_invite_direction = DIRECTION_IN;
1796 /* send progress, if tones are available and if we don't bridge */
1797 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1800 unsigned char payload_type;
1802 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1803 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1804 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1805 /* open local RTP peer (if not bridging) */
1806 if (rtp_connect() < 0) {
1807 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1808 nua_handle_destroy(nh);
1810 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1811 add_trace("respond", "value", "500 Internal Server Error");
1812 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1814 new_state(PORT_STATE_RELEASE);
1815 trigger_work(&p_s_delete);
1816 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1817 message->param.disconnectinfo.cause = 41;
1818 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1819 message_put(message);
1820 new_state(PORT_STATE_RELEASE);
1821 trigger_work(&p_s_delete);
1825 memset(&ia, 0, sizeof(ia));
1826 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1829 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1833 "m=audio %d RTP/AVP %d\r\n"
1834 "a=rtpmap:%d %s/8000\r\n"
1835 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
1836 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1838 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1839 NUTAG_MEDIA_ENABLE(0),
1840 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1841 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1842 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1843 add_trace("respond", "value", "183 SESSION PROGRESS");
1844 add_trace("reason", NULL, "audio available");
1845 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1846 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1847 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1852 void Psip::i_bye(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1854 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1855 struct lcr_msg *message;
1858 PDEBUG(DEBUG_SIP, "bye received\n");
1860 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
1861 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1862 cause = atoi(sip->sip_reason->re_cause);
1863 add_trace("cause", "value", "%d", cause);
1867 // let stack do bye automaticall, since it will not accept our response for some reason
1868 // nua_respond(nh, SIP_200_OK, TAG_END());
1869 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1870 add_trace("respond", "value", "200 OK");
1872 // nua_handle_destroy(nh);
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 ? : 16;
1881 message->param.disconnectinfo.location = LOCATION_BEYOND;
1882 message_put(message);
1884 free_epointlist(p_epointlist);
1886 new_state(PORT_STATE_RELEASE);
1887 trigger_work(&p_s_delete);
1890 void Psip::i_cancel(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1892 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1893 struct lcr_msg *message;
1895 PDEBUG(DEBUG_SIP, "cancel received\n");
1897 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
1900 nua_handle_destroy(nh);
1905 while(p_epointlist) {
1906 /* send setup message to endpoit */
1907 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1908 message->param.disconnectinfo.cause = 16;
1909 message->param.disconnectinfo.location = LOCATION_BEYOND;
1910 message_put(message);
1912 free_epointlist(p_epointlist);
1914 new_state(PORT_STATE_RELEASE);
1915 trigger_work(&p_s_delete);
1918 void Psip::r_bye(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1920 PDEBUG(DEBUG_SIP, "bye response received\n");
1922 nua_handle_destroy(nh);
1927 trigger_work(&p_s_delete);
1930 void Psip::r_cancel(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1932 PDEBUG(DEBUG_SIP, "cancel response received\n");
1934 nua_handle_destroy(nh);
1939 trigger_work(&p_s_delete);
1942 void Psip::r_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1944 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1945 struct lcr_msg *message;
1946 int cause = 0, location = 0;
1947 int media_types[32];
1948 uint8_t payload_types[32];
1951 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1953 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1954 add_trace("respond", "value", "%d", status);
1958 if (status == 183 || (status >= 200 && status <= 299)) {
1961 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1962 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1966 else if (!p_s_rtp_bridge) {
1967 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1968 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1975 nua_cancel(nh, TAG_END());
1976 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1977 add_trace("reason", NULL, "accepted codec does not match");
1980 location = LOCATION_PRIVATE_LOCAL;
1981 goto release_with_cause;
1984 /* connect to remote RTP (if not bridging) */
1985 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1986 nua_cancel(nh, TAG_END());
1987 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1988 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1991 location = LOCATION_PRIVATE_LOCAL;
1992 goto release_with_cause;
1996 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer\n");
1997 /* start option timer */
1998 if (inst->options_interval)
1999 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2004 PDEBUG(DEBUG_SIP, "do proceeding\n");
2005 new_state(PORT_STATE_OUT_PROCEEDING);
2006 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
2007 message_put(message);
2011 PDEBUG(DEBUG_SIP, "do alerting\n");
2012 new_state(PORT_STATE_OUT_ALERTING);
2013 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
2014 message_put(message);
2017 PDEBUG(DEBUG_SIP, "do progress\n");
2018 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
2019 message->param.progressinfo.progress = 8;
2020 message->param.progressinfo.location = 10;
2021 if (p_s_rtp_bridge) {
2022 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2023 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
2024 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
2025 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
2026 message->param.progressinfo.rtpinfo.payloads = 1;
2028 message_put(message);
2032 PDEBUG(DEBUG_SIP, "do connect\n");
2033 nua_ack(nh, TAG_END());
2034 new_state(PORT_STATE_CONNECT);
2035 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
2036 if (p_s_rtp_bridge) {
2037 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2038 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
2039 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
2040 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
2041 message->param.connectinfo.rtpinfo.payloads = 1;
2043 message_put(message);
2046 if (status >= 200 && status <= 299)
2048 if (status < 100 || status > 199)
2050 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
2055 cause = status2cause(status);
2056 location = LOCATION_BEYOND;
2059 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
2061 while(p_epointlist) {
2062 /* send setup message to endpoit */
2063 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2064 message->param.disconnectinfo.cause = cause;
2065 message->param.disconnectinfo.location = location;
2066 message_put(message);
2068 free_epointlist(p_epointlist);
2071 new_state(PORT_STATE_RELEASE);
2075 trigger_work(&p_s_delete);
2078 void Psip::r_options(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2080 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2081 int cause = 0, location = 0;
2082 struct lcr_msg *message;
2084 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
2086 if (status >= 200 && status <= 299) {
2087 PDEBUG(DEBUG_SIP, "options ok, scheduling timer\n");
2088 /* restart option timer */
2089 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2093 nua_handle_destroy(nh);
2098 cause = status2cause(status);
2099 location = LOCATION_BEYOND;
2101 while(p_epointlist) {
2102 /* send setup message to endpoit */
2103 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2104 message->param.disconnectinfo.cause = cause;
2105 message->param.disconnectinfo.location = location;
2106 message_put(message);
2108 free_epointlist(p_epointlist);
2110 new_state(PORT_STATE_RELEASE);
2111 trigger_work(&p_s_delete);
2114 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[])
2116 struct sip_inst *inst = (struct sip_inst *) magic;
2118 class Psip *psip = NULL;
2121 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2125 /* hunt for existing handles */
2128 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2129 psip = (class Psip *)port;
2130 if (psip->p_s_handle == nh) {
2140 if (!psip && inst->register_handle != nh) {
2142 case nua_i_register:
2143 PDEBUG(DEBUG_SIP, "New register instance\n");
2144 inst->register_handle = nh;
2149 struct interface *interface = interface_first;
2151 PDEBUG(DEBUG_SIP, "New psip instance\n");
2153 /* create call instance */
2154 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2156 if (!strcmp(interface->name, inst->interface_name))
2158 interface = interface->next;
2161 PERROR("Cannot find interface %s.\n", inst->interface_name);
2164 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2165 FATAL("Cannot create Port instance.\n");
2169 PDEBUG(DEBUG_SIP, "Destroying unknown instance\n");
2170 nua_handle_destroy(nh);
2174 /* handle register process */
2175 if (inst->register_handle == nh) {
2177 case nua_i_register:
2178 i_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2180 case nua_r_register:
2181 r_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2184 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2189 /* handle port process */
2191 PERROR("no SIP Port found for handel %p\n", nh);
2192 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2193 nua_handle_destroy(nh);
2198 sip_trace_header(psip, inst->interface_name, "STATUS", DIRECTION_OUT);
2199 add_trace("value", NULL, "%d", status);
2200 add_trace("phrase", NULL, "%s", phrase);
2207 rc = challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
2213 case nua_r_set_params:
2214 PDEBUG(DEBUG_SIP, "setparam response\n");
2217 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2220 PDEBUG(DEBUG_SIP, "error received\n");
2223 PDEBUG(DEBUG_SIP, "state change received\n");
2226 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2229 PDEBUG(DEBUG_SIP, "ack received\n");
2232 PDEBUG(DEBUG_SIP, "active received\n");
2235 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2238 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2241 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2244 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2247 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2249 case nua_i_terminated:
2250 PDEBUG(DEBUG_SIP, "terminated received\n");
2253 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2257 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)
2259 struct sip_inst *inst = (struct sip_inst *) magic;
2263 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2266 case stun_discovery_done:
2267 addrlen = sizeof(sa);
2268 memset(&sa, 0, addrlen);
2269 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2270 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2273 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2274 inst->stun_state = STUN_STATE_RESOLVED;
2275 /* start timer for next stun request with inst->stun_interval */
2276 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2277 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2278 add_trace("ip", "addr", "%s", inst->public_ip);
2283 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2284 inst->stun_state = STUN_STATE_FAILED;
2285 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2286 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2287 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2292 /* received shutdown due to termination of RTP */
2293 void Psip::rtp_shutdown(void)
2295 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2296 struct lcr_msg *message;
2298 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2300 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2303 nua_handle_destroy(p_s_handle);
2306 while(p_epointlist) {
2307 /* send setup message to endpoit */
2308 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2309 message->param.disconnectinfo.cause = 16;
2310 message->param.disconnectinfo.location = LOCATION_BEYOND;
2311 message_put(message);
2313 free_epointlist(p_epointlist);
2315 new_state(PORT_STATE_RELEASE);
2316 trigger_work(&p_s_delete);
2319 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2321 class Psip *psip = (class Psip *)instance;
2322 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2324 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2327 nua_options(psip->p_s_handle,
2333 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2335 struct sip_inst *inst = (struct sip_inst *)instance;
2337 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2338 inst->stun_state = STUN_STATE_UNRESOLVED;
2343 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2345 struct sip_inst *inst = (struct sip_inst *)instance;
2347 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2348 /* if we have a handle, destroy it and becom unregistered, so registration is
2350 if (inst->register_handle) {
2351 /* stop option timer */
2352 unsched_timer(&inst->register_option_timer);
2353 nua_handle_destroy(inst->register_handle);
2354 inst->register_handle = NULL;
2356 inst->register_state = REGISTER_STATE_UNREGISTERED;
2361 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2363 struct sip_inst *inst = (struct sip_inst *)instance;
2364 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2367 nua_options(inst->register_handle,
2373 int sip_init_inst(struct interface *interface)
2375 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2378 interface->sip_inst = inst;
2379 SCPY(inst->interface_name, interface->name);
2380 SCPY(inst->local_peer, interface->sip_local_peer);
2381 SCPY(inst->remote_peer, interface->sip_remote_peer);
2382 SCPY(inst->asserted_id, interface->sip_asserted_id);
2383 if (interface->sip_register) {
2384 inst->register_state = REGISTER_STATE_UNREGISTERED;
2385 SCPY(inst->register_user, interface->sip_register_user);
2386 SCPY(inst->register_host, interface->sip_register_host);
2388 SCPY(inst->auth_user, interface->sip_auth_user);
2389 SCPY(inst->auth_password, interface->sip_auth_password);
2390 inst->register_interval = interface->sip_register_interval;
2391 inst->options_interval = interface->sip_options_interval;
2393 inst->rtp_port_from = interface->rtp_port_from;
2394 inst->rtp_port_to = interface->rtp_port_to;
2395 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2396 inst->rtp_port_from = RTP_PORT_BASE;
2397 inst->rtp_port_to = RTP_PORT_MAX;
2399 inst->next_rtp_port = inst->rtp_port_from;
2402 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2403 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2404 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2405 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2406 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2407 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2409 /* init root object */
2410 inst->root = su_root_create(inst);
2412 PERROR("Failed to create SIP root\n");
2413 sip_exit_inst(interface);
2417 SPRINT(local, "sip:%s",inst->local_peer);
2418 if (!strchr(inst->local_peer, ':'))
2419 SCAT(local, ":5060");
2420 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2422 PERROR("Failed to create SIP stack object\n");
2423 sip_exit_inst(interface);
2426 nua_set_params(inst->nua,
2427 SIPTAG_ALLOW_STR("REGISTER,INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2428 NUTAG_APPL_METHOD("REGISTER"),
2429 NUTAG_APPL_METHOD("INVITE"),
2430 NUTAG_APPL_METHOD("ACK"),
2431 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2432 NUTAG_APPL_METHOD("CANCEL"),
2433 NUTAG_APPL_METHOD("OPTIONS"),
2434 NUTAG_APPL_METHOD("NOTIFY"),
2435 NUTAG_APPL_METHOD("INFO"),
2437 #ifdef NUTAG_AUTO100
2441 NUTAG_AUTOANSWER(0),
2444 SCPY(inst->public_ip, interface->sip_public_ip);
2445 if (interface->sip_stun_server[0]) {
2447 inst->stun_root = su_root_create(inst);
2448 if (!inst->stun_root) {
2449 PERROR("Failed to create STUN root\n");
2450 sip_exit_inst(interface);
2455 SCPY(inst->stun_server, interface->sip_stun_server);
2456 inst->stun_interval = interface->sip_stun_interval;
2457 inst->stun_handle = stun_handle_init(inst->root,
2458 STUNTAG_SERVER(inst->stun_server),
2460 if (!inst->stun_handle) {
2461 PERROR("Failed to create STUN handle\n");
2462 sip_exit_inst(interface);
2465 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2466 if (inst->stun_socket < 0) {
2467 PERROR("Failed to create STUN socket\n");
2468 sip_exit_inst(interface);
2471 inst->stun_state = STUN_STATE_UNRESOLVED;
2474 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2476 any_sip_interface = 1;
2481 void sip_exit_inst(struct interface *interface)
2483 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2487 del_timer(&inst->stun_retry_timer);
2488 del_timer(&inst->register_retry_timer);
2489 del_timer(&inst->register_option_timer);
2490 if (inst->stun_socket)
2491 su_close(inst->stun_socket);
2492 if (inst->stun_handle)
2493 stun_handle_destroy(inst->stun_handle);
2494 if (inst->register_handle)
2495 nua_handle_destroy(inst->register_handle);
2497 su_root_destroy(inst->root);
2499 nua_destroy(inst->nua);
2500 FREE(inst, sizeof(*inst));
2501 interface->sip_inst = NULL;
2503 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2505 /* check if there is any other SIP interface left */
2506 interface = interface_first;
2508 if (interface->sip_inst)
2510 interface = interface->next;
2513 any_sip_interface = 0;
2516 extern su_log_t su_log_default[];
2517 extern su_log_t nua_log[];
2518 //extern su_log_t soa_log[];
2524 /* init SOFIA lib */
2526 su_home_init(sip_home);
2528 if (options.deb & DEBUG_SIP) {
2529 su_log_set_level(su_log_default, 9);
2530 su_log_set_level(nua_log, 9);
2531 //su_log_set_level(soa_log, 9);
2534 for (i = 0; i < 256; i++)
2535 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);
2537 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2544 su_home_deinit(sip_home);
2547 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2550 static void sip_handle_stun(struct sip_inst *inst)
2554 switch (inst->stun_state) {
2555 case STUN_STATE_UNRESOLVED:
2556 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2557 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2558 STUNTAG_SOCKET(inst->stun_socket),
2559 STUNTAG_REGISTER_EVENTS(1),
2562 PERROR("Failed to call stun_bind()\n");
2563 inst->stun_state = STUN_STATE_FAILED;
2566 inst->stun_state = STUN_STATE_RESOLVING;
2567 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2568 add_trace("server", "addr", "%s", inst->stun_server);
2574 static void sip_handle_register(struct sip_inst *inst)
2576 char from[128] = "";
2578 char contact[128] = "";
2580 switch (inst->register_state) {
2581 case REGISTER_STATE_UNREGISTERED:
2582 /* wait for resoved stun */
2583 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2586 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2587 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2588 if (!inst->register_handle) {
2589 PERROR("Failed to create handle\n");
2590 inst->register_state = REGISTER_STATE_FAILED;
2593 /* apply handle to trace */
2594 // sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2595 // add_trace("handle", "new", "0x%x", inst->register_handle);
2598 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2599 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2600 if (inst->public_ip[0])
2601 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2603 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2604 add_trace("from", "uri", "%s", from);
2605 add_trace("to", "uri", "%s", to);
2608 nua_register(inst->register_handle,
2609 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2610 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2611 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2614 inst->register_state = REGISTER_STATE_REGISTERING;
2621 void sip_handle(void)
2623 struct interface *interface = interface_first;
2624 struct sip_inst *inst;
2627 if (interface->sip_inst) {
2628 inst = (struct sip_inst *) interface->sip_inst;
2629 su_root_step(inst->root, 0);
2630 sip_handle_stun(inst);
2631 sip_handle_register(inst);
2633 interface = interface->next;
2637 /* deletes when back in event loop */
2638 static int delete_event(struct lcr_work *work, void *instance, int index)
2640 class Psip *psip = (class Psip *)instance;
2649 * generate audio, if no data is received from bridge
2652 void Psip::set_tone(const char *dir, const char *tone)
2654 Port::set_tone(dir, tone);
2659 void Psip::update_load(void)
2661 /* don't trigger load event if event already active */
2662 if (p_s_load_timer.active)
2665 /* don't start timer if ... */
2666 if (!p_tone_name[0] && !p_dov_tx)
2669 p_s_next_tv_sec = 0;
2670 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2673 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2675 class Psip *psip = (class Psip *)instance;
2677 /* stop timer if ... */
2678 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2686 #define SEND_SIP_LEN 160
2688 void Psip::load_tx(void)
2691 struct timeval current_time;
2692 int tosend = SEND_SIP_LEN, i;
2693 unsigned char buf[SEND_SIP_LEN], *p = buf;
2696 gettimeofday(¤t_time, NULL);
2697 if (!p_s_next_tv_sec) {
2698 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2699 p_s_next_tv_sec = current_time.tv_sec;
2700 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2701 if (p_s_next_tv_usec >= 1000000) {
2702 p_s_next_tv_usec -= 1000000;
2705 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
2707 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2708 + (current_time.tv_usec - p_s_next_tv_usec);
2709 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2710 /* if clock drifts too much, set next timeout event to current timer + 160 */
2712 p_s_next_tv_sec = current_time.tv_sec;
2713 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2714 if (p_s_next_tv_usec >= 1000000) {
2715 p_s_next_tv_usec -= 1000000;
2719 /* if diff is positive, it took too long, so next timeout will be earlier */
2720 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2721 if (p_s_next_tv_usec >= 1000000) {
2722 p_s_next_tv_usec -= 1000000;
2726 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
2730 if (p_tone_name[0]) {
2731 tosend -= read_audio(p, tosend);
2734 tosend -= dov_tx(p, tosend);
2737 PERROR("buffer is not completely filled\n");
2742 for (i = 0; i < SEND_SIP_LEN; i++) {
2746 /* transmit data via rtp */
2747 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
2750 #warning den source-port beim registrieren einstellen