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];
60 char register_user[128];
61 char register_host[128];
62 nua_handle_t *register_handle;
63 struct lcr_timer register_retry_timer;
64 struct lcr_timer register_option_timer;
65 int register_interval;
68 char auth_password[128];
74 char stun_server[128];
75 stun_handle_t *stun_handle;
76 su_socket_t stun_socket;
77 struct lcr_timer stun_retry_timer;
80 unsigned short rtp_port_from;
81 unsigned short rtp_port_to;
82 unsigned short next_rtp_port;
86 static int delete_event(struct lcr_work *work, void *instance, int index);
87 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index);
88 static int load_timer(struct lcr_timer *timer, void *instance, int index);
93 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
96 if (interface->rtp_bridge)
98 p_s_sip_inst = interface->sip_inst;
99 memset(&p_s_delete, 0, sizeof(p_s_delete));
100 add_work(&p_s_delete, delete_event, this, 0);
103 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
104 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
105 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
106 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
107 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
108 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
109 p_s_rtp_ip_local = 0;
110 p_s_rtp_ip_remote = 0;
111 p_s_rtp_port_local = 0;
112 p_s_rtp_port_remote = 0;
117 p_s_rtp_tx_action = 0;
118 p_s_rtp_is_connected = 0;
120 /* create option timer */
121 memset(&p_s_invite_option_timer, 0, sizeof(p_s_invite_option_timer));
122 add_timer(&p_s_invite_option_timer, invite_option_timer, this, 0);
123 p_s_invite_direction = 0;
126 memset(&p_s_load_timer, 0, sizeof(p_s_load_timer));
127 add_timer(&p_s_load_timer, load_timer, this, 0);
130 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
132 FATAL("No SIP instance for interface\n");
141 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
143 del_timer(&p_s_invite_option_timer);
144 del_timer(&p_s_load_timer);
145 del_work(&p_s_delete);
150 static const char *media_type2name(uint8_t media_type) {
151 switch (media_type) {
152 case MEDIA_TYPE_ULAW:
154 case MEDIA_TYPE_ALAW:
158 case MEDIA_TYPE_GSM_HR:
160 case MEDIA_TYPE_GSM_EFR:
169 static void sip_trace_header(class Psip *sip, const char *interface_name, const char *message, int direction)
171 struct interface *interface = NULL;
174 interface = getinterfacebyname(interface_name);
176 /* init trace with given values */
179 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
180 sip?sip->p_dialinginfo.id:NULL,
191 /* according to RFC 3550 */
193 #if __BYTE_ORDER == __LITTLE_ENDIAN
194 uint8_t csrc_count:4,
198 uint8_t payload_type:7,
200 #elif __BYTE_ORDER == __BIG_ENDIAN
211 } __attribute__((packed));
216 } __attribute__((packed));
218 #define RTP_VERSION 2
220 #define PAYLOAD_TYPE_ULAW 0
221 #define PAYLOAD_TYPE_ALAW 8
222 #define PAYLOAD_TYPE_GSM 3
224 /* decode an rtp frame */
225 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
227 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
228 struct rtp_x_hdr *rtpxh;
232 unsigned char *from, *to;
236 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
239 if (rtph->version != RTP_VERSION) {
240 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
243 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
244 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
245 if (payload_len < 0) {
246 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
247 "csrc count = %d)\n", len, rtph->csrc_count);
250 if (rtph->extension) {
251 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
252 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
253 "extension header\n");
256 rtpxh = (struct rtp_x_hdr *)payload;
257 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
259 payload_len -= x_len;
260 if (payload_len < 0) {
261 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
262 "extension header exceeds frame length\n");
267 if (payload_len < 0) {
268 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
272 payload_len -= payload[payload_len - 1];
273 if (payload_len < 0) {
274 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
275 "greater than payload\n");
280 switch (rtph->payload_type) {
282 we only support alaw and ulaw!
283 case RTP_PT_GSM_FULL:
284 if (payload_len != 33) {
285 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
286 "payload length != 33 (len = %d)\n",
292 if (payload_len != 31) {
293 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
294 "payload length != 31 (len = %d)\n",
299 case RTP_PT_GSM_HALF:
300 if (payload_len != 14) {
301 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
302 "payload length != 14 (len = %d)\n",
308 case PAYLOAD_TYPE_ALAW:
309 if (options.law != 'a') {
310 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
314 case PAYLOAD_TYPE_ULAW:
315 if (options.law == 'a') {
316 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
321 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
322 "type %d\n", rtph->payload_type);
326 if (payload_len <= 0) {
327 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
333 psip->record(payload, payload_len, 0); // from down
335 psip->tap(payload, payload_len, 0); // from down
340 if (psip->p_echotest) {
341 /* echo rtp data we just received */
342 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
346 *to++ = flip[*from++];
348 psip->dov_rx(payload, payload_len);
349 psip->bridge_tx(payload, payload_len);
354 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
356 class Psip *psip = (class Psip *) instance;
358 unsigned char buffer[256];
361 if ((what & LCR_FD_READ)) {
362 len = read(fd->fd, &buffer, sizeof(buffer));
364 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
365 // psip->rtp_close();
366 // psip->rtp_shutdown();
369 if (psip->p_s_rtp_is_connected)
370 rc = rtp_decode(psip, buffer, len);
376 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
378 // class Psip *psip = (class Psip *) instance;
380 unsigned char buffer[256];
382 if ((what & LCR_FD_READ)) {
383 len = read(fd->fd, &buffer, sizeof(buffer));
385 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
386 // psip->rtp_close();
387 // psip->rtp_shutdown();
390 PDEBUG(DEBUG_SIP, "rtcp!\n");
396 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
399 socklen_t alen = sizeof(*sin_local);
401 sin_local->sin_family = AF_INET;
402 sin_local->sin_addr.s_addr = htonl(ip);
403 sin_local->sin_port = htons(port);
405 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
409 /* retrieve the address we actually bound to, in case we
410 * passed INADDR_ANY as IP address */
411 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
414 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
417 socklen_t alen = sizeof(*sin_local);
419 sin_remote->sin_family = AF_INET;
420 sin_remote->sin_addr.s_addr = htonl(ip);
421 sin_remote->sin_port = htons(port);
423 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
425 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
429 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
432 int Psip::rtp_open(void)
434 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
436 // struct in_addr ia;
438 unsigned short start_port;
440 PDEBUG(DEBUG_SIP, "rtp_open\n");
443 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
449 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
451 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
457 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
460 ip = htonl(INADDR_ANY);
461 start_port = inst->next_rtp_port;
463 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, inst->next_rtp_port);
467 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, inst->next_rtp_port + 1);
469 p_s_rtp_port_local = inst->next_rtp_port;
470 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
473 /* reopen rtp socket and try again with next udp port */
474 unregister_fd(&p_s_rtp_fd);
475 close(p_s_rtp_fd.fd);
477 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
483 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
486 inst->next_rtp_port = (inst->next_rtp_port + 2 > inst->rtp_port_to) ? inst->rtp_port_from : inst->next_rtp_port + 2;
487 if (inst->next_rtp_port == start_port)
489 /* we must use rc2, in order to preserve rc */
492 PDEBUG(DEBUG_SIP, "failed to find port\n");
496 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
497 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
498 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
500 return p_s_rtp_port_local;
503 int Psip::rtp_connect(void)
508 ia.s_addr = htonl(p_s_rtp_ip_remote);
509 if (p_s_rtp_is_connected)
510 PDEBUG(DEBUG_SIP, "reconnecting existing RTP connection to new/same destination\n");
511 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
513 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);
517 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);
521 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
522 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
523 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
524 p_s_rtp_is_connected = 1;
528 void Psip::rtp_close(void)
530 if (p_s_rtp_fd.fd > 0) {
531 unregister_fd(&p_s_rtp_fd);
532 close(p_s_rtp_fd.fd);
535 if (p_s_rtcp_fd.fd > 0) {
536 unregister_fd(&p_s_rtcp_fd);
537 close(p_s_rtcp_fd.fd);
540 if (p_s_rtp_is_connected) {
541 PDEBUG(DEBUG_SIP, "rtp closed\n");
542 p_s_rtp_is_connected = 0;
547 void tv_difference(struct timeval *diff, const struct timeval *from,
548 const struct timeval *__to)
550 struct timeval _to = *__to, *to = &_to;
552 if (to->tv_usec < from->tv_usec) {
554 to->tv_usec += 1000000;
557 diff->tv_usec = to->tv_usec - from->tv_usec;
558 diff->tv_sec = to->tv_sec - from->tv_sec;
561 /* encode and send a rtp frame */
562 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
564 struct rtp_hdr *rtph;
566 int duration; /* in samples */
567 unsigned char buffer[256];
571 record(data, len, 1); // from up
573 tap(data, len, 1); // from up
575 if (!p_s_rtp_is_connected) {
580 if (!p_s_rtp_tx_action) {
581 /* initialize sequences */
582 p_s_rtp_tx_action = 1;
583 p_s_rtp_tx_ssrc = rand();
584 p_s_rtp_tx_sequence = random();
585 p_s_rtp_tx_timestamp = random();
586 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
589 switch (payload_type) {
591 we only support alaw and ulaw!
592 case RTP_PT_GSM_FULL:
600 case RTP_PT_GSM_HALF:
605 case PAYLOAD_TYPE_ALAW:
606 case PAYLOAD_TYPE_ULAW:
611 PERROR("unsupported message type %d\n", payload_type);
617 struct timeval tv, tv_diff;
618 long int usec_diff, frame_diff;
620 gettimeofday(&tv, NULL);
621 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
622 p_s_rtp_tx_last_tv = tv;
624 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
625 frame_diff = (usec_diff / 20000);
627 if (abs(frame_diff) > 1) {
628 long int frame_diff_excess = frame_diff - 1;
630 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
631 p_s_rtp_tx_sequence += frame_diff_excess;
632 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
637 rtph = (struct rtp_hdr *) buffer;
638 rtph->version = RTP_VERSION;
641 rtph->csrc_count = 0;
643 rtph->payload_type = payload_type;
644 rtph->sequence = htons(p_s_rtp_tx_sequence++);
645 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
646 p_s_rtp_tx_timestamp += duration;
647 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
648 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
650 if (p_s_rtp_fd.fd > 0) {
651 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
652 if (len != sizeof(struct rtp_hdr) + payload_len) {
653 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
663 /* receive from remote */
664 int Psip::bridge_rx(unsigned char *data, int len)
668 /* don't bridge, if tones are provided */
669 if (p_tone_name[0] || p_dov_tx)
675 if ((ret = Port::bridge_rx(data, len)))
678 /* write to rx buffer */
680 p_s_rxdata[p_s_rxpos++] = flip[*data++];
681 if (p_s_rxpos == 160) {
684 /* transmit data via rtp */
685 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
692 /* taken from freeswitch */
693 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
694 static int status2cause(int status)
698 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
704 return 21; //SWITCH_CAUSE_CALL_REJECTED;
706 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
709 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
712 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
714 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
723 return 127; //SWITCH_CAUSE_INTERWORKING;
725 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
730 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
733 return 17; //SWITCH_CAUSE_USER_BUSY;
735 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
738 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
740 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
742 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
746 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
749 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
751 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
753 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
757 static int cause2status(int cause, int location, const char **st)
763 s = 404; *st = sip_404_Not_found;
766 s = 404; *st = sip_404_Not_found;
769 s = 404; *st = sip_404_Not_found;
772 s = 486; *st = sip_486_Busy_here;
775 s = 408; *st = sip_408_Request_timeout;
778 s = 480; *st = sip_480_Temporarily_unavailable;
781 s = 480; *st = sip_480_Temporarily_unavailable;
784 if (location == LOCATION_USER) {
785 s = 603; *st = sip_603_Decline;
787 s = 403; *st = sip_403_Forbidden;
791 //s = 301; *st = sip_301_Moved_permanently;
792 s = 410; *st = sip_410_Gone;
795 s = 410; *st = sip_410_Gone;
798 s = 404; *st = sip_404_Not_found;
801 s = 502; *st = sip_502_Bad_gateway;
804 s = 484; *st = sip_484_Address_incomplete;
807 s = 501; *st = sip_501_Not_implemented;
810 s = 480; *st = sip_480_Temporarily_unavailable;
813 s = 503; *st = sip_503_Service_unavailable;
816 s = 503; *st = sip_503_Service_unavailable;
819 s = 503; *st = sip_503_Service_unavailable;
822 s = 503; *st = sip_503_Service_unavailable;
825 s = 503; *st = sip_503_Service_unavailable;
828 s = 403; *st = sip_403_Forbidden;
831 s = 403; *st = sip_403_Forbidden;
834 s = 503; *st = sip_503_Service_unavailable;
837 s = 488; *st = sip_488_Not_acceptable;
840 s = 501; *st = sip_501_Not_implemented;
843 s = 488; *st = sip_488_Not_acceptable;
846 s = 501; *st = sip_501_Not_implemented;
849 s = 403; *st = sip_403_Forbidden;
852 s = 503; *st = sip_503_Service_unavailable;
855 s = 504; *st = sip_504_Gateway_time_out;
858 s = 500; *st = sip_500_Internal_server_error;
861 s = 500; *st = sip_500_Internal_server_error;
864 s = 468; *st = sip_486_Busy_here;
870 /* use STUN ip, or return the ip without change */
871 unsigned int Psip::get_local_ip(unsigned int ip)
873 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
875 if (inst->public_ip[0]) {
876 PDEBUG(DEBUG_SIP, "RTP local IP is replaced by STUN ip %s\n", inst->public_ip);
877 inet_pton(AF_INET, inst->public_ip, &ip);
884 * endpoint sends messages to the SIP port
887 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
889 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
890 const char *sdp_str = NULL;
891 struct lcr_msg *message;
892 struct interface *interface;
894 unsigned char payload_type;
896 interface = getinterfacebyname(inst->interface_name);
898 PERROR("Cannot find interface %s.\n", inst->interface_name);
902 if (param->connectinfo.rtpinfo.port) {
903 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
905 media_type = param->connectinfo.rtpinfo.media_types[0];
906 payload_type = param->connectinfo.rtpinfo.payload_types[0];
907 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
908 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
909 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
910 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
911 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
913 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
914 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
915 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
916 /* open local RTP peer (if not bridging) */
917 if (rtp_connect() < 0) {
918 nua_cancel(p_s_handle, TAG_END());
919 nua_handle_destroy(p_s_handle);
921 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
922 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
924 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
925 message->param.disconnectinfo.cause = 41;
926 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
927 message_put(message);
928 new_state(PORT_STATE_RELEASE);
929 trigger_work(&p_s_delete);
934 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &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");
953 memset(&ia, 0, sizeof(ia));
954 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
955 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
956 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
957 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
963 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
965 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
966 struct lcr_msg *message;
967 char cause_str[128] = "";
968 int cause = param->disconnectinfo.cause;
969 int location = param->disconnectinfo.cause;
971 const char *status_text;
973 if (cause > 0 && cause <= 127) {
974 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
978 case PORT_STATE_OUT_SETUP:
979 case PORT_STATE_OUT_PROCEEDING:
980 case PORT_STATE_OUT_ALERTING:
981 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
982 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
984 add_trace("cause", "value", "%d", cause);
986 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
988 case PORT_STATE_IN_SETUP:
989 case PORT_STATE_IN_PROCEEDING:
990 case PORT_STATE_IN_ALERTING:
991 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
992 status = cause2status(cause, location, &status_text);
993 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
995 add_trace("cause", "value", "%d", cause);
996 add_trace("respond", "value", "%d %s", status, status_text);
998 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
999 nua_handle_destroy(p_s_handle);
1001 trigger_work(&p_s_delete);
1004 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
1005 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_OUT);
1007 add_trace("cause", "value", "%d", cause);
1009 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
1012 if (message_id == MESSAGE_DISCONNECT) {
1013 while(p_epointlist) {
1014 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1015 message->param.disconnectinfo.cause = CAUSE_NORMAL;
1016 message->param.disconnectinfo.location = LOCATION_BEYOND;
1017 message_put(message);
1019 free_epointlist(p_epointlist);
1023 new_state(PORT_STATE_RELEASE);
1028 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
1030 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1031 char from[128] = "";
1032 char asserted_id[128] = "", asserted_msg[256] = "";
1034 char contact[128] = "";
1035 const char *local = inst->local_peer;
1037 const char *remote = inst->remote_peer;
1038 const char *sdp_str = NULL;
1039 struct epoint_list *epointlist;
1040 sip_cseq_t *cseq = NULL;
1041 struct lcr_msg *message;
1042 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
1043 unsigned char lcr_payload = { (options.law=='a') ? (unsigned char )PAYLOAD_TYPE_ALAW : (unsigned char )PAYLOAD_TYPE_ULAW };
1045 unsigned char *payload_types;
1050 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1051 add_trace("failed", "reason", "No remote peer set or no peer has registered to us.");
1053 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1054 message->param.disconnectinfo.cause = 27;
1055 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1056 message_put(message);
1057 new_state(PORT_STATE_RELEASE);
1058 trigger_work(&p_s_delete);
1062 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
1064 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
1065 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
1066 // memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
1067 do_screen(1, p_callerinfo.id, sizeof(p_callerinfo.id), &p_callerinfo.ntype, &p_callerinfo.present, inst->interface_name);
1068 // do_screen(1, p_redirinfo.id, sizeof(p_redirinfo.id), &p_redirinfo.ntype, &p_redirinfo.present, inst->interface_name);
1070 if (param->setup.rtpinfo.port) {
1071 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1073 media_types = param->setup.rtpinfo.media_types;
1074 payload_types = param->setup.rtpinfo.payload_types;
1075 payloads = param->setup.rtpinfo.payloads;
1076 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
1077 p_s_rtp_port_local = param->setup.rtpinfo.port;
1078 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
1079 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
1081 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
1083 media_types = &lcr_media;
1084 payload_types = &lcr_payload;
1087 /* open local RTP peer (if not bridging) */
1088 if (rtp_open() < 0) {
1089 PERROR("Failed to open RTP sockets\n");
1090 /* send release message to endpoit */
1091 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1092 message->param.disconnectinfo.cause = 41;
1093 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1094 message_put(message);
1095 new_state(PORT_STATE_RELEASE);
1096 trigger_work(&p_s_delete);
1099 if (!p_s_rtp_ip_local) {
1102 /* extract IP from local peer */
1103 SCPY(local_ip, local);
1104 p = strchr(local_ip, ':');
1107 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1108 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1109 p_s_rtp_ip_local = ntohl(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);
1130 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, payloads, payload_types, media_types);
1131 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1133 SPRINT(from, "sip:%s@%s", p_callerinfo.id, remote);
1134 SPRINT(to, "sip:%s@%s", p_dialinginfo.id, remote);
1135 if (inst->asserted_id[0]) {
1136 SPRINT(asserted_id, "sip:%s@%s", inst->asserted_id, remote);
1137 SPRINT(asserted_msg, "P-Asserted-Identity: <%s>", asserted_id);
1139 if (inst->public_ip[0]) {
1141 SPRINT(contact, "sip:%s@%s", p_callerinfo.id, inst->public_ip);
1142 p = strchr(inst->local_peer, ':');
1147 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_OUT);
1148 add_trace("from", "uri", "%s", from);
1149 add_trace("to", "uri", "%s", to);
1151 add_trace("assert-id", "uri", "%s", asserted_id);
1153 memset(&ia, 0, sizeof(ia));
1154 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1155 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1156 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1157 for (i = 0; i < payloads; i++)
1158 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1161 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1163 nua_invite(p_s_handle,
1164 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1165 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1166 TAG_IF(asserted_msg[0], SIPTAG_HEADER_STR(asserted_msg)),
1167 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
1168 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1169 NUTAG_MEDIA_ENABLE(0),
1170 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1171 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1172 new_state(PORT_STATE_OUT_SETUP);
1174 p_s_invite_direction = DIRECTION_OUT;
1177 PDEBUG(DEBUG_SIP, "do overlap\n");
1178 new_state(PORT_STATE_OUT_OVERLAP);
1179 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1180 message_put(message);
1182 PDEBUG(DEBUG_SIP, "do proceeding\n");
1183 new_state(PORT_STATE_OUT_PROCEEDING);
1184 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1185 message_put(message);
1188 /* attach only if not already */
1189 epointlist = p_epointlist;
1191 if (epointlist->epoint_id == epoint_id)
1193 epointlist = epointlist->next;
1196 epointlist_new(epoint_id);
1201 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1204 // struct in_addr ia;
1206 switch (param->notifyinfo.notify) {
1207 case INFO_NOTIFY_REMOTE_HOLD:
1209 sdp_str = generate_sdp(0, 0, 0, NULL, NULL);
1212 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\r\n"
1214 "c=IN IP4 0.0.0.0\r\n"
1217 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1218 nua_info(p_s_handle,
1219 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1220 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1221 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1222 NUTAG_MEDIA_ENABLE(0),
1223 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1224 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1227 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1229 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1230 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1231 nua_info(p_s_handle,
1232 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1233 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1234 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1235 NUTAG_MEDIA_ENABLE(0),
1236 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1237 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1245 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1249 /* prepare DTMF info payload */
1255 /* start invite to handle DTMF */
1256 nua_info(p_s_handle,
1257 NUTAG_MEDIA_ENABLE(0),
1258 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1259 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1264 /* NOTE: incomplete and not working */
1265 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1269 /* prepare DTMF info payload */
1273 , param->information.id);
1275 /* start invite to handle DTMF */
1276 nua_info(p_s_handle,
1277 NUTAG_MEDIA_ENABLE(0),
1278 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1279 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1284 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1286 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1288 if (Port::message_epoint(epoint_id, message_id, param))
1291 switch(message_id) {
1292 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1293 if (p_state != PORT_STATE_IN_SETUP
1294 && p_state != PORT_STATE_IN_PROCEEDING)
1296 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1297 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1298 add_trace("respond", "value", "180 Ringing");
1300 new_state(PORT_STATE_IN_ALERTING);
1303 case MESSAGE_CONNECT: /* call is connected on LCR side */
1304 if (p_state != PORT_STATE_IN_SETUP
1305 && p_state != PORT_STATE_IN_PROCEEDING
1306 && p_state != PORT_STATE_IN_ALERTING)
1308 message_connect(epoint_id, message_id, param);
1311 case MESSAGE_DISCONNECT: /* call has been disconnected */
1312 case MESSAGE_RELEASE: /* call has been released */
1313 message_release(epoint_id, message_id, param);
1316 case MESSAGE_SETUP: /* dial-out command received from epoint */
1317 message_setup(epoint_id, message_id, param);
1320 case MESSAGE_INFORMATION: /* overlap dialing */
1321 if (p_state != PORT_STATE_OUT_OVERLAP)
1323 message_information(epoint_id, message_id, param);
1326 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1327 if (p_state == PORT_STATE_CONNECT)
1328 message_dtmf(epoint_id, message_id, param);
1329 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1330 if (p_state == PORT_STATE_CONNECT)
1331 message_notify(epoint_id, message_id, param);
1335 PDEBUG(DEBUG_SIP, "PORT(%s) SIP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1341 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)
1345 if (!sip->sip_payload) {
1346 PDEBUG(DEBUG_SIP, "no payload given\n");
1350 sdp_parser_t *parser;
1353 sdp_attribute_t *attr;
1355 sdp_connection_t *conn;
1357 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1359 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1363 if (!(sdp = sdp_session(parser))) {
1364 sdp_parser_free(parser);
1367 for (m = sdp->sdp_media; m; m = m->m_next) {
1368 if (m->m_proto != sdp_proto_rtp)
1370 if (m->m_type != sdp_media_audio)
1372 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1374 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1375 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1377 if (m->m_connections) {
1378 conn = m->m_connections;
1379 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1380 inet_pton(AF_INET, conn->c_address, ip);
1381 *ip = ntohl(p_s_rtp_ip_remote);
1383 char *p = sip->sip_payload->pl_data;
1386 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1387 p = strstr(p, "c=IN IP4 ");
1389 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1390 sdp_parser_free(parser);
1394 if ((p = strchr(addr, '\n'))) *p = '\0';
1395 if ((p = strchr(addr, '\r'))) *p = '\0';
1396 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1397 inet_pton(AF_INET, addr, ip);
1398 *ip = ntohl(p_s_rtp_ip_remote);
1400 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1403 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1404 /* append to payload list, if there is space */
1405 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1406 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1407 media_type = MEDIA_TYPE_ALAW;
1408 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1409 media_type = MEDIA_TYPE_ULAW;
1410 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1411 media_type = MEDIA_TYPE_GSM;
1412 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1413 media_type = MEDIA_TYPE_GSM_EFR;
1414 else if (!strcmp(map->rm_encoding, "AMR"))
1415 media_type = MEDIA_TYPE_AMR;
1416 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1417 media_type = MEDIA_TYPE_GSM_HR;
1418 if (media_type && *payloads <= max_payloads) {
1419 *payload_types++ = map->rm_pt;
1420 *media_types++ = media_type;
1426 sdp_parser_free(parser);
1431 const char *Psip::generate_sdp(unsigned int rtp_ip_local, unsigned short rtp_port_local, int payloads, unsigned char *payload_types, int *media_types)
1434 static char sdp_str[256], sub_str[128];
1437 memset(&ia, 0, sizeof(ia));
1438 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1441 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\r\n"
1444 "t=0 0\r\n", inet_ntoa(ia), inet_ntoa(ia));
1446 SPRINT(sub_str, "m=audio %d RTP/AVP", p_s_rtp_port_local);
1447 SCAT(sdp_str, sub_str);
1448 for (i = 0; i < payloads; i++) {
1449 SPRINT(sub_str, " %d", payload_types[i]);
1450 SCAT(sdp_str, sub_str);
1452 SCAT(sdp_str, "\r\n");
1453 for (i = 0; i < payloads; i++) {
1454 SPRINT(sub_str, "a=rtpmap:%d %s/8000\r\n", payload_types[i], media_type2name(media_types[i]));
1455 SCAT(sdp_str, sub_str);
1462 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[])
1464 sip_www_authenticate_t const *authenticate = NULL;
1465 char const *realm = NULL;
1466 char const *scheme = NULL;
1469 char authentication[256] = "";
1470 PDEBUG(DEBUG_SIP, "challenge order received\n");
1472 if (!inst->auth_user[0]) {
1473 PDEBUG(DEBUG_SIP, "No credentials available\n");
1474 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1475 add_trace("error", NULL, "There are no credentials given for interface");
1480 if (sip->sip_www_authenticate) {
1481 authenticate = sip->sip_www_authenticate;
1482 } else if (sip->sip_proxy_authenticate) {
1483 authenticate = sip->sip_proxy_authenticate;
1485 PDEBUG(DEBUG_SIP, "No authentication header found\n");
1486 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1487 add_trace("error", NULL, "Authentication method unknwon");
1492 scheme = (char const *) authenticate->au_scheme;
1493 if (authenticate->au_params) {
1494 for (i = 0; (cur = (char *) authenticate->au_params[i]); i++) {
1495 if ((realm = strstr(cur, "realm="))) {
1502 if (!scheme || !realm) {
1503 PDEBUG(DEBUG_SIP, "No scheme or no realm in authentication header found\n");
1504 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1505 add_trace("error", NULL, "Authentication header has no realm or scheme");
1510 SPRINT(authentication, "%s:%s:%s:%s", scheme, realm, inst->auth_user, inst->auth_password);
1511 PDEBUG(DEBUG_SIP, "auth: '%s'\n", authentication);
1513 sip_trace_header(psip, inst->interface_name, "AUTHENTICATE", DIRECTION_OUT);
1514 add_trace("scheme", NULL, "%s", scheme);
1515 add_trace("realm", NULL, "%s", realm);
1516 add_trace("user", NULL, "%s", inst->auth_user);
1517 add_trace("pass", NULL, "%s", inst->auth_password);
1520 nua_authenticate(nh, /*SIPTAG_EXPIRES_STR("3600"),*/ NUTAG_AUTH(authentication), TAG_END());
1525 static void i_options(struct sip_inst *inst, int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1527 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1528 nua_saved_event_t saved[1];
1529 nua_save_event(nua, saved);
1530 nua_event_data_t const *data = nua_event_data(saved);
1532 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_IN);
1535 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1536 add_trace("respond", "value", "200 OK");
1539 nua_respond(nh, SIP_200_OK, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1540 nua_handle_destroy(nh);
1541 inst->register_handle = NULL;
1544 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[])
1546 #define NUTAG_WITH_THIS_MSG(msg) nutag_with, tag_ptr_v(msg)
1547 nua_saved_event_t saved[1];
1548 sip_contact_t const *contact = NULL;
1549 contact = sip->sip_contact;
1550 nua_save_event(nua, saved);
1551 nua_event_data_t const *data = nua_event_data(saved);
1553 if (!inst->allow_register) {
1554 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1555 add_trace("error", NULL, "forbidden, because we don't accept registration");
1557 nua_respond(nh, SIP_403_FORBIDDEN, NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1558 nua_handle_destroy(nh);
1559 inst->register_handle = NULL;
1563 if (contact->m_url->url_host)
1564 SCPY(inst->remote_peer, contact->m_url->url_host);
1565 if (contact->m_url->url_port && contact->m_url->url_port[0]) {
1566 SCAT(inst->remote_peer, ":");
1567 SCAT(inst->remote_peer, contact->m_url->url_port);
1570 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_IN);
1571 add_trace("contact", "uri", "%s", inst->remote_peer);
1574 sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
1575 add_trace("respond", "value", "200 OK");
1576 add_trace("reason", NULL, "peer registered");
1579 nua_respond(nh, SIP_200_OK, SIPTAG_CONTACT(sip->sip_contact), NUTAG_WITH_THIS_MSG(data->e_msg), TAG_END());
1580 nua_handle_destroy(nh);
1581 inst->register_handle = NULL;
1584 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[])
1588 sip_trace_header(NULL, inst->interface_name, "STATUS", DIRECTION_IN);
1589 add_trace("value", NULL, "%d", status);
1590 add_trace("phrase", NULL, "%s", phrase);
1596 /* if not registered, become registered and start register interval timer */
1597 if (inst->register_state != REGISTER_STATE_REGISTERED) {
1598 if (inst->register_interval)
1599 schedule_timer(&inst->register_retry_timer, inst->register_interval, 0);
1600 inst->register_state = REGISTER_STATE_REGISTERED;
1602 /* start option timer */
1603 if (inst->options_interval)
1604 PDEBUG(DEBUG_SIP, "register ok, scheduling option timer with %d seconds\n", inst->options_interval);
1605 schedule_timer(&inst->register_option_timer, inst->options_interval, 0);
1609 PDEBUG(DEBUG_SIP, "Register challenge received\n");
1610 rc = challenge(inst, NULL, status, phrase, nua, magic, nh, hmagic, sip, tags);
1615 if (status >= 200 && status <= 299)
1620 PDEBUG(DEBUG_SIP, "Register failed, starting register timer\n");
1621 inst->register_state = REGISTER_STATE_FAILED;
1622 nua_handle_destroy(nh);
1623 inst->register_handle = NULL;
1624 /* stop option timer */
1625 unsched_timer(&inst->register_option_timer);
1626 /* if failed, start register interval timer with REGISTER_RETRY_TIMER */
1627 schedule_timer(&inst->register_retry_timer, REGISTER_RETRY_TIMER);
1631 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[])
1633 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1634 const char *from = "", *to = "", *name = "";
1637 class Endpoint *epoint;
1638 struct lcr_msg *message;
1639 struct interface *interface;
1640 const char *sdp_str = NULL;
1641 int media_types[32];
1642 uint8_t payload_types[32];
1644 unsigned char payload_type;
1647 interface = getinterfacebyname(inst->interface_name);
1649 PERROR("Cannot find interface %s.\n", inst->interface_name);
1653 if (sip->sip_from) {
1654 if (sip->sip_from->a_url)
1655 from = sip->sip_from->a_url->url_user;
1656 if (sip->sip_from->a_display) {
1657 name = sip->sip_from->a_display;
1658 if (!strncmp(name, "\"IMSI", 5)) {
1659 strncpy(imsi, name + 5, 15);
1666 if (sip->sip_to->a_url)
1667 to = sip->sip_to->a_url->url_user;
1669 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1671 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
1672 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1674 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1675 if (!p_s_rtp_bridge) {
1678 /* check if supported payload type exists */
1679 for (i = 0; i < payloads; i++) {
1680 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1683 if (i == payloads) {
1684 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1692 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1694 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1695 nua_handle_destroy(nh);
1697 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1699 add_trace("respond", "value", "415 Unsupported Media");
1701 add_trace("respond", "value", "400 Bad Request");
1702 add_trace("reason", NULL, "offered codec does not match");
1704 if (p_state != PORT_STATE_IDLE) {
1705 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1706 message->param.disconnectinfo.cause = 41;
1707 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1708 message_put(message);
1710 new_state(PORT_STATE_RELEASE);
1711 trigger_work(&p_s_delete);
1715 /* handle re-invite */
1716 if (p_state != PORT_STATE_IDLE) {
1717 sip_trace_header(this, inst->interface_name, "RE-INVITE", DIRECTION_IN);
1719 if (p_s_rtp_bridge) {
1720 PDEBUG(DEBUG_SIP, "RE-INVITE not implemented for RTP forwarding\n");
1721 nua_respond(nh, SIP_501_NOT_IMPLEMENTED, TAG_END());
1722 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1723 add_trace("respond", "value", "501 NOT IMPLEMENTED");
1724 add_trace("reason", NULL, "RE-INVITE not implemented for RTP forwarding");
1727 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
1728 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1729 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1730 if (rtp_connect() < 0) {
1733 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1734 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1735 nua_respond(p_s_handle, SIP_200_OK,
1736 NUTAG_MEDIA_ENABLE(0),
1737 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1738 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1743 /* open local RTP peer (if not bridging) */
1744 if (!p_s_rtp_bridge && rtp_open() < 0) {
1745 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1746 nua_handle_destroy(nh);
1748 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1749 add_trace("respond", "value", "500 Internal Server Error");
1750 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1752 new_state(PORT_STATE_RELEASE);
1753 trigger_work(&p_s_delete);
1758 // sip_trace_header(this, inst->interface_name, "NEW handle", DIRECTION_IN);
1759 // add_trace("handle", "new", "0x%x", nh);
1764 sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
1765 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1766 /* caller information */
1768 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1769 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1770 add_trace("calling", "present", "unavailable");
1772 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1773 add_trace("calling", "present", "allowed");
1774 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1775 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1776 SCPY(p_callerinfo.id, from);
1777 add_trace("calling", "number", "%s", from);
1778 SCPY(p_callerinfo.name, name);
1780 add_trace("calling", "name", "%s", name);
1781 SCPY(p_callerinfo.imsi, imsi);
1783 add_trace("calling", "imsi", "%s", imsi);
1785 SCPY(p_callerinfo.interface, inst->interface_name);
1786 /* dialing information */
1788 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1789 SCAT(p_dialinginfo.id, to);
1790 add_trace("dialing", "number", "%s", to);
1793 /* bearer capability */
1794 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1795 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1796 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1797 add_trace("bearer", "capa", "speech");
1798 add_trace("bearer", "mode", "circuit");
1799 /* if packet mode works some day, see dss1.cpp for conditions */
1800 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1804 /* create endpoint */
1806 FATAL("Incoming call but already got an endpoint.\n");
1807 if (!(epoint = new Endpoint(p_serial, 0)))
1808 FATAL("No memory for Endpoint instance\n");
1809 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1810 epointlist_new(epoint->ep_serial);
1812 #ifdef NUTAG_AUTO100
1813 /* send trying (proceeding) */
1814 nua_respond(nh, SIP_100_TRYING, TAG_END());
1815 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1816 add_trace("respond", "value", "100 Trying");
1820 new_state(PORT_STATE_IN_PROCEEDING);
1822 /* send setup message to endpoit */
1823 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1824 message->param.setup.port_type = p_type;
1825 // message->param.setup.dtmf = 0;
1826 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1827 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1828 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1829 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1830 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1831 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1832 if (p_s_rtp_bridge) {
1835 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1836 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1837 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1838 /* add codecs to setup message */
1839 for (i = 0; i < payloads; i++) {
1840 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1841 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1842 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1845 message->param.setup.rtpinfo.payloads = i;
1847 message_put(message);
1849 /* start option timer */
1850 if (inst->options_interval) {
1851 PDEBUG(DEBUG_SIP, "Invite received, scheduling option timer with %d seconds\n", inst->options_interval);
1852 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
1855 p_s_invite_direction = DIRECTION_IN;
1857 /* send progress, if tones are available and if we don't bridge */
1858 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1859 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1860 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1861 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1862 /* open local RTP peer (if not bridging) */
1863 if (rtp_connect() < 0) {
1865 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1866 nua_handle_destroy(nh);
1868 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1869 add_trace("respond", "value", "500 Internal Server Error");
1870 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1872 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1873 message->param.disconnectinfo.cause = 41;
1874 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1875 message_put(message);
1876 new_state(PORT_STATE_RELEASE);
1877 trigger_work(&p_s_delete);
1881 sdp_str = generate_sdp(p_s_rtp_ip_local, p_s_rtp_port_local, 1, &payload_type, &media_type);
1882 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1884 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1885 NUTAG_MEDIA_ENABLE(0),
1886 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1887 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1888 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1889 add_trace("respond", "value", "183 SESSION PROGRESS");
1890 add_trace("reason", NULL, "audio available");
1892 memset(&ia, 0, sizeof(ia));
1893 ia.s_addr = htonl(get_local_ip(p_s_rtp_ip_local));
1894 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1895 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1896 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1901 void Psip::i_options(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
1903 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1905 PDEBUG(DEBUG_SIP, "options received\n");
1907 sip_trace_header(this, inst->interface_name, "OPTIONS", DIRECTION_IN);
1910 nua_respond(nh, SIP_200_OK, TAG_END());
1913 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[])
1915 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1916 struct lcr_msg *message;
1919 PDEBUG(DEBUG_SIP, "bye received\n");
1921 sip_trace_header(this, inst->interface_name, "BYE", DIRECTION_IN);
1922 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1923 cause = atoi(sip->sip_reason->re_cause);
1924 add_trace("cause", "value", "%d", cause);
1928 // let stack do bye automaticall, since it will not accept our response for some reason
1929 // nua_respond(nh, SIP_200_OK, TAG_END());
1930 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
1931 add_trace("respond", "value", "200 OK");
1933 // nua_handle_destroy(nh);
1938 while(p_epointlist) {
1939 /* send setup message to endpoit */
1940 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1941 message->param.disconnectinfo.cause = cause ? : 16;
1942 message->param.disconnectinfo.location = LOCATION_BEYOND;
1943 message_put(message);
1945 free_epointlist(p_epointlist);
1947 new_state(PORT_STATE_RELEASE);
1948 trigger_work(&p_s_delete);
1951 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[])
1953 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1954 struct lcr_msg *message;
1956 PDEBUG(DEBUG_SIP, "cancel received\n");
1958 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_IN);
1961 nua_handle_destroy(nh);
1966 while(p_epointlist) {
1967 /* send setup message to endpoit */
1968 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1969 message->param.disconnectinfo.cause = 16;
1970 message->param.disconnectinfo.location = LOCATION_BEYOND;
1971 message_put(message);
1973 free_epointlist(p_epointlist);
1975 new_state(PORT_STATE_RELEASE);
1976 trigger_work(&p_s_delete);
1979 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[])
1981 PDEBUG(DEBUG_SIP, "bye response received\n");
1983 nua_handle_destroy(nh);
1988 trigger_work(&p_s_delete);
1991 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[])
1993 PDEBUG(DEBUG_SIP, "cancel response received\n");
1995 nua_handle_destroy(nh);
2000 trigger_work(&p_s_delete);
2003 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[])
2005 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2006 struct lcr_msg *message;
2007 int cause = 0, location = 0;
2008 int media_types[32];
2009 uint8_t payload_types[32];
2012 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
2014 sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
2015 add_trace("respond", "value", "%d", status);
2019 if (status == 183 || (status >= 200 && status <= 299)) {
2022 sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
2023 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
2027 else if (!p_s_rtp_bridge) {
2028 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
2029 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
2036 nua_cancel(nh, TAG_END());
2037 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2038 add_trace("reason", NULL, "accepted codec does not match");
2041 location = LOCATION_PRIVATE_LOCAL;
2042 goto release_with_cause;
2045 /* connect to remote RTP (if not bridging) */
2046 if (!p_s_rtp_bridge && rtp_connect() < 0) {
2047 nua_cancel(nh, TAG_END());
2048 sip_trace_header(this, inst->interface_name, "CANCEL", DIRECTION_OUT);
2049 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
2052 location = LOCATION_PRIVATE_LOCAL;
2053 goto release_with_cause;
2057 /* start option timer */
2058 if (inst->options_interval) {
2059 PDEBUG(DEBUG_SIP, "Invite response, scheduling option timer with %d seconds\n", inst->options_interval);
2060 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2066 PDEBUG(DEBUG_SIP, "do proceeding\n");
2067 new_state(PORT_STATE_OUT_PROCEEDING);
2068 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
2069 message_put(message);
2073 PDEBUG(DEBUG_SIP, "do alerting\n");
2074 new_state(PORT_STATE_OUT_ALERTING);
2075 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
2076 message_put(message);
2079 PDEBUG(DEBUG_SIP, "do progress\n");
2080 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
2081 message->param.progressinfo.progress = 8;
2082 message->param.progressinfo.location = 10;
2083 if (p_s_rtp_bridge) {
2084 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2085 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
2086 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
2087 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
2088 message->param.progressinfo.rtpinfo.payloads = 1;
2090 message_put(message);
2094 PDEBUG(DEBUG_SIP, "do connect\n");
2095 nua_ack(nh, TAG_END());
2096 new_state(PORT_STATE_CONNECT);
2097 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
2098 if (p_s_rtp_bridge) {
2099 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
2100 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
2101 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
2102 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
2103 message->param.connectinfo.rtpinfo.payloads = 1;
2105 message_put(message);
2108 if (status >= 200 && status <= 299)
2110 if (status < 100 || status > 199)
2112 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
2117 cause = status2cause(status);
2118 location = LOCATION_BEYOND;
2121 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
2123 while(p_epointlist) {
2124 /* send setup message to endpoit */
2125 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2126 message->param.disconnectinfo.cause = cause;
2127 message->param.disconnectinfo.location = location;
2128 message_put(message);
2130 free_epointlist(p_epointlist);
2133 new_state(PORT_STATE_RELEASE);
2137 trigger_work(&p_s_delete);
2140 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[])
2142 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2143 int cause = 0, location = 0;
2144 struct lcr_msg *message;
2146 PDEBUG(DEBUG_SIP, "options result %d received\n", status);
2148 if (status >= 200 && status <= 299) {
2149 PDEBUG(DEBUG_SIP, "options ok, scheduling option timer with %d seconds\n", inst->options_interval);
2150 /* restart option timer */
2151 schedule_timer(&p_s_invite_option_timer, inst->options_interval, 0);
2155 nua_handle_destroy(nh);
2160 cause = status2cause(status);
2161 location = LOCATION_BEYOND;
2163 while(p_epointlist) {
2164 /* send setup message to endpoit */
2165 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2166 message->param.disconnectinfo.cause = cause;
2167 message->param.disconnectinfo.location = location;
2168 message_put(message);
2170 free_epointlist(p_epointlist);
2172 new_state(PORT_STATE_RELEASE);
2173 trigger_work(&p_s_delete);
2176 void Psip::i_state(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tags[])
2178 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2180 PDEBUG(DEBUG_SIP, "state change received\n");
2181 sip_trace_header(this, inst->interface_name, "STATUS", DIRECTION_OUT);
2182 add_trace("value", NULL, "%d", status);
2183 add_trace("phrase", NULL, "%s", phrase);
2187 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[])
2189 struct sip_inst *inst = (struct sip_inst *) magic;
2191 class Psip *psip = NULL;
2194 PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
2198 /* hunt for existing handles */
2201 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
2202 psip = (class Psip *)port;
2203 if (psip->p_s_handle == nh) {
2204 PDEBUG(DEBUG_SIP, "Event found for port %s\n", psip->p_name);
2216 if (!inst->register_handle) {
2217 PDEBUG(DEBUG_SIP, "New options instance\n");
2218 inst->register_handle = nh;
2221 case nua_i_register:
2222 if (!inst->register_handle) {
2223 PDEBUG(DEBUG_SIP, "New register instance\n");
2224 inst->register_handle = nh;
2230 struct interface *interface = interface_first;
2232 PDEBUG(DEBUG_SIP, "New psip instance\n");
2234 /* create call instance */
2235 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
2237 if (!strcmp(interface->name, inst->interface_name))
2239 interface = interface->next;
2242 FATAL("Cannot find interface %s.\n", inst->interface_name);
2243 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
2244 FATAL("Cannot create Port instance.\n");
2248 if (!psip && !inst->register_handle) {
2249 PDEBUG(DEBUG_SIP, "Destroying unknown instance\n");
2250 nua_handle_destroy(nh);
2255 /* handle register process */
2256 if (inst->register_handle == nh) {
2259 i_options(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2261 case nua_i_register:
2262 i_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2264 case nua_r_register:
2265 r_register(inst, status, phrase, nua, magic, nh, hmagic, sip, tags);
2268 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2273 /* handle port process */
2275 PERROR("no SIP Port found for handel %p\n", nh);
2276 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
2277 nua_handle_destroy(nh);
2284 rc = challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
2290 case nua_r_set_params:
2291 PDEBUG(DEBUG_SIP, "setparam response\n");
2294 psip->r_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2297 PDEBUG(DEBUG_SIP, "error received\n");
2300 psip->i_state(status, phrase, nua, magic, nh, hmagic, sip, tags);
2303 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2306 PDEBUG(DEBUG_SIP, "ack received\n");
2309 PDEBUG(DEBUG_SIP, "active received\n");
2312 psip->i_options(status, phrase, nua, magic, nh, hmagic, sip, tags);
2315 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2318 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2321 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
2324 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
2327 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
2329 case nua_i_terminated:
2330 PDEBUG(DEBUG_SIP, "terminated received\n");
2333 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
2337 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)
2339 struct sip_inst *inst = (struct sip_inst *) magic;
2343 PDEBUG(DEBUG_SIP, "Event %d from STUN stack received\n", event);
2346 case stun_discovery_done:
2347 addrlen = sizeof(sa);
2348 memset(&sa, 0, addrlen);
2349 if (stun_discovery_get_address(sd, &sa, &addrlen) < 0) {
2350 PDEBUG(DEBUG_SIP, "stun_discovery_get_address failed\n");
2353 su_inet_ntop(sa.su_family, SU_ADDR(&sa), inst->public_ip, sizeof(inst->public_ip));
2354 inst->stun_state = STUN_STATE_RESOLVED;
2355 /* start timer for next stun request with inst->stun_interval */
2356 schedule_timer(&inst->stun_retry_timer, inst->stun_interval, 0);
2357 sip_trace_header(NULL, inst->interface_name, "STUN resolved", DIRECTION_OUT);
2358 add_trace("ip", "addr", "%s", inst->public_ip);
2363 PDEBUG(DEBUG_SIP, "STUN failed, starting timer\n");
2364 inst->stun_state = STUN_STATE_FAILED;
2365 /* start timer for next stun request (after failing) with STUN_RETRY_TIMER */
2366 schedule_timer(&inst->stun_retry_timer, STUN_RETRY_TIMER);
2367 sip_trace_header(NULL, inst->interface_name, "STUN failed", DIRECTION_OUT);
2372 /* received shutdown due to termination of RTP */
2373 void Psip::rtp_shutdown(void)
2375 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
2376 struct lcr_msg *message;
2378 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
2380 sip_trace_header(this, inst->interface_name, "RTP terminated", DIRECTION_IN);
2383 nua_handle_destroy(p_s_handle);
2386 while(p_epointlist) {
2387 /* send setup message to endpoit */
2388 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
2389 message->param.disconnectinfo.cause = 16;
2390 message->param.disconnectinfo.location = LOCATION_BEYOND;
2391 message_put(message);
2393 free_epointlist(p_epointlist);
2395 new_state(PORT_STATE_RELEASE);
2396 trigger_work(&p_s_delete);
2399 static int invite_option_timer(struct lcr_timer *timer, void *instance, int index)
2401 class Psip *psip = (class Psip *)instance;
2402 struct sip_inst *inst = (struct sip_inst *) psip->p_s_sip_inst;
2404 sip_trace_header(psip, inst->interface_name, "OPTIONS", psip->p_s_invite_direction);
2407 nua_options(psip->p_s_handle,
2413 static int stun_retry_timer(struct lcr_timer *timer, void *instance, int index)
2415 struct sip_inst *inst = (struct sip_inst *)instance;
2417 PDEBUG(DEBUG_SIP, "timeout, restart stun lookup\n");
2418 inst->stun_state = STUN_STATE_UNRESOLVED;
2423 static int register_retry_timer(struct lcr_timer *timer, void *instance, int index)
2425 struct sip_inst *inst = (struct sip_inst *)instance;
2427 PDEBUG(DEBUG_SIP, "timeout, restart register\n");
2428 /* if we have a handle, destroy it and becom unregistered, so registration is
2430 if (inst->register_handle) {
2431 /* stop option timer */
2432 unsched_timer(&inst->register_option_timer);
2433 nua_handle_destroy(inst->register_handle);
2434 inst->register_handle = NULL;
2436 inst->register_state = REGISTER_STATE_UNREGISTERED;
2441 static int register_option_timer(struct lcr_timer *timer, void *instance, int index)
2443 struct sip_inst *inst = (struct sip_inst *)instance;
2444 sip_trace_header(NULL, inst->interface_name, "OPTIONS", DIRECTION_OUT);
2447 nua_options(inst->register_handle,
2453 int sip_init_inst(struct interface *interface)
2455 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
2458 interface->sip_inst = inst;
2459 SCPY(inst->interface_name, interface->name);
2460 SCPY(inst->local_peer, interface->sip_local_peer);
2461 SCPY(inst->remote_peer, interface->sip_remote_peer);
2462 if (!inst->remote_peer[0])
2463 inst->allow_register = 1;
2464 SCPY(inst->asserted_id, interface->sip_asserted_id);
2465 if (interface->sip_register) {
2466 inst->register_state = REGISTER_STATE_UNREGISTERED;
2467 SCPY(inst->register_user, interface->sip_register_user);
2468 SCPY(inst->register_host, interface->sip_register_host);
2470 SCPY(inst->auth_user, interface->sip_auth_user);
2471 SCPY(inst->auth_password, interface->sip_auth_password);
2472 inst->register_interval = interface->sip_register_interval;
2473 inst->options_interval = interface->sip_options_interval;
2475 inst->rtp_port_from = interface->rtp_port_from;
2476 inst->rtp_port_to = interface->rtp_port_to;
2477 if (!inst->rtp_port_from || !inst->rtp_port_to) {
2478 inst->rtp_port_from = RTP_PORT_BASE;
2479 inst->rtp_port_to = RTP_PORT_MAX;
2481 inst->next_rtp_port = inst->rtp_port_from;
2484 memset(&inst->stun_retry_timer, 0, sizeof(inst->stun_retry_timer));
2485 add_timer(&inst->stun_retry_timer, stun_retry_timer, inst, 0);
2486 memset(&inst->register_retry_timer, 0, sizeof(inst->register_retry_timer));
2487 add_timer(&inst->register_retry_timer, register_retry_timer, inst, 0);
2488 memset(&inst->register_option_timer, 0, sizeof(inst->register_option_timer));
2489 add_timer(&inst->register_option_timer, register_option_timer, inst, 0);
2491 /* init root object */
2492 inst->root = su_root_create(inst);
2494 PERROR("Failed to create SIP root\n");
2495 sip_exit_inst(interface);
2499 SPRINT(local, "sip:%s",inst->local_peer);
2500 if (!strchr(inst->local_peer, ':'))
2501 SCAT(local, ":5060");
2502 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
2504 PERROR("Failed to create SIP stack object\n");
2505 sip_exit_inst(interface);
2508 nua_set_params(inst->nua,
2509 SIPTAG_ALLOW_STR("REGISTER,INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
2510 NUTAG_APPL_METHOD("REGISTER"),
2511 NUTAG_APPL_METHOD("INVITE"),
2512 NUTAG_APPL_METHOD("ACK"),
2513 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
2514 NUTAG_APPL_METHOD("CANCEL"),
2515 NUTAG_APPL_METHOD("OPTIONS"),
2516 NUTAG_APPL_METHOD("NOTIFY"),
2517 NUTAG_APPL_METHOD("INFO"),
2519 #ifdef NUTAG_AUTO100
2523 NUTAG_AUTOANSWER(0),
2526 SCPY(inst->public_ip, interface->sip_public_ip);
2527 if (interface->sip_stun_server[0]) {
2528 SCPY(inst->stun_server, interface->sip_stun_server);
2529 inst->stun_interval = interface->sip_stun_interval;
2530 inst->stun_handle = stun_handle_init(inst->root,
2531 STUNTAG_SERVER(inst->stun_server),
2533 if (!inst->stun_handle) {
2534 PERROR("Failed to create STUN handle\n");
2535 sip_exit_inst(interface);
2538 inst->stun_socket = su_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
2539 if (inst->stun_socket < 0) {
2540 PERROR("Failed to create STUN socket\n");
2541 sip_exit_inst(interface);
2544 inst->stun_state = STUN_STATE_UNRESOLVED;
2547 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
2549 any_sip_interface = 1;
2554 void sip_exit_inst(struct interface *interface)
2556 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
2560 del_timer(&inst->stun_retry_timer);
2561 del_timer(&inst->register_retry_timer);
2562 del_timer(&inst->register_option_timer);
2563 if (inst->stun_socket)
2564 su_close(inst->stun_socket);
2565 if (inst->stun_handle)
2566 stun_handle_destroy(inst->stun_handle);
2567 if (inst->register_handle)
2568 nua_handle_destroy(inst->register_handle);
2570 su_root_destroy(inst->root);
2572 nua_destroy(inst->nua);
2573 FREE(inst, sizeof(*inst));
2574 interface->sip_inst = NULL;
2576 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
2578 /* check if there is any other SIP interface left */
2579 interface = interface_first;
2581 if (interface->sip_inst)
2583 interface = interface->next;
2586 any_sip_interface = 0;
2589 extern su_log_t su_log_default[];
2590 extern su_log_t nua_log[];
2596 /* init SOFIA lib */
2598 su_home_init(sip_home);
2600 if (options.deb & DEBUG_SIP) {
2601 su_log_set_level(su_log_default, 9);
2602 su_log_set_level(nua_log, 9);
2603 //su_log_set_level(soa_log, 9);
2606 for (i = 0; i < 256; i++)
2607 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);
2609 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2616 su_home_deinit(sip_home);
2619 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2622 static void sip_handle_stun(struct sip_inst *inst)
2626 switch (inst->stun_state) {
2627 case STUN_STATE_UNRESOLVED:
2628 PDEBUG(DEBUG_SIP, "Trying to to get local IP from stun server\n");
2629 rc = stun_bind(inst->stun_handle, stun_bind_cb, (stun_discovery_magic_t *)inst,
2630 STUNTAG_SOCKET(inst->stun_socket),
2631 STUNTAG_REGISTER_EVENTS(1),
2634 PERROR("Failed to call stun_bind()\n");
2635 inst->stun_state = STUN_STATE_FAILED;
2638 inst->stun_state = STUN_STATE_RESOLVING;
2639 sip_trace_header(NULL, inst->interface_name, "STUN resolving", DIRECTION_OUT);
2640 add_trace("server", "addr", "%s", inst->stun_server);
2646 static void sip_handle_register(struct sip_inst *inst)
2648 char from[128] = "";
2650 char contact[128] = "";
2652 switch (inst->register_state) {
2653 case REGISTER_STATE_UNREGISTERED:
2654 /* wait for resoved stun */
2655 if (inst->stun_handle && inst->stun_state != STUN_STATE_RESOLVED)
2658 PDEBUG(DEBUG_SIP, "Registering to peer\n");
2659 inst->register_handle = nua_handle(inst->nua, NULL, TAG_END());
2660 if (!inst->register_handle) {
2661 PERROR("Failed to create handle\n");
2662 inst->register_state = REGISTER_STATE_FAILED;
2665 /* apply handle to trace */
2666 // sip_trace_header(NULL, inst->interface_name, "NEW handle", DIRECTION_NONE);
2667 // add_trace("handle", "new", "0x%x", inst->register_handle);
2670 SPRINT(from, "sip:%s@%s", inst->register_user, inst->register_host);
2671 SPRINT(to, "sip:%s@%s", inst->register_user, inst->register_host);
2672 if (inst->public_ip[0]) {
2674 SPRINT(contact, "sip:%s@%s", inst->register_user, inst->public_ip);
2675 p = strchr(inst->local_peer, ':');
2680 sip_trace_header(NULL, inst->interface_name, "REGISTER", DIRECTION_OUT);
2681 add_trace("from", "uri", "%s", from);
2682 add_trace("to", "uri", "%s", to);
2685 nua_register(inst->register_handle,
2686 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
2687 TAG_IF(to[0], SIPTAG_TO_STR(to)),
2688 TAG_IF(contact[0], SIPTAG_CONTACT_STR(contact)),
2691 inst->register_state = REGISTER_STATE_REGISTERING;
2698 void sip_handle(void)
2700 struct interface *interface = interface_first;
2701 struct sip_inst *inst;
2704 if (interface->sip_inst) {
2705 inst = (struct sip_inst *) interface->sip_inst;
2706 su_root_step(inst->root, 0);
2707 sip_handle_stun(inst);
2708 sip_handle_register(inst);
2710 interface = interface->next;
2714 /* deletes when back in event loop */
2715 static int delete_event(struct lcr_work *work, void *instance, int index)
2717 class Psip *psip = (class Psip *)instance;
2726 * generate audio, if no data is received from bridge
2729 void Psip::set_tone(const char *dir, const char *tone)
2731 Port::set_tone(dir, tone);
2736 void Psip::update_load(void)
2738 /* don't trigger load event if event already active */
2739 if (p_s_load_timer.active)
2742 /* don't start timer if ... */
2743 if (!p_tone_name[0] && !p_dov_tx)
2746 p_s_next_tv_sec = 0;
2747 schedule_timer(&p_s_load_timer, 0, 0); /* no delay the first time */
2750 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2752 class Psip *psip = (class Psip *)instance;
2754 /* stop timer if ... */
2755 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2763 #define SEND_SIP_LEN 160
2765 void Psip::load_tx(void)
2768 struct timeval current_time;
2769 int tosend = SEND_SIP_LEN, i;
2770 unsigned char buf[SEND_SIP_LEN], *p = buf;
2773 gettimeofday(¤t_time, NULL);
2774 if (!p_s_next_tv_sec) {
2775 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2776 p_s_next_tv_sec = current_time.tv_sec;
2777 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2778 if (p_s_next_tv_usec >= 1000000) {
2779 p_s_next_tv_usec -= 1000000;
2782 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125);
2784 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2785 + (current_time.tv_usec - p_s_next_tv_usec);
2786 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2787 /* if clock drifts too much, set next timeout event to current timer + 160 */
2789 p_s_next_tv_sec = current_time.tv_sec;
2790 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2791 if (p_s_next_tv_usec >= 1000000) {
2792 p_s_next_tv_usec -= 1000000;
2796 /* if diff is positive, it took too long, so next timeout will be earlier */
2797 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2798 if (p_s_next_tv_usec >= 1000000) {
2799 p_s_next_tv_usec -= 1000000;
2803 schedule_timer(&p_s_load_timer, 0, SEND_SIP_LEN * 125 - diff);
2807 if (p_tone_name[0]) {
2808 tosend -= read_audio(p, tosend);
2811 tosend -= dov_tx(p, tosend);
2814 PERROR("buffer is not completely filled\n");
2819 for (i = 0; i < SEND_SIP_LEN; i++) {
2823 /* transmit data via rtp */
2824 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);