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>
18 #ifndef SOFIA_SIP_GCC_4_8_PATCH_APLLIED
19 #warning ********************************************************
20 #warning Please apply the sofia-sip-gcc-4.8.patch !
21 #warning If this issue is already fixed, just remove this check.
22 #warning ********************************************************
28 unsigned char flip[256];
30 int any_sip_interface = 0;
32 //pthread_mutex_t mutex_msg;
33 su_home_t sip_home[1];
36 char interface_name[64];
43 static int delete_event(struct lcr_work *work, void *instance, int index);
44 static int load_timer(struct lcr_timer *timer, void *instance, int index);
49 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
52 if (interface->rtp_bridge)
54 p_s_sip_inst = interface->sip_inst;
55 memset(&p_s_delete, 0, sizeof(p_s_delete));
56 add_work(&p_s_delete, delete_event, this, 0);
59 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
60 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
61 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
62 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
63 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
64 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
66 p_s_rtp_ip_remote = 0;
67 p_s_rtp_port_local = 0;
68 p_s_rtp_port_remote = 0;
73 p_s_rtp_tx_action = 0;
76 memset(&p_s_loadtimer, 0, sizeof(p_s_loadtimer));
77 add_timer(&p_s_loadtimer, load_timer, this, 0);
80 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
82 FATAL("No SIP instance for interface\n");
91 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
93 del_timer(&p_s_loadtimer);
94 del_work(&p_s_delete);
99 static const char *media_type2name(uint8_t media_type) {
100 switch (media_type) {
101 case MEDIA_TYPE_ULAW:
103 case MEDIA_TYPE_ALAW:
107 case MEDIA_TYPE_GSM_HR:
109 case MEDIA_TYPE_GSM_EFR:
118 static void sip_trace_header(class Psip *sip, const char *message, int direction)
120 /* init trace with given values */
123 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
124 sip?sip->p_dialinginfo.id:NULL,
135 /* according to RFC 3550 */
137 #if __BYTE_ORDER == __LITTLE_ENDIAN
138 uint8_t csrc_count:4,
142 uint8_t payload_type:7,
144 #elif __BYTE_ORDER == __BIG_ENDIAN
155 } __attribute__((packed));
160 } __attribute__((packed));
162 #define RTP_VERSION 2
164 #define PAYLOAD_TYPE_ULAW 0
165 #define PAYLOAD_TYPE_ALAW 8
166 #define PAYLOAD_TYPE_GSM 3
168 /* decode an rtp frame */
169 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
171 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
172 struct rtp_x_hdr *rtpxh;
176 unsigned char *from, *to;
180 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
183 if (rtph->version != RTP_VERSION) {
184 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
187 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
188 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
189 if (payload_len < 0) {
190 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
191 "csrc count = %d)\n", len, rtph->csrc_count);
194 if (rtph->extension) {
195 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
196 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
197 "extension header\n");
200 rtpxh = (struct rtp_x_hdr *)payload;
201 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
203 payload_len -= x_len;
204 if (payload_len < 0) {
205 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
206 "extension header exceeds frame length\n");
211 if (payload_len < 0) {
212 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
216 payload_len -= payload[payload_len - 1];
217 if (payload_len < 0) {
218 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
219 "greater than payload\n");
224 switch (rtph->payload_type) {
226 we only support alaw and ulaw!
227 case RTP_PT_GSM_FULL:
228 if (payload_len != 33) {
229 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
230 "payload length != 33 (len = %d)\n",
236 if (payload_len != 31) {
237 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
238 "payload length != 31 (len = %d)\n",
243 case RTP_PT_GSM_HALF:
244 if (payload_len != 14) {
245 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
246 "payload length != 14 (len = %d)\n",
252 case PAYLOAD_TYPE_ALAW:
253 if (options.law != 'a') {
254 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
258 case PAYLOAD_TYPE_ULAW:
259 if (options.law == 'a') {
260 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
265 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
266 "type %d\n", rtph->payload_type);
270 if (payload_len <= 0) {
271 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
277 psip->record(payload, payload_len, 0); // from down
279 psip->tap(payload, payload_len, 0); // from down
284 if (psip->p_echotest) {
285 /* echo rtp data we just received */
286 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
290 *to++ = flip[*from++];
292 psip->dov_rx(payload, payload_len);
293 psip->bridge_tx(payload, payload_len);
298 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
300 class Psip *psip = (class Psip *) instance;
302 unsigned char buffer[256];
305 if ((what & LCR_FD_READ)) {
306 len = read(fd->fd, &buffer, sizeof(buffer));
308 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
309 // psip->rtp_close();
310 // psip->rtp_shutdown();
313 if (psip->p_s_rtp_is_connected)
314 rc = rtp_decode(psip, buffer, len);
320 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
322 // class Psip *psip = (class Psip *) instance;
324 unsigned char buffer[256];
326 if ((what & LCR_FD_READ)) {
327 len = read(fd->fd, &buffer, sizeof(buffer));
329 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
330 // psip->rtp_close();
331 // psip->rtp_shutdown();
334 PDEBUG(DEBUG_SIP, "rtcp!\n");
340 #define RTP_PORT_BASE 30000
341 #define RTP_PORT_MAX 39998
342 static unsigned short next_udp_port = RTP_PORT_BASE;
344 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
347 socklen_t alen = sizeof(*sin_local);
349 sin_local->sin_family = AF_INET;
350 sin_local->sin_addr.s_addr = htonl(ip);
351 sin_local->sin_port = htons(port);
353 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
357 /* retrieve the address we actually bound to, in case we
358 * passed INADDR_ANY as IP address */
359 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
362 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
365 socklen_t alen = sizeof(*sin_local);
367 sin_remote->sin_family = AF_INET;
368 sin_remote->sin_addr.s_addr = htonl(ip);
369 sin_remote->sin_port = htons(port);
371 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
373 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
377 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
380 int Psip::rtp_open(void)
385 unsigned short start_port;
388 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
394 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
396 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
402 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
405 ip = htonl(INADDR_ANY);
407 start_port = next_udp_port;
409 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
413 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port + 1);
415 p_s_rtp_port_local = next_udp_port;
416 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
419 /* reopen rtp socket and try again with next udp port */
420 unregister_fd(&p_s_rtp_fd);
421 close(p_s_rtp_fd.fd);
423 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
429 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
432 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
433 if (next_udp_port == start_port)
435 /* we must use rc2, in order to preserve rc */
438 PDEBUG(DEBUG_SIP, "failed to find port\n");
442 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
443 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
444 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
446 return p_s_rtp_port_local;
449 int Psip::rtp_connect(void)
454 ia.s_addr = htonl(p_s_rtp_ip_remote);
455 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
457 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);
461 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);
465 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
466 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
467 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
468 p_s_rtp_is_connected = 1;
472 void Psip::rtp_close(void)
474 if (p_s_rtp_fd.fd > 0) {
475 unregister_fd(&p_s_rtp_fd);
476 close(p_s_rtp_fd.fd);
479 if (p_s_rtcp_fd.fd > 0) {
480 unregister_fd(&p_s_rtcp_fd);
481 close(p_s_rtcp_fd.fd);
484 if (p_s_rtp_is_connected) {
485 PDEBUG(DEBUG_SIP, "rtp closed\n");
486 p_s_rtp_is_connected = 0;
491 void tv_difference(struct timeval *diff, const struct timeval *from,
492 const struct timeval *__to)
494 struct timeval _to = *__to, *to = &_to;
496 if (to->tv_usec < from->tv_usec) {
498 to->tv_usec += 1000000;
501 diff->tv_usec = to->tv_usec - from->tv_usec;
502 diff->tv_sec = to->tv_sec - from->tv_sec;
505 /* encode and send a rtp frame */
506 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
508 struct rtp_hdr *rtph;
510 int duration; /* in samples */
511 unsigned char buffer[256];
515 record(data, len, 1); // from up
517 tap(data, len, 1); // from up
519 if (!p_s_rtp_is_connected) {
524 if (!p_s_rtp_tx_action) {
525 /* initialize sequences */
526 p_s_rtp_tx_action = 1;
527 p_s_rtp_tx_ssrc = rand();
528 p_s_rtp_tx_sequence = random();
529 p_s_rtp_tx_timestamp = random();
530 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
533 switch (payload_type) {
535 we only support alaw and ulaw!
536 case RTP_PT_GSM_FULL:
544 case RTP_PT_GSM_HALF:
549 case PAYLOAD_TYPE_ALAW:
550 case PAYLOAD_TYPE_ULAW:
555 PERROR("unsupported message type %d\n", payload_type);
561 struct timeval tv, tv_diff;
562 long int usec_diff, frame_diff;
564 gettimeofday(&tv, NULL);
565 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
566 p_s_rtp_tx_last_tv = tv;
568 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
569 frame_diff = (usec_diff / 20000);
571 if (abs(frame_diff) > 1) {
572 long int frame_diff_excess = frame_diff - 1;
574 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
575 p_s_rtp_tx_sequence += frame_diff_excess;
576 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
581 rtph = (struct rtp_hdr *) buffer;
582 rtph->version = RTP_VERSION;
585 rtph->csrc_count = 0;
587 rtph->payload_type = payload_type;
588 rtph->sequence = htons(p_s_rtp_tx_sequence++);
589 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
590 p_s_rtp_tx_timestamp += duration;
591 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
592 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
594 if (p_s_rtp_fd.fd > 0) {
595 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
596 if (len != sizeof(struct rtp_hdr) + payload_len) {
597 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
607 /* receive from remote */
608 int Psip::bridge_rx(unsigned char *data, int len)
612 /* don't bridge, if tones are provided */
613 if (p_tone_name[0] || p_dov_tx)
619 if ((ret = Port::bridge_rx(data, len)))
622 /* write to rx buffer */
624 p_s_rxdata[p_s_rxpos++] = flip[*data++];
625 if (p_s_rxpos == 160) {
628 /* transmit data via rtp */
629 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
636 /* taken from freeswitch */
637 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
638 static int status2cause(int status)
642 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
648 return 21; //SWITCH_CAUSE_CALL_REJECTED;
650 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
653 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
656 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
658 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
667 return 127; //SWITCH_CAUSE_INTERWORKING;
669 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
674 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
677 return 17; //SWITCH_CAUSE_USER_BUSY;
679 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
682 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
684 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
686 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
690 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
693 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
695 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
697 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
701 static int cause2status(int cause, int location, const char **st)
707 s = 404; *st = sip_404_Not_found;
710 s = 404; *st = sip_404_Not_found;
713 s = 404; *st = sip_404_Not_found;
716 s = 486; *st = sip_486_Busy_here;
719 s = 408; *st = sip_408_Request_timeout;
722 s = 480; *st = sip_480_Temporarily_unavailable;
725 s = 480; *st = sip_480_Temporarily_unavailable;
728 if (location == LOCATION_USER) {
729 s = 603; *st = sip_603_Decline;
731 s = 403; *st = sip_403_Forbidden;
735 //s = 301; *st = sip_301_Moved_permanently;
736 s = 410; *st = sip_410_Gone;
739 s = 410; *st = sip_410_Gone;
742 s = 502; *st = sip_502_Bad_gateway;
745 s = 484; *st = sip_484_Address_incomplete;
748 s = 501; *st = sip_501_Not_implemented;
751 s = 480; *st = sip_480_Temporarily_unavailable;
754 s = 503; *st = sip_503_Service_unavailable;
757 s = 503; *st = sip_503_Service_unavailable;
760 s = 503; *st = sip_503_Service_unavailable;
763 s = 503; *st = sip_503_Service_unavailable;
766 s = 503; *st = sip_503_Service_unavailable;
769 s = 403; *st = sip_403_Forbidden;
772 s = 403; *st = sip_403_Forbidden;
775 s = 503; *st = sip_503_Service_unavailable;
778 s = 488; *st = sip_488_Not_acceptable;
781 s = 501; *st = sip_501_Not_implemented;
784 s = 488; *st = sip_488_Not_acceptable;
787 s = 501; *st = sip_501_Not_implemented;
790 s = 403; *st = sip_403_Forbidden;
793 s = 503; *st = sip_503_Service_unavailable;
796 s = 504; *st = sip_504_Gateway_time_out;
799 s = 468; *st = sip_486_Busy_here;
806 * endpoint sends messages to the SIP port
809 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
813 struct lcr_msg *message;
815 unsigned char payload_type;
817 if (param->connectinfo.rtpinfo.port) {
818 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
820 media_type = param->connectinfo.rtpinfo.media_types[0];
821 payload_type = param->connectinfo.rtpinfo.payload_types[0];
822 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
823 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
824 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
825 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
826 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
828 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
829 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
830 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
831 /* open local RTP peer (if not bridging) */
832 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
833 nua_cancel(p_s_handle, TAG_END());
834 nua_handle_destroy(p_s_handle);
836 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
837 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
839 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
840 message->param.disconnectinfo.cause = 41;
841 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
842 message_put(message);
843 new_state(PORT_STATE_RELEASE);
844 trigger_work(&p_s_delete);
849 ia.s_addr = htonl(p_s_rtp_ip_local);
853 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
857 "m=audio %d RTP/AVP %d\n"
858 "a=rtpmap:%d %s/8000\n"
859 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
860 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
863 * If this response causes corrupt messages, like SDP body inside or
864 * before header, check if the sofia-sip-gcc-4.8.patch was applied.
865 * If it is still corrupted, try to disable optimization when compiling
868 nua_respond(p_s_handle, SIP_200_OK,
869 NUTAG_MEDIA_ENABLE(0),
870 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
871 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
872 new_state(PORT_STATE_CONNECT);
873 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
874 add_trace("respond", "value", "200 OK");
875 add_trace("reason", NULL, "call connected");
876 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
877 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
878 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
884 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
886 struct lcr_msg *message;
887 char cause_str[128] = "";
888 int cause = param->disconnectinfo.cause;
889 int location = param->disconnectinfo.cause;
891 const char *status_text;
893 if (cause > 0 && cause <= 127) {
894 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
898 case PORT_STATE_OUT_SETUP:
899 case PORT_STATE_OUT_PROCEEDING:
900 case PORT_STATE_OUT_ALERTING:
901 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
902 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
904 add_trace("cause", "value", "%d", cause);
906 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
908 case PORT_STATE_IN_SETUP:
909 case PORT_STATE_IN_PROCEEDING:
910 case PORT_STATE_IN_ALERTING:
911 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
912 status = cause2status(cause, location, &status_text);
913 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
915 add_trace("cause", "value", "%d", cause);
916 add_trace("respond", "value", "%d %s", status, status_text);
918 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
919 nua_handle_destroy(p_s_handle);
921 trigger_work(&p_s_delete);
924 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
925 sip_trace_header(this, "BYE", DIRECTION_OUT);
927 add_trace("cause", "value", "%d", cause);
929 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
932 if (message_id == MESSAGE_DISCONNECT) {
933 while(p_epointlist) {
934 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
935 message->param.disconnectinfo.cause = CAUSE_NORMAL;
936 message->param.disconnectinfo.location = LOCATION_BEYOND;
937 message_put(message);
939 free_epointlist(p_epointlist);
943 new_state(PORT_STATE_RELEASE);
948 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
950 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
953 const char *local = inst->local_peer;
955 const char *remote = inst->remote_peer;
956 char sdp_str[512], pt_str[32];
958 struct epoint_list *epointlist;
959 sip_cseq_t *cseq = NULL;
960 struct lcr_msg *message;
961 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
962 unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
964 unsigned char *payload_types;
968 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
970 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
971 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
972 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
974 if (param->setup.rtpinfo.port) {
975 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
977 media_types = param->setup.rtpinfo.media_types;
978 payload_types = param->setup.rtpinfo.payload_types;
979 payloads = param->setup.rtpinfo.payloads;
980 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
981 p_s_rtp_port_local = param->setup.rtpinfo.port;
982 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
983 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
985 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
987 media_types = &lcr_media;
988 payload_types = &lcr_payload;
991 /* open local RTP peer (if not bridging) */
992 if (rtp_open() < 0) {
993 PERROR("Failed to open RTP sockets\n");
994 /* send release message to endpoit */
995 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
996 message->param.disconnectinfo.cause = 41;
997 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
998 message_put(message);
999 new_state(PORT_STATE_RELEASE);
1000 trigger_work(&p_s_delete);
1005 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1007 PERROR("Failed to create handle\n");
1008 /* send release message to endpoit */
1009 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1010 message->param.disconnectinfo.cause = 41;
1011 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1012 message_put(message);
1013 new_state(PORT_STATE_RELEASE);
1014 trigger_work(&p_s_delete);
1018 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1019 add_trace("handle", "new", "0x%x", p_s_handle);
1022 if (!p_s_rtp_ip_local) {
1025 /* extract IP from local peer */
1026 SCPY(local_ip, local);
1027 p = strchr(local_ip, ':');
1030 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1031 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1032 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1034 ia.s_addr = htonl(p_s_rtp_ip_local);
1037 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1041 "m=audio %d RTP/AVP"
1042 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1043 for (i = 0; i < payloads; i++) {
1044 SPRINT(pt_str, " %d", payload_types[i]);
1045 SCAT(sdp_str, pt_str);
1047 SCAT(sdp_str, "\n");
1048 for (i = 0; i < payloads; i++) {
1049 SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
1050 SCAT(sdp_str, pt_str);
1052 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1054 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
1055 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
1057 sip_trace_header(this, "INVITE", DIRECTION_OUT);
1058 add_trace("from", "uri", "%s", from);
1059 add_trace("to", "uri", "%s", to);
1060 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1061 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1062 for (i = 0; i < payloads; i++)
1063 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1066 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1068 nua_invite(p_s_handle,
1069 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1070 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1071 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1072 NUTAG_MEDIA_ENABLE(0),
1073 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1074 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1075 new_state(PORT_STATE_OUT_SETUP);
1078 PDEBUG(DEBUG_SIP, "do overlap\n");
1079 new_state(PORT_STATE_OUT_OVERLAP);
1080 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1081 message_put(message);
1083 PDEBUG(DEBUG_SIP, "do proceeding\n");
1084 new_state(PORT_STATE_OUT_PROCEEDING);
1085 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1086 message_put(message);
1089 /* attach only if not already */
1090 epointlist = p_epointlist;
1092 if (epointlist->epoint_id == epoint_id)
1094 epointlist = epointlist->next;
1097 epointlist_new(epoint_id);
1102 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1104 // char sdp_str[256];
1105 // struct in_addr ia;
1107 switch (param->notifyinfo.notify) {
1108 case INFO_NOTIFY_REMOTE_HOLD:
1112 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
1114 "c=IN IP4 0.0.0.0\n"
1117 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1118 nua_info(p_s_handle,
1119 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1120 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1121 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1122 NUTAG_MEDIA_ENABLE(0),
1123 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1124 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1127 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1129 ia.s_addr = htonl(p_s_rtp_ip_local);
1132 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1136 "m=audio %d RTP/AVP %d\n"
1137 "a=rtpmap:%d %s/8000\n"
1138 , 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));
1139 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1140 nua_info(p_s_handle,
1141 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1142 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1143 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1144 NUTAG_MEDIA_ENABLE(0),
1145 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1146 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1154 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1158 /* prepare DTMF info payload */
1164 /* start invite to handle DTMF */
1165 nua_info(p_s_handle,
1166 NUTAG_MEDIA_ENABLE(0),
1167 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1168 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1173 /* NOTE: incomplete and not working */
1174 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1178 /* prepare DTMF info payload */
1182 , param->information.id);
1184 /* start invite to handle DTMF */
1185 nua_info(p_s_handle,
1186 NUTAG_MEDIA_ENABLE(0),
1187 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1188 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1193 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1195 if (Port::message_epoint(epoint_id, message_id, param))
1198 switch(message_id) {
1199 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1200 if (p_state != PORT_STATE_IN_SETUP
1201 && p_state != PORT_STATE_IN_PROCEEDING)
1203 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1204 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1205 add_trace("respond", "value", "180 Ringing");
1207 new_state(PORT_STATE_IN_ALERTING);
1210 case MESSAGE_CONNECT: /* call is connected on LCR side */
1211 if (p_state != PORT_STATE_IN_SETUP
1212 && p_state != PORT_STATE_IN_PROCEEDING
1213 && p_state != PORT_STATE_IN_ALERTING)
1215 message_connect(epoint_id, message_id, param);
1218 case MESSAGE_DISCONNECT: /* call has been disconnected */
1219 case MESSAGE_RELEASE: /* call has been released */
1220 message_release(epoint_id, message_id, param);
1223 case MESSAGE_SETUP: /* dial-out command received from epoint */
1224 message_setup(epoint_id, message_id, param);
1227 case MESSAGE_INFORMATION: /* overlap dialing */
1228 if (p_state != PORT_STATE_OUT_OVERLAP)
1230 message_information(epoint_id, message_id, param);
1233 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1234 if (p_state == PORT_STATE_CONNECT)
1235 message_dtmf(epoint_id, message_id, param);
1236 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1237 if (p_state == PORT_STATE_CONNECT)
1238 message_notify(epoint_id, message_id, param);
1242 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1248 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)
1252 if (!sip->sip_payload) {
1253 PDEBUG(DEBUG_SIP, "no payload given\n");
1257 sdp_parser_t *parser;
1260 sdp_attribute_t *attr;
1262 sdp_connection_t *conn;
1264 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1266 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1270 if (!(sdp = sdp_session(parser))) {
1271 sdp_parser_free(parser);
1274 for (m = sdp->sdp_media; m; m = m->m_next) {
1275 if (m->m_proto != sdp_proto_rtp)
1277 if (m->m_type != sdp_media_audio)
1279 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1281 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1282 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1284 if (m->m_connections) {
1285 conn = m->m_connections;
1286 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1287 inet_pton(AF_INET, conn->c_address, ip);
1288 *ip = ntohl(p_s_rtp_ip_remote);
1290 char *p = sip->sip_payload->pl_data;
1293 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1294 p = strstr(p, "c=IN IP4 ");
1296 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1297 sdp_parser_free(parser);
1301 if ((p = strchr(addr, '\n'))) *p = '\0';
1302 if ((p = strchr(addr, '\r'))) *p = '\0';
1303 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1304 inet_pton(AF_INET, addr, ip);
1305 *ip = ntohl(p_s_rtp_ip_remote);
1307 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1310 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1311 /* append to payload list, if there is space */
1312 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1313 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1314 media_type = MEDIA_TYPE_ALAW;
1315 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1316 media_type = MEDIA_TYPE_ULAW;
1317 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1318 media_type = MEDIA_TYPE_GSM;
1319 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1320 media_type = MEDIA_TYPE_GSM_EFR;
1321 else if (!strcmp(map->rm_encoding, "AMR"))
1322 media_type = MEDIA_TYPE_AMR;
1323 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1324 media_type = MEDIA_TYPE_GSM_HR;
1325 if (media_type && *payloads <= max_payloads) {
1326 *payload_types++ = map->rm_pt;
1327 *media_types++ = media_type;
1333 sdp_parser_free(parser);
1338 void Psip::i_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tagss[])
1340 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1341 const char *from = "", *to = "", *name = "";
1344 class Endpoint *epoint;
1345 struct lcr_msg *message;
1346 struct interface *interface;
1347 int media_types[32];
1348 uint8_t payload_types[32];
1352 interface = getinterfacebyname(inst->interface_name);
1354 PERROR("Cannot find interface %s.\n", inst->interface_name);
1358 if (sip->sip_from) {
1359 if (sip->sip_from->a_url)
1360 from = sip->sip_from->a_url->url_user;
1361 if (sip->sip_from->a_display) {
1362 name = sip->sip_from->a_display;
1363 if (!strncmp(name, "\"IMSI", 5)) {
1364 strncpy(imsi, name + 5, 15);
1371 if (sip->sip_to->a_url)
1372 to = sip->sip_to->a_url->url_user;
1374 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1376 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1377 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1379 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1380 if (!p_s_rtp_bridge) {
1383 /* check if supported payload type exists */
1384 for (i = 0; i < payloads; i++) {
1385 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1388 if (i == payloads) {
1389 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1397 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1399 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1400 nua_handle_destroy(nh);
1402 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1404 add_trace("respond", "value", "415 Unsupported Media");
1406 add_trace("respond", "value", "400 Bad Request");
1407 add_trace("reason", NULL, "offered codec does not match");
1409 new_state(PORT_STATE_RELEASE);
1410 trigger_work(&p_s_delete);
1414 /* open local RTP peer (if not bridging) */
1415 if (!p_s_rtp_bridge && rtp_open() < 0) {
1416 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1417 nua_handle_destroy(nh);
1419 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1420 add_trace("respond", "value", "500 Internal Server Error");
1421 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1423 new_state(PORT_STATE_RELEASE);
1424 trigger_work(&p_s_delete);
1429 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1430 add_trace("handle", "new", "0x%x", nh);
1434 sip_trace_header(this, "INVITE", DIRECTION_IN);
1435 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1436 /* caller information */
1438 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1439 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1440 add_trace("calling", "present", "unavailable");
1442 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1443 add_trace("calling", "present", "allowed");
1444 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1445 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1446 SCPY(p_callerinfo.id, from);
1447 add_trace("calling", "number", "%s", from);
1448 SCPY(p_callerinfo.name, name);
1450 add_trace("calling", "name", "%s", name);
1451 SCPY(p_callerinfo.imsi, imsi);
1453 add_trace("calling", "imsi", "%s", imsi);
1455 SCPY(p_callerinfo.interface, inst->interface_name);
1456 /* dialing information */
1458 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1459 SCAT(p_dialinginfo.id, to);
1460 add_trace("dialing", "number", "%s", to);
1463 /* bearer capability */
1464 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1465 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1466 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1467 add_trace("bearer", "capa", "speech");
1468 add_trace("bearer", "mode", "circuit");
1469 /* if packet mode works some day, see dss1.cpp for conditions */
1470 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1474 /* create endpoint */
1476 FATAL("Incoming call but already got an endpoint.\n");
1477 if (!(epoint = new Endpoint(p_serial, 0)))
1478 FATAL("No memory for Endpoint instance\n");
1479 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1480 epointlist_new(epoint->ep_serial);
1482 #ifdef NUTAG_AUTO100
1483 /* send trying (proceeding) */
1484 nua_respond(nh, SIP_100_TRYING, TAG_END());
1485 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1486 add_trace("respond", "value", "100 Trying");
1490 new_state(PORT_STATE_IN_PROCEEDING);
1492 /* send setup message to endpoit */
1493 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1494 message->param.setup.port_type = p_type;
1495 // message->param.setup.dtmf = 0;
1496 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1497 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1498 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1499 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1500 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1501 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1502 if (p_s_rtp_bridge) {
1505 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1506 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1507 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1508 /* add codecs to setup message */
1509 for (i = 0; i < payloads; i++) {
1510 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1511 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1512 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1515 message->param.setup.rtpinfo.payloads = i;
1517 message_put(message);
1519 /* send progress, if tones are available and if we don't bridge */
1520 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1523 unsigned char payload_type;
1525 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1526 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1527 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1528 /* open local RTP peer (if not bridging) */
1529 if (rtp_connect() < 0) {
1530 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1531 nua_handle_destroy(nh);
1533 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1534 add_trace("respond", "value", "500 Internal Server Error");
1535 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1537 new_state(PORT_STATE_RELEASE);
1538 trigger_work(&p_s_delete);
1539 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1540 message->param.disconnectinfo.cause = 41;
1541 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1542 message_put(message);
1543 new_state(PORT_STATE_RELEASE);
1544 trigger_work(&p_s_delete);
1548 ia.s_addr = htonl(p_s_rtp_ip_local);
1552 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1556 "m=audio %d RTP/AVP %d\n"
1557 "a=rtpmap:%d %s/8000\n"
1558 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
1559 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1561 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1562 NUTAG_MEDIA_ENABLE(0),
1563 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1564 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1565 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1566 add_trace("respond", "value", "183 SESSION PROGRESS");
1567 add_trace("reason", NULL, "audio available");
1568 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1569 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1570 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1575 void Psip::i_bye(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tagss[])
1577 struct lcr_msg *message;
1580 PDEBUG(DEBUG_SIP, "bye received\n");
1582 sip_trace_header(this, "BYE", DIRECTION_IN);
1583 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1584 cause = atoi(sip->sip_reason->re_cause);
1585 add_trace("cause", "value", "%d", cause);
1589 // let stack do bye automaticall, since it will not accept our response for some reason
1590 // nua_respond(nh, SIP_200_OK, TAG_END());
1591 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1592 add_trace("respond", "value", "200 OK");
1594 // nua_handle_destroy(nh);
1599 while(p_epointlist) {
1600 /* send setup message to endpoit */
1601 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1602 message->param.disconnectinfo.cause = cause ? : 16;
1603 message->param.disconnectinfo.location = LOCATION_BEYOND;
1604 message_put(message);
1606 free_epointlist(p_epointlist);
1608 new_state(PORT_STATE_RELEASE);
1609 trigger_work(&p_s_delete);
1612 void Psip::i_cancel(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tagss[])
1614 struct lcr_msg *message;
1616 PDEBUG(DEBUG_SIP, "cancel received\n");
1618 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1621 nua_handle_destroy(nh);
1626 while(p_epointlist) {
1627 /* send setup message to endpoit */
1628 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1629 message->param.disconnectinfo.cause = 16;
1630 message->param.disconnectinfo.location = LOCATION_BEYOND;
1631 message_put(message);
1633 free_epointlist(p_epointlist);
1635 new_state(PORT_STATE_RELEASE);
1636 trigger_work(&p_s_delete);
1639 void Psip::r_bye(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tagss[])
1641 PDEBUG(DEBUG_SIP, "bye response received\n");
1643 nua_handle_destroy(nh);
1648 trigger_work(&p_s_delete);
1651 void Psip::r_cancel(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tagss[])
1653 PDEBUG(DEBUG_SIP, "cancel response received\n");
1655 nua_handle_destroy(nh);
1660 trigger_work(&p_s_delete);
1663 void Psip::r_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *magic, nua_handle_t *nh, nua_hmagic_t *hmagic, sip_t const *sip, tagi_t tagss[])
1665 struct lcr_msg *message;
1666 int cause = 0, location = 0;
1667 int media_types[32];
1668 uint8_t payload_types[32];
1671 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1673 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1674 add_trace("respond", "value", "%d", status);
1678 if (status == 183 || (status >= 200 && status <= 299)) {
1681 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1682 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1686 else if (!p_s_rtp_bridge) {
1687 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1688 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1695 nua_cancel(nh, TAG_END());
1696 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1697 add_trace("reason", NULL, "accepted codec does not match");
1700 location = LOCATION_PRIVATE_LOCAL;
1701 goto release_with_cause;
1704 /* connect to remote RTP (if not bridging) */
1705 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1706 nua_cancel(nh, TAG_END());
1707 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1708 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1711 location = LOCATION_PRIVATE_LOCAL;
1712 goto release_with_cause;
1720 PDEBUG(DEBUG_SIP, "do proceeding\n");
1721 new_state(PORT_STATE_OUT_PROCEEDING);
1722 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1723 message_put(message);
1727 PDEBUG(DEBUG_SIP, "do alerting\n");
1728 new_state(PORT_STATE_OUT_ALERTING);
1729 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1730 message_put(message);
1733 PDEBUG(DEBUG_SIP, "do progress\n");
1734 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1735 message->param.progressinfo.progress = 8;
1736 message->param.progressinfo.location = 10;
1737 if (p_s_rtp_bridge) {
1738 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1739 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1740 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1741 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1742 message->param.progressinfo.rtpinfo.payloads = 1;
1744 message_put(message);
1747 if (status < 100 || status > 199)
1749 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1755 if (status >= 200 && status <= 299) {
1756 PDEBUG(DEBUG_SIP, "do connect\n");
1757 nua_ack(nh, TAG_END());
1758 new_state(PORT_STATE_CONNECT);
1759 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1760 if (p_s_rtp_bridge) {
1761 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1762 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1763 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1764 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1765 message->param.connectinfo.rtpinfo.payloads = 1;
1767 message_put(message);
1770 cause = status2cause(status);
1771 location = LOCATION_BEYOND;
1774 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1776 while(p_epointlist) {
1777 /* send setup message to endpoit */
1778 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1779 message->param.disconnectinfo.cause = cause;
1780 message->param.disconnectinfo.location = location;
1781 message_put(message);
1783 free_epointlist(p_epointlist);
1786 new_state(PORT_STATE_RELEASE);
1790 trigger_work(&p_s_delete);
1793 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[])
1795 struct sip_inst *inst = (struct sip_inst *) magic;
1797 class Psip *psip = NULL;
1799 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1803 /* create or find port instance */
1804 if (event == nua_i_invite)
1807 struct interface *interface = interface_first;
1809 /* create call instance */
1810 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1812 if (!strcmp(interface->name, inst->interface_name))
1814 interface = interface->next;
1817 PERROR("Cannot find interface %s.\n", inst->interface_name);
1820 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1821 FATAL("Cannot create Port instance.\n");
1825 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1826 psip = (class Psip *)port;
1827 if (psip->p_s_handle == nh) {
1835 PERROR("no SIP Port found for handel %p\n", nh);
1836 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1837 nua_handle_destroy(nh);
1842 case nua_r_set_params:
1843 PDEBUG(DEBUG_SIP, "setparam response\n");
1846 PDEBUG(DEBUG_SIP, "error received\n");
1849 PDEBUG(DEBUG_SIP, "state change received\n");
1851 case nua_i_register:
1852 PDEBUG(DEBUG_SIP, "register received\n");
1855 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1858 PDEBUG(DEBUG_SIP, "ack received\n");
1861 PDEBUG(DEBUG_SIP, "active received\n");
1864 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1867 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1870 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1873 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1876 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1878 case nua_i_terminated:
1879 PDEBUG(DEBUG_SIP, "terminated received\n");
1882 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1886 /* received shutdown due to termination of RTP */
1887 void Psip::rtp_shutdown(void)
1889 struct lcr_msg *message;
1891 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1893 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1896 nua_handle_destroy(p_s_handle);
1899 while(p_epointlist) {
1900 /* send setup message to endpoit */
1901 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1902 message->param.disconnectinfo.cause = 16;
1903 message->param.disconnectinfo.location = LOCATION_BEYOND;
1904 message_put(message);
1906 free_epointlist(p_epointlist);
1908 new_state(PORT_STATE_RELEASE);
1909 trigger_work(&p_s_delete);
1912 int sip_init_inst(struct interface *interface)
1914 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1917 interface->sip_inst = inst;
1918 SCPY(inst->interface_name, interface->name);
1919 SCPY(inst->local_peer, interface->sip_local_peer);
1920 SCPY(inst->remote_peer, interface->sip_remote_peer);
1922 /* init root object */
1923 inst->root = su_root_create(inst);
1925 PERROR("Failed to create SIP root\n");
1926 sip_exit_inst(interface);
1930 SPRINT(local, "sip:%s",inst->local_peer);
1931 if (!strchr(inst->local_peer, ':'))
1932 SCAT(local, ":5060");
1933 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
1935 PERROR("Failed to create SIP stack object\n");
1936 sip_exit_inst(interface);
1939 nua_set_params(inst->nua,
1940 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1941 NUTAG_APPL_METHOD("INVITE"),
1942 NUTAG_APPL_METHOD("ACK"),
1943 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1944 NUTAG_APPL_METHOD("CANCEL"),
1945 NUTAG_APPL_METHOD("OPTIONS"),
1946 NUTAG_APPL_METHOD("NOTIFY"),
1947 NUTAG_APPL_METHOD("INFO"),
1949 #ifdef NUTAG_AUTO100
1953 NUTAG_AUTOANSWER(0),
1956 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1958 any_sip_interface = 1;
1963 void sip_exit_inst(struct interface *interface)
1965 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1970 su_root_destroy(inst->root);
1972 nua_destroy(inst->nua);
1974 FREE(inst, sizeof(*inst));
1975 interface->sip_inst = NULL;
1977 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1979 /* check if there is any other SIP interface left */
1980 interface = interface_first;
1982 if (interface->sip_inst)
1984 interface = interface->next;
1987 any_sip_interface = 0;
1990 extern su_log_t su_log_default[];
1991 extern su_log_t nua_log[];
1992 //extern su_log_t soa_log[];
1998 /* init SOFIA lib */
2000 su_home_init(sip_home);
2002 if (options.deb & DEBUG_SIP) {
2003 su_log_set_level(su_log_default, 9);
2004 su_log_set_level(nua_log, 9);
2005 //su_log_set_level(soa_log, 9);
2008 for (i = 0; i < 256; i++)
2009 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);
2011 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
2018 su_home_deinit(sip_home);
2021 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
2024 void sip_handle(void)
2026 struct interface *interface = interface_first;
2027 struct sip_inst *inst;
2030 if (interface->sip_inst) {
2031 inst = (struct sip_inst *) interface->sip_inst;
2032 su_root_step(inst->root, 0);
2034 interface = interface->next;
2038 /* deletes when back in event loop */
2039 static int delete_event(struct lcr_work *work, void *instance, int index)
2041 class Psip *psip = (class Psip *)instance;
2050 * generate audio, if no data is received from bridge
2053 void Psip::set_tone(const char *dir, const char *tone)
2055 Port::set_tone(dir, tone);
2060 void Psip::update_load(void)
2062 /* don't trigger load event if event already active */
2063 if (p_s_loadtimer.active)
2066 /* don't start timer if ... */
2067 if (!p_tone_name[0] && !p_dov_tx)
2070 p_s_next_tv_sec = 0;
2071 schedule_timer(&p_s_loadtimer, 0, 0); /* no delay the first time */
2074 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2076 class Psip *psip = (class Psip *)instance;
2078 /* stop timer if ... */
2079 if (!psip->p_tone_name[0] && !psip->p_dov_tx)
2087 #define SEND_SIP_LEN 160
2089 void Psip::load_tx(void)
2092 struct timeval current_time;
2093 int tosend = SEND_SIP_LEN, i;
2094 unsigned char buf[SEND_SIP_LEN], *p = buf;
2097 gettimeofday(¤t_time, NULL);
2098 if (!p_s_next_tv_sec) {
2099 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2100 p_s_next_tv_sec = current_time.tv_sec;
2101 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2102 if (p_s_next_tv_usec >= 1000000) {
2103 p_s_next_tv_usec -= 1000000;
2106 schedule_timer(&p_s_loadtimer, 0, SEND_SIP_LEN * 125);
2108 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2109 + (current_time.tv_usec - p_s_next_tv_usec);
2110 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2111 /* if clock drifts too much, set next timeout event to current timer + 160 */
2113 p_s_next_tv_sec = current_time.tv_sec;
2114 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2115 if (p_s_next_tv_usec >= 1000000) {
2116 p_s_next_tv_usec -= 1000000;
2120 /* if diff is positive, it took too long, so next timeout will be earlier */
2121 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2122 if (p_s_next_tv_usec >= 1000000) {
2123 p_s_next_tv_usec -= 1000000;
2127 schedule_timer(&p_s_loadtimer, 0, SEND_SIP_LEN * 125 - diff);
2131 if (p_tone_name[0]) {
2132 tosend -= read_audio(p, tosend);
2135 tosend -= dov_tx(p, tosend);
2138 PERROR("buffer is not completely filled\n");
2143 for (i = 0; i < SEND_SIP_LEN; i++) {
2147 /* transmit data via rtp */
2148 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);