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 //pthread_mutex_t mutex_msg;
23 su_home_t sip_home[1];
26 char interface_name[64];
33 static int delete_event(struct lcr_work *work, void *instance, int index);
38 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings, interface)
41 if (interface->rtp_bridge)
43 p_s_sip_inst = interface->sip_inst;
44 memset(&p_s_delete, 0, sizeof(p_s_delete));
45 add_work(&p_s_delete, delete_event, this, 0);
48 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
49 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
50 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
51 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
52 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
53 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
55 p_s_rtp_ip_remote = 0;
56 p_s_rtp_port_local = 0;
57 p_s_rtp_port_remote = 0;
62 p_s_rtp_tx_action = 0;
64 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
66 FATAL("No SIP instance for interface\n");
75 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
77 del_work(&p_s_delete);
82 static const char *media_type2name(uint8_t media_type) {
90 case MEDIA_TYPE_GSM_HR:
92 case MEDIA_TYPE_GSM_EFR:
101 static void sip_trace_header(class Psip *sip, const char *message, int direction)
103 /* init trace with given values */
106 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
107 sip?sip->p_dialinginfo.id:NULL,
118 /* according to RFC 3550 */
120 #if __BYTE_ORDER == __LITTLE_ENDIAN
121 uint8_t csrc_count:4,
125 uint8_t payload_type:7,
127 #elif __BYTE_ORDER == __BIG_ENDIAN
138 } __attribute__((packed));
143 } __attribute__((packed));
145 #define RTP_VERSION 2
147 #define PAYLOAD_TYPE_ULAW 0
148 #define PAYLOAD_TYPE_ALAW 8
149 #define PAYLOAD_TYPE_GSM 3
151 /* decode an rtp frame */
152 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
154 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
155 struct rtp_x_hdr *rtpxh;
159 unsigned char *from, *to;
163 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
166 if (rtph->version != RTP_VERSION) {
167 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
170 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
171 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
172 if (payload_len < 0) {
173 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
174 "csrc count = %d)\n", len, rtph->csrc_count);
177 if (rtph->extension) {
178 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
179 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
180 "extension header\n");
183 rtpxh = (struct rtp_x_hdr *)payload;
184 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
186 payload_len -= x_len;
187 if (payload_len < 0) {
188 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
189 "extension header exceeds frame length\n");
194 if (payload_len < 0) {
195 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
199 payload_len -= payload[payload_len - 1];
200 if (payload_len < 0) {
201 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
202 "greater than payload\n");
207 switch (rtph->payload_type) {
209 we only support alaw and ulaw!
210 case RTP_PT_GSM_FULL:
211 if (payload_len != 33) {
212 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
213 "payload length != 33 (len = %d)\n",
219 if (payload_len != 31) {
220 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
221 "payload length != 31 (len = %d)\n",
226 case RTP_PT_GSM_HALF:
227 if (payload_len != 14) {
228 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
229 "payload length != 14 (len = %d)\n",
235 case PAYLOAD_TYPE_ALAW:
236 if (options.law != 'a') {
237 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
241 case PAYLOAD_TYPE_ULAW:
242 if (options.law == 'a') {
243 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
248 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
249 "type %d\n", rtph->payload_type);
253 if (payload_len <= 0) {
254 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
261 if (psip->p_echotest) {
262 /* echo rtp data we just received */
263 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
267 *to++ = flip[*from++];
268 psip->bridge_tx(payload, payload_len);
273 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
275 class Psip *psip = (class Psip *) instance;
277 unsigned char buffer[256];
280 if ((what & LCR_FD_READ)) {
281 len = read(fd->fd, &buffer, sizeof(buffer));
283 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
284 // psip->rtp_close();
285 // psip->rtp_shutdown();
288 if (psip->p_s_rtp_is_connected)
289 rc = rtp_decode(psip, buffer, len);
295 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
297 // class Psip *psip = (class Psip *) instance;
299 unsigned char buffer[256];
301 if ((what & LCR_FD_READ)) {
302 len = read(fd->fd, &buffer, sizeof(buffer));
304 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
305 // psip->rtp_close();
306 // psip->rtp_shutdown();
309 PDEBUG(DEBUG_SIP, "rtcp!\n");
315 #define RTP_PORT_BASE 30000
316 #define RTP_PORT_MAX 39998
317 static unsigned short next_udp_port = RTP_PORT_BASE;
319 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
322 socklen_t alen = sizeof(*sin_local);
324 sin_local->sin_family = AF_INET;
325 sin_local->sin_addr.s_addr = htonl(ip);
326 sin_local->sin_port = htons(port);
328 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
332 /* retrieve the address we actually bound to, in case we
333 * passed INADDR_ANY as IP address */
334 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
337 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
340 socklen_t alen = sizeof(*sin_local);
342 sin_remote->sin_family = AF_INET;
343 sin_remote->sin_addr.s_addr = htonl(ip);
344 sin_remote->sin_port = htons(port);
346 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
348 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
352 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
355 int Psip::rtp_open(void)
360 unsigned short start_port;
363 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
369 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
371 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
377 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
380 ip = htonl(INADDR_ANY);
382 start_port = next_udp_port;
384 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
388 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port+1);
390 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
393 /* reopen rtp socket and try again with next udp port */
394 unregister_fd(&p_s_rtp_fd);
395 close(p_s_rtp_fd.fd);
397 rc2 = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
403 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
406 next_udp_port = (next_udp_port + 2 > RTP_PORT_MAX) ? RTP_PORT_BASE : next_udp_port + 2;
407 if (next_udp_port == start_port)
409 /* we must use rc2, in order to preserve rc */
412 PDEBUG(DEBUG_SIP, "failed to find port\n");
416 p_s_rtp_port_local = next_udp_port;
417 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
418 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
419 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
421 return p_s_rtp_port_local;
424 int Psip::rtp_connect(void)
429 ia.s_addr = htonl(p_s_rtp_ip_remote);
430 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
432 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);
436 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);
440 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
441 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
442 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
443 p_s_rtp_is_connected = 1;
447 void Psip::rtp_close(void)
449 if (p_s_rtp_fd.fd > 0) {
450 unregister_fd(&p_s_rtp_fd);
451 close(p_s_rtp_fd.fd);
454 if (p_s_rtcp_fd.fd > 0) {
455 unregister_fd(&p_s_rtcp_fd);
456 close(p_s_rtcp_fd.fd);
459 if (p_s_rtp_is_connected) {
460 PDEBUG(DEBUG_SIP, "rtp closed\n");
461 p_s_rtp_is_connected = 0;
466 void tv_difference(struct timeval *diff, const struct timeval *from,
467 const struct timeval *__to)
469 struct timeval _to = *__to, *to = &_to;
471 if (to->tv_usec < from->tv_usec) {
473 to->tv_usec += 1000000;
476 diff->tv_usec = to->tv_usec - from->tv_usec;
477 diff->tv_sec = to->tv_sec - from->tv_sec;
480 /* encode and send a rtp frame */
481 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
483 struct rtp_hdr *rtph;
485 int duration; /* in samples */
486 unsigned char buffer[256];
488 if (!p_s_rtp_is_connected) {
493 if (!p_s_rtp_tx_action) {
494 /* initialize sequences */
495 p_s_rtp_tx_action = 1;
496 p_s_rtp_tx_ssrc = rand();
497 p_s_rtp_tx_sequence = random();
498 p_s_rtp_tx_timestamp = random();
499 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
502 switch (payload_type) {
504 we only support alaw and ulaw!
505 case RTP_PT_GSM_FULL:
513 case RTP_PT_GSM_HALF:
518 case PAYLOAD_TYPE_ALAW:
519 case PAYLOAD_TYPE_ULAW:
524 PERROR("unsupported message type %d\n", payload_type);
530 struct timeval tv, tv_diff;
531 long int usec_diff, frame_diff;
533 gettimeofday(&tv, NULL);
534 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
535 p_s_rtp_tx_last_tv = tv;
537 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
538 frame_diff = (usec_diff / 20000);
540 if (abs(frame_diff) > 1) {
541 long int frame_diff_excess = frame_diff - 1;
543 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
544 p_s_rtp_tx_sequence += frame_diff_excess;
545 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
550 rtph = (struct rtp_hdr *) buffer;
551 rtph->version = RTP_VERSION;
554 rtph->csrc_count = 0;
556 rtph->payload_type = payload_type;
557 rtph->sequence = htons(p_s_rtp_tx_sequence++);
558 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
559 p_s_rtp_tx_timestamp += duration;
560 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
561 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
563 if (p_s_rtp_fd.fd > 0) {
564 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
565 if (len != sizeof(struct rtp_hdr) + payload_len) {
566 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
576 /* receive from remote */
577 int Psip::bridge_rx(unsigned char *data, int len)
579 /* write to rx buffer */
581 p_s_rxdata[p_s_rxpos++] = flip[*data++];
582 if (p_s_rxpos == 160) {
585 /* transmit data via rtp */
586 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
593 /* taken from freeswitch */
594 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
595 static int status2cause(int status)
599 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
605 return 21; //SWITCH_CAUSE_CALL_REJECTED;
607 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
610 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
613 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
615 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
624 return 127; //SWITCH_CAUSE_INTERWORKING;
626 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
631 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
634 return 17; //SWITCH_CAUSE_USER_BUSY;
636 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
639 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
641 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
643 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
647 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
650 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
652 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
654 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
658 static int cause2status(int cause, int location, const char **st)
664 s = 404; *st = sip_404_Not_found;
667 s = 404; *st = sip_404_Not_found;
670 s = 404; *st = sip_404_Not_found;
673 s = 486; *st = sip_486_Busy_here;
676 s = 408; *st = sip_408_Request_timeout;
679 s = 480; *st = sip_480_Temporarily_unavailable;
682 s = 480; *st = sip_480_Temporarily_unavailable;
685 if (location == LOCATION_USER) {
686 s = 603; *st = sip_603_Decline;
688 s = 403; *st = sip_403_Forbidden;
692 //s = 301; *st = sip_301_Moved_permanently;
693 s = 410; *st = sip_410_Gone;
696 s = 410; *st = sip_410_Gone;
699 s = 502; *st = sip_502_Bad_gateway;
702 s = 484; *st = sip_484_Address_incomplete;
705 s = 501; *st = sip_501_Not_implemented;
708 s = 480; *st = sip_480_Temporarily_unavailable;
711 s = 503; *st = sip_503_Service_unavailable;
714 s = 503; *st = sip_503_Service_unavailable;
717 s = 503; *st = sip_503_Service_unavailable;
720 s = 503; *st = sip_503_Service_unavailable;
723 s = 503; *st = sip_503_Service_unavailable;
726 s = 403; *st = sip_403_Forbidden;
729 s = 403; *st = sip_403_Forbidden;
732 s = 503; *st = sip_503_Service_unavailable;
735 s = 488; *st = sip_488_Not_acceptable;
738 s = 501; *st = sip_501_Not_implemented;
741 s = 488; *st = sip_488_Not_acceptable;
744 s = 501; *st = sip_501_Not_implemented;
747 s = 403; *st = sip_403_Forbidden;
750 s = 503; *st = sip_503_Service_unavailable;
753 s = 504; *st = sip_504_Gateway_time_out;
756 s = 468; *st = sip_486_Busy_here;
763 * endpoint sends messages to the SIP port
766 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
770 struct lcr_msg *message;
772 unsigned char payload_type;
774 if (param->connectinfo.rtpinfo.port) {
775 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
777 media_type = param->connectinfo.rtpinfo.media_types[0];
778 payload_type = param->connectinfo.rtpinfo.payload_types[0];
779 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
780 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
781 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
782 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
783 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
785 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
786 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
787 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
788 /* open local RTP peer (if not bridging) */
789 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
790 nua_cancel(p_s_handle, TAG_END());
791 nua_handle_destroy(p_s_handle);
793 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
794 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
796 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
797 message->param.disconnectinfo.cause = 41;
798 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
799 message_put(message);
800 new_state(PORT_STATE_RELEASE);
801 trigger_work(&p_s_delete);
806 ia.s_addr = htonl(p_s_rtp_ip_local);
810 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
814 "m=audio %d RTP/AVP %d\n"
815 "a=rtpmap:%d %s/8000\n"
816 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
817 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
819 nua_respond(p_s_handle, SIP_200_OK,
820 NUTAG_MEDIA_ENABLE(0),
821 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
822 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
823 new_state(PORT_STATE_CONNECT);
824 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
825 add_trace("respond", "value", "200 OK");
826 add_trace("reason", NULL, "call connected");
827 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
828 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
829 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
835 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
837 struct lcr_msg *message;
838 char cause_str[128] = "";
839 int cause = param->disconnectinfo.cause;
840 int location = param->disconnectinfo.cause;
842 const char *status_text;
844 if (cause > 0 && cause <= 127) {
845 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
849 case PORT_STATE_OUT_SETUP:
850 case PORT_STATE_OUT_PROCEEDING:
851 case PORT_STATE_OUT_ALERTING:
852 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
853 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
855 add_trace("cause", "value", "%d", cause);
857 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
859 case PORT_STATE_IN_SETUP:
860 case PORT_STATE_IN_PROCEEDING:
861 case PORT_STATE_IN_ALERTING:
862 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
863 status = cause2status(cause, location, &status_text);
864 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
866 add_trace("cause", "value", "%d", cause);
867 add_trace("respond", "value", "%d %s", status, status_text);
869 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
870 nua_handle_destroy(p_s_handle);
872 trigger_work(&p_s_delete);
875 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
876 sip_trace_header(this, "BYE", DIRECTION_OUT);
878 add_trace("cause", "value", "%d", cause);
880 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
883 if (message_id == MESSAGE_DISCONNECT) {
884 while(p_epointlist) {
885 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
886 message->param.disconnectinfo.cause = CAUSE_NORMAL;
887 message->param.disconnectinfo.location = LOCATION_BEYOND;
888 message_put(message);
890 free_epointlist(p_epointlist);
894 new_state(PORT_STATE_RELEASE);
899 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
901 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
904 const char *local = inst->local_peer;
906 const char *remote = inst->remote_peer;
907 char sdp_str[512], pt_str[32];
909 struct epoint_list *epointlist;
910 sip_cseq_t *cseq = NULL;
911 struct lcr_msg *message;
912 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
913 unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
915 unsigned char *payload_types;
919 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
921 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
922 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
923 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
925 if (param->setup.rtpinfo.port) {
926 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
928 media_types = param->setup.rtpinfo.media_types;
929 payload_types = param->setup.rtpinfo.payload_types;
930 payloads = param->setup.rtpinfo.payloads;
931 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
932 p_s_rtp_port_local = param->setup.rtpinfo.port;
933 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
934 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
936 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
938 media_types = &lcr_media;
939 payload_types = &lcr_payload;
942 /* open local RTP peer (if not bridging) */
943 if (rtp_open() < 0) {
944 PERROR("Failed to open RTP sockets\n");
945 /* send release message to endpoit */
946 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
947 message->param.disconnectinfo.cause = 41;
948 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
949 message_put(message);
950 new_state(PORT_STATE_RELEASE);
951 trigger_work(&p_s_delete);
956 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
958 PERROR("Failed to create handle\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);
969 sip_trace_header(this, "NEW handle", DIRECTION_IN);
970 add_trace("handle", "new", "0x%x", p_s_handle);
973 if (!p_s_rtp_ip_local) {
976 /* extract IP from local peer */
977 SCPY(local_ip, local);
978 p = strchr(local_ip, ':');
981 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
982 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
983 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
985 ia.s_addr = htonl(p_s_rtp_ip_local);
988 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
993 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
994 for (i = 0; i < payloads; i++) {
995 SPRINT(pt_str, " %d", payload_types[i]);
996 SCAT(sdp_str, pt_str);
999 for (i = 0; i < payloads; i++) {
1000 SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
1001 SCAT(sdp_str, pt_str);
1003 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1005 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
1006 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
1008 sip_trace_header(this, "INVITE", DIRECTION_OUT);
1009 add_trace("from", "uri", "%s", from);
1010 add_trace("to", "uri", "%s", to);
1011 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1012 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1013 for (i = 0; i < payloads; i++)
1014 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
1017 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1019 nua_invite(p_s_handle,
1020 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1021 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1022 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1023 NUTAG_MEDIA_ENABLE(0),
1024 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1025 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1026 new_state(PORT_STATE_OUT_SETUP);
1029 PDEBUG(DEBUG_SIP, "do overlap\n");
1030 new_state(PORT_STATE_OUT_OVERLAP);
1031 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1032 message_put(message);
1034 PDEBUG(DEBUG_SIP, "do proceeding\n");
1035 new_state(PORT_STATE_OUT_PROCEEDING);
1036 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1037 message_put(message);
1040 /* attach only if not already */
1041 epointlist = p_epointlist;
1043 if (epointlist->epoint_id == epoint_id)
1045 epointlist = epointlist->next;
1048 epointlist_new(epoint_id);
1053 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1055 // char sdp_str[256];
1056 // struct in_addr ia;
1058 switch (param->notifyinfo.notify) {
1059 case INFO_NOTIFY_REMOTE_HOLD:
1063 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
1065 "c=IN IP4 0.0.0.0\n"
1068 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1069 nua_info(p_s_handle,
1070 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1071 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1072 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1073 NUTAG_MEDIA_ENABLE(0),
1074 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1075 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1078 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1080 ia.s_addr = htonl(p_s_rtp_ip_local);
1083 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1087 "m=audio %d RTP/AVP %d\n"
1088 "a=rtpmap:%d %s/8000\n"
1089 , 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));
1090 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1091 nua_info(p_s_handle,
1092 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1093 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1094 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1095 NUTAG_MEDIA_ENABLE(0),
1096 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1097 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1105 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1109 /* prepare DTMF info payload */
1115 /* start invite to handle DTMF */
1116 nua_info(p_s_handle,
1117 NUTAG_MEDIA_ENABLE(0),
1118 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1119 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1124 /* NOTE: incomplete and not working */
1125 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1129 /* prepare DTMF info payload */
1133 , param->information.id);
1135 /* start invite to handle DTMF */
1136 nua_info(p_s_handle,
1137 NUTAG_MEDIA_ENABLE(0),
1138 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1139 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1144 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1146 if (Port::message_epoint(epoint_id, message_id, param))
1149 switch(message_id) {
1150 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1151 if (p_state != PORT_STATE_IN_SETUP
1152 && p_state != PORT_STATE_IN_PROCEEDING)
1154 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1155 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1156 add_trace("respond", "value", "180 Ringing");
1158 new_state(PORT_STATE_IN_ALERTING);
1161 case MESSAGE_CONNECT: /* call is connected on LCR side */
1162 if (p_state != PORT_STATE_IN_SETUP
1163 && p_state != PORT_STATE_IN_PROCEEDING
1164 && p_state != PORT_STATE_IN_ALERTING)
1166 message_connect(epoint_id, message_id, param);
1169 case MESSAGE_DISCONNECT: /* call has been disconnected */
1170 case MESSAGE_RELEASE: /* call has been released */
1171 message_release(epoint_id, message_id, param);
1174 case MESSAGE_SETUP: /* dial-out command received from epoint */
1175 message_setup(epoint_id, message_id, param);
1178 case MESSAGE_INFORMATION: /* overlap dialing */
1179 if (p_state != PORT_STATE_OUT_OVERLAP)
1181 message_information(epoint_id, message_id, param);
1184 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1185 if (p_state == PORT_STATE_CONNECT)
1186 message_dtmf(epoint_id, message_id, param);
1187 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1188 if (p_state == PORT_STATE_CONNECT)
1189 message_notify(epoint_id, message_id, param);
1193 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1199 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)
1203 if (!sip->sip_payload) {
1204 PDEBUG(DEBUG_SIP, "no payload given\n");
1208 sdp_parser_t *parser;
1211 sdp_attribute_t *attr;
1213 sdp_connection_t *conn;
1215 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1217 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1221 if (!(sdp = sdp_session(parser))) {
1222 sdp_parser_free(parser);
1225 for (m = sdp->sdp_media; m; m = m->m_next) {
1226 if (m->m_proto != sdp_proto_rtp)
1228 if (m->m_type != sdp_media_audio)
1230 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1232 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1233 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1235 if (m->m_connections) {
1236 conn = m->m_connections;
1237 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1238 inet_pton(AF_INET, conn->c_address, ip);
1239 *ip = ntohl(p_s_rtp_ip_remote);
1241 char *p = sip->sip_payload->pl_data;
1244 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1245 p = strstr(p, "c=IN IP4 ");
1247 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1248 sdp_parser_free(parser);
1252 if ((p = strchr(addr, '\n'))) *p = '\0';
1253 if ((p = strchr(addr, '\r'))) *p = '\0';
1254 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1255 inet_pton(AF_INET, addr, ip);
1256 *ip = ntohl(p_s_rtp_ip_remote);
1258 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1261 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1262 /* append to payload list, if there is space */
1263 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1264 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1265 media_type = MEDIA_TYPE_ALAW;
1266 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1267 media_type = MEDIA_TYPE_ULAW;
1268 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1269 media_type = MEDIA_TYPE_GSM;
1270 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1271 media_type = MEDIA_TYPE_GSM_EFR;
1272 else if (!strcmp(map->rm_encoding, "AMR"))
1273 media_type = MEDIA_TYPE_AMR;
1274 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1275 media_type = MEDIA_TYPE_GSM_HR;
1276 if (media_type && *payloads <= max_payloads) {
1277 *payload_types++ = map->rm_pt;
1278 *media_types++ = media_type;
1284 sdp_parser_free(parser);
1289 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[])
1291 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1292 const char *from = "", *to = "";
1294 class Endpoint *epoint;
1295 struct lcr_msg *message;
1296 struct interface *interface;
1297 int media_types[32];
1298 uint8_t payload_types[32];
1301 interface = getinterfacebyname(inst->interface_name);
1303 PERROR("Cannot find interface %s.\n", inst->interface_name);
1307 if (sip->sip_from && sip->sip_from->a_url)
1308 from = sip->sip_from->a_url->url_user;
1309 if (sip->sip_to && sip->sip_to->a_url)
1310 to = sip->sip_to->a_url->url_user;
1311 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1313 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1314 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1316 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1317 if (!p_s_rtp_bridge) {
1320 /* check if supported payload type exists */
1321 for (i = 0; i < payloads; i++) {
1322 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1325 if (i == payloads) {
1326 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1334 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1336 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1337 nua_handle_destroy(nh);
1339 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1341 add_trace("respond", "value", "415 Unsupported Media");
1343 add_trace("respond", "value", "400 Bad Request");
1344 add_trace("reason", NULL, "offered codec does not match");
1346 new_state(PORT_STATE_RELEASE);
1347 trigger_work(&p_s_delete);
1351 /* open local RTP peer (if not bridging) */
1352 if (!p_s_rtp_bridge && rtp_open() < 0) {
1353 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1354 nua_handle_destroy(nh);
1356 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1357 add_trace("respond", "value", "500 Internal Server Error");
1358 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1360 new_state(PORT_STATE_RELEASE);
1361 trigger_work(&p_s_delete);
1366 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1367 add_trace("handle", "new", "0x%x", nh);
1371 sip_trace_header(this, "INVITE", DIRECTION_IN);
1372 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1373 /* caller information */
1375 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1376 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1377 add_trace("calling", "present", "unavailable");
1379 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1380 add_trace("calling", "present", "allowed");
1381 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1382 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1383 SCPY(p_callerinfo.id, from);
1384 add_trace("calling", "number", "%s", from);
1386 SCPY(p_callerinfo.interface, inst->interface_name);
1387 /* dialing information */
1389 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1390 SCAT(p_dialinginfo.id, to);
1391 add_trace("dialing", "number", "%s", to);
1394 /* bearer capability */
1395 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1396 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1397 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1398 add_trace("bearer", "capa", "speech");
1399 add_trace("bearer", "mode", "circuit");
1400 /* if packet mode works some day, see dss1.cpp for conditions */
1401 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1405 /* create endpoint */
1407 FATAL("Incoming call but already got an endpoint.\n");
1408 if (!(epoint = new Endpoint(p_serial, 0)))
1409 FATAL("No memory for Endpoint instance\n");
1410 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1411 epointlist_new(epoint->ep_serial);
1413 #ifdef NUTAG_AUTO100
1414 /* send trying (proceeding) */
1415 nua_respond(nh, SIP_100_TRYING, TAG_END());
1416 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1417 add_trace("respond", "value", "100 Trying");
1421 new_state(PORT_STATE_IN_PROCEEDING);
1423 /* send setup message to endpoit */
1424 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1425 message->param.setup.port_type = p_type;
1426 // message->param.setup.dtmf = 0;
1427 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1428 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1429 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1430 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1431 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1432 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1433 if (p_s_rtp_bridge) {
1436 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1437 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1438 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1439 /* add codecs to setup message */
1440 for (i = 0; i < payloads; i++) {
1441 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1442 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1443 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1446 message->param.setup.rtpinfo.payloads = i;
1448 message_put(message);
1452 - send tones that are clocked with timer, unless data is received from bridge
1453 /* send progress, if tones are available and if we don't bridge */
1454 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1457 unsigned char payload_type;
1459 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1460 payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
1461 /* open local RTP peer (if not bridging) */
1462 if (rtp_connect() < 0) {
1463 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1464 nua_handle_destroy(nh);
1466 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1467 add_trace("respond", "value", "500 Internal Server Error");
1468 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1470 new_state(PORT_STATE_RELEASE);
1471 trigger_work(&p_s_delete);
1472 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1473 message->param.disconnectinfo.cause = 41;
1474 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1475 message_put(message);
1476 new_state(PORT_STATE_RELEASE);
1477 trigger_work(&p_s_delete);
1481 ia.s_addr = htonl(p_s_rtp_ip_local);
1485 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1489 "m=audio %d RTP/AVP %d\n"
1490 "a=rtpmap:%d %s/8000\n"
1491 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, payload_type2name(payload_type));
1492 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1494 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1495 NUTAG_MEDIA_ENABLE(0),
1496 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1497 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1498 new_state(PORT_STATE_CONNECT);
1499 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1500 add_trace("respond", "value", "183 SESSION PROGRESS");
1501 add_trace("reason", NULL, "audio available");
1502 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1503 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1504 add_trace("rtp", "payload", "%d", payload_type);
1510 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[])
1512 struct lcr_msg *message;
1515 PDEBUG(DEBUG_SIP, "bye received\n");
1517 sip_trace_header(this, "BYE", DIRECTION_IN);
1518 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1519 cause = atoi(sip->sip_reason->re_cause);
1520 add_trace("cause", "value", "%d", cause);
1524 // let stack do bye automaticall, since it will not accept our response for some reason
1525 // nua_respond(nh, SIP_200_OK, TAG_END());
1526 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1527 add_trace("respond", "value", "200 OK");
1529 // nua_handle_destroy(nh);
1534 while(p_epointlist) {
1535 /* send setup message to endpoit */
1536 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1537 message->param.disconnectinfo.cause = cause ? : 16;
1538 message->param.disconnectinfo.location = LOCATION_BEYOND;
1539 message_put(message);
1541 free_epointlist(p_epointlist);
1543 new_state(PORT_STATE_RELEASE);
1544 trigger_work(&p_s_delete);
1547 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[])
1549 struct lcr_msg *message;
1551 PDEBUG(DEBUG_SIP, "cancel received\n");
1553 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1556 nua_handle_destroy(nh);
1561 while(p_epointlist) {
1562 /* send setup message to endpoit */
1563 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1564 message->param.disconnectinfo.cause = 16;
1565 message->param.disconnectinfo.location = LOCATION_BEYOND;
1566 message_put(message);
1568 free_epointlist(p_epointlist);
1570 new_state(PORT_STATE_RELEASE);
1571 trigger_work(&p_s_delete);
1574 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[])
1576 PDEBUG(DEBUG_SIP, "bye response received\n");
1578 nua_handle_destroy(nh);
1583 trigger_work(&p_s_delete);
1586 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[])
1588 PDEBUG(DEBUG_SIP, "cancel response received\n");
1590 nua_handle_destroy(nh);
1595 trigger_work(&p_s_delete);
1598 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[])
1600 struct lcr_msg *message;
1601 int cause = 0, location = 0;
1602 int media_types[32];
1603 uint8_t payload_types[32];
1606 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1608 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1609 add_trace("respond", "value", "%d", status);
1613 if (status == 183 || (status >= 200 && status <= 299)) {
1616 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1617 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1621 else if (!p_s_rtp_bridge) {
1622 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1623 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1630 nua_cancel(nh, TAG_END());
1631 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1632 add_trace("reason", NULL, "accepted codec does not match");
1635 location = LOCATION_PRIVATE_LOCAL;
1636 goto release_with_cause;
1639 /* connect to remote RTP (if not bridging) */
1640 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1641 nua_cancel(nh, TAG_END());
1642 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1643 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1646 location = LOCATION_PRIVATE_LOCAL;
1647 goto release_with_cause;
1655 PDEBUG(DEBUG_SIP, "do proceeding\n");
1656 new_state(PORT_STATE_OUT_PROCEEDING);
1657 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1658 message_put(message);
1662 PDEBUG(DEBUG_SIP, "do alerting\n");
1663 new_state(PORT_STATE_OUT_ALERTING);
1664 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1665 message_put(message);
1668 PDEBUG(DEBUG_SIP, "do progress\n");
1669 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1670 message->param.progressinfo.progress = 8;
1671 message->param.progressinfo.location = 10;
1672 if (p_s_rtp_bridge) {
1673 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1674 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1675 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1676 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1677 message->param.progressinfo.rtpinfo.payloads = 1;
1679 message_put(message);
1682 if (status < 100 || status > 199)
1684 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1690 if (status >= 200 && status <= 299) {
1691 PDEBUG(DEBUG_SIP, "do connect\n");
1692 nua_ack(nh, TAG_END());
1693 new_state(PORT_STATE_CONNECT);
1694 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1695 if (p_s_rtp_bridge) {
1696 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1697 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1698 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1699 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1700 message->param.connectinfo.rtpinfo.payloads = 1;
1702 message_put(message);
1705 cause = status2cause(status);
1706 location = LOCATION_BEYOND;
1709 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1711 while(p_epointlist) {
1712 /* send setup message to endpoit */
1713 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1714 message->param.disconnectinfo.cause = cause;
1715 message->param.disconnectinfo.location = location;
1716 message_put(message);
1718 free_epointlist(p_epointlist);
1721 new_state(PORT_STATE_RELEASE);
1725 trigger_work(&p_s_delete);
1728 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[])
1730 struct sip_inst *inst = (struct sip_inst *) magic;
1732 class Psip *psip = NULL;
1734 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1738 /* create or find port instance */
1739 if (event == nua_i_invite)
1742 struct interface *interface = interface_first;
1744 /* create call instance */
1745 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1747 if (!strcmp(interface->name, inst->interface_name))
1749 interface = interface->next;
1752 PERROR("Cannot find interface %s.\n", inst->interface_name);
1755 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1756 FATAL("Cannot create Port instance.\n");
1760 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1761 psip = (class Psip *)port;
1762 if (psip->p_s_handle == nh) {
1770 PERROR("no SIP Port found for handel %p\n", nh);
1771 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1772 nua_handle_destroy(nh);
1777 case nua_r_set_params:
1778 PDEBUG(DEBUG_SIP, "setparam response\n");
1781 PDEBUG(DEBUG_SIP, "error received\n");
1784 PDEBUG(DEBUG_SIP, "state change received\n");
1786 case nua_i_register:
1787 PDEBUG(DEBUG_SIP, "register received\n");
1790 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1793 PDEBUG(DEBUG_SIP, "ack received\n");
1796 PDEBUG(DEBUG_SIP, "active received\n");
1799 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1802 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1805 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1808 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1811 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1813 case nua_i_terminated:
1814 PDEBUG(DEBUG_SIP, "terminated received\n");
1817 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1821 /* received shutdown due to termination of RTP */
1822 void Psip::rtp_shutdown(void)
1824 struct lcr_msg *message;
1826 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1828 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1831 nua_handle_destroy(p_s_handle);
1834 while(p_epointlist) {
1835 /* send setup message to endpoit */
1836 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1837 message->param.disconnectinfo.cause = 16;
1838 message->param.disconnectinfo.location = LOCATION_BEYOND;
1839 message_put(message);
1841 free_epointlist(p_epointlist);
1843 new_state(PORT_STATE_RELEASE);
1844 trigger_work(&p_s_delete);
1847 int sip_init_inst(struct interface *interface)
1849 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1852 interface->sip_inst = inst;
1853 SCPY(inst->interface_name, interface->name);
1854 SCPY(inst->local_peer, interface->sip_local_peer);
1855 SCPY(inst->remote_peer, interface->sip_remote_peer);
1857 /* init root object */
1858 inst->root = su_root_create(inst);
1860 PERROR("Failed to create SIP root\n");
1861 sip_exit_inst(interface);
1865 SPRINT(local, "sip:%s",inst->local_peer);
1866 if (!strchr(inst->local_peer, ':'))
1867 SCAT(local, ":5060");
1868 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
1870 PERROR("Failed to create SIP stack object\n");
1871 sip_exit_inst(interface);
1874 nua_set_params(inst->nua,
1875 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1876 NUTAG_APPL_METHOD("INVITE"),
1877 NUTAG_APPL_METHOD("ACK"),
1878 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1879 NUTAG_APPL_METHOD("CANCEL"),
1880 NUTAG_APPL_METHOD("OPTIONS"),
1881 NUTAG_APPL_METHOD("NOTIFY"),
1882 NUTAG_APPL_METHOD("INFO"),
1884 #ifdef NUTAG_AUTO100
1888 NUTAG_AUTOANSWER(0),
1891 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1896 void sip_exit_inst(struct interface *interface)
1898 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1903 su_root_destroy(inst->root);
1905 nua_destroy(inst->nua);
1907 FREE(inst, sizeof(*inst));
1908 interface->sip_inst = NULL;
1910 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1913 extern su_log_t su_log_default[];
1914 extern su_log_t nua_log[];
1915 //extern su_log_t soa_log[];
1921 /* init SOFIA lib */
1923 su_home_init(sip_home);
1925 if (options.deb & DEBUG_SIP) {
1926 su_log_set_level(su_log_default, 9);
1927 su_log_set_level(nua_log, 9);
1928 //su_log_set_level(soa_log, 9);
1931 for (i = 0; i < 256; i++)
1932 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);
1934 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1941 su_home_deinit(sip_home);
1944 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1947 void sip_handle(void)
1949 struct interface *interface = interface_first;
1950 struct sip_inst *inst;
1953 if (interface->sip_inst) {
1954 inst = (struct sip_inst *) interface->sip_inst;
1955 su_root_step(inst->root, 0);
1957 interface = interface->next;
1961 /* deletes when back in event loop */
1962 static int delete_event(struct lcr_work *work, void *instance, int index)
1964 class Psip *psip = (class Psip *)instance;