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;
888 struct lcr_msg *message;
890 unsigned char payload_type;
892 if (param->connectinfo.rtpinfo.port) {
893 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
895 media_type = param->connectinfo.rtpinfo.media_types[0];
896 payload_type = param->connectinfo.rtpinfo.payload_types[0];
897 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
898 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
899 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
900 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
901 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
903 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
904 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
905 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
906 /* open local RTP peer (if not bridging) */
907 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
908 nua_cancel(p_s_handle, TAG_END());
909 nua_handle_destroy(p_s_handle);
911 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
912 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
914 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
915 message->param.disconnectinfo.cause = 41;
916 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
917 message_put(message);
918 new_state(PORT_STATE_RELEASE);
919 trigger_work(&p_s_delete);
924 memset(&ia, 0, sizeof(ia));
925 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
928 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
932 "m=audio %d RTP/AVP %d\r\n"
933 "a=rtpmap:%d %s/8000\r\n"
934 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
935 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
938 * If this response causes corrupt messages, like SDP body inside or
939 * before header, check if the sofia-sip-gcc-4.8.patch was applied.
940 * If it is still corrupted, try to disable optimization when compiling
943 nua_respond(p_s_handle, SIP_200_OK,
944 NUTAG_MEDIA_ENABLE(0),
945 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
946 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
948 new_state(PORT_STATE_CONNECT);
949 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
950 add_trace("respond", "value", "200 OK");
951 add_trace("reason", NULL, "call connected");
952 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
953 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
954 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
960 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
962 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
963 struct lcr_msg *message;
964 char cause_str[128] = "";
965 int cause = param->disconnectinfo.cause;
966 int location = param->disconnectinfo.cause;
968 const char *status_text;
970 if (cause > 0 && cause <= 127) {
971 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
975 case PORT_STATE_OUT_SETUP:
976 case PORT_STATE_OUT_PROCEEDING:
977 case PORT_STATE_OUT_ALERTING:
978 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
979 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
981 add_trace("cause", "value", "%d", cause);
983 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
985 case PORT_STATE_IN_SETUP:
986 case PORT_STATE_IN_PROCEEDING:
987 case PORT_STATE_IN_ALERTING:
988 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
989 status = cause2status(cause, location, &status_text);
990 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
992 add_trace("cause", "value", "%d", cause);
993 add_trace("respond", "value", "%d %s", status, status_text);
995 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
996 nua_handle_destroy(p_s_handle);
998 trigger_work(&p_s_delete);
1001 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1002 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1004 add_trace("cause", "value", "%d", cause);
1006 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1009 if (message_id == MESSAGE_DISCONNECT) {
1010 while(p_epointlist) {
1011 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1012 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1013 message->param.disconnectinfo.location = LOCATION_BEYOND;
1014 message_put(message);
1016 free_epointlist(p_epointlist);
1020 new_state(PORT_STATE_RELEASE);
1025 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1027 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1028 char from[128] = "";
1029 char asserted_id[128] = "", asserted_msg[256] = "";
1031 char contact[128] = "";
1032 const char *local = inst->local_peer;
1034 const char *remote = inst->remote_peer;
1035 char sdp_str[512], pt_str[32];
1037 struct epoint_list *epointlist;
1038 sip_cseq_t *cseq = NULL;
1039 struct lcr_msg *message;
1040 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1041 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1043 unsigned char *payload_types;
1048 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1049 add_trace("failed", "reason", "No remote peer set or no peer has registered to us.");
1051 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1052 message->param.disconnectinfo.cause = 27;
1053 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1054 message_put(message);
1055 new_state(PORT_STATE_RELEASE);
1056 trigger_work(&p_s_delete);
1060 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1062 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1063 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1064 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1065 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1066 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1068 if (param->setup.rtpinfo.port) {
1069 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1071 media_types = param->setup.rtpinfo.media_types;
1072 payload_types = param->setup.rtpinfo.payload_types;
1073 payloads = param->setup.rtpinfo.payloads;
1074 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1075 p_s_rtp_port_local = param->setup.rtpinfo.port;
1076 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1077 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1079 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1081 media_types = &lcr_media;
1082 payload_types = &lcr_payload;
1085 /* open local RTP peer (if not bridging) */
1086 if (rtp_open() < 0) {
1087 PERROR("Failed to open RTP sockets\n");
1088 /* send release message to endpoit */
1089 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1090 message->param.disconnectinfo.cause = 41;
1091 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1092 message_put(message);
1093 new_state(PORT_STATE_RELEASE);
1094 trigger_work(&p_s_delete);
1097 if (!p_s_rtp_ip_local) {
1100 /* extract IP from local peer */
1101 SCPY(local_ip, local);
1102 p = strchr(local_ip, ':');
1105 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1106 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1107 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1111 memset(&ia, 0, sizeof(ia));
1112 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1113 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1115 PERROR("Failed to create handle\n");
1116 /* send release message to endpoit */
1117 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1118 message->param.disconnectinfo.cause = 41;
1119 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1120 message_put(message);
1121 new_state(PORT_STATE_RELEASE);
1122 trigger_work(&p_s_delete);
1125 /* apply handle to trace */
1126 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1127 // add_trace("handle", "new", "0x%x", p_s_handle);
1132 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1136 "m=audio %d RTP/AVP"
1137 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1138 for (i = 0; i < payloads; i++) {
1139 SPRINT(pt_str, " %d", payload_types[i]);
1140 SCAT(sdp_str, pt_str);
1142 SCAT(sdp_str, "\r\n");
1143 for (i = 0; i < payloads; i++) {
1144 SPRINT(pt_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1145 SCAT(sdp_str, pt_str);
1147 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1149 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1150 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1151 if (inst->asserted_id[0]) {
1152 SPRINT(asserted_id, "sip:%s@%s", inst->asserted_id, remote);
1153 SPRINT(asserted_msg, "P-Asserted-Identity: <%s>", asserted_id);
1155 if (inst->public_ip[0])
1156 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1158 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1159 add_trace("from", "uri", "%s", from);
1160 add_trace("to", "uri", "%s", to);
1162 add_trace("assert-id", "uri", "%s", asserted_id);
1163 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1164 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1165 for (i = 0; i < payloads; i++)
1166 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1169 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1171 nua_invite(p_s_handle,
1172 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1173 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1174 TAG_IF(asserted_msg[0], SIPTAG_HEADER_STR(asserted_msg)),
1175 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1176 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1177 NUTAG_MEDIA_ENABLE(0),
1178 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1179 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1180 new_state(PORT_STATE_OUT_SETUP);
1182 p_s_invite_direction = DIRECTION_OUT;
1185 PDEBUG(DEBUG_SIP, "do overlap\n");
1186 new_state(PORT_STATE_OUT_OVERLAP);
1187 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1188 message_put(message);
1190 PDEBUG(DEBUG_SIP, "do proceeding\n");
1191 new_state(PORT_STATE_OUT_PROCEEDING);
1192 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1193 message_put(message);
1196 /* attach only if not already */
1197 epointlist = p_epointlist;
1199 if (epointlist->epoint_id == epoint_id)
1201 epointlist = epointlist->next;
1204 epointlist_new(epoint_id);
1209 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1212 // struct in_addr ia;
1214 switch (param->notifyinfo.notify) {
1215 case INFO_NOTIFY_REMOTE_HOLD:
1219 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1221 "c=IN IP4 0.0.0.0\r\n"
1224 PDEBUG(DEBUG_SIP, "Using SDP for hold: %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());
1234 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1236 memset(&ia, 0, sizeof(ia));
1237 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1240 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1244 "m=audio %d RTP/AVP %d\r\n"
1245 "a=rtpmap:%d %s/8000\r\n"
1246 , 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));
1247 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1248 nua_info(p_s_handle,
1249 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1250 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1251 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1252 NUTAG_MEDIA_ENABLE(0),
1253 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1254 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1262 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1266 /* prepare DTMF info payload */
1272 /* start invite to handle DTMF */
1273 nua_info(p_s_handle,
1274 NUTAG_MEDIA_ENABLE(0),
1275 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1276 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1281 /* NOTE: incomplete and not working */
1282 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1286 /* prepare DTMF info payload */
1290 , param->information.id);
1292 /* start invite to handle DTMF */
1293 nua_info(p_s_handle,
1294 NUTAG_MEDIA_ENABLE(0),
1295 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1296 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1301 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1303 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1305 if (Port::message_epoint(epoint_id, message_id, param))
1308 switch(message_id) {
1309 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1310 if (p_state != PORT_STATE_IN_SETUP
1311 && p_state != PORT_STATE_IN_PROCEEDING)
1313 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1314 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1315 add_trace("respond", "value", "180 Ringing");
1317 new_state(PORT_STATE_IN_ALERTING);
1320 case MESSAGE_CONNECT: /* call is connected on LCR side */
1321 if (p_state != PORT_STATE_IN_SETUP
1322 && p_state != PORT_STATE_IN_PROCEEDING
1323 && p_state != PORT_STATE_IN_ALERTING)
1325 message_connect(epoint_id, message_id, param);
1328 case MESSAGE_DISCONNECT: /* call has been disconnected */
1329 case MESSAGE_RELEASE: /* call has been released */
1330 message_release(epoint_id, message_id, param);
1333 case MESSAGE_SETUP: /* dial-out command received from epoint */
1334 message_setup(epoint_id, message_id, param);
1337 case MESSAGE_INFORMATION: /* overlap dialing */
1338 if (p_state != PORT_STATE_OUT_OVERLAP)
1340 message_information(epoint_id, message_id, param);
1343 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1344 if (p_state == PORT_STATE_CONNECT)
1345 message_dtmf(epoint_id, message_id, param);
1346 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1347 if (p_state == PORT_STATE_CONNECT)
1348 message_notify(epoint_id, message_id, param);
1352 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1358 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)
1362 if (!sip->sip_payload) {
1363 PDEBUG(DEBUG_SIP, "no payload given\n");
1367 sdp_parser_t *parser;
1370 sdp_attribute_t *attr;
1372 sdp_connection_t *conn;
1374 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1376 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1380 if (!(sdp = sdp_session(parser))) {
1381 sdp_parser_free(parser);
1384 for (m = sdp->sdp_media; m; m = m->m_next) {
1385 if (m->m_proto != sdp_proto_rtp)
1387 if (m->m_type != sdp_media_audio)
1389 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1391 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1392 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1394 if (m->m_connections) {
1395 conn = m->m_connections;
1396 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1397 inet_pton(AF_INET, conn->c_address, ip);
1398 *ip = ntohl(p_s_rtp_ip_remote);
1400 char *p = sip->sip_payload->pl_data;
1403 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1404 p = strstr(p, "c=IN IP4 ");
1406 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1407 sdp_parser_free(parser);
1411 if ((p = strchr(addr, '\n'))) *p = '\0';
1412 if ((p = strchr(addr, '\r'))) *p = '\0';
1413 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1414 inet_pton(AF_INET, addr, ip);
1415 *ip = ntohl(p_s_rtp_ip_remote);
1417 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1420 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1421 /* append to payload list, if there is space */
1422 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1423 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1424 media_type = MEDIA_TYPE_ALAW;
1425 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1426 media_type = MEDIA_TYPE_ULAW;
1427 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1428 media_type = MEDIA_TYPE_GSM;
1429 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1430 media_type = MEDIA_TYPE_GSM_EFR;
1431 else if (!strcmp(map->rm_encoding, "AMR"))
1432 media_type = MEDIA_TYPE_AMR;
1433 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1434 media_type = MEDIA_TYPE_GSM_HR;
1435 if (media_type && *payloads <= max_payloads) {
1436 *payload_types++ = map->rm_pt;
1437 *media_types++ = media_type;
1443 sdp_parser_free(parser);
1448 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[])
1450 sip_www_authenticate_t const *authenticate = NULL;
1451 char const *realm = NULL;
1452 char const *scheme = NULL;
1455 char authentication[256] = "";
1456 PDEBUG(DEBUG_SIP, "challenge order received\n");
1458 if (!inst->auth_user[0]) {
1459 PDEBUG(DEBUG_SIP, "No credentials available\n");
1460 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1461 add_trace("error", NULL, "There are no credentials given for interface");
1466 if (sip->sip_www_authenticate) {
1467 authenticate = sip->sip_www_authenticate;
1468 } else if (sip->sip_proxy_authenticate) {
1469 authenticate = sip->sip_proxy_authenticate;
1471 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1472 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1473 add_trace("error", NULL, "Authentication method unknwon");
1478 scheme = (char const *) authenticate->au_scheme;
1479 if (authenticate->au_params) {
1480 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1481 if ((realm = strstr(cur, "realm="))) {
1488 if (!scheme || !realm) {
1489 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1490 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1491 add_trace("error", NULL, "Authentication header has no realm or scheme");
1496 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1497 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1499 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1500 add_trace("scheme", NULL, "%s", scheme);
1501 add_trace("realm", NULL, "%s", realm);
1502 add_trace("user", NULL, "%s", inst->auth_user);
1503 add_trace("pass", NULL, "%s", inst->auth_password);
1506 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1511 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[])
1513 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1514 nua_saved_event_t saved[1];
1515 sip_contact_t const *contact = NULL;
1516 contact = sip->sip_contact;
1517 nua_save_event(nua, saved);
1518 nua_event_data_t const *data = nua_event_data(saved);
1520 if (contact->m_url->url_host)
1521 SCPY(inst->remote_peer, contact->m_url->url_host);
1522 if (contact->m_url->url_port && contact->m_url->url_port[0]) {
1523 SCAT(inst->remote_peer, ":");
1524 SCAT(inst->remote_peer, contact->m_url->url_port);
1527 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1528 add_trace("contact", "uri", "%s", inst->remote_peer);
1531 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1532 add_trace("respond", "value", "200 OK");
1533 add_trace("reason", NULL, "peer registered");
1536 nua_respond(nh, SIP_200_OK, SIPTAG_CONTACT(sip->sip_contact), NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1537 nua_handle_destroy(nh);
1538 inst->register_handle = NULL;
1541 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[])
1545 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_IN);
1546 add_trace("value", NULL, "%d", status);
1547 add_trace("phrase", NULL, "%s", phrase);
1553 /* if not registered, become registered and start register interval timer */
1554 if (inst->register_state != REGISTER_STATE_REGISTERED) {
1555 if (inst->register_interval)
1556 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
1557 inst->register_state = REGISTER_STATE_REGISTERED;
1559 /* start option timer */
1560 if (inst->options_interval)
1561 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1565 PDEBUG(DEBUG_SIP, "Register challenge received\n");
1566 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
1571 if (status >= 200 && status <= 299)
1576 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
1577 inst->register_state = REGISTER_STATE_FAILED;
1578 nua_handle_destroy(nh);
1579 inst->register_handle = NULL;
1580 /* stop option timer */
1581 unsched_timer(&inst->register_option_timer);
1582 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1583 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1587 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[])
1589 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1590 const char *from = "", *to = "", *name = "";
1593 class Endpoint *epoint;
1594 struct lcr_msg *message;
1595 struct interface *interface;
1596 int media_types[32];
1597 uint8_t payload_types[32];
1601 interface = getinterfacebyname(inst->interface_name);
1603 PERROR("Cannot find interface %s.\n", inst->interface_name);
1607 if (sip->sip_from) {
1608 if (sip->sip_from->a_url)
1609 from = sip->sip_from->a_url->url_user;
1610 if (sip->sip_from->a_display) {
1611 name = sip->sip_from->a_display;
1612 if (!strncmp(name, "\"IMSI", 5)) {
1613 strncpy(imsi, name + 5, 15);
1620 if (sip->sip_to->a_url)
1621 to = sip->sip_to->a_url->url_user;
1623 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1625 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1626 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1628 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1629 if (!p_s_rtp_bridge) {
1632 /* check if supported payload type exists */
1633 for (i = 0; i < payloads; i++) {
1634 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1637 if (i == payloads) {
1638 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1646 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1648 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1649 nua_handle_destroy(nh);
1651 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1653 add_trace("respond", "value", "415 Unsupported Media");
1655 add_trace("respond", "value", "400 Bad Request");
1656 add_trace("reason", NULL, "offered codec does not match");
1658 new_state(PORT_STATE_RELEASE);
1659 trigger_work(&p_s_delete);
1663 /* open local RTP peer (if not bridging) */
1664 if (!p_s_rtp_bridge && rtp_open() < 0) {
1665 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1666 nua_handle_destroy(nh);
1668 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1669 add_trace("respond", "value", "500 Internal Server Error");
1670 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1672 new_state(PORT_STATE_RELEASE);
1673 trigger_work(&p_s_delete);
1678 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1679 // add_trace("handle", "new", "0x%x", nh);
1684 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1685 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1686 /* caller information */
1688 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1689 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1690 add_trace("calling", "present", "unavailable");
1692 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1693 add_trace("calling", "present", "allowed");
1694 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1695 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1696 SCPY(p_callerinfo.id, from);
1697 add_trace("calling", "number", "%s", from);
1698 SCPY(p_callerinfo.name, name);
1700 add_trace("calling", "name", "%s", name);
1701 SCPY(p_callerinfo.imsi, imsi);
1703 add_trace("calling", "imsi", "%s", imsi);
1705 SCPY(p_callerinfo.interface, inst->interface_name);
1706 /* dialing information */
1708 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1709 SCAT(p_dialinginfo.id, to);
1710 add_trace("dialing", "number", "%s", to);
1713 /* bearer capability */
1714 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1715 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1716 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1717 add_trace("bearer", "capa", "speech");
1718 add_trace("bearer", "mode", "circuit");
1719 /* if packet mode works some day, see dss1.cpp for conditions */
1720 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1724 /* create endpoint */
1726 FATAL("Incoming call but already got an endpoint.\n");
1727 if (!(epoint = new Endpoint(p_serial, 0)))
1728 FATAL("No memory for Endpoint instance\n");
1729 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1730 epointlist_new(epoint->ep_serial);
1732 #ifdef NUTAG_AUTO100
1733 /* send trying (proceeding) */
1734 nua_respond(nh, SIP_100_TRYING, TAG_END());
1735 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1736 add_trace("respond", "value", "100 Trying");
1740 new_state(PORT_STATE_IN_PROCEEDING);
1742 /* send setup message to endpoit */
1743 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1744 message->param.setup.port_type = p_type;
1745 // message->param.setup.dtmf = 0;
1746 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1747 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1748 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1749 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1750 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1751 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1752 if (p_s_rtp_bridge) {
1755 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1756 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1757 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1758 /* add codecs to setup message */
1759 for (i = 0; i < payloads; i++) {
1760 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1761 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1762 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1765 message->param.setup.rtpinfo.payloads = i;
1767 message_put(message);
1769 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer\n");
1770 /* start option timer */
1771 if (inst->options_interval)
1772 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1773 p_s_invite_direction = DIRECTION_IN;
1775 /* send progress, if tones are available and if we don't bridge */
1776 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1779 unsigned char payload_type;
1781 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1782 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1783 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1784 /* open local RTP peer (if not bridging) */
1785 if (rtp_connect() < 0) {
1786 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1787 nua_handle_destroy(nh);
1789 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1790 add_trace("respond", "value", "500 Internal Server Error");
1791 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1793 new_state(PORT_STATE_RELEASE);
1794 trigger_work(&p_s_delete);
1795 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1796 message->param.disconnectinfo.cause = 41;
1797 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1798 message_put(message);
1799 new_state(PORT_STATE_RELEASE);
1800 trigger_work(&p_s_delete);
1804 memset(&ia, 0, sizeof(ia));
1805 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1808 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1812 "m=audio %d RTP/AVP %d\r\n"
1813 "a=rtpmap:%d %s/8000\r\n"
1814 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
1815 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1817 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1818 NUTAG_MEDIA_ENABLE(0),
1819 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1820 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1821 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1822 add_trace("respond", "value", "183 SESSION PROGRESS");
1823 add_trace("reason", NULL, "audio available");
1824 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1825 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1826 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1831 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[])
1833 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1834 struct lcr_msg *message;
1837 PDEBUG(DEBUG_SIP, "bye received\n");
1839 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
1840 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1841 cause = atoi(sip->sip_reason->re_cause);
1842 add_trace("cause", "value", "%d", cause);
1846 // let stack do bye automaticall, since it will not accept our response for some reason
1847 // nua_respond(nh, SIP_200_OK, TAG_END());
1848 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1849 add_trace("respond", "value", "200 OK");
1851 // nua_handle_destroy(nh);
1856 while(p_epointlist) {
1857 /* send setup message to endpoit */
1858 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1859 message->param.disconnectinfo.cause = cause ? : 16;
1860 message->param.disconnectinfo.location = LOCATION_BEYOND;
1861 message_put(message);
1863 free_epointlist(p_epointlist);
1865 new_state(PORT_STATE_RELEASE);
1866 trigger_work(&p_s_delete);
1869 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[])
1871 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1872 struct lcr_msg *message;
1874 PDEBUG(DEBUG_SIP, "cancel received\n");
1876 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
1879 nua_handle_destroy(nh);
1884 while(p_epointlist) {
1885 /* send setup message to endpoit */
1886 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1887 message->param.disconnectinfo.cause = 16;
1888 message->param.disconnectinfo.location = LOCATION_BEYOND;
1889 message_put(message);
1891 free_epointlist(p_epointlist);
1893 new_state(PORT_STATE_RELEASE);
1894 trigger_work(&p_s_delete);
1897 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[])
1899 PDEBUG(DEBUG_SIP, "bye response received\n");
1901 nua_handle_destroy(nh);
1906 trigger_work(&p_s_delete);
1909 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[])
1911 PDEBUG(DEBUG_SIP, "cancel response received\n");
1913 nua_handle_destroy(nh);
1918 trigger_work(&p_s_delete);
1921 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[])
1923 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1924 struct lcr_msg *message;
1925 int cause = 0, location = 0;
1926 int media_types[32];
1927 uint8_t payload_types[32];
1930 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1932 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1933 add_trace("respond", "value", "%d", status);
1937 if (status == 183 || (status >= 200 && status <= 299)) {
1940 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1941 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1945 else if (!p_s_rtp_bridge) {
1946 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1947 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1954 nua_cancel(nh, TAG_END());
1955 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1956 add_trace("reason", NULL, "accepted codec does not match");
1959 location = LOCATION_PRIVATE_LOCAL;
1960 goto release_with_cause;
1963 /* connect to remote RTP (if not bridging) */
1964 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1965 nua_cancel(nh, TAG_END());
1966 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
1967 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1970 location = LOCATION_PRIVATE_LOCAL;
1971 goto release_with_cause;
1975 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer\n");
1976 /* start option timer */
1977 if (inst->options_interval)
1978 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1983 PDEBUG(DEBUG_SIP, "do proceeding\n");
1984 new_state(PORT_STATE_OUT_PROCEEDING);
1985 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1986 message_put(message);
1990 PDEBUG(DEBUG_SIP, "do alerting\n");
1991 new_state(PORT_STATE_OUT_ALERTING);
1992 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1993 message_put(message);
1996 PDEBUG(DEBUG_SIP, "do progress\n");
1997 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1998 message->param.progressinfo.progress = 8;
1999 message->param.progressinfo.location = 10;
2000 if (p_s_rtp_bridge) {
2001 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2002 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
2003 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
2004 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
2005 message->param.progressinfo.rtpinfo.payloads = 1;
2007 message_put(message);
2011 PDEBUG(DEBUG_SIP, "do connect\n");
2012 nua_ack(nh, TAG_END());
2013 new_state(PORT_STATE_CONNECT);
2014 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
2015 if (p_s_rtp_bridge) {
2016 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2017 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
2018 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
2019 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
2020 message->param.connectinfo.rtpinfo.payloads = 1;
2022 message_put(message);
2025 if (status >= 200 && status <= 299)
2027 if (status < 100 || status > 199)
2029 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
2034 cause = status2cause(status);
2035 location = LOCATION_BEYOND;
2038 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
2040 while(p_epointlist) {
2041 /* send setup message to endpoit */
2042 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2043 message->param.disconnectinfo.cause = cause;
2044 message->param.disconnectinfo.location = location;
2045 message_put(message);
2047 free_epointlist(p_epointlist);
2050 new_state(PORT_STATE_RELEASE);
2054 trigger_work(&p_s_delete);
2057 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[])
2059 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2060 int cause = 0, location = 0;
2061 struct lcr_msg *message;
2063 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
2065 if (status >= 200 && status <= 299) {
2066 PDEBUG(DEBUG_SIP, "options ok, scheduling timer\n");
2067 /* restart option timer */
2068 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2072 nua_handle_destroy(nh);
2077 cause = status2cause(status);
2078 location = LOCATION_BEYOND;
2080 while(p_epointlist) {
2081 /* send setup message to endpoit */
2082 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2083 message->param.disconnectinfo.cause = cause;
2084 message->param.disconnectinfo.location = location;
2085 message_put(message);
2087 free_epointlist(p_epointlist);
2089 new_state(PORT_STATE_RELEASE);
2090 trigger_work(&p_s_delete);
2093 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[])
2095 struct sip_inst *inst = (struct sip_inst *) magic;
2097 class Psip *psip = NULL;
2100 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2104 /* hunt for existing handles */
2107 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2108 psip = (class Psip *)port;
2109 if (psip->p_s_handle == nh) {
2119 if (!psip && inst->register_handle != nh) {
2121 case nua_i_register:
2122 PDEBUG(DEBUG_SIP, "New register instance\n");
2123 inst->register_handle = nh;
2128 struct interface *interface = interface_first;
2130 PDEBUG(DEBUG_SIP, "New psip instance\n");
2132 /* create call instance */
2133 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2135 if (!strcmp(interface->name, inst->interface_name))
2137 interface = interface->next;
2140 PERROR("Cannot find interface %s.\n", inst->interface_name);
2143 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2144 FATAL("Cannot create Port instance.\n");
2148 PDEBUG(DEBUG_SIP, "Destroying unknown instance\n");
2149 nua_handle_destroy(nh);
2153 /* handle register process */
2154 if (inst->register_handle == nh) {
2156 case nua_i_register:
2157 i_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2159 case nua_r_register:
2160 r_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2163 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2168 /* handle port process */
2170 PERROR("no SIP Port found for handel %p\n", nh);
2171 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2172 nua_handle_destroy(nh);
2177 sip_trace_header(psip, inst->interface_name, "STATUS", DIRECTION_OUT);
2178 add_trace("value", NULL, "%d", status);
2179 add_trace("phrase", NULL, "%s", phrase);
2186 rc = challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
2192 case nua_r_set_params:
2193 PDEBUG(DEBUG_SIP, "setparam response\n");
2196 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2199 PDEBUG(DEBUG_SIP, "error received\n");
2202 PDEBUG(DEBUG_SIP, "state change received\n");
2205 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2208 PDEBUG(DEBUG_SIP, "ack received\n");
2211 PDEBUG(DEBUG_SIP, "active received\n");
2214 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2217 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2220 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2223 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2226 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2228 case nua_i_terminated:
2229 PDEBUG(DEBUG_SIP, "terminated received\n");
2232 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2236 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)
2238 struct sip_inst *inst = (struct sip_inst *) magic;
2242 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2245 case stun_discovery_done:
2246 addrlen = sizeof(sa);
2247 memset(&sa, 0, addrlen);
2248 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2249 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2252 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2253 inst->stun_state = STUN_STATE_RESOLVED;
2254 /* start timer for next stun request with inst->stun_interval */
2255 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2256 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2257 add_trace("ip", "addr", "%s", inst->public_ip);
2262 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2263 inst->stun_state = STUN_STATE_FAILED;
2264 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2265 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2266 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2271 /* received shutdown due to termination of RTP */
2272 void Psip::rtp_shutdown(void)
2274 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2275 struct lcr_msg *message;
2277 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2279 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2282 nua_handle_destroy(p_s_handle);
2285 while(p_epointlist) {
2286 /* send setup message to endpoit */
2287 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2288 message->param.disconnectinfo.cause = 16;
2289 message->param.disconnectinfo.location = LOCATION_BEYOND;
2290 message_put(message);
2292 free_epointlist(p_epointlist);
2294 new_state(PORT_STATE_RELEASE);
2295 trigger_work(&p_s_delete);
2298 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2300 class Psip *psip = (class Psip *)instance;
2301 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2303 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2306 nua_options(psip->p_s_handle,
2312 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2314 struct sip_inst *inst = (struct sip_inst *)instance;
2316 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2317 inst->stun_state = STUN_STATE_UNRESOLVED;
2322 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2324 struct sip_inst *inst = (struct sip_inst *)instance;
2326 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2327 /* if we have a handle, destroy it and becom unregistered, so registration is
2329 if (inst->register_handle) {
2330 /* stop option timer */
2331 unsched_timer(&inst->register_option_timer);
2332 nua_handle_destroy(inst->register_handle);
2333 inst->register_handle = NULL;
2335 inst->register_state = REGISTER_STATE_UNREGISTERED;
2340 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2342 struct sip_inst *inst = (struct sip_inst *)instance;
2343 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2346 nua_options(inst->register_handle,
2352 int sip_init_inst(struct interface *interface)
2354 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2357 interface->sip_inst = inst;
2358 SCPY(inst->interface_name, interface->name);
2359 SCPY(inst->local_peer, interface->sip_local_peer);
2360 SCPY(inst->remote_peer, interface->sip_remote_peer);
2361 SCPY(inst->asserted_id, interface->sip_asserted_id);
2362 if (interface->sip_register) {
2363 inst->register_state = REGISTER_STATE_UNREGISTERED;
2364 SCPY(inst->register_user, interface->sip_register_user);
2365 SCPY(inst->register_host, interface->sip_register_host);
2367 SCPY(inst->auth_user, interface->sip_auth_user);
2368 SCPY(inst->auth_password, interface->sip_auth_password);
2369 inst->register_interval = interface->sip_register_interval;
2370 inst->options_interval = interface->sip_options_interval;
2372 inst->rtp_port_from = interface->rtp_port_from;
2373 inst->rtp_port_to = interface->rtp_port_to;
2374 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2375 inst->rtp_port_from = RTP_PORT_BASE;
2376 inst->rtp_port_to = RTP_PORT_MAX;
2378 inst->next_rtp_port = inst->rtp_port_from;
2381 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2382 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2383 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2384 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2385 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2386 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2388 /* init root object */
2389 inst->root = su_root_create(inst);
2391 PERROR("Failed to create SIP root\n");
2392 sip_exit_inst(interface);
2396 SPRINT(local, "sip:%s",inst->local_peer);
2397 if (!strchr(inst->local_peer, ':'))
2398 SCAT(local, ":5060");
2399 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2401 PERROR("Failed to create SIP stack object\n");
2402 sip_exit_inst(interface);
2405 nua_set_params(inst->nua,
2406 SIPTAG_ALLOW_STR("REGISTER,INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2407 NUTAG_APPL_METHOD("REGISTER"),
2408 NUTAG_APPL_METHOD("INVITE"),
2409 NUTAG_APPL_METHOD("ACK"),
2410 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2411 NUTAG_APPL_METHOD("CANCEL"),
2412 NUTAG_APPL_METHOD("OPTIONS"),
2413 NUTAG_APPL_METHOD("NOTIFY"),
2414 NUTAG_APPL_METHOD("INFO"),
2416 #ifdef NUTAG_AUTO100
2420 NUTAG_AUTOANSWER(0),
2423 SCPY(inst->public_ip, interface->sip_public_ip);
2424 if (interface->sip_stun_server[0]) {
2426 inst->stun_root = su_root_create(inst);
2427 if (!inst->stun_root) {
2428 PERROR("Failed to create STUN root\n");
2429 sip_exit_inst(interface);
2434 SCPY(inst->stun_server, interface->sip_stun_server);
2435 inst->stun_interval = interface->sip_stun_interval;
2436 inst->stun_handle = stun_handle_init(inst->root,
2437 STUNTAG_SERVER(inst->stun_server),
2439 if (!inst->stun_handle) {
2440 PERROR("Failed to create STUN handle\n");
2441 sip_exit_inst(interface);
2444 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2445 if (inst->stun_socket < 0) {
2446 PERROR("Failed to create STUN socket\n");
2447 sip_exit_inst(interface);
2450 inst->stun_state = STUN_STATE_UNRESOLVED;
2453 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2455 any_sip_interface = 1;
2460 void sip_exit_inst(struct interface *interface)
2462 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2466 del_timer(&inst->stun_retry_timer);
2467 del_timer(&inst->register_retry_timer);
2468 del_timer(&inst->register_option_timer);
2469 if (inst->stun_socket)
2470 su_close(inst->stun_socket);
2471 if (inst->stun_handle)
2472 stun_handle_destroy(inst->stun_handle);
2473 if (inst->register_handle)
2474 nua_handle_destroy(inst->register_handle);
2476 su_root_destroy(inst->root);
2478 nua_destroy(inst->nua);
2479 FREE(inst, sizeof(*inst));
2480 interface->sip_inst = NULL;
2482 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2484 /* check if there is any other SIP interface left */
2485 interface = interface_first;
2487 if (interface->sip_inst)
2489 interface = interface->next;
2492 any_sip_interface = 0;
2495 extern su_log_t su_log_default[];
2496 extern su_log_t nua_log[];
2497 //extern su_log_t soa_log[];
2503 /* init SOFIA lib */
2505 su_home_init(sip_home);
2507 if (options.deb & DEBUG_SIP) {
2508 su_log_set_level(su_log_default, 9);
2509 su_log_set_level(nua_log, 9);
2510 //su_log_set_level(soa_log, 9);
2513 for (i = 0; i < 256; i++)
2514 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);
2516 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2523 su_home_deinit(sip_home);
2526 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2529 static void sip_handle_stun(struct sip_inst *inst)
2533 switch (inst->stun_state) {
2534 case STUN_STATE_UNRESOLVED:
2535 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2536 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2537 STUNTAG_SOCKET(inst->stun_socket),
2538 STUNTAG_REGISTER_EVENTS(1),
2541 PERROR("Failed to call stun_bind()\n");
2542 inst->stun_state = STUN_STATE_FAILED;
2545 inst->stun_state = STUN_STATE_RESOLVING;
2546 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2547 add_trace("server", "addr", "%s", inst->stun_server);
2553 static void sip_handle_register(struct sip_inst *inst)
2555 char from[128] = "";
2557 char contact[128] = "";
2559 switch (inst->register_state) {
2560 case REGISTER_STATE_UNREGISTERED:
2561 /* wait for resoved stun */
2562 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2565 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2566 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2567 if (!inst->register_handle) {
2568 PERROR("Failed to create handle\n");
2569 inst->register_state = REGISTER_STATE_FAILED;
2572 /* apply handle to trace */
2573 // sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2574 // add_trace("handle", "new", "0x%x", inst->register_handle);
2577 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2578 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2579 if (inst->public_ip[0])
2580 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2582 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2583 add_trace("from", "uri", "%s", from);
2584 add_trace("to", "uri", "%s", to);
2587 nua_register(inst->register_handle,
2588 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2589 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2590 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2593 inst->register_state = REGISTER_STATE_REGISTERING;
2600 void sip_handle(void)
2602 struct interface *interface = interface_first;
2603 struct sip_inst *inst;
2606 if (interface->sip_inst) {
2607 inst = (struct sip_inst *) interface->sip_inst;
2608 su_root_step(inst->root, 0);
2609 sip_handle_stun(inst);
2610 sip_handle_register(inst);
2612 interface = interface->next;
2616 /* deletes when back in event loop */
2617 static int delete_event(struct lcr_work *work, void *instance, int index)
2619 class Psip *psip = (class Psip *)instance;
2628 * generate audio, if no data is received from bridge
2631 void Psip::set_tone(const char *dir, const char *tone)
2633 Port::set_tone(dir, tone);
2638 void Psip::update_load(void)
2640 /* don't trigger load event if event already active */
2641 if (p_s_load_timer.active)
2644 /* don't start timer if ... */
2645 if (!p_tone_name[0] && !p_dov_tx)
2648 p_s_next_tv_sec = 0;
2649 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2652 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2654 class Psip *psip = (class Psip *)instance;
2656 /* stop timer if ... */
2657 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2665 #define SEND_SIP_LEN 160
2667 void Psip::load_tx(void)
2670 struct timeval current_time;
2671 int tosend = SEND_SIP_LEN, i;
2672 unsigned char buf[SEND_SIP_LEN], *p = buf;
2675 gettimeofday(¤t_time, NULL);
2676 if (!p_s_next_tv_sec) {
2677 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2678 p_s_next_tv_sec = current_time.tv_sec;
2679 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2680 if (p_s_next_tv_usec >= 1000000) {
2681 p_s_next_tv_usec -= 1000000;
2684 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
2686 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2687 + (current_time.tv_usec - p_s_next_tv_usec);
2688 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2689 /* if clock drifts too much, set next timeout event to current timer + 160 */
2691 p_s_next_tv_sec = current_time.tv_sec;
2692 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2693 if (p_s_next_tv_usec >= 1000000) {
2694 p_s_next_tv_usec -= 1000000;
2698 /* if diff is positive, it took too long, so next timeout will be earlier */
2699 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2700 if (p_s_next_tv_usec >= 1000000) {
2701 p_s_next_tv_usec -= 1000000;
2705 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
2709 if (p_tone_name[0]) {
2710 tosend -= read_audio(p, tosend);
2713 tosend -= dov_tx(p, tosend);
2716 PERROR("buffer is not completely filled\n");
2721 for (i = 0; i < SEND_SIP_LEN; i++) {
2725 /* transmit data via rtp */
2726 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
2729 #warning den source-port beim registrieren einstellen