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)
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 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 rc = rtp_decode(psip, buffer, len);
294 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
296 // class Psip *psip = (class Psip *) instance;
298 unsigned char buffer[256];
300 if ((what & LCR_FD_READ)) {
301 len = read(fd->fd, &buffer, sizeof(buffer));
303 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
304 // psip->rtp_close();
305 // psip->rtp_shutdown();
308 PDEBUG(DEBUG_SIP, "rtcp!\n");
314 #define RTP_PORT_BASE 30000
315 static unsigned int next_udp_port = RTP_PORT_BASE;
317 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
320 socklen_t alen = sizeof(*sin_local);
322 sin_local->sin_family = AF_INET;
323 sin_local->sin_addr.s_addr = htonl(ip);
324 sin_local->sin_port = htons(port);
326 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
330 /* retrieve the address we actually bound to, in case we
331 * passed INADDR_ANY as IP address */
332 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
335 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
338 socklen_t alen = sizeof(*sin_local);
340 sin_remote->sin_family = AF_INET;
341 sin_remote->sin_addr.s_addr = htonl(ip);
342 sin_remote->sin_port = htons(port);
344 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
346 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
350 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
353 int Psip::rtp_open(void)
360 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
366 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
368 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
374 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
377 ip = htonl(INADDR_ANY);
379 for (next_udp_port = next_udp_port % 0xffff;
380 next_udp_port < 0xffff; next_udp_port += 2) {
381 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
385 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port+1);
390 PDEBUG(DEBUG_SIP, "failed to find port\n");
394 p_s_rtp_port_local = next_udp_port;
395 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
396 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
397 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
399 return p_s_rtp_port_local;
402 int Psip::rtp_connect(void)
407 ia.s_addr = htonl(p_s_rtp_ip_remote);
408 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
410 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);
414 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);
418 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
419 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
420 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
421 p_s_rtp_is_connected = 1;
425 void Psip::rtp_close(void)
427 if (p_s_rtp_fd.fd > 0) {
428 unregister_fd(&p_s_rtp_fd);
429 close(p_s_rtp_fd.fd);
432 if (p_s_rtcp_fd.fd > 0) {
433 unregister_fd(&p_s_rtcp_fd);
434 close(p_s_rtcp_fd.fd);
437 if (p_s_rtp_is_connected) {
438 PDEBUG(DEBUG_SIP, "rtp closed\n");
439 p_s_rtp_is_connected = 0;
444 void tv_difference(struct timeval *diff, const struct timeval *from,
445 const struct timeval *__to)
447 struct timeval _to = *__to, *to = &_to;
449 if (to->tv_usec < from->tv_usec) {
451 to->tv_usec += 1000000;
454 diff->tv_usec = to->tv_usec - from->tv_usec;
455 diff->tv_sec = to->tv_sec - from->tv_sec;
458 /* encode and send a rtp frame */
459 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
461 struct rtp_hdr *rtph;
463 int duration; /* in samples */
464 unsigned char buffer[256];
466 if (!p_s_rtp_is_connected) {
471 if (!p_s_rtp_tx_action) {
472 /* initialize sequences */
473 p_s_rtp_tx_action = 1;
474 p_s_rtp_tx_ssrc = rand();
475 p_s_rtp_tx_sequence = random();
476 p_s_rtp_tx_timestamp = random();
477 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
480 switch (payload_type) {
482 we only support alaw and ulaw!
483 case RTP_PT_GSM_FULL:
491 case RTP_PT_GSM_HALF:
496 case PAYLOAD_TYPE_ALAW:
497 case PAYLOAD_TYPE_ULAW:
502 PERROR("unsupported message type %d\n", payload_type);
508 struct timeval tv, tv_diff;
509 long int usec_diff, frame_diff;
511 gettimeofday(&tv, NULL);
512 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
513 p_s_rtp_tx_last_tv = tv;
515 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
516 frame_diff = (usec_diff / 20000);
518 if (abs(frame_diff) > 1) {
519 long int frame_diff_excess = frame_diff - 1;
521 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
522 p_s_rtp_tx_sequence += frame_diff_excess;
523 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
528 rtph = (struct rtp_hdr *) buffer;
529 rtph->version = RTP_VERSION;
532 rtph->csrc_count = 0;
534 rtph->payload_type = payload_type;
535 rtph->sequence = htons(p_s_rtp_tx_sequence++);
536 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
537 p_s_rtp_tx_timestamp += duration;
538 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
539 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
541 if (p_s_rtp_fd.fd > 0) {
542 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
543 if (len != sizeof(struct rtp_hdr) + payload_len) {
544 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
554 /* receive from remote */
555 int Psip::bridge_rx(unsigned char *data, int len)
557 /* write to rx buffer */
559 p_s_rxdata[p_s_rxpos++] = flip[*data++];
560 if (p_s_rxpos == 160) {
563 /* transmit data via rtp */
564 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
571 /* taken from freeswitch */
572 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
573 static int status2cause(int status)
577 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
583 return 21; //SWITCH_CAUSE_CALL_REJECTED;
585 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
588 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
591 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
593 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
602 return 127; //SWITCH_CAUSE_INTERWORKING;
604 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
609 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
612 return 17; //SWITCH_CAUSE_USER_BUSY;
614 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
617 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
619 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
621 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
625 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
628 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
630 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
632 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
636 static int cause2status(int cause, int location, const char **st)
642 s = 404; *st = sip_404_Not_found;
645 s = 404; *st = sip_404_Not_found;
648 s = 404; *st = sip_404_Not_found;
651 s = 486; *st = sip_486_Busy_here;
654 s = 408; *st = sip_408_Request_timeout;
657 s = 480; *st = sip_480_Temporarily_unavailable;
660 s = 480; *st = sip_480_Temporarily_unavailable;
663 if (location == LOCATION_USER) {
664 s = 603; *st = sip_603_Decline;
666 s = 403; *st = sip_403_Forbidden;
670 //s = 301; *st = sip_301_Moved_permanently;
671 s = 410; *st = sip_410_Gone;
674 s = 410; *st = sip_410_Gone;
677 s = 502; *st = sip_502_Bad_gateway;
680 s = 484; *st = sip_484_Address_incomplete;
683 s = 501; *st = sip_501_Not_implemented;
686 s = 480; *st = sip_480_Temporarily_unavailable;
689 s = 503; *st = sip_503_Service_unavailable;
692 s = 503; *st = sip_503_Service_unavailable;
695 s = 503; *st = sip_503_Service_unavailable;
698 s = 503; *st = sip_503_Service_unavailable;
701 s = 503; *st = sip_503_Service_unavailable;
704 s = 403; *st = sip_403_Forbidden;
707 s = 403; *st = sip_403_Forbidden;
710 s = 503; *st = sip_503_Service_unavailable;
713 s = 488; *st = sip_488_Not_acceptable;
716 s = 501; *st = sip_501_Not_implemented;
719 s = 488; *st = sip_488_Not_acceptable;
722 s = 501; *st = sip_501_Not_implemented;
725 s = 403; *st = sip_403_Forbidden;
728 s = 503; *st = sip_503_Service_unavailable;
731 s = 504; *st = sip_504_Gateway_time_out;
734 s = 468; *st = sip_486_Busy_here;
741 * endpoint sends messages to the SIP port
744 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
748 struct lcr_msg *message;
750 unsigned char payload_type;
752 if (param->connectinfo.rtpinfo.port) {
753 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
755 media_type = param->connectinfo.rtpinfo.media_types[0];
756 payload_type = param->connectinfo.rtpinfo.payload_types[0];
757 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
758 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
759 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
760 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
761 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
763 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
764 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
765 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
766 /* open local RTP peer (if not bridging) */
767 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
768 nua_cancel(p_s_handle, TAG_END());
769 nua_handle_destroy(p_s_handle);
771 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
772 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
774 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
775 message->param.disconnectinfo.cause = 41;
776 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
777 message_put(message);
778 new_state(PORT_STATE_RELEASE);
779 trigger_work(&p_s_delete);
784 ia.s_addr = htonl(p_s_rtp_ip_local);
788 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
792 "m=audio %d RTP/AVP %d\n"
793 "a=rtpmap:%d %s/8000\n"
794 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
795 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
797 nua_respond(p_s_handle, SIP_200_OK,
798 NUTAG_MEDIA_ENABLE(0),
799 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
800 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
801 new_state(PORT_STATE_CONNECT);
802 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
803 add_trace("respond", "value", "200 OK");
804 add_trace("reason", NULL, "call connected");
805 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
806 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
807 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
813 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
815 struct lcr_msg *message;
816 char cause_str[128] = "";
817 int cause = param->disconnectinfo.cause;
818 int location = param->disconnectinfo.cause;
820 const char *status_text;
822 if (cause > 0 && cause <= 127) {
823 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
827 case PORT_STATE_OUT_SETUP:
828 case PORT_STATE_OUT_PROCEEDING:
829 case PORT_STATE_OUT_ALERTING:
830 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
831 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
833 add_trace("cause", "value", "%d", cause);
835 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
837 case PORT_STATE_IN_SETUP:
838 case PORT_STATE_IN_PROCEEDING:
839 case PORT_STATE_IN_ALERTING:
840 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
841 status = cause2status(cause, location, &status_text);
842 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
844 add_trace("cause", "value", "%d", cause);
845 add_trace("respond", "value", "%d %s", status, status_text);
847 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
848 nua_handle_destroy(p_s_handle);
850 trigger_work(&p_s_delete);
853 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
854 sip_trace_header(this, "BYE", DIRECTION_OUT);
856 add_trace("cause", "value", "%d", cause);
858 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
861 if (message_id == MESSAGE_DISCONNECT) {
862 while(p_epointlist) {
863 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
864 message->param.disconnectinfo.cause = CAUSE_NORMAL;
865 message->param.disconnectinfo.location = LOCATION_BEYOND;
866 message_put(message);
868 free_epointlist(p_epointlist);
872 new_state(PORT_STATE_RELEASE);
877 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
879 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
882 const char *local = inst->local_peer;
884 const char *remote = inst->remote_peer;
885 char sdp_str[512], pt_str[32];
887 struct epoint_list *epointlist;
888 sip_cseq_t *cseq = NULL;
889 struct lcr_msg *message;
890 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
891 unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
893 unsigned char *payload_types;
897 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
899 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
900 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
901 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
903 if (param->setup.rtpinfo.port) {
904 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
906 media_types = param->setup.rtpinfo.media_types;
907 payload_types = param->setup.rtpinfo.payload_types;
908 payloads = param->setup.rtpinfo.payloads;
909 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
910 p_s_rtp_port_local = param->setup.rtpinfo.port;
911 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
912 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
914 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
916 media_types = &lcr_media;
917 payload_types = &lcr_payload;
920 /* open local RTP peer (if not bridging) */
921 if (rtp_open() < 0) {
922 PERROR("Failed to open RTP sockets\n");
923 /* send release message to endpoit */
924 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
925 message->param.disconnectinfo.cause = 41;
926 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
927 message_put(message);
928 new_state(PORT_STATE_RELEASE);
929 trigger_work(&p_s_delete);
934 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
936 PERROR("Failed to create handle\n");
937 /* send release message to endpoit */
938 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
939 message->param.disconnectinfo.cause = 41;
940 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
941 message_put(message);
942 new_state(PORT_STATE_RELEASE);
943 trigger_work(&p_s_delete);
947 sip_trace_header(this, "NEW handle", DIRECTION_IN);
948 add_trace("handle", "new", "0x%x", p_s_handle);
951 if (!p_s_rtp_ip_local) {
954 /* extract IP from local peer */
955 SCPY(local_ip, local);
956 p = strchr(local_ip, ':');
959 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
960 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
961 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
963 ia.s_addr = htonl(p_s_rtp_ip_local);
966 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
971 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
972 for (i = 0; i < payloads; i++) {
973 SPRINT(pt_str, " %d", payload_types[i]);
974 SCAT(sdp_str, pt_str);
977 for (i = 0; i < payloads; i++) {
978 SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
979 SCAT(sdp_str, pt_str);
981 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
983 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
984 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
986 sip_trace_header(this, "INVITE", DIRECTION_OUT);
987 add_trace("from", "uri", "%s", from);
988 add_trace("to", "uri", "%s", to);
989 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
990 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
991 for (i = 0; i < payloads; i++)
992 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
995 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
997 nua_invite(p_s_handle,
998 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
999 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1000 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1001 NUTAG_MEDIA_ENABLE(0),
1002 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1003 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1004 new_state(PORT_STATE_OUT_SETUP);
1007 PDEBUG(DEBUG_SIP, "do overlap\n");
1008 new_state(PORT_STATE_OUT_OVERLAP);
1009 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1010 message_put(message);
1012 PDEBUG(DEBUG_SIP, "do proceeding\n");
1013 new_state(PORT_STATE_OUT_PROCEEDING);
1014 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1015 message_put(message);
1018 /* attach only if not already */
1019 epointlist = p_epointlist;
1021 if (epointlist->epoint_id == epoint_id)
1023 epointlist = epointlist->next;
1026 epointlist_new(epoint_id);
1031 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1033 // char sdp_str[256];
1034 // struct in_addr ia;
1036 switch (param->notifyinfo.notify) {
1037 case INFO_NOTIFY_REMOTE_HOLD:
1041 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
1043 "c=IN IP4 0.0.0.0\n"
1046 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1047 nua_info(p_s_handle,
1048 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1049 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1050 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1051 NUTAG_MEDIA_ENABLE(0),
1052 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1053 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1056 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1058 ia.s_addr = htonl(p_s_rtp_ip_local);
1061 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1065 "m=audio %d RTP/AVP %d\n"
1066 "a=rtpmap:%d %s/8000\n"
1067 , 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));
1068 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %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());
1083 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1087 /* prepare DTMF info payload */
1093 /* start invite to handle DTMF */
1094 nua_info(p_s_handle,
1095 NUTAG_MEDIA_ENABLE(0),
1096 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1097 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1102 /* NOTE: incomplete and not working */
1103 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1107 /* prepare DTMF info payload */
1111 , param->information.id);
1113 /* start invite to handle DTMF */
1114 nua_info(p_s_handle,
1115 NUTAG_MEDIA_ENABLE(0),
1116 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1117 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1122 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1124 if (Port::message_epoint(epoint_id, message_id, param))
1127 switch(message_id) {
1128 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1129 if (p_state != PORT_STATE_IN_SETUP
1130 && p_state != PORT_STATE_IN_PROCEEDING)
1132 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1133 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1134 add_trace("respond", "value", "180 Ringing");
1136 new_state(PORT_STATE_IN_ALERTING);
1139 case MESSAGE_CONNECT: /* call is connected on LCR side */
1140 if (p_state != PORT_STATE_IN_SETUP
1141 && p_state != PORT_STATE_IN_PROCEEDING
1142 && p_state != PORT_STATE_IN_ALERTING)
1144 message_connect(epoint_id, message_id, param);
1147 case MESSAGE_DISCONNECT: /* call has been disconnected */
1148 case MESSAGE_RELEASE: /* call has been released */
1149 message_release(epoint_id, message_id, param);
1152 case MESSAGE_SETUP: /* dial-out command received from epoint */
1153 message_setup(epoint_id, message_id, param);
1156 case MESSAGE_INFORMATION: /* overlap dialing */
1157 if (p_state != PORT_STATE_OUT_OVERLAP)
1159 message_information(epoint_id, message_id, param);
1162 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1163 if (p_state == PORT_STATE_CONNECT)
1164 message_dtmf(epoint_id, message_id, param);
1165 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1166 if (p_state == PORT_STATE_CONNECT)
1167 message_notify(epoint_id, message_id, param);
1171 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1177 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)
1181 if (!sip->sip_payload) {
1182 PDEBUG(DEBUG_SIP, "no payload given\n");
1186 sdp_parser_t *parser;
1189 sdp_attribute_t *attr;
1191 sdp_connection_t *conn;
1193 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1195 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1199 if (!(sdp = sdp_session(parser))) {
1200 sdp_parser_free(parser);
1203 for (m = sdp->sdp_media; m; m = m->m_next) {
1204 if (m->m_proto != sdp_proto_rtp)
1206 if (m->m_type != sdp_media_audio)
1208 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1210 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1211 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1213 if (m->m_connections) {
1214 conn = m->m_connections;
1215 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1216 inet_pton(AF_INET, conn->c_address, ip);
1217 *ip = ntohl(p_s_rtp_ip_remote);
1219 char *p = sip->sip_payload->pl_data;
1222 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1223 p = strstr(p, "c=IN IP4 ");
1225 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1226 sdp_parser_free(parser);
1230 if ((p = strchr(addr, '\n'))) *p = '\0';
1231 if ((p = strchr(addr, '\r'))) *p = '\0';
1232 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1233 inet_pton(AF_INET, addr, ip);
1234 *ip = ntohl(p_s_rtp_ip_remote);
1236 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1239 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1240 /* append to payload list, if there is space */
1241 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1242 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1243 media_type = MEDIA_TYPE_ALAW;
1244 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1245 media_type = MEDIA_TYPE_ULAW;
1246 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1247 media_type = MEDIA_TYPE_GSM;
1248 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1249 media_type = MEDIA_TYPE_GSM_EFR;
1250 else if (!strcmp(map->rm_encoding, "AMR"))
1251 media_type = MEDIA_TYPE_AMR;
1252 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1253 media_type = MEDIA_TYPE_GSM_HR;
1254 if (media_type && *payloads <= max_payloads) {
1255 *payload_types++ = map->rm_pt;
1256 *media_types++ = media_type;
1262 sdp_parser_free(parser);
1267 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[])
1269 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1270 const char *from = "", *to = "";
1272 class Endpoint *epoint;
1273 struct lcr_msg *message;
1274 struct interface *interface;
1275 int media_types[32];
1276 uint8_t payload_types[32];
1279 interface = getinterfacebyname(inst->interface_name);
1281 PERROR("Cannot find interface %s.\n", inst->interface_name);
1285 if (sip->sip_from && sip->sip_from->a_url)
1286 from = sip->sip_from->a_url->url_user;
1287 if (sip->sip_to && sip->sip_to->a_url)
1288 to = sip->sip_to->a_url->url_user;
1289 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1291 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1292 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1294 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1295 if (!p_s_rtp_bridge) {
1298 /* check if supported payload type exists */
1299 for (i = 0; i < payloads; i++) {
1300 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1303 if (i == payloads) {
1304 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1312 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1314 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1315 nua_handle_destroy(nh);
1317 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1319 add_trace("respond", "value", "415 Unsupported Media");
1321 add_trace("respond", "value", "400 Bad Request");
1322 add_trace("reason", NULL, "offered codec does not match");
1324 new_state(PORT_STATE_RELEASE);
1325 trigger_work(&p_s_delete);
1329 /* open local RTP peer (if not bridging) */
1330 if (!p_s_rtp_bridge && rtp_open() < 0) {
1331 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1332 nua_handle_destroy(nh);
1334 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1335 add_trace("respond", "value", "500 Internal Server Error");
1336 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1338 new_state(PORT_STATE_RELEASE);
1339 trigger_work(&p_s_delete);
1344 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1345 add_trace("handle", "new", "0x%x", nh);
1349 sip_trace_header(this, "INVITE", DIRECTION_IN);
1350 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1351 /* caller information */
1353 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1354 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1355 add_trace("calling", "present", "unavailable");
1357 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1358 add_trace("calling", "present", "allowed");
1359 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1360 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1361 SCPY(p_callerinfo.id, from);
1362 add_trace("calling", "number", "%s", from);
1364 SCPY(p_callerinfo.interface, inst->interface_name);
1365 /* dialing information */
1367 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1368 SCAT(p_dialinginfo.id, to);
1369 add_trace("dialing", "number", "%s", to);
1372 /* bearer capability */
1373 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1374 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1375 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1376 add_trace("bearer", "capa", "speech");
1377 add_trace("bearer", "mode", "circuit");
1378 /* if packet mode works some day, see dss1.cpp for conditions */
1379 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1383 /* create endpoint */
1385 FATAL("Incoming call but already got an endpoint.\n");
1386 if (!(epoint = new Endpoint(p_serial, 0)))
1387 FATAL("No memory for Endpoint instance\n");
1388 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1389 epointlist_new(epoint->ep_serial);
1391 #ifdef NUTAG_AUTO100
1392 /* send trying (proceeding) */
1393 nua_respond(nh, SIP_100_TRYING, TAG_END());
1394 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1395 add_trace("respond", "value", "100 Trying");
1399 new_state(PORT_STATE_IN_PROCEEDING);
1401 /* send setup message to endpoit */
1402 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1403 message->param.setup.port_type = p_type;
1404 // message->param.setup.dtmf = 0;
1405 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1406 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1407 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1408 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1409 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1410 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1411 if (p_s_rtp_bridge) {
1414 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1415 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1416 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1417 /* add codecs to setup message */
1418 for (i = 0; i < payloads; i++) {
1419 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1420 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1421 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1424 message->param.setup.rtpinfo.payloads = i;
1426 message_put(message);
1430 - send tones that are clocked with timer, unless data is received from bridge
1431 /* send progress, if tones are available and if we don't bridge */
1432 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1435 unsigned char payload_type;
1437 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1438 payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
1439 /* open local RTP peer (if not bridging) */
1440 if (rtp_connect() < 0) {
1441 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1442 nua_handle_destroy(nh);
1444 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1445 add_trace("respond", "value", "500 Internal Server Error");
1446 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1448 new_state(PORT_STATE_RELEASE);
1449 trigger_work(&p_s_delete);
1450 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1451 message->param.disconnectinfo.cause = 41;
1452 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1453 message_put(message);
1454 new_state(PORT_STATE_RELEASE);
1455 trigger_work(&p_s_delete);
1459 ia.s_addr = htonl(p_s_rtp_ip_local);
1463 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1467 "m=audio %d RTP/AVP %d\n"
1468 "a=rtpmap:%d %s/8000\n"
1469 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, payload_type2name(payload_type));
1470 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1472 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1473 NUTAG_MEDIA_ENABLE(0),
1474 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1475 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1476 new_state(PORT_STATE_CONNECT);
1477 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1478 add_trace("respond", "value", "183 SESSION PROGRESS");
1479 add_trace("reason", NULL, "audio available");
1480 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1481 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1482 add_trace("rtp", "payload", "%d", payload_type);
1488 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[])
1490 struct lcr_msg *message;
1493 PDEBUG(DEBUG_SIP, "bye received\n");
1495 sip_trace_header(this, "BYE", DIRECTION_IN);
1496 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1497 cause = atoi(sip->sip_reason->re_cause);
1498 add_trace("cause", "value", "%d", cause);
1502 // let stack do bye automaticall, since it will not accept our response for some reason
1503 // nua_respond(nh, SIP_200_OK, TAG_END());
1504 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1505 add_trace("respond", "value", "200 OK");
1507 // nua_handle_destroy(nh);
1512 while(p_epointlist) {
1513 /* send setup message to endpoit */
1514 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1515 message->param.disconnectinfo.cause = cause ? : 16;
1516 message->param.disconnectinfo.location = LOCATION_BEYOND;
1517 message_put(message);
1519 free_epointlist(p_epointlist);
1521 new_state(PORT_STATE_RELEASE);
1522 trigger_work(&p_s_delete);
1525 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[])
1527 struct lcr_msg *message;
1529 PDEBUG(DEBUG_SIP, "cancel received\n");
1531 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1534 nua_handle_destroy(nh);
1539 while(p_epointlist) {
1540 /* send setup message to endpoit */
1541 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1542 message->param.disconnectinfo.cause = 16;
1543 message->param.disconnectinfo.location = LOCATION_BEYOND;
1544 message_put(message);
1546 free_epointlist(p_epointlist);
1548 new_state(PORT_STATE_RELEASE);
1549 trigger_work(&p_s_delete);
1552 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[])
1554 PDEBUG(DEBUG_SIP, "bye response received\n");
1556 nua_handle_destroy(nh);
1561 trigger_work(&p_s_delete);
1564 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[])
1566 PDEBUG(DEBUG_SIP, "cancel response received\n");
1568 nua_handle_destroy(nh);
1573 trigger_work(&p_s_delete);
1576 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[])
1578 struct lcr_msg *message;
1579 int cause = 0, location = 0;
1580 int media_types[32];
1581 uint8_t payload_types[32];
1584 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1586 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1587 add_trace("respond", "value", "%d", status);
1591 if (status == 183 || (status >= 200 && status <= 299)) {
1594 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1595 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1599 else if (!p_s_rtp_bridge) {
1600 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1601 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1608 nua_cancel(nh, TAG_END());
1609 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1610 add_trace("reason", NULL, "accepted codec does not match");
1613 location = LOCATION_PRIVATE_LOCAL;
1614 goto release_with_cause;
1617 /* connect to remote RTP (if not bridging) */
1618 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1619 nua_cancel(nh, TAG_END());
1620 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1621 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1624 location = LOCATION_PRIVATE_LOCAL;
1625 goto release_with_cause;
1633 PDEBUG(DEBUG_SIP, "do proceeding\n");
1634 new_state(PORT_STATE_OUT_PROCEEDING);
1635 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1636 message_put(message);
1640 PDEBUG(DEBUG_SIP, "do alerting\n");
1641 new_state(PORT_STATE_OUT_ALERTING);
1642 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1643 message_put(message);
1646 PDEBUG(DEBUG_SIP, "do progress\n");
1647 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1648 message->param.progressinfo.progress = 8;
1649 message->param.progressinfo.location = 10;
1650 if (p_s_rtp_bridge) {
1651 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1652 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1653 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1654 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1655 message->param.progressinfo.rtpinfo.payloads = 1;
1657 message_put(message);
1660 if (status < 100 || status > 199)
1662 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1668 if (status >= 200 && status <= 299) {
1669 PDEBUG(DEBUG_SIP, "do connect\n");
1670 nua_ack(nh, TAG_END());
1671 new_state(PORT_STATE_CONNECT);
1672 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1673 if (p_s_rtp_bridge) {
1674 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1675 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1676 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1677 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1678 message->param.connectinfo.rtpinfo.payloads = 1;
1680 message_put(message);
1683 cause = status2cause(status);
1684 location = LOCATION_BEYOND;
1687 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1689 while(p_epointlist) {
1690 /* send setup message to endpoit */
1691 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1692 message->param.disconnectinfo.cause = cause;
1693 message->param.disconnectinfo.location = location;
1694 message_put(message);
1696 free_epointlist(p_epointlist);
1699 new_state(PORT_STATE_RELEASE);
1703 trigger_work(&p_s_delete);
1706 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[])
1708 struct sip_inst *inst = (struct sip_inst *) magic;
1710 class Psip *psip = NULL;
1712 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1716 /* create or find port instance */
1717 if (event == nua_i_invite)
1720 struct interface *interface = interface_first;
1722 /* create call instance */
1723 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1725 if (!strcmp(interface->name, inst->interface_name))
1727 interface = interface->next;
1730 PERROR("Cannot find interface %s.\n", inst->interface_name);
1733 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1734 FATAL("Cannot create Port instance.\n");
1738 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1739 psip = (class Psip *)port;
1740 if (psip->p_s_handle == nh) {
1748 PERROR("no SIP Port found for handel %p\n", nh);
1749 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1750 nua_handle_destroy(nh);
1755 case nua_r_set_params:
1756 PDEBUG(DEBUG_SIP, "setparam response\n");
1759 PDEBUG(DEBUG_SIP, "error received\n");
1762 PDEBUG(DEBUG_SIP, "state change received\n");
1764 case nua_i_register:
1765 PDEBUG(DEBUG_SIP, "register received\n");
1768 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1771 PDEBUG(DEBUG_SIP, "ack received\n");
1774 PDEBUG(DEBUG_SIP, "active received\n");
1777 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1780 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1783 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1786 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1789 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1791 case nua_i_terminated:
1792 PDEBUG(DEBUG_SIP, "terminated received\n");
1795 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1799 /* received shutdown due to termination of RTP */
1800 void Psip::rtp_shutdown(void)
1802 struct lcr_msg *message;
1804 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1806 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1809 nua_handle_destroy(p_s_handle);
1812 while(p_epointlist) {
1813 /* send setup message to endpoit */
1814 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1815 message->param.disconnectinfo.cause = 16;
1816 message->param.disconnectinfo.location = LOCATION_BEYOND;
1817 message_put(message);
1819 free_epointlist(p_epointlist);
1821 new_state(PORT_STATE_RELEASE);
1822 trigger_work(&p_s_delete);
1825 int sip_init_inst(struct interface *interface)
1827 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1829 interface->sip_inst = inst;
1830 SCPY(inst->interface_name, interface->name);
1831 SCPY(inst->local_peer, interface->sip_local_peer);
1832 SCPY(inst->remote_peer, interface->sip_remote_peer);
1834 /* init root object */
1835 inst->root = su_root_create(inst);
1837 PERROR("Failed to create SIP root\n");
1838 sip_exit_inst(interface);
1842 inst->nua = nua_create(inst->root, sip_callback, inst, TAG_NULL());
1844 PERROR("Failed to create SIP stack object\n");
1845 sip_exit_inst(interface);
1848 nua_set_params(inst->nua,
1849 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1850 NUTAG_APPL_METHOD("INVITE"),
1851 NUTAG_APPL_METHOD("ACK"),
1852 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1853 NUTAG_APPL_METHOD("CANCEL"),
1854 NUTAG_APPL_METHOD("OPTIONS"),
1855 NUTAG_APPL_METHOD("NOTIFY"),
1856 NUTAG_APPL_METHOD("INFO"),
1858 #ifdef NUTAG_AUTO100
1862 NUTAG_AUTOANSWER(0),
1865 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1870 void sip_exit_inst(struct interface *interface)
1872 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1877 su_root_destroy(inst->root);
1879 nua_destroy(inst->nua);
1881 FREE(inst, sizeof(*inst));
1882 interface->sip_inst = NULL;
1884 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1887 extern su_log_t su_log_default[];
1888 extern su_log_t nua_log[];
1889 //extern su_log_t soa_log[];
1895 /* init SOFIA lib */
1897 su_home_init(sip_home);
1899 if (options.deb & DEBUG_SIP) {
1900 su_log_set_level(su_log_default, 9);
1901 su_log_set_level(nua_log, 9);
1902 //su_log_set_level(soa_log, 9);
1905 for (i = 0; i < 256; i++)
1906 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);
1908 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1915 su_home_deinit(sip_home);
1918 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1921 void sip_handle(void)
1923 struct interface *interface = interface_first;
1924 struct sip_inst *inst;
1927 if (interface->sip_inst) {
1928 inst = (struct sip_inst *) interface->sip_inst;
1929 su_root_step(inst->root, 0);
1931 interface = interface->next;
1935 /* deletes when back in event loop */
1936 static int delete_event(struct lcr_work *work, void *instance, int index)
1938 class Psip *psip = (class Psip *)instance;