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);
40 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
43 if (interface->rtp_bridge)
45 p_s_sip_inst = interface->sip_inst;
46 memset(&p_s_delete, 0, sizeof(p_s_delete));
47 add_work(&p_s_delete, delete_event, this, 0);
50 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
51 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
52 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
53 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
54 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
55 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
57 p_s_rtp_ip_remote = 0;
58 p_s_rtp_port_local = 0;
59 p_s_rtp_port_remote = 0;
64 p_s_rtp_tx_action = 0;
66 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
68 FATAL("No SIP instance for interface\n");
77 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
79 del_work(&p_s_delete);
84 static const char *media_type2name(uint8_t media_type) {
92 case MEDIA_TYPE_GSM_HR:
94 case MEDIA_TYPE_GSM_EFR:
103 static void sip_trace_header(class Psip *sip, const char *message, int direction)
105 /* init trace with given values */
108 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
109 sip?sip->p_dialinginfo.id:NULL,
120 /* according to RFC 3550 */
122 #if __BYTE_ORDER == __LITTLE_ENDIAN
123 uint8_t csrc_count:4,
127 uint8_t payload_type:7,
129 #elif __BYTE_ORDER == __BIG_ENDIAN
140 } __attribute__((packed));
145 } __attribute__((packed));
147 #define RTP_VERSION 2
149 #define PAYLOAD_TYPE_ULAW 0
150 #define PAYLOAD_TYPE_ALAW 8
151 #define PAYLOAD_TYPE_GSM 3
153 /* decode an rtp frame */
154 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
156 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
157 struct rtp_x_hdr *rtpxh;
161 unsigned char *from, *to;
165 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
168 if (rtph->version != RTP_VERSION) {
169 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
172 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
173 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
174 if (payload_len < 0) {
175 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
176 "csrc count = %d)\n", len, rtph->csrc_count);
179 if (rtph->extension) {
180 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
181 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
182 "extension header\n");
185 rtpxh = (struct rtp_x_hdr *)payload;
186 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
188 payload_len -= x_len;
189 if (payload_len < 0) {
190 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
191 "extension header exceeds frame length\n");
196 if (payload_len < 0) {
197 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
201 payload_len -= payload[payload_len - 1];
202 if (payload_len < 0) {
203 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
204 "greater than payload\n");
209 switch (rtph->payload_type) {
211 we only support alaw and ulaw!
212 case RTP_PT_GSM_FULL:
213 if (payload_len != 33) {
214 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
215 "payload length != 33 (len = %d)\n",
221 if (payload_len != 31) {
222 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
223 "payload length != 31 (len = %d)\n",
228 case RTP_PT_GSM_HALF:
229 if (payload_len != 14) {
230 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
231 "payload length != 14 (len = %d)\n",
237 case PAYLOAD_TYPE_ALAW:
238 if (options.law != 'a') {
239 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
243 case PAYLOAD_TYPE_ULAW:
244 if (options.law == 'a') {
245 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
250 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
251 "type %d\n", rtph->payload_type);
255 if (payload_len <= 0) {
256 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
262 psip->record(payload, payload_len, 0); // from down
264 psip->tap(payload, payload_len, 0); // from down
269 if (psip->p_echotest) {
270 /* echo rtp data we just received */
271 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
275 *to++ = flip[*from++];
276 psip->bridge_tx(payload, payload_len);
281 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
283 class Psip *psip = (class Psip *) instance;
285 unsigned char buffer[256];
288 if ((what & LCR_FD_READ)) {
289 len = read(fd->fd, &buffer, sizeof(buffer));
291 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
292 // psip->rtp_close();
293 // psip->rtp_shutdown();
296 if (psip->p_s_rtp_is_connected)
297 rc = rtp_decode(psip, buffer, len);
303 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
305 // class Psip *psip = (class Psip *) instance;
307 unsigned char buffer[256];
309 if ((what & LCR_FD_READ)) {
310 len = read(fd->fd, &buffer, sizeof(buffer));
312 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
313 // psip->rtp_close();
314 // psip->rtp_shutdown();
317 PDEBUG(DEBUG_SIP, "rtcp!\n");
323 #define RTP_PORT_BASE 30000
324 #define RTP_PORT_MAX 39998
325 static unsigned short next_udp_port = RTP_PORT_BASE;
327 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
330 socklen_t alen = sizeof(*sin_local);
332 sin_local->sin_family = AF_INET;
333 sin_local->sin_addr.s_addr = htonl(ip);
334 sin_local->sin_port = htons(port);
336 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
340 /* retrieve the address we actually bound to, in case we
341 * passed INADDR_ANY as IP address */
342 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
345 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
348 socklen_t alen = sizeof(*sin_local);
350 sin_remote->sin_family = AF_INET;
351 sin_remote->sin_addr.s_addr = htonl(ip);
352 sin_remote->sin_port = htons(port);
354 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
356 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
360 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
363 int Psip::rtp_open(void)
368 unsigned short start_port;
371 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
377 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
379 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
385 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
388 ip = htonl(INADDR_ANY);
390 start_port = next_udp_port;
392 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
396 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port + 1);
398 p_s_rtp_port_local = next_udp_port;
399 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
402 /* reopen rtp socket and try again with next udp port */
403 unregister_fd(&p_s_rtp_fd);
404 close(p_s_rtp_fd.fd);
406 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
412 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
415 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
416 if (next_udp_port == start_port)
418 /* we must use rc2, in order to preserve rc */
421 PDEBUG(DEBUG_SIP, "failed to find port\n");
425 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
426 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
427 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
429 return p_s_rtp_port_local;
432 int Psip::rtp_connect(void)
437 ia.s_addr = htonl(p_s_rtp_ip_remote);
438 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
440 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);
444 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);
448 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
449 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
450 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
451 p_s_rtp_is_connected = 1;
455 void Psip::rtp_close(void)
457 if (p_s_rtp_fd.fd > 0) {
458 unregister_fd(&p_s_rtp_fd);
459 close(p_s_rtp_fd.fd);
462 if (p_s_rtcp_fd.fd > 0) {
463 unregister_fd(&p_s_rtcp_fd);
464 close(p_s_rtcp_fd.fd);
467 if (p_s_rtp_is_connected) {
468 PDEBUG(DEBUG_SIP, "rtp closed\n");
469 p_s_rtp_is_connected = 0;
474 void tv_difference(struct timeval *diff, const struct timeval *from,
475 const struct timeval *__to)
477 struct timeval _to = *__to, *to = &_to;
479 if (to->tv_usec < from->tv_usec) {
481 to->tv_usec += 1000000;
484 diff->tv_usec = to->tv_usec - from->tv_usec;
485 diff->tv_sec = to->tv_sec - from->tv_sec;
488 /* encode and send a rtp frame */
489 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
491 struct rtp_hdr *rtph;
493 int duration; /* in samples */
494 unsigned char buffer[256];
498 record(data, len, 1); // from up
500 tap(data, len, 1); // from up
502 if (!p_s_rtp_is_connected) {
507 if (!p_s_rtp_tx_action) {
508 /* initialize sequences */
509 p_s_rtp_tx_action = 1;
510 p_s_rtp_tx_ssrc = rand();
511 p_s_rtp_tx_sequence = random();
512 p_s_rtp_tx_timestamp = random();
513 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
516 switch (payload_type) {
518 we only support alaw and ulaw!
519 case RTP_PT_GSM_FULL:
527 case RTP_PT_GSM_HALF:
532 case PAYLOAD_TYPE_ALAW:
533 case PAYLOAD_TYPE_ULAW:
538 PERROR("unsupported message type %d\n", payload_type);
544 struct timeval tv, tv_diff;
545 long int usec_diff, frame_diff;
547 gettimeofday(&tv, NULL);
548 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
549 p_s_rtp_tx_last_tv = tv;
551 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
552 frame_diff = (usec_diff / 20000);
554 if (abs(frame_diff) > 1) {
555 long int frame_diff_excess = frame_diff - 1;
557 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
558 p_s_rtp_tx_sequence += frame_diff_excess;
559 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
564 rtph = (struct rtp_hdr *) buffer;
565 rtph->version = RTP_VERSION;
568 rtph->csrc_count = 0;
570 rtph->payload_type = payload_type;
571 rtph->sequence = htons(p_s_rtp_tx_sequence++);
572 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
573 p_s_rtp_tx_timestamp += duration;
574 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
575 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
577 if (p_s_rtp_fd.fd > 0) {
578 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
579 if (len != sizeof(struct rtp_hdr) + payload_len) {
580 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
590 /* receive from remote */
591 int Psip::bridge_rx(unsigned char *data, int len)
593 /* write to rx buffer */
595 p_s_rxdata[p_s_rxpos++] = flip[*data++];
596 if (p_s_rxpos == 160) {
599 /* transmit data via rtp */
600 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
607 /* taken from freeswitch */
608 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
609 static int status2cause(int status)
613 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
619 return 21; //SWITCH_CAUSE_CALL_REJECTED;
621 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
624 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
627 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
629 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
638 return 127; //SWITCH_CAUSE_INTERWORKING;
640 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
645 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
648 return 17; //SWITCH_CAUSE_USER_BUSY;
650 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
653 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
655 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
657 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
661 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
664 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
666 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
668 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
672 static int cause2status(int cause, int location, const char **st)
678 s = 404; *st = sip_404_Not_found;
681 s = 404; *st = sip_404_Not_found;
684 s = 404; *st = sip_404_Not_found;
687 s = 486; *st = sip_486_Busy_here;
690 s = 408; *st = sip_408_Request_timeout;
693 s = 480; *st = sip_480_Temporarily_unavailable;
696 s = 480; *st = sip_480_Temporarily_unavailable;
699 if (location == LOCATION_USER) {
700 s = 603; *st = sip_603_Decline;
702 s = 403; *st = sip_403_Forbidden;
706 //s = 301; *st = sip_301_Moved_permanently;
707 s = 410; *st = sip_410_Gone;
710 s = 410; *st = sip_410_Gone;
713 s = 502; *st = sip_502_Bad_gateway;
716 s = 484; *st = sip_484_Address_incomplete;
719 s = 501; *st = sip_501_Not_implemented;
722 s = 480; *st = sip_480_Temporarily_unavailable;
725 s = 503; *st = sip_503_Service_unavailable;
728 s = 503; *st = sip_503_Service_unavailable;
731 s = 503; *st = sip_503_Service_unavailable;
734 s = 503; *st = sip_503_Service_unavailable;
737 s = 503; *st = sip_503_Service_unavailable;
740 s = 403; *st = sip_403_Forbidden;
743 s = 403; *st = sip_403_Forbidden;
746 s = 503; *st = sip_503_Service_unavailable;
749 s = 488; *st = sip_488_Not_acceptable;
752 s = 501; *st = sip_501_Not_implemented;
755 s = 488; *st = sip_488_Not_acceptable;
758 s = 501; *st = sip_501_Not_implemented;
761 s = 403; *st = sip_403_Forbidden;
764 s = 503; *st = sip_503_Service_unavailable;
767 s = 504; *st = sip_504_Gateway_time_out;
770 s = 468; *st = sip_486_Busy_here;
777 * endpoint sends messages to the SIP port
780 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
784 struct lcr_msg *message;
786 unsigned char payload_type;
788 if (param->connectinfo.rtpinfo.port) {
789 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
791 media_type = param->connectinfo.rtpinfo.media_types[0];
792 payload_type = param->connectinfo.rtpinfo.payload_types[0];
793 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
794 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
795 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
796 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
797 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
799 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
800 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
801 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
802 /* open local RTP peer (if not bridging) */
803 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
804 nua_cancel(p_s_handle, TAG_END());
805 nua_handle_destroy(p_s_handle);
807 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
808 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
810 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
811 message->param.disconnectinfo.cause = 41;
812 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
813 message_put(message);
814 new_state(PORT_STATE_RELEASE);
815 trigger_work(&p_s_delete);
820 ia.s_addr = htonl(p_s_rtp_ip_local);
824 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
828 "m=audio %d RTP/AVP %d\n"
829 "a=rtpmap:%d %s/8000\n"
830 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
831 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
833 nua_respond(p_s_handle, SIP_200_OK,
834 NUTAG_MEDIA_ENABLE(0),
835 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
836 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
837 new_state(PORT_STATE_CONNECT);
838 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
839 add_trace("respond", "value", "200 OK");
840 add_trace("reason", NULL, "call connected");
841 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
842 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
843 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
849 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
851 struct lcr_msg *message;
852 char cause_str[128] = "";
853 int cause = param->disconnectinfo.cause;
854 int location = param->disconnectinfo.cause;
856 const char *status_text;
858 if (cause > 0 && cause <= 127) {
859 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
863 case PORT_STATE_OUT_SETUP:
864 case PORT_STATE_OUT_PROCEEDING:
865 case PORT_STATE_OUT_ALERTING:
866 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
867 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
869 add_trace("cause", "value", "%d", cause);
871 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
873 case PORT_STATE_IN_SETUP:
874 case PORT_STATE_IN_PROCEEDING:
875 case PORT_STATE_IN_ALERTING:
876 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
877 status = cause2status(cause, location, &status_text);
878 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
880 add_trace("cause", "value", "%d", cause);
881 add_trace("respond", "value", "%d %s", status, status_text);
883 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
884 nua_handle_destroy(p_s_handle);
886 trigger_work(&p_s_delete);
889 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
890 sip_trace_header(this, "BYE", DIRECTION_OUT);
892 add_trace("cause", "value", "%d", cause);
894 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
897 if (message_id == MESSAGE_DISCONNECT) {
898 while(p_epointlist) {
899 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
900 message->param.disconnectinfo.cause = CAUSE_NORMAL;
901 message->param.disconnectinfo.location = LOCATION_BEYOND;
902 message_put(message);
904 free_epointlist(p_epointlist);
908 new_state(PORT_STATE_RELEASE);
913 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
915 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
918 const char *local = inst->local_peer;
920 const char *remote = inst->remote_peer;
921 char sdp_str[512], pt_str[32];
923 struct epoint_list *epointlist;
924 sip_cseq_t *cseq = NULL;
925 struct lcr_msg *message;
926 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
927 unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
929 unsigned char *payload_types;
933 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
935 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
936 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
937 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
939 if (param->setup.rtpinfo.port) {
940 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
942 media_types = param->setup.rtpinfo.media_types;
943 payload_types = param->setup.rtpinfo.payload_types;
944 payloads = param->setup.rtpinfo.payloads;
945 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
946 p_s_rtp_port_local = param->setup.rtpinfo.port;
947 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
948 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
950 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
952 media_types = &lcr_media;
953 payload_types = &lcr_payload;
956 /* open local RTP peer (if not bridging) */
957 if (rtp_open() < 0) {
958 PERROR("Failed to open RTP sockets\n");
959 /* send release message to endpoit */
960 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
961 message->param.disconnectinfo.cause = 41;
962 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
963 message_put(message);
964 new_state(PORT_STATE_RELEASE);
965 trigger_work(&p_s_delete);
970 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
972 PERROR("Failed to create handle\n");
973 /* send release message to endpoit */
974 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
975 message->param.disconnectinfo.cause = 41;
976 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
977 message_put(message);
978 new_state(PORT_STATE_RELEASE);
979 trigger_work(&p_s_delete);
983 sip_trace_header(this, "NEW handle", DIRECTION_IN);
984 add_trace("handle", "new", "0x%x", p_s_handle);
987 if (!p_s_rtp_ip_local) {
990 /* extract IP from local peer */
991 SCPY(local_ip, local);
992 p = strchr(local_ip, ':');
995 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
996 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
997 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
999 ia.s_addr = htonl(p_s_rtp_ip_local);
1002 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1006 "m=audio %d RTP/AVP"
1007 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
1008 for (i = 0; i < payloads; i++) {
1009 SPRINT(pt_str, " %d", payload_types[i]);
1010 SCAT(sdp_str, pt_str);
1012 SCAT(sdp_str, "\n");
1013 for (i = 0; i < payloads; i++) {
1014 SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
1015 SCAT(sdp_str, pt_str);
1017 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1019 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
1020 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
1022 sip_trace_header(this, "INVITE", DIRECTION_OUT);
1023 add_trace("from", "uri", "%s", from);
1024 add_trace("to", "uri", "%s", to);
1025 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1026 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1027 for (i = 0; i < payloads; i++)
1028 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1031 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1033 nua_invite(p_s_handle,
1034 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1035 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1036 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1037 NUTAG_MEDIA_ENABLE(0),
1038 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1039 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1040 new_state(PORT_STATE_OUT_SETUP);
1043 PDEBUG(DEBUG_SIP, "do overlap\n");
1044 new_state(PORT_STATE_OUT_OVERLAP);
1045 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1046 message_put(message);
1048 PDEBUG(DEBUG_SIP, "do proceeding\n");
1049 new_state(PORT_STATE_OUT_PROCEEDING);
1050 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1051 message_put(message);
1054 /* attach only if not already */
1055 epointlist = p_epointlist;
1057 if (epointlist->epoint_id == epoint_id)
1059 epointlist = epointlist->next;
1062 epointlist_new(epoint_id);
1067 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1069 // char sdp_str[256];
1070 // struct in_addr ia;
1072 switch (param->notifyinfo.notify) {
1073 case INFO_NOTIFY_REMOTE_HOLD:
1077 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
1079 "c=IN IP4 0.0.0.0\n"
1082 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1083 nua_info(p_s_handle,
1084 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1085 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1086 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1087 NUTAG_MEDIA_ENABLE(0),
1088 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1089 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1092 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1094 ia.s_addr = htonl(p_s_rtp_ip_local);
1097 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1101 "m=audio %d RTP/AVP %d\n"
1102 "a=rtpmap:%d %s/8000\n"
1103 , 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));
1104 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1105 nua_info(p_s_handle,
1106 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1107 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1108 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1109 NUTAG_MEDIA_ENABLE(0),
1110 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1111 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1119 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1123 /* prepare DTMF info payload */
1129 /* start invite to handle DTMF */
1130 nua_info(p_s_handle,
1131 NUTAG_MEDIA_ENABLE(0),
1132 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1133 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1138 /* NOTE: incomplete and not working */
1139 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1143 /* prepare DTMF info payload */
1147 , param->information.id);
1149 /* start invite to handle DTMF */
1150 nua_info(p_s_handle,
1151 NUTAG_MEDIA_ENABLE(0),
1152 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1153 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1158 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1160 if (Port::message_epoint(epoint_id, message_id, param))
1163 switch(message_id) {
1164 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1165 if (p_state != PORT_STATE_IN_SETUP
1166 && p_state != PORT_STATE_IN_PROCEEDING)
1168 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1169 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1170 add_trace("respond", "value", "180 Ringing");
1172 new_state(PORT_STATE_IN_ALERTING);
1175 case MESSAGE_CONNECT: /* call is connected on LCR side */
1176 if (p_state != PORT_STATE_IN_SETUP
1177 && p_state != PORT_STATE_IN_PROCEEDING
1178 && p_state != PORT_STATE_IN_ALERTING)
1180 message_connect(epoint_id, message_id, param);
1183 case MESSAGE_DISCONNECT: /* call has been disconnected */
1184 case MESSAGE_RELEASE: /* call has been released */
1185 message_release(epoint_id, message_id, param);
1188 case MESSAGE_SETUP: /* dial-out command received from epoint */
1189 message_setup(epoint_id, message_id, param);
1192 case MESSAGE_INFORMATION: /* overlap dialing */
1193 if (p_state != PORT_STATE_OUT_OVERLAP)
1195 message_information(epoint_id, message_id, param);
1198 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1199 if (p_state == PORT_STATE_CONNECT)
1200 message_dtmf(epoint_id, message_id, param);
1201 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1202 if (p_state == PORT_STATE_CONNECT)
1203 message_notify(epoint_id, message_id, param);
1207 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1213 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)
1217 if (!sip->sip_payload) {
1218 PDEBUG(DEBUG_SIP, "no payload given\n");
1222 sdp_parser_t *parser;
1225 sdp_attribute_t *attr;
1227 sdp_connection_t *conn;
1229 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1231 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1235 if (!(sdp = sdp_session(parser))) {
1236 sdp_parser_free(parser);
1239 for (m = sdp->sdp_media; m; m = m->m_next) {
1240 if (m->m_proto != sdp_proto_rtp)
1242 if (m->m_type != sdp_media_audio)
1244 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1246 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1247 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1249 if (m->m_connections) {
1250 conn = m->m_connections;
1251 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1252 inet_pton(AF_INET, conn->c_address, ip);
1253 *ip = ntohl(p_s_rtp_ip_remote);
1255 char *p = sip->sip_payload->pl_data;
1258 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1259 p = strstr(p, "c=IN IP4 ");
1261 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1262 sdp_parser_free(parser);
1266 if ((p = strchr(addr, '\n'))) *p = '\0';
1267 if ((p = strchr(addr, '\r'))) *p = '\0';
1268 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1269 inet_pton(AF_INET, addr, ip);
1270 *ip = ntohl(p_s_rtp_ip_remote);
1272 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1275 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1276 /* append to payload list, if there is space */
1277 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1278 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1279 media_type = MEDIA_TYPE_ALAW;
1280 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1281 media_type = MEDIA_TYPE_ULAW;
1282 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1283 media_type = MEDIA_TYPE_GSM;
1284 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1285 media_type = MEDIA_TYPE_GSM_EFR;
1286 else if (!strcmp(map->rm_encoding, "AMR"))
1287 media_type = MEDIA_TYPE_AMR;
1288 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1289 media_type = MEDIA_TYPE_GSM_HR;
1290 if (media_type && *payloads <= max_payloads) {
1291 *payload_types++ = map->rm_pt;
1292 *media_types++ = media_type;
1298 sdp_parser_free(parser);
1303 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[])
1305 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1306 const char *from = "", *to = "";
1308 class Endpoint *epoint;
1309 struct lcr_msg *message;
1310 struct interface *interface;
1311 int media_types[32];
1312 uint8_t payload_types[32];
1315 interface = getinterfacebyname(inst->interface_name);
1317 PERROR("Cannot find interface %s.\n", inst->interface_name);
1321 if (sip->sip_from && sip->sip_from->a_url)
1322 from = sip->sip_from->a_url->url_user;
1323 if (sip->sip_to && sip->sip_to->a_url)
1324 to = sip->sip_to->a_url->url_user;
1325 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1327 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1328 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1330 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1331 if (!p_s_rtp_bridge) {
1334 /* check if supported payload type exists */
1335 for (i = 0; i < payloads; i++) {
1336 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1339 if (i == payloads) {
1340 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1348 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1350 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1351 nua_handle_destroy(nh);
1353 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1355 add_trace("respond", "value", "415 Unsupported Media");
1357 add_trace("respond", "value", "400 Bad Request");
1358 add_trace("reason", NULL, "offered codec does not match");
1360 new_state(PORT_STATE_RELEASE);
1361 trigger_work(&p_s_delete);
1365 /* open local RTP peer (if not bridging) */
1366 if (!p_s_rtp_bridge && rtp_open() < 0) {
1367 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1368 nua_handle_destroy(nh);
1370 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1371 add_trace("respond", "value", "500 Internal Server Error");
1372 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1374 new_state(PORT_STATE_RELEASE);
1375 trigger_work(&p_s_delete);
1380 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1381 add_trace("handle", "new", "0x%x", nh);
1385 sip_trace_header(this, "INVITE", DIRECTION_IN);
1386 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1387 /* caller information */
1389 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1390 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1391 add_trace("calling", "present", "unavailable");
1393 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1394 add_trace("calling", "present", "allowed");
1395 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1396 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1397 SCPY(p_callerinfo.id, from);
1398 add_trace("calling", "number", "%s", from);
1400 SCPY(p_callerinfo.interface, inst->interface_name);
1401 /* dialing information */
1403 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1404 SCAT(p_dialinginfo.id, to);
1405 add_trace("dialing", "number", "%s", to);
1408 /* bearer capability */
1409 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1410 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1411 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1412 add_trace("bearer", "capa", "speech");
1413 add_trace("bearer", "mode", "circuit");
1414 /* if packet mode works some day, see dss1.cpp for conditions */
1415 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1419 /* create endpoint */
1421 FATAL("Incoming call but already got an endpoint.\n");
1422 if (!(epoint = new Endpoint(p_serial, 0)))
1423 FATAL("No memory for Endpoint instance\n");
1424 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1425 epointlist_new(epoint->ep_serial);
1427 #ifdef NUTAG_AUTO100
1428 /* send trying (proceeding) */
1429 nua_respond(nh, SIP_100_TRYING, TAG_END());
1430 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1431 add_trace("respond", "value", "100 Trying");
1435 new_state(PORT_STATE_IN_PROCEEDING);
1437 /* send setup message to endpoit */
1438 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1439 message->param.setup.port_type = p_type;
1440 // message->param.setup.dtmf = 0;
1441 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1442 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1443 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1444 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1445 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1446 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1447 if (p_s_rtp_bridge) {
1450 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1451 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1452 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1453 /* add codecs to setup message */
1454 for (i = 0; i < payloads; i++) {
1455 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1456 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1457 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1460 message->param.setup.rtpinfo.payloads = i;
1462 message_put(message);
1466 - send tones that are clocked with timer, unless data is received from bridge
1467 /* send progress, if tones are available and if we don't bridge */
1468 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1471 unsigned char payload_type;
1473 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1474 payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
1475 /* open local RTP peer (if not bridging) */
1476 if (rtp_connect() < 0) {
1477 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1478 nua_handle_destroy(nh);
1480 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1481 add_trace("respond", "value", "500 Internal Server Error");
1482 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1484 new_state(PORT_STATE_RELEASE);
1485 trigger_work(&p_s_delete);
1486 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1487 message->param.disconnectinfo.cause = 41;
1488 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1489 message_put(message);
1490 new_state(PORT_STATE_RELEASE);
1491 trigger_work(&p_s_delete);
1495 ia.s_addr = htonl(p_s_rtp_ip_local);
1499 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1503 "m=audio %d RTP/AVP %d\n"
1504 "a=rtpmap:%d %s/8000\n"
1505 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, payload_type2name(payload_type));
1506 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1508 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1509 NUTAG_MEDIA_ENABLE(0),
1510 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1511 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1512 new_state(PORT_STATE_CONNECT);
1513 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1514 add_trace("respond", "value", "183 SESSION PROGRESS");
1515 add_trace("reason", NULL, "audio available");
1516 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1517 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1518 add_trace("rtp", "payload", "%d", payload_type);
1524 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[])
1526 struct lcr_msg *message;
1529 PDEBUG(DEBUG_SIP, "bye received\n");
1531 sip_trace_header(this, "BYE", DIRECTION_IN);
1532 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1533 cause = atoi(sip->sip_reason->re_cause);
1534 add_trace("cause", "value", "%d", cause);
1538 // let stack do bye automaticall, since it will not accept our response for some reason
1539 // nua_respond(nh, SIP_200_OK, TAG_END());
1540 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1541 add_trace("respond", "value", "200 OK");
1543 // nua_handle_destroy(nh);
1548 while(p_epointlist) {
1549 /* send setup message to endpoit */
1550 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1551 message->param.disconnectinfo.cause = cause ? : 16;
1552 message->param.disconnectinfo.location = LOCATION_BEYOND;
1553 message_put(message);
1555 free_epointlist(p_epointlist);
1557 new_state(PORT_STATE_RELEASE);
1558 trigger_work(&p_s_delete);
1561 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[])
1563 struct lcr_msg *message;
1565 PDEBUG(DEBUG_SIP, "cancel received\n");
1567 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1570 nua_handle_destroy(nh);
1575 while(p_epointlist) {
1576 /* send setup message to endpoit */
1577 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1578 message->param.disconnectinfo.cause = 16;
1579 message->param.disconnectinfo.location = LOCATION_BEYOND;
1580 message_put(message);
1582 free_epointlist(p_epointlist);
1584 new_state(PORT_STATE_RELEASE);
1585 trigger_work(&p_s_delete);
1588 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[])
1590 PDEBUG(DEBUG_SIP, "bye response received\n");
1592 nua_handle_destroy(nh);
1597 trigger_work(&p_s_delete);
1600 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[])
1602 PDEBUG(DEBUG_SIP, "cancel response received\n");
1604 nua_handle_destroy(nh);
1609 trigger_work(&p_s_delete);
1612 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[])
1614 struct lcr_msg *message;
1615 int cause = 0, location = 0;
1616 int media_types[32];
1617 uint8_t payload_types[32];
1620 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1622 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1623 add_trace("respond", "value", "%d", status);
1627 if (status == 183 || (status >= 200 && status <= 299)) {
1630 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1631 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1635 else if (!p_s_rtp_bridge) {
1636 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1637 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1644 nua_cancel(nh, TAG_END());
1645 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1646 add_trace("reason", NULL, "accepted codec does not match");
1649 location = LOCATION_PRIVATE_LOCAL;
1650 goto release_with_cause;
1653 /* connect to remote RTP (if not bridging) */
1654 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1655 nua_cancel(nh, TAG_END());
1656 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1657 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1660 location = LOCATION_PRIVATE_LOCAL;
1661 goto release_with_cause;
1669 PDEBUG(DEBUG_SIP, "do proceeding\n");
1670 new_state(PORT_STATE_OUT_PROCEEDING);
1671 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1672 message_put(message);
1676 PDEBUG(DEBUG_SIP, "do alerting\n");
1677 new_state(PORT_STATE_OUT_ALERTING);
1678 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1679 message_put(message);
1682 PDEBUG(DEBUG_SIP, "do progress\n");
1683 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1684 message->param.progressinfo.progress = 8;
1685 message->param.progressinfo.location = 10;
1686 if (p_s_rtp_bridge) {
1687 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1688 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1689 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1690 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1691 message->param.progressinfo.rtpinfo.payloads = 1;
1693 message_put(message);
1696 if (status < 100 || status > 199)
1698 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1704 if (status >= 200 && status <= 299) {
1705 PDEBUG(DEBUG_SIP, "do connect\n");
1706 nua_ack(nh, TAG_END());
1707 new_state(PORT_STATE_CONNECT);
1708 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1709 if (p_s_rtp_bridge) {
1710 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1711 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1712 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1713 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1714 message->param.connectinfo.rtpinfo.payloads = 1;
1716 message_put(message);
1719 cause = status2cause(status);
1720 location = LOCATION_BEYOND;
1723 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1725 while(p_epointlist) {
1726 /* send setup message to endpoit */
1727 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1728 message->param.disconnectinfo.cause = cause;
1729 message->param.disconnectinfo.location = location;
1730 message_put(message);
1732 free_epointlist(p_epointlist);
1735 new_state(PORT_STATE_RELEASE);
1739 trigger_work(&p_s_delete);
1742 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[])
1744 struct sip_inst *inst = (struct sip_inst *) magic;
1746 class Psip *psip = NULL;
1748 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1752 /* create or find port instance */
1753 if (event == nua_i_invite)
1756 struct interface *interface = interface_first;
1758 /* create call instance */
1759 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1761 if (!strcmp(interface->name, inst->interface_name))
1763 interface = interface->next;
1766 PERROR("Cannot find interface %s.\n", inst->interface_name);
1769 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1770 FATAL("Cannot create Port instance.\n");
1774 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1775 psip = (class Psip *)port;
1776 if (psip->p_s_handle == nh) {
1784 PERROR("no SIP Port found for handel %p\n", nh);
1785 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1786 nua_handle_destroy(nh);
1791 case nua_r_set_params:
1792 PDEBUG(DEBUG_SIP, "setparam response\n");
1795 PDEBUG(DEBUG_SIP, "error received\n");
1798 PDEBUG(DEBUG_SIP, "state change received\n");
1800 case nua_i_register:
1801 PDEBUG(DEBUG_SIP, "register received\n");
1804 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1807 PDEBUG(DEBUG_SIP, "ack received\n");
1810 PDEBUG(DEBUG_SIP, "active received\n");
1813 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1816 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1819 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1822 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1825 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1827 case nua_i_terminated:
1828 PDEBUG(DEBUG_SIP, "terminated received\n");
1831 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1835 /* received shutdown due to termination of RTP */
1836 void Psip::rtp_shutdown(void)
1838 struct lcr_msg *message;
1840 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1842 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1845 nua_handle_destroy(p_s_handle);
1848 while(p_epointlist) {
1849 /* send setup message to endpoit */
1850 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1851 message->param.disconnectinfo.cause = 16;
1852 message->param.disconnectinfo.location = LOCATION_BEYOND;
1853 message_put(message);
1855 free_epointlist(p_epointlist);
1857 new_state(PORT_STATE_RELEASE);
1858 trigger_work(&p_s_delete);
1861 int sip_init_inst(struct interface *interface)
1863 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1866 interface->sip_inst = inst;
1867 SCPY(inst->interface_name, interface->name);
1868 SCPY(inst->local_peer, interface->sip_local_peer);
1869 SCPY(inst->remote_peer, interface->sip_remote_peer);
1871 /* init root object */
1872 inst->root = su_root_create(inst);
1874 PERROR("Failed to create SIP root\n");
1875 sip_exit_inst(interface);
1879 SPRINT(local, "sip:%s",inst->local_peer);
1880 if (!strchr(inst->local_peer, ':'))
1881 SCAT(local, ":5060");
1882 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
1884 PERROR("Failed to create SIP stack object\n");
1885 sip_exit_inst(interface);
1888 nua_set_params(inst->nua,
1889 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1890 NUTAG_APPL_METHOD("INVITE"),
1891 NUTAG_APPL_METHOD("ACK"),
1892 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1893 NUTAG_APPL_METHOD("CANCEL"),
1894 NUTAG_APPL_METHOD("OPTIONS"),
1895 NUTAG_APPL_METHOD("NOTIFY"),
1896 NUTAG_APPL_METHOD("INFO"),
1898 #ifdef NUTAG_AUTO100
1902 NUTAG_AUTOANSWER(0),
1905 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1907 any_sip_interface = 1;
1912 void sip_exit_inst(struct interface *interface)
1914 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1919 su_root_destroy(inst->root);
1921 nua_destroy(inst->nua);
1923 FREE(inst, sizeof(*inst));
1924 interface->sip_inst = NULL;
1926 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1928 /* check if there is any other SIP interface left */
1929 interface = interface_first;
1931 if (interface->sip_inst)
1933 interface = interface->next;
1936 any_sip_interface = 0;
1939 extern su_log_t su_log_default[];
1940 extern su_log_t nua_log[];
1941 //extern su_log_t soa_log[];
1947 /* init SOFIA lib */
1949 su_home_init(sip_home);
1951 if (options.deb & DEBUG_SIP) {
1952 su_log_set_level(su_log_default, 9);
1953 su_log_set_level(nua_log, 9);
1954 //su_log_set_level(soa_log, 9);
1957 for (i = 0; i < 256; i++)
1958 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);
1960 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1967 su_home_deinit(sip_home);
1970 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1973 void sip_handle(void)
1975 struct interface *interface = interface_first;
1976 struct sip_inst *inst;
1979 if (interface->sip_inst) {
1980 inst = (struct sip_inst *) interface->sip_inst;
1981 su_root_step(inst->root, 0);
1983 interface = interface->next;
1987 /* deletes when back in event loop */
1988 static int delete_event(struct lcr_work *work, void *instance, int index)
1990 class Psip *psip = (class Psip *)instance;