1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #include <sofia-sip/sip_status.h>
14 #include <sofia-sip/su_log.h>
15 #include <sofia-sip/sdp.h>
16 #include <sofia-sip/sip_header.h>
18 unsigned char flip[256];
20 //pthread_mutex_t mutex_msg;
21 su_home_t sip_home[1];
24 char interface_name[64];
31 static int delete_event(struct lcr_work *work, void *instance, int index);
36 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings)
39 if (interface->rtp_bridge)
41 p_s_sip_inst = interface->sip_inst;
42 memset(&p_s_delete, 0, sizeof(p_s_delete));
43 add_work(&p_s_delete, delete_event, this, 0);
46 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
47 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
48 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
49 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
50 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
51 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
52 p_s_rtp_payload_type = 0;
54 p_s_rtp_ip_remote = 0;
55 p_s_rtp_port_local = 0;
56 p_s_rtp_port_remote = 0;
61 p_s_rtp_tx_action = 0;
63 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
72 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
74 del_work(&p_s_delete);
79 const char *payload_type2name(uint8_t payload_type) {
80 switch (payload_type) {
95 static void sip_trace_header(class Psip *sip, const char *message, int direction)
97 /* init trace with given values */
100 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
101 sip?sip->p_dialinginfo.id:NULL,
112 /* according to RFC 3550 */
114 #if __BYTE_ORDER == __LITTLE_ENDIAN
115 uint8_t csrc_count:4,
119 uint8_t payload_type:7,
121 #elif __BYTE_ORDER == __BIG_ENDIAN
132 } __attribute__((packed));
137 } __attribute__((packed));
139 #define RTP_VERSION 2
141 #define RTP_PT_ULAW 0
142 #define RTP_PT_ALAW 8
143 #define RTP_PT_GSM_FULL 3
144 #define RTP_PT_GSM_HALF 96
145 #define RTP_PT_GSM_EFR 97
146 #define RTP_PT_AMR 98
148 /* decode an rtp frame */
149 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
151 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
152 struct rtp_x_hdr *rtpxh;
156 unsigned char *from, *to;
160 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
163 if (rtph->version != RTP_VERSION) {
164 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
167 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
168 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
169 if (payload_len < 0) {
170 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
171 "csrc count = %d)\n", len, rtph->csrc_count);
174 if (rtph->extension) {
175 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
176 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
177 "extension header\n");
180 rtpxh = (struct rtp_x_hdr *)payload;
181 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
183 payload_len -= x_len;
184 if (payload_len < 0) {
185 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
186 "extension header exceeds frame length\n");
191 if (payload_len < 0) {
192 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
196 payload_len -= payload[payload_len - 1];
197 if (payload_len < 0) {
198 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
199 "greater than payload\n");
204 switch (rtph->payload_type) {
205 case RTP_PT_GSM_FULL:
206 if (payload_len != 33) {
207 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
208 "payload length != 33 (len = %d)\n",
214 if (payload_len != 31) {
215 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
216 "payload length != 31 (len = %d)\n",
222 if (options.law != 'a') {
223 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
228 if (options.law == 'a') {
229 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
234 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
235 "type %d\n", rtph->payload_type);
239 if (payload_len <= 0) {
240 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
248 *to++ = flip[*from++];
249 psip->bridge_tx(payload, payload_len);
254 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
256 class Psip *psip = (class Psip *) instance;
258 unsigned char buffer[256];
261 if ((what & LCR_FD_READ)) {
262 len = read(fd->fd, &buffer, sizeof(buffer));
264 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
265 // psip->rtp_close();
266 // psip->rtp_shutdown();
269 rc = rtp_decode(psip, buffer, len);
275 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
277 // class Psip *psip = (class Psip *) instance;
279 unsigned char buffer[256];
281 if ((what & LCR_FD_READ)) {
282 len = read(fd->fd, &buffer, sizeof(buffer));
284 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
285 // psip->rtp_close();
286 // psip->rtp_shutdown();
289 PDEBUG(DEBUG_SIP, "rtcp!");
295 #define RTP_PORT_BASE 30000
296 static unsigned int next_udp_port = RTP_PORT_BASE;
298 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
301 socklen_t alen = sizeof(*sin_local);
303 sin_local->sin_family = AF_INET;
304 sin_local->sin_addr.s_addr = htonl(ip);
305 sin_local->sin_port = htons(port);
307 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
311 /* retrieve the address we actually bound to, in case we
312 * passed INADDR_ANY as IP address */
313 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
316 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
319 socklen_t alen = sizeof(*sin_local);
321 sin_remote->sin_family = AF_INET;
322 sin_remote->sin_addr.s_addr = htonl(ip);
323 sin_remote->sin_port = htons(port);
325 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
327 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
331 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
334 int Psip::rtp_open(void)
341 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
347 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
349 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
355 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
358 ip = htonl(INADDR_ANY);
360 for (next_udp_port = next_udp_port % 0xffff;
361 next_udp_port < 0xffff; next_udp_port += 2) {
362 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
366 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port+1);
371 PDEBUG(DEBUG_SIP, "failed to find port\n");
375 p_s_rtp_port_local = next_udp_port;
376 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
377 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
378 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
380 return p_s_rtp_port_local;
383 int Psip::rtp_connect(void)
388 ia.s_addr = htonl(p_s_rtp_ip_remote);
389 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
391 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);
395 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);
399 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
400 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
401 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
402 p_s_rtp_is_connected = 1;
406 void Psip::rtp_close(void)
408 if (p_s_rtp_fd.fd > 0) {
409 unregister_fd(&p_s_rtp_fd);
410 close(p_s_rtp_fd.fd);
413 if (p_s_rtcp_fd.fd > 0) {
414 unregister_fd(&p_s_rtcp_fd);
415 close(p_s_rtcp_fd.fd);
418 if (p_s_rtp_is_connected) {
419 PDEBUG(DEBUG_SIP, "rtp closed\n");
420 p_s_rtp_is_connected = 0;
425 void tv_difference(struct timeval *diff, const struct timeval *from,
426 const struct timeval *__to)
428 struct timeval _to = *__to, *to = &_to;
430 if (to->tv_usec < from->tv_usec) {
432 to->tv_usec += 1000000;
435 diff->tv_usec = to->tv_usec - from->tv_usec;
436 diff->tv_sec = to->tv_sec - from->tv_sec;
439 /* encode and send a rtp frame */
440 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
442 struct rtp_hdr *rtph;
444 int duration; /* in samples */
445 unsigned char buffer[256];
447 if (!p_s_rtp_is_connected) {
452 if (!p_s_rtp_tx_action) {
453 /* initialize sequences */
454 p_s_rtp_tx_action = 1;
455 p_s_rtp_tx_ssrc = rand();
456 p_s_rtp_tx_sequence = random();
457 p_s_rtp_tx_timestamp = random();
458 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
461 switch (payload_type) {
462 case RTP_PT_GSM_FULL:
476 PERROR("unsupported message type %d\n", payload_type);
482 struct timeval tv, tv_diff;
483 long int usec_diff, frame_diff;
485 gettimeofday(&tv, NULL);
486 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
487 p_s_rtp_tx_last_tv = tv;
489 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
490 frame_diff = (usec_diff / 20000);
492 if (abs(frame_diff) > 1) {
493 long int frame_diff_excess = frame_diff - 1;
495 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
496 p_s_rtp_tx_sequence += frame_diff_excess;
497 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
502 rtph = (struct rtp_hdr *) buffer;
503 rtph->version = RTP_VERSION;
506 rtph->csrc_count = 0;
508 rtph->payload_type = payload_type;
509 rtph->sequence = htons(p_s_rtp_tx_sequence++);
510 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
511 p_s_rtp_tx_timestamp += duration;
512 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
513 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
515 if (p_s_rtp_fd.fd > 0) {
516 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
517 if (len != sizeof(struct rtp_hdr) + payload_len) {
518 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
528 /* receive from remote */
529 int Psip::bridge_rx(unsigned char *data, int len)
531 /* write to rx buffer */
533 p_s_rxdata[p_s_rxpos++] = flip[*data++];
534 if (p_s_rxpos == 160) {
537 /* transmit data via rtp */
538 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW);
545 /* taken from freeswitch */
546 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
547 static int status2cause(int status)
551 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
557 return 21; //SWITCH_CAUSE_CALL_REJECTED;
559 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
562 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
565 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
567 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
576 return 127; //SWITCH_CAUSE_INTERWORKING;
578 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
583 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
586 return 17; //SWITCH_CAUSE_USER_BUSY;
588 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
591 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
593 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
595 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
599 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
602 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
604 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
606 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
610 static int cause2status(int cause, int location, const char **st)
616 s = 404; *st = sip_404_Not_found;
619 s = 404; *st = sip_404_Not_found;
622 s = 404; *st = sip_404_Not_found;
625 s = 486; *st = sip_486_Busy_here;
628 s = 408; *st = sip_408_Request_timeout;
631 s = 480; *st = sip_480_Temporarily_unavailable;
634 s = 480; *st = sip_480_Temporarily_unavailable;
637 if (location == LOCATION_USER) {
638 s = 603; *st = sip_603_Decline;
640 s = 403; *st = sip_403_Forbidden;
644 //s = 301; *st = sip_301_Moved_permanently;
645 s = 410; *st = sip_410_Gone;
648 s = 410; *st = sip_410_Gone;
651 s = 502; *st = sip_502_Bad_gateway;
654 s = 484; *st = sip_484_Address_incomplete;
657 s = 501; *st = sip_501_Not_implemented;
660 s = 480; *st = sip_480_Temporarily_unavailable;
663 s = 503; *st = sip_503_Service_unavailable;
666 s = 503; *st = sip_503_Service_unavailable;
669 s = 503; *st = sip_503_Service_unavailable;
672 s = 503; *st = sip_503_Service_unavailable;
675 s = 503; *st = sip_503_Service_unavailable;
678 s = 403; *st = sip_403_Forbidden;
681 s = 403; *st = sip_403_Forbidden;
684 s = 503; *st = sip_503_Service_unavailable;
687 s = 488; *st = sip_488_Not_acceptable;
690 s = 501; *st = sip_501_Not_implemented;
693 s = 488; *st = sip_488_Not_acceptable;
696 s = 501; *st = sip_501_Not_implemented;
699 s = 403; *st = sip_403_Forbidden;
702 s = 503; *st = sip_503_Service_unavailable;
705 s = 504; *st = sip_504_Gateway_time_out;
708 s = 468; *st = sip_486_Busy_here;
715 * endpoint sends messages to the SIP port
718 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
722 struct lcr_msg *message;
724 if (param->connectinfo.rtpinfo.port) {
725 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
727 p_s_rtp_payload_type = param->connectinfo.rtpinfo.payload_type;
728 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
729 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
730 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
731 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
733 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
734 p_s_rtp_payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
735 /* open local RTP peer (if not bridging) */
736 if (rtp_connect() < 0) {
737 nua_cancel(p_s_handle, TAG_END());
738 nua_handle_destroy(p_s_handle);
740 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
741 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
743 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
744 message->param.disconnectinfo.cause = 41;
745 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
746 message_put(message);
747 new_state(PORT_STATE_RELEASE);
748 trigger_work(&p_s_delete);
753 ia.s_addr = htonl(p_s_rtp_ip_local);
757 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
761 "m=audio %d RTP/AVP %d\n"
762 "a=rtpmap:%d %s/8000\n"
763 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, p_s_rtp_payload_type, p_s_rtp_payload_type, payload_type2name(p_s_rtp_payload_type));
764 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
766 nua_respond(p_s_handle, SIP_200_OK,
767 NUTAG_MEDIA_ENABLE(0),
768 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
769 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
770 new_state(PORT_STATE_CONNECT);
771 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
772 add_trace("respond", "value", "200 OK");
773 add_trace("reason", NULL, "call connected");
779 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
781 struct lcr_msg *message;
782 char cause_str[128] = "";
783 int cause = param->disconnectinfo.cause;
784 int location = param->disconnectinfo.cause;
786 const char *status_text;
788 if (cause > 0 && cause <= 127) {
789 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
793 case PORT_STATE_OUT_SETUP:
794 case PORT_STATE_OUT_PROCEEDING:
795 case PORT_STATE_OUT_ALERTING:
796 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
797 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
799 add_trace("cause", "value", "%d", cause);
801 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
803 case PORT_STATE_IN_SETUP:
804 case PORT_STATE_IN_PROCEEDING:
805 case PORT_STATE_IN_ALERTING:
806 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
807 status = cause2status(cause, location, &status_text);
808 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
810 add_trace("cause", "value", "%d", cause);
811 add_trace("respond", "value", "%d %s", status, status_text);
813 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
814 nua_handle_destroy(p_s_handle);
816 trigger_work(&p_s_delete);
819 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
820 sip_trace_header(this, "BYE", DIRECTION_OUT);
822 add_trace("cause", "value", "%d", cause);
824 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
827 if (message_id == MESSAGE_DISCONNECT) {
828 while(p_epointlist) {
829 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
830 message->param.disconnectinfo.cause = CAUSE_NORMAL;
831 message->param.disconnectinfo.location = LOCATION_BEYOND;
832 message_put(message);
834 free_epointlist(p_epointlist);
838 new_state(PORT_STATE_RELEASE);
843 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
845 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
848 const char *local = inst->local_ip;
849 const char *remote = inst->remote_ip;
852 struct epoint_list *epointlist;
853 sip_cseq_t *cseq = NULL;
855 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
857 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
858 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
859 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
861 if (param->setup.rtpinfo.port) {
862 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
864 p_s_rtp_payload_type = param->setup.rtpinfo.payload_type;
865 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
866 p_s_rtp_port_local = param->setup.rtpinfo.port;
867 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
868 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
870 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
872 p_s_rtp_payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
874 /* open local RTP peer (if not bridging) */
875 if (rtp_open() < 0) {
876 struct lcr_msg *message;
878 PERROR("Failed to open RTP sockets\n");
879 /* send release message to endpoit */
880 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
881 message->param.disconnectinfo.cause = 41;
882 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
883 message_put(message);
884 new_state(PORT_STATE_RELEASE);
885 trigger_work(&p_s_delete);
889 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
890 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
892 sip_trace_header(this, "INVITE", DIRECTION_OUT);
893 add_trace("from", "uri", "%s", from);
894 add_trace("to", "uri", "%s", to);
895 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
898 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
900 struct lcr_msg *message;
902 PERROR("Failed to create handle\n");
903 /* send release message to endpoit */
904 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
905 message->param.disconnectinfo.cause = 41;
906 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
907 message_put(message);
908 new_state(PORT_STATE_RELEASE);
909 trigger_work(&p_s_delete);
913 sip_trace_header(this, "NEW handle", DIRECTION_IN);
914 add_trace("handle", "new", "0x%x", p_s_handle);
917 if (!p_s_rtp_ip_local) {
918 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local);
919 inet_pton(AF_INET, local, &p_s_rtp_ip_local);
920 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
922 ia.s_addr = htonl(p_s_rtp_ip_local);
925 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
929 "m=audio %d RTP/AVP %d\n"
930 "a=rtpmap:%d %s/8000\n"
931 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, p_s_rtp_payload_type, p_s_rtp_payload_type, payload_type2name(p_s_rtp_payload_type));
932 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
934 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
936 nua_invite(p_s_handle,
937 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
938 TAG_IF(to[0], SIPTAG_TO_STR(to)),
939 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
940 NUTAG_MEDIA_ENABLE(0),
941 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
942 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
943 new_state(PORT_STATE_OUT_SETUP);
945 /* attach only if not already */
946 epointlist = p_epointlist;
948 if (epointlist->epoint_id == epoint_id)
950 epointlist = epointlist->next;
953 epointlist_new(epoint_id);
958 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
960 // char sdp_str[256];
961 // struct in_addr ia;
963 switch (param->notifyinfo.notify) {
964 case INFO_NOTIFY_REMOTE_HOLD:
968 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
973 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
975 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
976 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
977 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
978 NUTAG_MEDIA_ENABLE(0),
979 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
980 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
983 case INFO_NOTIFY_REMOTE_RETRIEVAL:
985 ia.s_addr = htonl(p_s_rtp_ip_local);
988 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
992 "m=audio %d RTP/AVP %d\n"
993 "a=rtpmap:%d %s/8000\n"
994 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, p_s_rtp_payload_type, p_s_rtp_payload_type, payload_type2name(p_s_rtp_payload_type));
995 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
997 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
998 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
999 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1000 NUTAG_MEDIA_ENABLE(0),
1001 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1002 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1010 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1014 /* prepare DTMF info payload */
1020 /* start invite to handle DTMF */
1021 nua_info(p_s_handle,
1022 NUTAG_MEDIA_ENABLE(0),
1023 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1024 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1029 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1031 if (Port::message_epoint(epoint_id, message_id, param))
1034 switch(message_id) {
1035 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1036 if (p_state != PORT_STATE_IN_SETUP
1037 && p_state != PORT_STATE_IN_PROCEEDING)
1039 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1040 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1041 add_trace("respond", "value", "180 Ringing");
1043 new_state(PORT_STATE_IN_ALERTING);
1046 case MESSAGE_CONNECT: /* call is connected on LCR side */
1047 if (p_state != PORT_STATE_IN_SETUP
1048 && p_state != PORT_STATE_IN_PROCEEDING
1049 && p_state != PORT_STATE_IN_ALERTING)
1051 message_connect(epoint_id, message_id, param);
1054 case MESSAGE_DISCONNECT: /* call has been disconnected */
1055 case MESSAGE_RELEASE: /* call has been released */
1056 message_release(epoint_id, message_id, param);
1059 case MESSAGE_SETUP: /* dial-out command received from epoint */
1060 message_setup(epoint_id, message_id, param);
1063 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1064 if (p_state == PORT_STATE_CONNECT)
1065 message_dtmf(epoint_id, message_id, param);
1066 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1067 if (p_state == PORT_STATE_CONNECT)
1068 message_notify(epoint_id, message_id, param);
1072 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1078 int Psip::parse_sdp(sip_t const *sip, unsigned int *ip, unsigned short *port, uint8_t payload_type)
1080 int codec_supported = 0;
1082 if (!sip->sip_payload) {
1083 PDEBUG(DEBUG_SIP, "no payload given\n");
1087 sdp_parser_t *parser;
1090 sdp_attribute_t *attr;
1092 sdp_connection_t *conn;
1094 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1096 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1100 if (!(sdp = sdp_session(parser))) {
1101 sdp_parser_free(parser);
1104 for (m = sdp->sdp_media; m; m = m->m_next) {
1105 if (m->m_proto != sdp_proto_rtp)
1107 if (m->m_type != sdp_media_audio)
1109 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1111 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1112 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1114 if (m->m_connections) {
1115 conn = m->m_connections;
1116 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1117 inet_pton(AF_INET, conn->c_address, ip);
1118 *ip = ntohl(p_s_rtp_ip_remote);
1120 char *p = sip->sip_payload->pl_data;
1123 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1124 p = strstr(p, "c=IN IP4 ");
1126 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1127 sdp_parser_free(parser);
1131 if ((p = strchr(addr, '\n'))) *p = '\0';
1132 if ((p = strchr(addr, '\r'))) *p = '\0';
1133 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1134 inet_pton(AF_INET, addr, ip);
1135 *ip = ntohl(p_s_rtp_ip_remote);
1137 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1138 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1139 if (map->rm_pt == payload_type) {
1140 PDEBUG(DEBUG_SIP, "supported codec found\n");
1141 codec_supported = 1;
1148 sdp_parser_free(parser);
1150 if (!codec_supported)
1156 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[])
1158 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1159 const char *from = "", *to = "";
1161 class Endpoint *epoint;
1162 struct lcr_msg *message;
1163 uint8_t payload_type;
1164 struct interface *interface = interface_first;
1167 if (!strcmp(interface->name, inst->interface_name))
1169 interface = interface->next;
1172 PERROR("Cannot find interface %s.\n", inst->interface_name);
1176 if (sip->sip_from && sip->sip_from->a_url)
1177 from = sip->sip_from->a_url->url_user;
1178 if (sip->sip_to && sip->sip_to->a_url)
1179 to = sip->sip_to->a_url->url_user;
1180 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1183 payload_type = 3; // FIXME: use list and forward list to remote side
1185 payload_type = (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW;
1186 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_type);
1189 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1191 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1192 nua_handle_destroy(nh);
1194 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1196 add_trace("respond", "value", "415 Unsupported Media");
1198 add_trace("respond", "value", "400 Bad Request");
1199 add_trace("reason", NULL, "offered codec does not match");
1201 new_state(PORT_STATE_RELEASE);
1202 trigger_work(&p_s_delete);
1206 /* open local RTP peer (if not bridging) */
1207 if (!p_s_rtp_bridge && rtp_open() < 0) {
1208 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1209 nua_handle_destroy(nh);
1211 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1212 add_trace("respond", "value", "500 Internal Server Error");
1213 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1215 new_state(PORT_STATE_RELEASE);
1216 trigger_work(&p_s_delete);
1221 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1222 add_trace("handle", "new", "0x%x", nh);
1226 sip_trace_header(this, "INVITE", DIRECTION_IN);
1227 add_trace("RTP", "port", "%d", p_s_rtp_port_remote);
1228 /* caller information */
1230 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1231 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1232 add_trace("calling", "present", "unavailable");
1234 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1235 add_trace("calling", "present", "allowed");
1236 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1237 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1238 SCPY(p_callerinfo.id, from);
1239 add_trace("calling", "number", "%s", from);
1241 SCPY(p_callerinfo.interface, inst->interface_name);
1242 /* dialing information */
1244 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1245 SCAT(p_dialinginfo.id, to);
1246 add_trace("dialing", "number", "%s", to);
1249 /* bearer capability */
1250 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1251 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1252 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1253 add_trace("bearer", "capa", "speech");
1254 add_trace("bearer", "mode", "circuit");
1255 /* if packet mode works some day, see dss1.cpp for conditions */
1256 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1260 /* create endpoint */
1262 FATAL("Incoming call but already got an endpoint.\n");
1263 if (!(epoint = new Endpoint(p_serial, 0)))
1264 FATAL("No memory for Endpoint instance\n");
1265 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1266 epointlist_new(epoint->ep_serial);
1268 /* send trying (proceeding) */
1269 nua_respond(nh, SIP_100_TRYING, TAG_END());
1270 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1271 add_trace("respond", "value", "100 Trying");
1274 new_state(PORT_STATE_IN_PROCEEDING);
1276 /* send setup message to endpoit */
1277 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1278 message->param.setup.port_type = p_type;
1279 // message->param.setup.dtmf = 0;
1280 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1281 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1282 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1283 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1284 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1285 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1286 if (p_s_rtp_bridge) {
1287 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1288 message->param.setup.rtpinfo.payload_type = payload_type;
1289 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1290 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1292 message_put(message);
1295 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[])
1297 struct lcr_msg *message;
1300 PDEBUG(DEBUG_SIP, "bye received\n");
1302 sip_trace_header(this, "BYE", DIRECTION_IN);
1303 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1304 cause = atoi(sip->sip_reason->re_cause);
1305 add_trace("cause", "value", "%d", cause);
1309 // let stack do bye automaticall, since it will not accept our response for some reason
1310 // nua_respond(nh, SIP_200_OK, TAG_END());
1311 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1312 add_trace("respond", "value", "200 OK");
1314 // nua_handle_destroy(nh);
1319 while(p_epointlist) {
1320 /* send setup message to endpoit */
1321 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1322 message->param.disconnectinfo.cause = cause ? : 16;
1323 message->param.disconnectinfo.location = LOCATION_BEYOND;
1324 message_put(message);
1326 free_epointlist(p_epointlist);
1328 new_state(PORT_STATE_RELEASE);
1329 trigger_work(&p_s_delete);
1332 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[])
1334 struct lcr_msg *message;
1336 PDEBUG(DEBUG_SIP, "cancel received\n");
1338 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1341 nua_handle_destroy(nh);
1346 while(p_epointlist) {
1347 /* send setup message to endpoit */
1348 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1349 message->param.disconnectinfo.cause = 16;
1350 message->param.disconnectinfo.location = LOCATION_BEYOND;
1351 message_put(message);
1353 free_epointlist(p_epointlist);
1355 new_state(PORT_STATE_RELEASE);
1356 trigger_work(&p_s_delete);
1359 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[])
1361 PDEBUG(DEBUG_SIP, "bye response received\n");
1363 nua_handle_destroy(nh);
1368 trigger_work(&p_s_delete);
1371 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[])
1373 PDEBUG(DEBUG_SIP, "cancel response received\n");
1375 nua_handle_destroy(nh);
1380 trigger_work(&p_s_delete);
1383 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[])
1385 struct lcr_msg *message;
1386 int cause = 0, location = 0;
1388 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1390 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1391 add_trace("respond", "value", "%d", status);
1395 if (status == 183 || (status >= 200 && status <= 299)) {
1397 uint8_t payload_type;
1400 payload_type = p_s_rtp_payload_type;
1402 payload_type = (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW;
1403 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_type);
1406 nua_cancel(nh, TAG_END());
1408 nua_cancel(nh, TAG_END());
1409 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1410 add_trace("reason", NULL, "offered codec does not match");
1413 location = LOCATION_PRIVATE_LOCAL;
1414 goto release_with_cause;
1417 /* connect to remote RTP (if not bridging) */
1418 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1419 nua_cancel(nh, TAG_END());
1420 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1421 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1424 location = LOCATION_PRIVATE_LOCAL;
1425 goto release_with_cause;
1432 PDEBUG(DEBUG_SIP, "do proceeding\n");
1433 new_state(PORT_STATE_OUT_PROCEEDING);
1434 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1435 message_put(message);
1438 PDEBUG(DEBUG_SIP, "do alerting\n");
1439 new_state(PORT_STATE_OUT_ALERTING);
1440 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1441 message_put(message);
1444 PDEBUG(DEBUG_SIP, "do progress\n");
1445 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1446 message->param.progressinfo.progress = 8;
1447 message->param.progressinfo.location = 10;
1448 if (p_s_rtp_bridge) {
1449 message->param.progressinfo.rtpinfo.payload_type = p_s_rtp_payload_type;
1450 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1451 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1453 message_put(message);
1456 if (status < 100 || status > 199)
1458 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1464 if (status >= 200 && status <= 299) {
1465 PDEBUG(DEBUG_SIP, "do connect\n");
1466 nua_ack(nh, TAG_END());
1467 new_state(PORT_STATE_CONNECT);
1468 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1469 if (p_s_rtp_bridge) {
1470 message->param.connectinfo.rtpinfo.payload_type = p_s_rtp_payload_type;
1471 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1472 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1474 message_put(message);
1477 cause = status2cause(status);
1478 location = LOCATION_BEYOND;
1481 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1483 while(p_epointlist) {
1484 /* send setup message to endpoit */
1485 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1486 message->param.disconnectinfo.cause = cause;
1487 message->param.disconnectinfo.location = location;
1488 message_put(message);
1490 free_epointlist(p_epointlist);
1493 new_state(PORT_STATE_RELEASE);
1497 trigger_work(&p_s_delete);
1500 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[])
1502 struct sip_inst *inst = (struct sip_inst *) magic;
1504 class Psip *psip = NULL;
1506 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1510 /* create or find port instance */
1511 if (event == nua_i_invite)
1514 struct interface *interface = interface_first;
1516 /* create call instance */
1517 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1519 if (!strcmp(interface->name, inst->interface_name))
1521 interface = interface->next;
1524 PERROR("Cannot find interface %s.\n", inst->interface_name);
1527 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1528 FATAL("Cannot create Port instance.\n");
1532 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1533 psip = (class Psip *)port;
1534 if (psip->p_s_handle == nh) {
1542 PERROR("no SIP Port found for handel %p\n", nh);
1543 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1544 nua_handle_destroy(nh);
1549 case nua_r_set_params:
1550 PDEBUG(DEBUG_SIP, "setparam response\n");
1553 PDEBUG(DEBUG_SIP, "error received\n");
1556 PDEBUG(DEBUG_SIP, "state change received\n");
1558 case nua_i_register:
1559 PDEBUG(DEBUG_SIP, "register received\n");
1562 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1565 PDEBUG(DEBUG_SIP, "ack received\n");
1568 PDEBUG(DEBUG_SIP, "active received\n");
1571 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1574 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1577 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1580 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1583 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1585 case nua_i_terminated:
1586 PDEBUG(DEBUG_SIP, "terminated received\n");
1589 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1593 /* received shutdown due to termination of RTP */
1594 void Psip::rtp_shutdown(void)
1596 struct lcr_msg *message;
1598 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1600 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1603 nua_handle_destroy(p_s_handle);
1606 while(p_epointlist) {
1607 /* send setup message to endpoit */
1608 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1609 message->param.disconnectinfo.cause = 16;
1610 message->param.disconnectinfo.location = LOCATION_BEYOND;
1611 message_put(message);
1613 free_epointlist(p_epointlist);
1615 new_state(PORT_STATE_RELEASE);
1616 trigger_work(&p_s_delete);
1619 int sip_init_inst(struct interface *interface)
1621 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1623 interface->sip_inst = inst;
1624 SCPY(inst->interface_name, interface->name);
1625 SCPY(inst->local_ip, interface->sip_local_ip);
1626 SCPY(inst->remote_ip, interface->sip_remote_ip);
1628 /* init root object */
1629 inst->root = su_root_create(inst);
1631 PERROR("Failed to create SIP root\n");
1632 sip_exit_inst(interface);
1636 inst->nua = nua_create(inst->root, sip_callback, inst, TAG_NULL());
1638 PERROR("Failed to create SIP stack object\n");
1639 sip_exit_inst(interface);
1642 nua_set_params(inst->nua,
1643 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1644 NUTAG_APPL_METHOD("INVITE"),
1645 NUTAG_APPL_METHOD("ACK"),
1646 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1647 NUTAG_APPL_METHOD("CANCEL"),
1648 NUTAG_APPL_METHOD("OPTIONS"),
1649 NUTAG_APPL_METHOD("NOTIFY"),
1650 NUTAG_APPL_METHOD("INFO"),
1654 NUTAG_AUTOANSWER(0),
1657 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1662 void sip_exit_inst(struct interface *interface)
1664 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1669 su_root_destroy(inst->root);
1671 nua_destroy(inst->nua);
1673 FREE(inst, sizeof(*inst));
1674 interface->sip_inst = NULL;
1676 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1679 extern su_log_t su_log_default[];
1680 extern su_log_t nua_log[];
1681 //extern su_log_t soa_log[];
1687 /* init SOFIA lib */
1689 su_home_init(sip_home);
1691 if (options.deb & DEBUG_SIP) {
1692 su_log_set_level(su_log_default, 9);
1693 su_log_set_level(nua_log, 9);
1694 //su_log_set_level(soa_log, 9);
1697 for (i = 0; i < 256; i++)
1698 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);
1700 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1707 su_home_deinit(sip_home);
1710 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1713 void sip_handle(void)
1715 struct interface *interface = interface_first;
1716 struct sip_inst *inst;
1719 if (interface->sip_inst) {
1720 inst = (struct sip_inst *) interface->sip_inst;
1721 su_root_step(inst->root, 0);
1723 interface = interface->next;
1727 /* deletes when back in event loop */
1728 static int delete_event(struct lcr_work *work, void *instance, int index)
1730 class Psip *psip = (class Psip *)instance;