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>
20 unsigned char flip[256];
22 int any_sip_interface = 0;
24 //pthread_mutex_t mutex_msg;
25 su_home_t sip_home[1];
28 char interface_name[64];
35 static int delete_event(struct lcr_work *work, void *instance, int index);
36 static int load_timer(struct lcr_timer *timer, void *instance, int index);
41 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
44 if (interface->rtp_bridge)
46 p_s_sip_inst = interface->sip_inst;
47 memset(&p_s_delete, 0, sizeof(p_s_delete));
48 add_work(&p_s_delete, delete_event, this, 0);
51 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
52 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
53 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
54 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
55 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
56 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
58 p_s_rtp_ip_remote = 0;
59 p_s_rtp_port_local = 0;
60 p_s_rtp_port_remote = 0;
65 p_s_rtp_tx_action = 0;
68 memset(&p_s_loadtimer, 0, sizeof(p_s_loadtimer));
69 add_timer(&p_s_loadtimer, load_timer, this, 0);
72 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
74 FATAL("No SIP instance for interface\n");
83 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
85 del_timer(&p_s_loadtimer);
86 del_work(&p_s_delete);
91 static const char *media_type2name(uint8_t media_type) {
99 case MEDIA_TYPE_GSM_HR:
101 case MEDIA_TYPE_GSM_EFR:
110 static void sip_trace_header(class Psip *sip, const char *message, int direction)
112 /* init trace with given values */
115 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
116 sip?sip->p_dialinginfo.id:NULL,
127 /* according to RFC 3550 */
129 #if __BYTE_ORDER == __LITTLE_ENDIAN
130 uint8_t csrc_count:4,
134 uint8_t payload_type:7,
136 #elif __BYTE_ORDER == __BIG_ENDIAN
147 } __attribute__((packed));
152 } __attribute__((packed));
154 #define RTP_VERSION 2
156 #define PAYLOAD_TYPE_ULAW 0
157 #define PAYLOAD_TYPE_ALAW 8
158 #define PAYLOAD_TYPE_GSM 3
160 /* decode an rtp frame */
161 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
163 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
164 struct rtp_x_hdr *rtpxh;
168 unsigned char *from, *to;
172 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
175 if (rtph->version != RTP_VERSION) {
176 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
179 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
180 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
181 if (payload_len < 0) {
182 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
183 "csrc count = %d)\n", len, rtph->csrc_count);
186 if (rtph->extension) {
187 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
188 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
189 "extension header\n");
192 rtpxh = (struct rtp_x_hdr *)payload;
193 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
195 payload_len -= x_len;
196 if (payload_len < 0) {
197 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
198 "extension header exceeds frame length\n");
203 if (payload_len < 0) {
204 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
208 payload_len -= payload[payload_len - 1];
209 if (payload_len < 0) {
210 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
211 "greater than payload\n");
216 switch (rtph->payload_type) {
218 we only support alaw and ulaw!
219 case RTP_PT_GSM_FULL:
220 if (payload_len != 33) {
221 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
222 "payload length != 33 (len = %d)\n",
228 if (payload_len != 31) {
229 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
230 "payload length != 31 (len = %d)\n",
235 case RTP_PT_GSM_HALF:
236 if (payload_len != 14) {
237 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
238 "payload length != 14 (len = %d)\n",
244 case PAYLOAD_TYPE_ALAW:
245 if (options.law != 'a') {
246 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
250 case PAYLOAD_TYPE_ULAW:
251 if (options.law == 'a') {
252 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
257 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
258 "type %d\n", rtph->payload_type);
262 if (payload_len <= 0) {
263 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
269 psip->record(payload, payload_len, 0); // from down
271 psip->tap(payload, payload_len, 0); // from down
276 if (psip->p_echotest) {
277 /* echo rtp data we just received */
278 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
282 *to++ = flip[*from++];
283 psip->bridge_tx(payload, payload_len);
288 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
290 class Psip *psip = (class Psip *) instance;
292 unsigned char buffer[256];
295 if ((what & LCR_FD_READ)) {
296 len = read(fd->fd, &buffer, sizeof(buffer));
298 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
299 // psip->rtp_close();
300 // psip->rtp_shutdown();
303 if (psip->p_s_rtp_is_connected)
304 rc = rtp_decode(psip, buffer, len);
310 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
312 // class Psip *psip = (class Psip *) instance;
314 unsigned char buffer[256];
316 if ((what & LCR_FD_READ)) {
317 len = read(fd->fd, &buffer, sizeof(buffer));
319 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
320 // psip->rtp_close();
321 // psip->rtp_shutdown();
324 PDEBUG(DEBUG_SIP, "rtcp!\n");
330 #define RTP_PORT_BASE 30000
331 #define RTP_PORT_MAX 39998
332 static unsigned short next_udp_port = RTP_PORT_BASE;
334 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
337 socklen_t alen = sizeof(*sin_local);
339 sin_local->sin_family = AF_INET;
340 sin_local->sin_addr.s_addr = htonl(ip);
341 sin_local->sin_port = htons(port);
343 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
347 /* retrieve the address we actually bound to, in case we
348 * passed INADDR_ANY as IP address */
349 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
352 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
355 socklen_t alen = sizeof(*sin_local);
357 sin_remote->sin_family = AF_INET;
358 sin_remote->sin_addr.s_addr = htonl(ip);
359 sin_remote->sin_port = htons(port);
361 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
363 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
367 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
370 int Psip::rtp_open(void)
375 unsigned short start_port;
378 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
384 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
386 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
392 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
395 ip = htonl(INADDR_ANY);
397 start_port = next_udp_port;
399 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
403 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port + 1);
405 p_s_rtp_port_local = next_udp_port;
406 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
409 /* reopen rtp socket and try again with next udp port */
410 unregister_fd(&p_s_rtp_fd);
411 close(p_s_rtp_fd.fd);
413 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
419 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
422 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
423 if (next_udp_port == start_port)
425 /* we must use rc2, in order to preserve rc */
428 PDEBUG(DEBUG_SIP, "failed to find port\n");
432 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
433 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
434 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
436 return p_s_rtp_port_local;
439 int Psip::rtp_connect(void)
444 ia.s_addr = htonl(p_s_rtp_ip_remote);
445 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
447 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);
451 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);
455 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
456 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
457 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
458 p_s_rtp_is_connected = 1;
462 void Psip::rtp_close(void)
464 if (p_s_rtp_fd.fd > 0) {
465 unregister_fd(&p_s_rtp_fd);
466 close(p_s_rtp_fd.fd);
469 if (p_s_rtcp_fd.fd > 0) {
470 unregister_fd(&p_s_rtcp_fd);
471 close(p_s_rtcp_fd.fd);
474 if (p_s_rtp_is_connected) {
475 PDEBUG(DEBUG_SIP, "rtp closed\n");
476 p_s_rtp_is_connected = 0;
481 void tv_difference(struct timeval *diff, const struct timeval *from,
482 const struct timeval *__to)
484 struct timeval _to = *__to, *to = &_to;
486 if (to->tv_usec < from->tv_usec) {
488 to->tv_usec += 1000000;
491 diff->tv_usec = to->tv_usec - from->tv_usec;
492 diff->tv_sec = to->tv_sec - from->tv_sec;
495 /* encode and send a rtp frame */
496 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
498 struct rtp_hdr *rtph;
500 int duration; /* in samples */
501 unsigned char buffer[256];
505 record(data, len, 1); // from up
507 tap(data, len, 1); // from up
509 if (!p_s_rtp_is_connected) {
514 if (!p_s_rtp_tx_action) {
515 /* initialize sequences */
516 p_s_rtp_tx_action = 1;
517 p_s_rtp_tx_ssrc = rand();
518 p_s_rtp_tx_sequence = random();
519 p_s_rtp_tx_timestamp = random();
520 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
523 switch (payload_type) {
525 we only support alaw and ulaw!
526 case RTP_PT_GSM_FULL:
534 case RTP_PT_GSM_HALF:
539 case PAYLOAD_TYPE_ALAW:
540 case PAYLOAD_TYPE_ULAW:
545 PERROR("unsupported message type %d\n", payload_type);
551 struct timeval tv, tv_diff;
552 long int usec_diff, frame_diff;
554 gettimeofday(&tv, NULL);
555 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
556 p_s_rtp_tx_last_tv = tv;
558 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
559 frame_diff = (usec_diff / 20000);
561 if (abs(frame_diff) > 1) {
562 long int frame_diff_excess = frame_diff - 1;
564 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
565 p_s_rtp_tx_sequence += frame_diff_excess;
566 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
571 rtph = (struct rtp_hdr *) buffer;
572 rtph->version = RTP_VERSION;
575 rtph->csrc_count = 0;
577 rtph->payload_type = payload_type;
578 rtph->sequence = htons(p_s_rtp_tx_sequence++);
579 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
580 p_s_rtp_tx_timestamp += duration;
581 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
582 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
584 if (p_s_rtp_fd.fd > 0) {
585 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
586 if (len != sizeof(struct rtp_hdr) + payload_len) {
587 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
597 /* receive from remote */
598 int Psip::bridge_rx(unsigned char *data, int len)
600 /* don't bridge, if tones are provided */
604 /* write to rx buffer */
606 p_s_rxdata[p_s_rxpos++] = flip[*data++];
607 if (p_s_rxpos == 160) {
610 /* transmit data via rtp */
611 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
618 /* taken from freeswitch */
619 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
620 static int status2cause(int status)
624 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
630 return 21; //SWITCH_CAUSE_CALL_REJECTED;
632 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
635 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
638 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
640 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
649 return 127; //SWITCH_CAUSE_INTERWORKING;
651 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
656 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
659 return 17; //SWITCH_CAUSE_USER_BUSY;
661 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
664 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
666 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
668 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
672 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
675 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
677 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
679 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
683 static int cause2status(int cause, int location, const char **st)
689 s = 404; *st = sip_404_Not_found;
692 s = 404; *st = sip_404_Not_found;
695 s = 404; *st = sip_404_Not_found;
698 s = 486; *st = sip_486_Busy_here;
701 s = 408; *st = sip_408_Request_timeout;
704 s = 480; *st = sip_480_Temporarily_unavailable;
707 s = 480; *st = sip_480_Temporarily_unavailable;
710 if (location == LOCATION_USER) {
711 s = 603; *st = sip_603_Decline;
713 s = 403; *st = sip_403_Forbidden;
717 //s = 301; *st = sip_301_Moved_permanently;
718 s = 410; *st = sip_410_Gone;
721 s = 410; *st = sip_410_Gone;
724 s = 502; *st = sip_502_Bad_gateway;
727 s = 484; *st = sip_484_Address_incomplete;
730 s = 501; *st = sip_501_Not_implemented;
733 s = 480; *st = sip_480_Temporarily_unavailable;
736 s = 503; *st = sip_503_Service_unavailable;
739 s = 503; *st = sip_503_Service_unavailable;
742 s = 503; *st = sip_503_Service_unavailable;
745 s = 503; *st = sip_503_Service_unavailable;
748 s = 503; *st = sip_503_Service_unavailable;
751 s = 403; *st = sip_403_Forbidden;
754 s = 403; *st = sip_403_Forbidden;
757 s = 503; *st = sip_503_Service_unavailable;
760 s = 488; *st = sip_488_Not_acceptable;
763 s = 501; *st = sip_501_Not_implemented;
766 s = 488; *st = sip_488_Not_acceptable;
769 s = 501; *st = sip_501_Not_implemented;
772 s = 403; *st = sip_403_Forbidden;
775 s = 503; *st = sip_503_Service_unavailable;
778 s = 504; *st = sip_504_Gateway_time_out;
781 s = 468; *st = sip_486_Busy_here;
788 * endpoint sends messages to the SIP port
791 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
795 struct lcr_msg *message;
797 unsigned char payload_type;
799 if (param->connectinfo.rtpinfo.port) {
800 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
802 media_type = param->connectinfo.rtpinfo.media_types[0];
803 payload_type = param->connectinfo.rtpinfo.payload_types[0];
804 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
805 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
806 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
807 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
808 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
810 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
811 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
812 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
813 /* open local RTP peer (if not bridging) */
814 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
815 nua_cancel(p_s_handle, TAG_END());
816 nua_handle_destroy(p_s_handle);
818 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
819 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
821 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
822 message->param.disconnectinfo.cause = 41;
823 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
824 message_put(message);
825 new_state(PORT_STATE_RELEASE);
826 trigger_work(&p_s_delete);
831 ia.s_addr = htonl(p_s_rtp_ip_local);
835 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
839 "m=audio %d RTP/AVP %d\n"
840 "a=rtpmap:%d %s/8000\n"
841 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
842 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
844 nua_respond(p_s_handle, SIP_200_OK,
845 NUTAG_MEDIA_ENABLE(0),
846 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
847 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
848 new_state(PORT_STATE_CONNECT);
849 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
850 add_trace("respond", "value", "200 OK");
851 add_trace("reason", NULL, "call connected");
852 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
853 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
854 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
860 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
862 struct lcr_msg *message;
863 char cause_str[128] = "";
864 int cause = param->disconnectinfo.cause;
865 int location = param->disconnectinfo.cause;
867 const char *status_text;
869 if (cause > 0 && cause <= 127) {
870 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
874 case PORT_STATE_OUT_SETUP:
875 case PORT_STATE_OUT_PROCEEDING:
876 case PORT_STATE_OUT_ALERTING:
877 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
878 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
880 add_trace("cause", "value", "%d", cause);
882 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
884 case PORT_STATE_IN_SETUP:
885 case PORT_STATE_IN_PROCEEDING:
886 case PORT_STATE_IN_ALERTING:
887 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
888 status = cause2status(cause, location, &status_text);
889 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
891 add_trace("cause", "value", "%d", cause);
892 add_trace("respond", "value", "%d %s", status, status_text);
894 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
895 nua_handle_destroy(p_s_handle);
897 trigger_work(&p_s_delete);
900 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
901 sip_trace_header(this, "BYE", DIRECTION_OUT);
903 add_trace("cause", "value", "%d", cause);
905 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
908 if (message_id == MESSAGE_DISCONNECT) {
909 while(p_epointlist) {
910 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
911 message->param.disconnectinfo.cause = CAUSE_NORMAL;
912 message->param.disconnectinfo.location = LOCATION_BEYOND;
913 message_put(message);
915 free_epointlist(p_epointlist);
919 new_state(PORT_STATE_RELEASE);
924 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
926 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
929 const char *local = inst->local_peer;
931 const char *remote = inst->remote_peer;
932 char sdp_str[512], pt_str[32];
934 struct epoint_list *epointlist;
935 sip_cseq_t *cseq = NULL;
936 struct lcr_msg *message;
937 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
938 unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
940 unsigned char *payload_types;
944 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
946 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
947 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
948 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
950 if (param->setup.rtpinfo.port) {
951 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
953 media_types = param->setup.rtpinfo.media_types;
954 payload_types = param->setup.rtpinfo.payload_types;
955 payloads = param->setup.rtpinfo.payloads;
956 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
957 p_s_rtp_port_local = param->setup.rtpinfo.port;
958 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
959 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
961 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
963 media_types = &lcr_media;
964 payload_types = &lcr_payload;
967 /* open local RTP peer (if not bridging) */
968 if (rtp_open() < 0) {
969 PERROR("Failed to open RTP sockets\n");
970 /* send release message to endpoit */
971 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
972 message->param.disconnectinfo.cause = 41;
973 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
974 message_put(message);
975 new_state(PORT_STATE_RELEASE);
976 trigger_work(&p_s_delete);
981 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
983 PERROR("Failed to create handle\n");
984 /* send release message to endpoit */
985 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
986 message->param.disconnectinfo.cause = 41;
987 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
988 message_put(message);
989 new_state(PORT_STATE_RELEASE);
990 trigger_work(&p_s_delete);
994 sip_trace_header(this, "NEW handle", DIRECTION_IN);
995 add_trace("handle", "new", "0x%x", p_s_handle);
998 if (!p_s_rtp_ip_local) {
1001 /* extract IP from local peer */
1002 SCPY(local_ip, local);
1003 p = strchr(local_ip, ':');
1006 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
1007 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
1008 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
1010 ia.s_addr = htonl(p_s_rtp_ip_local);
1013 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1017 "m=audio %d RTP/AVP"
1018 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1019 for (i = 0; i < payloads; i++) {
1020 SPRINT(pt_str, " %d", payload_types[i]);
1021 SCAT(sdp_str, pt_str);
1023 SCAT(sdp_str, "\n");
1024 for (i = 0; i < payloads; i++) {
1025 SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
1026 SCAT(sdp_str, pt_str);
1028 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1030 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
1031 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
1033 sip_trace_header(this, "INVITE", DIRECTION_OUT);
1034 add_trace("from", "uri", "%s", from);
1035 add_trace("to", "uri", "%s", to);
1036 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1037 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1038 for (i = 0; i < payloads; i++)
1039 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1042 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1044 nua_invite(p_s_handle,
1045 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1046 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1047 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1048 NUTAG_MEDIA_ENABLE(0),
1049 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1050 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1051 new_state(PORT_STATE_OUT_SETUP);
1054 PDEBUG(DEBUG_SIP, "do overlap\n");
1055 new_state(PORT_STATE_OUT_OVERLAP);
1056 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1057 message_put(message);
1059 PDEBUG(DEBUG_SIP, "do proceeding\n");
1060 new_state(PORT_STATE_OUT_PROCEEDING);
1061 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1062 message_put(message);
1065 /* attach only if not already */
1066 epointlist = p_epointlist;
1068 if (epointlist->epoint_id == epoint_id)
1070 epointlist = epointlist->next;
1073 epointlist_new(epoint_id);
1078 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1080 // char sdp_str[256];
1081 // struct in_addr ia;
1083 switch (param->notifyinfo.notify) {
1084 case INFO_NOTIFY_REMOTE_HOLD:
1088 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
1090 "c=IN IP4 0.0.0.0\n"
1093 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1094 nua_info(p_s_handle,
1095 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1096 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1097 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1098 NUTAG_MEDIA_ENABLE(0),
1099 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1100 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1103 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1105 ia.s_addr = htonl(p_s_rtp_ip_local);
1108 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1112 "m=audio %d RTP/AVP %d\n"
1113 "a=rtpmap:%d %s/8000\n"
1114 , 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));
1115 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1116 nua_info(p_s_handle,
1117 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1118 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1119 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1120 NUTAG_MEDIA_ENABLE(0),
1121 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1122 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1130 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1134 /* prepare DTMF info payload */
1140 /* start invite to handle DTMF */
1141 nua_info(p_s_handle,
1142 NUTAG_MEDIA_ENABLE(0),
1143 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1144 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1149 /* NOTE: incomplete and not working */
1150 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1154 /* prepare DTMF info payload */
1158 , param->information.id);
1160 /* start invite to handle DTMF */
1161 nua_info(p_s_handle,
1162 NUTAG_MEDIA_ENABLE(0),
1163 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1164 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1169 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1171 if (Port::message_epoint(epoint_id, message_id, param))
1174 switch(message_id) {
1175 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1176 if (p_state != PORT_STATE_IN_SETUP
1177 && p_state != PORT_STATE_IN_PROCEEDING)
1179 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1180 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1181 add_trace("respond", "value", "180 Ringing");
1183 new_state(PORT_STATE_IN_ALERTING);
1186 case MESSAGE_CONNECT: /* call is connected on LCR side */
1187 if (p_state != PORT_STATE_IN_SETUP
1188 && p_state != PORT_STATE_IN_PROCEEDING
1189 && p_state != PORT_STATE_IN_ALERTING)
1191 message_connect(epoint_id, message_id, param);
1194 case MESSAGE_DISCONNECT: /* call has been disconnected */
1195 case MESSAGE_RELEASE: /* call has been released */
1196 message_release(epoint_id, message_id, param);
1199 case MESSAGE_SETUP: /* dial-out command received from epoint */
1200 message_setup(epoint_id, message_id, param);
1203 case MESSAGE_INFORMATION: /* overlap dialing */
1204 if (p_state != PORT_STATE_OUT_OVERLAP)
1206 message_information(epoint_id, message_id, param);
1209 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1210 if (p_state == PORT_STATE_CONNECT)
1211 message_dtmf(epoint_id, message_id, param);
1212 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1213 if (p_state == PORT_STATE_CONNECT)
1214 message_notify(epoint_id, message_id, param);
1218 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1224 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)
1228 if (!sip->sip_payload) {
1229 PDEBUG(DEBUG_SIP, "no payload given\n");
1233 sdp_parser_t *parser;
1236 sdp_attribute_t *attr;
1238 sdp_connection_t *conn;
1240 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1242 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1246 if (!(sdp = sdp_session(parser))) {
1247 sdp_parser_free(parser);
1250 for (m = sdp->sdp_media; m; m = m->m_next) {
1251 if (m->m_proto != sdp_proto_rtp)
1253 if (m->m_type != sdp_media_audio)
1255 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1257 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1258 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1260 if (m->m_connections) {
1261 conn = m->m_connections;
1262 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1263 inet_pton(AF_INET, conn->c_address, ip);
1264 *ip = ntohl(p_s_rtp_ip_remote);
1266 char *p = sip->sip_payload->pl_data;
1269 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1270 p = strstr(p, "c=IN IP4 ");
1272 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1273 sdp_parser_free(parser);
1277 if ((p = strchr(addr, '\n'))) *p = '\0';
1278 if ((p = strchr(addr, '\r'))) *p = '\0';
1279 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1280 inet_pton(AF_INET, addr, ip);
1281 *ip = ntohl(p_s_rtp_ip_remote);
1283 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1286 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1287 /* append to payload list, if there is space */
1288 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1289 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1290 media_type = MEDIA_TYPE_ALAW;
1291 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1292 media_type = MEDIA_TYPE_ULAW;
1293 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1294 media_type = MEDIA_TYPE_GSM;
1295 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1296 media_type = MEDIA_TYPE_GSM_EFR;
1297 else if (!strcmp(map->rm_encoding, "AMR"))
1298 media_type = MEDIA_TYPE_AMR;
1299 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1300 media_type = MEDIA_TYPE_GSM_HR;
1301 if (media_type && *payloads <= max_payloads) {
1302 *payload_types++ = map->rm_pt;
1303 *media_types++ = media_type;
1309 sdp_parser_free(parser);
1314 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[])
1316 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1317 const char *from = "", *to = "";
1319 class Endpoint *epoint;
1320 struct lcr_msg *message;
1321 struct interface *interface;
1322 int media_types[32];
1323 uint8_t payload_types[32];
1327 interface = getinterfacebyname(inst->interface_name);
1329 PERROR("Cannot find interface %s.\n", inst->interface_name);
1333 if (sip->sip_from && sip->sip_from->a_url)
1334 from = sip->sip_from->a_url->url_user;
1335 if (sip->sip_to && sip->sip_to->a_url)
1336 to = sip->sip_to->a_url->url_user;
1337 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1339 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1340 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1342 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1343 if (!p_s_rtp_bridge) {
1346 /* check if supported payload type exists */
1347 for (i = 0; i < payloads; i++) {
1348 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1351 if (i == payloads) {
1352 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1360 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1362 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1363 nua_handle_destroy(nh);
1365 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1367 add_trace("respond", "value", "415 Unsupported Media");
1369 add_trace("respond", "value", "400 Bad Request");
1370 add_trace("reason", NULL, "offered codec does not match");
1372 new_state(PORT_STATE_RELEASE);
1373 trigger_work(&p_s_delete);
1377 /* open local RTP peer (if not bridging) */
1378 if (!p_s_rtp_bridge && rtp_open() < 0) {
1379 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1380 nua_handle_destroy(nh);
1382 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1383 add_trace("respond", "value", "500 Internal Server Error");
1384 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1386 new_state(PORT_STATE_RELEASE);
1387 trigger_work(&p_s_delete);
1392 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1393 add_trace("handle", "new", "0x%x", nh);
1397 sip_trace_header(this, "INVITE", DIRECTION_IN);
1398 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1399 /* caller information */
1401 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1402 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1403 add_trace("calling", "present", "unavailable");
1405 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1406 add_trace("calling", "present", "allowed");
1407 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1408 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1409 SCPY(p_callerinfo.id, from);
1410 add_trace("calling", "number", "%s", from);
1412 SCPY(p_callerinfo.interface, inst->interface_name);
1413 /* dialing information */
1415 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1416 SCAT(p_dialinginfo.id, to);
1417 add_trace("dialing", "number", "%s", to);
1420 /* bearer capability */
1421 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1422 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1423 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1424 add_trace("bearer", "capa", "speech");
1425 add_trace("bearer", "mode", "circuit");
1426 /* if packet mode works some day, see dss1.cpp for conditions */
1427 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1431 /* create endpoint */
1433 FATAL("Incoming call but already got an endpoint.\n");
1434 if (!(epoint = new Endpoint(p_serial, 0)))
1435 FATAL("No memory for Endpoint instance\n");
1436 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1437 epointlist_new(epoint->ep_serial);
1439 #ifdef NUTAG_AUTO100
1440 /* send trying (proceeding) */
1441 nua_respond(nh, SIP_100_TRYING, TAG_END());
1442 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1443 add_trace("respond", "value", "100 Trying");
1447 new_state(PORT_STATE_IN_PROCEEDING);
1449 /* send setup message to endpoit */
1450 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1451 message->param.setup.port_type = p_type;
1452 // message->param.setup.dtmf = 0;
1453 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1454 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1455 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1456 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1457 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1458 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1459 if (p_s_rtp_bridge) {
1462 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1463 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1464 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1465 /* add codecs to setup message */
1466 for (i = 0; i < payloads; i++) {
1467 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1468 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1469 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1472 message->param.setup.rtpinfo.payloads = i;
1474 message_put(message);
1476 /* send progress, if tones are available and if we don't bridge */
1477 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1480 unsigned char payload_type;
1482 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1483 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
1484 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
1485 /* open local RTP peer (if not bridging) */
1486 if (rtp_connect() < 0) {
1487 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1488 nua_handle_destroy(nh);
1490 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1491 add_trace("respond", "value", "500 Internal Server Error");
1492 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1494 new_state(PORT_STATE_RELEASE);
1495 trigger_work(&p_s_delete);
1496 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1497 message->param.disconnectinfo.cause = 41;
1498 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1499 message_put(message);
1500 new_state(PORT_STATE_RELEASE);
1501 trigger_work(&p_s_delete);
1505 ia.s_addr = htonl(p_s_rtp_ip_local);
1509 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1513 "m=audio %d RTP/AVP %d\n"
1514 "a=rtpmap:%d %s/8000\n"
1515 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
1516 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1518 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1519 NUTAG_MEDIA_ENABLE(0),
1520 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1521 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1522 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1523 add_trace("respond", "value", "183 SESSION PROGRESS");
1524 add_trace("reason", NULL, "audio available");
1525 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1526 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1527 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
1532 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[])
1534 struct lcr_msg *message;
1537 PDEBUG(DEBUG_SIP, "bye received\n");
1539 sip_trace_header(this, "BYE", DIRECTION_IN);
1540 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1541 cause = atoi(sip->sip_reason->re_cause);
1542 add_trace("cause", "value", "%d", cause);
1546 // let stack do bye automaticall, since it will not accept our response for some reason
1547 // nua_respond(nh, SIP_200_OK, TAG_END());
1548 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1549 add_trace("respond", "value", "200 OK");
1551 // nua_handle_destroy(nh);
1556 while(p_epointlist) {
1557 /* send setup message to endpoit */
1558 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1559 message->param.disconnectinfo.cause = cause ? : 16;
1560 message->param.disconnectinfo.location = LOCATION_BEYOND;
1561 message_put(message);
1563 free_epointlist(p_epointlist);
1565 new_state(PORT_STATE_RELEASE);
1566 trigger_work(&p_s_delete);
1569 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[])
1571 struct lcr_msg *message;
1573 PDEBUG(DEBUG_SIP, "cancel received\n");
1575 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1578 nua_handle_destroy(nh);
1583 while(p_epointlist) {
1584 /* send setup message to endpoit */
1585 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1586 message->param.disconnectinfo.cause = 16;
1587 message->param.disconnectinfo.location = LOCATION_BEYOND;
1588 message_put(message);
1590 free_epointlist(p_epointlist);
1592 new_state(PORT_STATE_RELEASE);
1593 trigger_work(&p_s_delete);
1596 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[])
1598 PDEBUG(DEBUG_SIP, "bye response received\n");
1600 nua_handle_destroy(nh);
1605 trigger_work(&p_s_delete);
1608 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[])
1610 PDEBUG(DEBUG_SIP, "cancel response received\n");
1612 nua_handle_destroy(nh);
1617 trigger_work(&p_s_delete);
1620 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[])
1622 struct lcr_msg *message;
1623 int cause = 0, location = 0;
1624 int media_types[32];
1625 uint8_t payload_types[32];
1628 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1630 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1631 add_trace("respond", "value", "%d", status);
1635 if (status == 183 || (status >= 200 && status <= 299)) {
1638 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1639 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1643 else if (!p_s_rtp_bridge) {
1644 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1645 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1652 nua_cancel(nh, TAG_END());
1653 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1654 add_trace("reason", NULL, "accepted codec does not match");
1657 location = LOCATION_PRIVATE_LOCAL;
1658 goto release_with_cause;
1661 /* connect to remote RTP (if not bridging) */
1662 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1663 nua_cancel(nh, TAG_END());
1664 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1665 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1668 location = LOCATION_PRIVATE_LOCAL;
1669 goto release_with_cause;
1677 PDEBUG(DEBUG_SIP, "do proceeding\n");
1678 new_state(PORT_STATE_OUT_PROCEEDING);
1679 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1680 message_put(message);
1684 PDEBUG(DEBUG_SIP, "do alerting\n");
1685 new_state(PORT_STATE_OUT_ALERTING);
1686 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1687 message_put(message);
1690 PDEBUG(DEBUG_SIP, "do progress\n");
1691 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1692 message->param.progressinfo.progress = 8;
1693 message->param.progressinfo.location = 10;
1694 if (p_s_rtp_bridge) {
1695 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1696 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1697 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1698 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1699 message->param.progressinfo.rtpinfo.payloads = 1;
1701 message_put(message);
1704 if (status < 100 || status > 199)
1706 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1712 if (status >= 200 && status <= 299) {
1713 PDEBUG(DEBUG_SIP, "do connect\n");
1714 nua_ack(nh, TAG_END());
1715 new_state(PORT_STATE_CONNECT);
1716 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1717 if (p_s_rtp_bridge) {
1718 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1719 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1720 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1721 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1722 message->param.connectinfo.rtpinfo.payloads = 1;
1724 message_put(message);
1727 cause = status2cause(status);
1728 location = LOCATION_BEYOND;
1731 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1733 while(p_epointlist) {
1734 /* send setup message to endpoit */
1735 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1736 message->param.disconnectinfo.cause = cause;
1737 message->param.disconnectinfo.location = location;
1738 message_put(message);
1740 free_epointlist(p_epointlist);
1743 new_state(PORT_STATE_RELEASE);
1747 trigger_work(&p_s_delete);
1750 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[])
1752 struct sip_inst *inst = (struct sip_inst *) magic;
1754 class Psip *psip = NULL;
1756 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1760 /* create or find port instance */
1761 if (event == nua_i_invite)
1764 struct interface *interface = interface_first;
1766 /* create call instance */
1767 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1769 if (!strcmp(interface->name, inst->interface_name))
1771 interface = interface->next;
1774 PERROR("Cannot find interface %s.\n", inst->interface_name);
1777 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1778 FATAL("Cannot create Port instance.\n");
1782 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1783 psip = (class Psip *)port;
1784 if (psip->p_s_handle == nh) {
1792 PERROR("no SIP Port found for handel %p\n", nh);
1793 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1794 nua_handle_destroy(nh);
1799 case nua_r_set_params:
1800 PDEBUG(DEBUG_SIP, "setparam response\n");
1803 PDEBUG(DEBUG_SIP, "error received\n");
1806 PDEBUG(DEBUG_SIP, "state change received\n");
1808 case nua_i_register:
1809 PDEBUG(DEBUG_SIP, "register received\n");
1812 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1815 PDEBUG(DEBUG_SIP, "ack received\n");
1818 PDEBUG(DEBUG_SIP, "active received\n");
1821 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1824 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1827 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1830 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1833 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1835 case nua_i_terminated:
1836 PDEBUG(DEBUG_SIP, "terminated received\n");
1839 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1843 /* received shutdown due to termination of RTP */
1844 void Psip::rtp_shutdown(void)
1846 struct lcr_msg *message;
1848 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1850 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1853 nua_handle_destroy(p_s_handle);
1856 while(p_epointlist) {
1857 /* send setup message to endpoit */
1858 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1859 message->param.disconnectinfo.cause = 16;
1860 message->param.disconnectinfo.location = LOCATION_BEYOND;
1861 message_put(message);
1863 free_epointlist(p_epointlist);
1865 new_state(PORT_STATE_RELEASE);
1866 trigger_work(&p_s_delete);
1869 int sip_init_inst(struct interface *interface)
1871 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1874 interface->sip_inst = inst;
1875 SCPY(inst->interface_name, interface->name);
1876 SCPY(inst->local_peer, interface->sip_local_peer);
1877 SCPY(inst->remote_peer, interface->sip_remote_peer);
1879 /* init root object */
1880 inst->root = su_root_create(inst);
1882 PERROR("Failed to create SIP root\n");
1883 sip_exit_inst(interface);
1887 SPRINT(local, "sip:%s",inst->local_peer);
1888 if (!strchr(inst->local_peer, ':'))
1889 SCAT(local, ":5060");
1890 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
1892 PERROR("Failed to create SIP stack object\n");
1893 sip_exit_inst(interface);
1896 nua_set_params(inst->nua,
1897 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1898 NUTAG_APPL_METHOD("INVITE"),
1899 NUTAG_APPL_METHOD("ACK"),
1900 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1901 NUTAG_APPL_METHOD("CANCEL"),
1902 NUTAG_APPL_METHOD("OPTIONS"),
1903 NUTAG_APPL_METHOD("NOTIFY"),
1904 NUTAG_APPL_METHOD("INFO"),
1906 #ifdef NUTAG_AUTO100
1910 NUTAG_AUTOANSWER(0),
1913 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1915 any_sip_interface = 1;
1920 void sip_exit_inst(struct interface *interface)
1922 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1927 su_root_destroy(inst->root);
1929 nua_destroy(inst->nua);
1931 FREE(inst, sizeof(*inst));
1932 interface->sip_inst = NULL;
1934 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1936 /* check if there is any other SIP interface left */
1937 interface = interface_first;
1939 if (interface->sip_inst)
1941 interface = interface->next;
1944 any_sip_interface = 0;
1947 extern su_log_t su_log_default[];
1948 extern su_log_t nua_log[];
1949 //extern su_log_t soa_log[];
1955 /* init SOFIA lib */
1957 su_home_init(sip_home);
1959 if (options.deb & DEBUG_SIP) {
1960 su_log_set_level(su_log_default, 9);
1961 su_log_set_level(nua_log, 9);
1962 //su_log_set_level(soa_log, 9);
1965 for (i = 0; i < 256; i++)
1966 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);
1968 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1975 su_home_deinit(sip_home);
1978 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1981 void sip_handle(void)
1983 struct interface *interface = interface_first;
1984 struct sip_inst *inst;
1987 if (interface->sip_inst) {
1988 inst = (struct sip_inst *) interface->sip_inst;
1989 su_root_step(inst->root, 0);
1991 interface = interface->next;
1995 /* deletes when back in event loop */
1996 static int delete_event(struct lcr_work *work, void *instance, int index)
1998 class Psip *psip = (class Psip *)instance;
2007 * generate audio, if no data is received from bridge
2010 void Psip::set_tone(const char *dir, const char *tone)
2012 Port::set_tone(dir, tone);
2017 void Psip::update_load(void)
2019 /* don't trigger load event if event already active */
2020 if (p_s_loadtimer.active)
2023 /* don't start timer if ... */
2024 if (!p_tone_name[0])
2027 p_s_next_tv_sec = 0;
2028 schedule_timer(&p_s_loadtimer, 0, 0); /* no delay the first time */
2031 static int load_timer(struct lcr_timer *timer, void *instance, int index)
2033 class Psip *psip = (class Psip *)instance;
2035 /* stop timer if ... */
2036 if (!psip->p_tone_name[0])
2044 #define SEND_SIP_LEN 160
2046 void Psip::load_tx(void)
2049 struct timeval current_time;
2050 int tosend = SEND_SIP_LEN, i;
2051 unsigned char buf[SEND_SIP_LEN], *p = buf;
2054 gettimeofday(¤t_time, NULL);
2055 if (!p_s_next_tv_sec) {
2056 /* if timer expired the first time, set next expected timeout 160 samples in advance */
2057 p_s_next_tv_sec = current_time.tv_sec;
2058 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2059 if (p_s_next_tv_usec >= 1000000) {
2060 p_s_next_tv_usec -= 1000000;
2063 schedule_timer(&p_s_loadtimer, 0, SEND_SIP_LEN * 125);
2065 diff = 1000000 * (current_time.tv_sec - p_s_next_tv_sec)
2066 + (current_time.tv_usec - p_s_next_tv_usec);
2067 if (diff < -SEND_SIP_LEN * 125 || diff > SEND_SIP_LEN * 125) {
2068 /* if clock drifts too much, set next timeout event to current timer + 160 */
2070 p_s_next_tv_sec = current_time.tv_sec;
2071 p_s_next_tv_usec = current_time.tv_usec + SEND_SIP_LEN * 125;
2072 if (p_s_next_tv_usec >= 1000000) {
2073 p_s_next_tv_usec -= 1000000;
2077 /* if diff is positive, it took too long, so next timeout will be earlier */
2078 p_s_next_tv_usec += SEND_SIP_LEN * 125;
2079 if (p_s_next_tv_usec >= 1000000) {
2080 p_s_next_tv_usec -= 1000000;
2084 schedule_timer(&p_s_loadtimer, 0, SEND_SIP_LEN * 125 - diff);
2088 if (p_tone_name[0]) {
2089 tosend -= read_audio(p, tosend);
2092 PERROR("buffer is not completely filled\n");
2097 for (i = 0; i < SEND_SIP_LEN; i++) {
2101 /* transmit data via rtp */
2102 rtp_send_frame(buf, SEND_SIP_LEN, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);