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));
54 p_s_rtp_payload_type = 0;
56 p_s_rtp_ip_remote = 0;
57 p_s_rtp_port_local = 0;
58 p_s_rtp_port_remote = 0;
63 p_s_rtp_tx_action = 0;
65 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
74 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
76 del_work(&p_s_delete);
81 const char *payload_type2name(uint8_t payload_type) {
82 switch (payload_type) {
97 static void sip_trace_header(class Psip *sip, const char *message, int direction)
99 /* init trace with given values */
102 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
103 sip?sip->p_dialinginfo.id:NULL,
114 /* according to RFC 3550 */
116 #if __BYTE_ORDER == __LITTLE_ENDIAN
117 uint8_t csrc_count:4,
121 uint8_t payload_type:7,
123 #elif __BYTE_ORDER == __BIG_ENDIAN
134 } __attribute__((packed));
139 } __attribute__((packed));
141 #define RTP_VERSION 2
143 #define RTP_PT_ULAW 0
144 #define RTP_PT_ALAW 8
145 #define RTP_PT_GSM_FULL 3
146 #define RTP_PT_GSM_HALF 96
147 #define RTP_PT_GSM_EFR 97
148 #define RTP_PT_AMR 98
150 /* decode an rtp frame */
151 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
153 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
154 struct rtp_x_hdr *rtpxh;
158 unsigned char *from, *to;
162 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
165 if (rtph->version != RTP_VERSION) {
166 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
169 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
170 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
171 if (payload_len < 0) {
172 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
173 "csrc count = %d)\n", len, rtph->csrc_count);
176 if (rtph->extension) {
177 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
178 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
179 "extension header\n");
182 rtpxh = (struct rtp_x_hdr *)payload;
183 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
185 payload_len -= x_len;
186 if (payload_len < 0) {
187 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
188 "extension header exceeds frame length\n");
193 if (payload_len < 0) {
194 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
198 payload_len -= payload[payload_len - 1];
199 if (payload_len < 0) {
200 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
201 "greater than payload\n");
206 switch (rtph->payload_type) {
207 case RTP_PT_GSM_FULL:
208 if (payload_len != 33) {
209 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
210 "payload length != 33 (len = %d)\n",
216 if (payload_len != 31) {
217 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
218 "payload length != 31 (len = %d)\n",
224 if (options.law != 'a') {
225 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
230 if (options.law == 'a') {
231 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
236 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
237 "type %d\n", rtph->payload_type);
241 if (payload_len <= 0) {
242 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
250 *to++ = flip[*from++];
251 psip->bridge_tx(payload, payload_len);
256 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
258 class Psip *psip = (class Psip *) instance;
260 unsigned char buffer[256];
263 if ((what & LCR_FD_READ)) {
264 len = read(fd->fd, &buffer, sizeof(buffer));
266 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
267 // psip->rtp_close();
268 // psip->rtp_shutdown();
271 rc = rtp_decode(psip, buffer, len);
277 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
279 // class Psip *psip = (class Psip *) instance;
281 unsigned char buffer[256];
283 if ((what & LCR_FD_READ)) {
284 len = read(fd->fd, &buffer, sizeof(buffer));
286 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
287 // psip->rtp_close();
288 // psip->rtp_shutdown();
291 PDEBUG(DEBUG_SIP, "rtcp!");
297 #define RTP_PORT_BASE 30000
298 static unsigned int next_udp_port = RTP_PORT_BASE;
300 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
303 socklen_t alen = sizeof(*sin_local);
305 sin_local->sin_family = AF_INET;
306 sin_local->sin_addr.s_addr = htonl(ip);
307 sin_local->sin_port = htons(port);
309 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
313 /* retrieve the address we actually bound to, in case we
314 * passed INADDR_ANY as IP address */
315 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
318 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
321 socklen_t alen = sizeof(*sin_local);
323 sin_remote->sin_family = AF_INET;
324 sin_remote->sin_addr.s_addr = htonl(ip);
325 sin_remote->sin_port = htons(port);
327 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
329 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
333 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
336 int Psip::rtp_open(void)
343 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
349 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
351 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
357 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
360 ip = htonl(INADDR_ANY);
362 for (next_udp_port = next_udp_port % 0xffff;
363 next_udp_port < 0xffff; next_udp_port += 2) {
364 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
368 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port+1);
373 PDEBUG(DEBUG_SIP, "failed to find port\n");
377 p_s_rtp_port_local = next_udp_port;
378 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
379 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
380 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
382 return p_s_rtp_port_local;
385 int Psip::rtp_connect(void)
390 ia.s_addr = htonl(p_s_rtp_ip_remote);
391 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
393 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);
397 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);
401 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
402 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
403 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
404 p_s_rtp_is_connected = 1;
408 void Psip::rtp_close(void)
410 if (p_s_rtp_fd.fd > 0) {
411 unregister_fd(&p_s_rtp_fd);
412 close(p_s_rtp_fd.fd);
415 if (p_s_rtcp_fd.fd > 0) {
416 unregister_fd(&p_s_rtcp_fd);
417 close(p_s_rtcp_fd.fd);
420 if (p_s_rtp_is_connected) {
421 PDEBUG(DEBUG_SIP, "rtp closed\n");
422 p_s_rtp_is_connected = 0;
427 void tv_difference(struct timeval *diff, const struct timeval *from,
428 const struct timeval *__to)
430 struct timeval _to = *__to, *to = &_to;
432 if (to->tv_usec < from->tv_usec) {
434 to->tv_usec += 1000000;
437 diff->tv_usec = to->tv_usec - from->tv_usec;
438 diff->tv_sec = to->tv_sec - from->tv_sec;
441 /* encode and send a rtp frame */
442 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
444 struct rtp_hdr *rtph;
446 int duration; /* in samples */
447 unsigned char buffer[256];
449 if (!p_s_rtp_is_connected) {
454 if (!p_s_rtp_tx_action) {
455 /* initialize sequences */
456 p_s_rtp_tx_action = 1;
457 p_s_rtp_tx_ssrc = rand();
458 p_s_rtp_tx_sequence = random();
459 p_s_rtp_tx_timestamp = random();
460 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
463 switch (payload_type) {
464 case RTP_PT_GSM_FULL:
478 PERROR("unsupported message type %d\n", payload_type);
484 struct timeval tv, tv_diff;
485 long int usec_diff, frame_diff;
487 gettimeofday(&tv, NULL);
488 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
489 p_s_rtp_tx_last_tv = tv;
491 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
492 frame_diff = (usec_diff / 20000);
494 if (abs(frame_diff) > 1) {
495 long int frame_diff_excess = frame_diff - 1;
497 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
498 p_s_rtp_tx_sequence += frame_diff_excess;
499 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
504 rtph = (struct rtp_hdr *) buffer;
505 rtph->version = RTP_VERSION;
508 rtph->csrc_count = 0;
510 rtph->payload_type = payload_type;
511 rtph->sequence = htons(p_s_rtp_tx_sequence++);
512 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
513 p_s_rtp_tx_timestamp += duration;
514 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
515 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
517 if (p_s_rtp_fd.fd > 0) {
518 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
519 if (len != sizeof(struct rtp_hdr) + payload_len) {
520 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
530 /* receive from remote */
531 int Psip::bridge_rx(unsigned char *data, int len)
533 /* write to rx buffer */
535 p_s_rxdata[p_s_rxpos++] = flip[*data++];
536 if (p_s_rxpos == 160) {
539 /* transmit data via rtp */
540 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW);
547 /* taken from freeswitch */
548 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
549 static int status2cause(int status)
553 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
559 return 21; //SWITCH_CAUSE_CALL_REJECTED;
561 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
564 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
567 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
569 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
578 return 127; //SWITCH_CAUSE_INTERWORKING;
580 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
585 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
588 return 17; //SWITCH_CAUSE_USER_BUSY;
590 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
593 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
595 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
597 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
601 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
604 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
606 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
608 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
612 static int cause2status(int cause, int location, const char **st)
618 s = 404; *st = sip_404_Not_found;
621 s = 404; *st = sip_404_Not_found;
624 s = 404; *st = sip_404_Not_found;
627 s = 486; *st = sip_486_Busy_here;
630 s = 408; *st = sip_408_Request_timeout;
633 s = 480; *st = sip_480_Temporarily_unavailable;
636 s = 480; *st = sip_480_Temporarily_unavailable;
639 if (location == LOCATION_USER) {
640 s = 603; *st = sip_603_Decline;
642 s = 403; *st = sip_403_Forbidden;
646 //s = 301; *st = sip_301_Moved_permanently;
647 s = 410; *st = sip_410_Gone;
650 s = 410; *st = sip_410_Gone;
653 s = 502; *st = sip_502_Bad_gateway;
656 s = 484; *st = sip_484_Address_incomplete;
659 s = 501; *st = sip_501_Not_implemented;
662 s = 480; *st = sip_480_Temporarily_unavailable;
665 s = 503; *st = sip_503_Service_unavailable;
668 s = 503; *st = sip_503_Service_unavailable;
671 s = 503; *st = sip_503_Service_unavailable;
674 s = 503; *st = sip_503_Service_unavailable;
677 s = 503; *st = sip_503_Service_unavailable;
680 s = 403; *st = sip_403_Forbidden;
683 s = 403; *st = sip_403_Forbidden;
686 s = 503; *st = sip_503_Service_unavailable;
689 s = 488; *st = sip_488_Not_acceptable;
692 s = 501; *st = sip_501_Not_implemented;
695 s = 488; *st = sip_488_Not_acceptable;
698 s = 501; *st = sip_501_Not_implemented;
701 s = 403; *st = sip_403_Forbidden;
704 s = 503; *st = sip_503_Service_unavailable;
707 s = 504; *st = sip_504_Gateway_time_out;
710 s = 468; *st = sip_486_Busy_here;
717 * endpoint sends messages to the SIP port
720 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
724 struct lcr_msg *message;
726 if (param->connectinfo.rtpinfo.port) {
727 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
729 p_s_rtp_payload_type = param->connectinfo.rtpinfo.payload_type;
730 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
731 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
732 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
733 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
735 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
736 p_s_rtp_payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
737 /* open local RTP peer (if not bridging) */
738 if (rtp_connect() < 0) {
739 nua_cancel(p_s_handle, TAG_END());
740 nua_handle_destroy(p_s_handle);
742 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
743 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
745 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
746 message->param.disconnectinfo.cause = 41;
747 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
748 message_put(message);
749 new_state(PORT_STATE_RELEASE);
750 trigger_work(&p_s_delete);
755 ia.s_addr = htonl(p_s_rtp_ip_local);
759 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
763 "m=audio %d RTP/AVP %d\n"
764 "a=rtpmap:%d %s/8000\n"
765 , 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));
766 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
768 nua_respond(p_s_handle, SIP_200_OK,
769 NUTAG_MEDIA_ENABLE(0),
770 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
771 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
772 new_state(PORT_STATE_CONNECT);
773 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
774 add_trace("respond", "value", "200 OK");
775 add_trace("reason", NULL, "call connected");
781 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
783 struct lcr_msg *message;
784 char cause_str[128] = "";
785 int cause = param->disconnectinfo.cause;
786 int location = param->disconnectinfo.cause;
788 const char *status_text;
790 if (cause > 0 && cause <= 127) {
791 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
795 case PORT_STATE_OUT_SETUP:
796 case PORT_STATE_OUT_PROCEEDING:
797 case PORT_STATE_OUT_ALERTING:
798 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
799 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
801 add_trace("cause", "value", "%d", cause);
803 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
805 case PORT_STATE_IN_SETUP:
806 case PORT_STATE_IN_PROCEEDING:
807 case PORT_STATE_IN_ALERTING:
808 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
809 status = cause2status(cause, location, &status_text);
810 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
812 add_trace("cause", "value", "%d", cause);
813 add_trace("respond", "value", "%d %s", status, status_text);
815 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
816 nua_handle_destroy(p_s_handle);
818 trigger_work(&p_s_delete);
821 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
822 sip_trace_header(this, "BYE", DIRECTION_OUT);
824 add_trace("cause", "value", "%d", cause);
826 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
829 if (message_id == MESSAGE_DISCONNECT) {
830 while(p_epointlist) {
831 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
832 message->param.disconnectinfo.cause = CAUSE_NORMAL;
833 message->param.disconnectinfo.location = LOCATION_BEYOND;
834 message_put(message);
836 free_epointlist(p_epointlist);
840 new_state(PORT_STATE_RELEASE);
845 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
847 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
850 const char *local = inst->local_ip;
851 const char *remote = inst->remote_ip;
854 struct epoint_list *epointlist;
855 sip_cseq_t *cseq = NULL;
856 struct lcr_msg *message;
858 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
860 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
861 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
862 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
864 if (param->setup.rtpinfo.port) {
865 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
867 p_s_rtp_payload_type = param->setup.rtpinfo.payload_type;
868 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
869 p_s_rtp_port_local = param->setup.rtpinfo.port;
870 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
871 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
873 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
875 p_s_rtp_payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
877 /* open local RTP peer (if not bridging) */
878 if (rtp_open() < 0) {
879 PERROR("Failed to open RTP sockets\n");
880 /* send release message to endpoit */
881 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
882 message->param.disconnectinfo.cause = 41;
883 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
884 message_put(message);
885 new_state(PORT_STATE_RELEASE);
886 trigger_work(&p_s_delete);
890 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
891 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
893 sip_trace_header(this, "INVITE", DIRECTION_OUT);
894 add_trace("from", "uri", "%s", from);
895 add_trace("to", "uri", "%s", to);
896 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
899 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
901 PERROR("Failed to create handle\n");
902 /* send release message to endpoit */
903 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
904 message->param.disconnectinfo.cause = 41;
905 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
906 message_put(message);
907 new_state(PORT_STATE_RELEASE);
908 trigger_work(&p_s_delete);
912 sip_trace_header(this, "NEW handle", DIRECTION_IN);
913 add_trace("handle", "new", "0x%x", p_s_handle);
916 if (!p_s_rtp_ip_local) {
917 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local);
918 inet_pton(AF_INET, local, &p_s_rtp_ip_local);
919 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
921 ia.s_addr = htonl(p_s_rtp_ip_local);
924 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
928 "m=audio %d RTP/AVP %d\n"
929 "a=rtpmap:%d %s/8000\n"
930 , 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));
931 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
933 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
935 nua_invite(p_s_handle,
936 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
937 TAG_IF(to[0], SIPTAG_TO_STR(to)),
938 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
939 NUTAG_MEDIA_ENABLE(0),
940 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
941 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
942 new_state(PORT_STATE_OUT_SETUP);
945 PDEBUG(DEBUG_SIP, "do overlap\n");
946 new_state(PORT_STATE_OUT_OVERLAP);
947 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
948 message_put(message);
950 PDEBUG(DEBUG_SIP, "do overlap\n");
951 new_state(PORT_STATE_OUT_PROCEEDING);
952 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
953 message_put(message);
956 /* attach only if not already */
957 epointlist = p_epointlist;
959 if (epointlist->epoint_id == epoint_id)
961 epointlist = epointlist->next;
964 epointlist_new(epoint_id);
969 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
971 // char sdp_str[256];
972 // struct in_addr ia;
974 switch (param->notifyinfo.notify) {
975 case INFO_NOTIFY_REMOTE_HOLD:
979 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
984 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
986 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
987 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
988 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
989 NUTAG_MEDIA_ENABLE(0),
990 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
991 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
994 case INFO_NOTIFY_REMOTE_RETRIEVAL:
996 ia.s_addr = htonl(p_s_rtp_ip_local);
999 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1003 "m=audio %d RTP/AVP %d\n"
1004 "a=rtpmap:%d %s/8000\n"
1005 , 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));
1006 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1007 nua_info(p_s_handle,
1008 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1009 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1010 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1011 NUTAG_MEDIA_ENABLE(0),
1012 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1013 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1021 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1025 /* prepare DTMF info payload */
1031 /* start invite to handle DTMF */
1032 nua_info(p_s_handle,
1033 NUTAG_MEDIA_ENABLE(0),
1034 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1035 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1040 /* NOTE: incomplete and not working */
1041 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1045 /* prepare DTMF info payload */
1049 , param->information.id);
1051 /* start invite to handle DTMF */
1052 nua_info(p_s_handle,
1053 NUTAG_MEDIA_ENABLE(0),
1054 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1055 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1060 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1062 if (Port::message_epoint(epoint_id, message_id, param))
1065 switch(message_id) {
1066 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1067 if (p_state != PORT_STATE_IN_SETUP
1068 && p_state != PORT_STATE_IN_PROCEEDING)
1070 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1071 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1072 add_trace("respond", "value", "180 Ringing");
1074 new_state(PORT_STATE_IN_ALERTING);
1077 case MESSAGE_CONNECT: /* call is connected on LCR side */
1078 if (p_state != PORT_STATE_IN_SETUP
1079 && p_state != PORT_STATE_IN_PROCEEDING
1080 && p_state != PORT_STATE_IN_ALERTING)
1082 message_connect(epoint_id, message_id, param);
1085 case MESSAGE_DISCONNECT: /* call has been disconnected */
1086 case MESSAGE_RELEASE: /* call has been released */
1087 message_release(epoint_id, message_id, param);
1090 case MESSAGE_SETUP: /* dial-out command received from epoint */
1091 message_setup(epoint_id, message_id, param);
1094 case MESSAGE_INFORMATION: /* overlap dialing */
1095 if (p_state != PORT_STATE_OUT_OVERLAP)
1097 message_information(epoint_id, message_id, param);
1100 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1101 if (p_state == PORT_STATE_CONNECT)
1102 message_dtmf(epoint_id, message_id, param);
1103 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1104 if (p_state == PORT_STATE_CONNECT)
1105 message_notify(epoint_id, message_id, param);
1109 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1115 int Psip::parse_sdp(sip_t const *sip, unsigned int *ip, unsigned short *port, uint8_t payload_type)
1117 int codec_supported = 0;
1119 if (!sip->sip_payload) {
1120 PDEBUG(DEBUG_SIP, "no payload given\n");
1124 sdp_parser_t *parser;
1127 sdp_attribute_t *attr;
1129 sdp_connection_t *conn;
1131 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1133 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1137 if (!(sdp = sdp_session(parser))) {
1138 sdp_parser_free(parser);
1141 for (m = sdp->sdp_media; m; m = m->m_next) {
1142 if (m->m_proto != sdp_proto_rtp)
1144 if (m->m_type != sdp_media_audio)
1146 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1148 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1149 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1151 if (m->m_connections) {
1152 conn = m->m_connections;
1153 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1154 inet_pton(AF_INET, conn->c_address, ip);
1155 *ip = ntohl(p_s_rtp_ip_remote);
1157 char *p = sip->sip_payload->pl_data;
1160 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1161 p = strstr(p, "c=IN IP4 ");
1163 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1164 sdp_parser_free(parser);
1168 if ((p = strchr(addr, '\n'))) *p = '\0';
1169 if ((p = strchr(addr, '\r'))) *p = '\0';
1170 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1171 inet_pton(AF_INET, addr, ip);
1172 *ip = ntohl(p_s_rtp_ip_remote);
1174 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1175 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1176 if (map->rm_pt == payload_type) {
1177 PDEBUG(DEBUG_SIP, "supported codec found\n");
1178 codec_supported = 1;
1185 sdp_parser_free(parser);
1187 if (!codec_supported)
1193 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[])
1195 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1196 const char *from = "", *to = "";
1198 class Endpoint *epoint;
1199 struct lcr_msg *message;
1200 uint8_t payload_type;
1201 struct interface *interface;
1203 interface = getinterfacebyname(inst->interface_name);
1205 PERROR("Cannot find interface %s.\n", inst->interface_name);
1209 if (sip->sip_from && sip->sip_from->a_url)
1210 from = sip->sip_from->a_url->url_user;
1211 if (sip->sip_to && sip->sip_to->a_url)
1212 to = sip->sip_to->a_url->url_user;
1213 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1216 payload_type = 3; // FIXME: use list and forward list to remote side
1218 payload_type = (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW;
1219 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_type);
1222 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1224 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1225 nua_handle_destroy(nh);
1227 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1229 add_trace("respond", "value", "415 Unsupported Media");
1231 add_trace("respond", "value", "400 Bad Request");
1232 add_trace("reason", NULL, "offered codec does not match");
1234 new_state(PORT_STATE_RELEASE);
1235 trigger_work(&p_s_delete);
1239 /* open local RTP peer (if not bridging) */
1240 if (!p_s_rtp_bridge && rtp_open() < 0) {
1241 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1242 nua_handle_destroy(nh);
1244 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1245 add_trace("respond", "value", "500 Internal Server Error");
1246 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1248 new_state(PORT_STATE_RELEASE);
1249 trigger_work(&p_s_delete);
1254 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1255 add_trace("handle", "new", "0x%x", nh);
1259 sip_trace_header(this, "INVITE", DIRECTION_IN);
1260 add_trace("RTP", "port", "%d", p_s_rtp_port_remote);
1261 /* caller information */
1263 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1264 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1265 add_trace("calling", "present", "unavailable");
1267 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1268 add_trace("calling", "present", "allowed");
1269 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1270 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1271 SCPY(p_callerinfo.id, from);
1272 add_trace("calling", "number", "%s", from);
1274 SCPY(p_callerinfo.interface, inst->interface_name);
1275 /* dialing information */
1277 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1278 SCAT(p_dialinginfo.id, to);
1279 add_trace("dialing", "number", "%s", to);
1282 /* bearer capability */
1283 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1284 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1285 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1286 add_trace("bearer", "capa", "speech");
1287 add_trace("bearer", "mode", "circuit");
1288 /* if packet mode works some day, see dss1.cpp for conditions */
1289 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1293 /* create endpoint */
1295 FATAL("Incoming call but already got an endpoint.\n");
1296 if (!(epoint = new Endpoint(p_serial, 0)))
1297 FATAL("No memory for Endpoint instance\n");
1298 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1299 epointlist_new(epoint->ep_serial);
1301 /* send trying (proceeding) */
1302 nua_respond(nh, SIP_100_TRYING, TAG_END());
1303 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1304 add_trace("respond", "value", "100 Trying");
1307 new_state(PORT_STATE_IN_PROCEEDING);
1309 /* send setup message to endpoit */
1310 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1311 message->param.setup.port_type = p_type;
1312 // message->param.setup.dtmf = 0;
1313 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1314 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1315 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1316 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1317 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1318 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1319 if (p_s_rtp_bridge) {
1320 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1321 message->param.setup.rtpinfo.payload_type = payload_type;
1322 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1323 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1325 message_put(message);
1328 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[])
1330 struct lcr_msg *message;
1333 PDEBUG(DEBUG_SIP, "bye received\n");
1335 sip_trace_header(this, "BYE", DIRECTION_IN);
1336 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1337 cause = atoi(sip->sip_reason->re_cause);
1338 add_trace("cause", "value", "%d", cause);
1342 // let stack do bye automaticall, since it will not accept our response for some reason
1343 // nua_respond(nh, SIP_200_OK, TAG_END());
1344 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1345 add_trace("respond", "value", "200 OK");
1347 // nua_handle_destroy(nh);
1352 while(p_epointlist) {
1353 /* send setup message to endpoit */
1354 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1355 message->param.disconnectinfo.cause = cause ? : 16;
1356 message->param.disconnectinfo.location = LOCATION_BEYOND;
1357 message_put(message);
1359 free_epointlist(p_epointlist);
1361 new_state(PORT_STATE_RELEASE);
1362 trigger_work(&p_s_delete);
1365 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[])
1367 struct lcr_msg *message;
1369 PDEBUG(DEBUG_SIP, "cancel received\n");
1371 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1374 nua_handle_destroy(nh);
1379 while(p_epointlist) {
1380 /* send setup message to endpoit */
1381 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1382 message->param.disconnectinfo.cause = 16;
1383 message->param.disconnectinfo.location = LOCATION_BEYOND;
1384 message_put(message);
1386 free_epointlist(p_epointlist);
1388 new_state(PORT_STATE_RELEASE);
1389 trigger_work(&p_s_delete);
1392 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[])
1394 PDEBUG(DEBUG_SIP, "bye response received\n");
1396 nua_handle_destroy(nh);
1401 trigger_work(&p_s_delete);
1404 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[])
1406 PDEBUG(DEBUG_SIP, "cancel response received\n");
1408 nua_handle_destroy(nh);
1413 trigger_work(&p_s_delete);
1416 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[])
1418 struct lcr_msg *message;
1419 int cause = 0, location = 0;
1421 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1423 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1424 add_trace("respond", "value", "%d", status);
1428 if (status == 183 || (status >= 200 && status <= 299)) {
1430 uint8_t payload_type;
1433 payload_type = p_s_rtp_payload_type;
1435 payload_type = (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW;
1436 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_type);
1439 nua_cancel(nh, TAG_END());
1441 nua_cancel(nh, TAG_END());
1442 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1443 add_trace("reason", NULL, "offered codec does not match");
1446 location = LOCATION_PRIVATE_LOCAL;
1447 goto release_with_cause;
1450 /* connect to remote RTP (if not bridging) */
1451 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1452 nua_cancel(nh, TAG_END());
1453 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1454 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1457 location = LOCATION_PRIVATE_LOCAL;
1458 goto release_with_cause;
1465 #ifdef NUTAG_AUTO100
1466 PDEBUG(DEBUG_SIP, "do proceeding\n");
1467 new_state(PORT_STATE_OUT_PROCEEDING);
1468 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1469 message_put(message);
1473 PDEBUG(DEBUG_SIP, "do alerting\n");
1474 new_state(PORT_STATE_OUT_ALERTING);
1475 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1476 message_put(message);
1479 PDEBUG(DEBUG_SIP, "do progress\n");
1480 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1481 message->param.progressinfo.progress = 8;
1482 message->param.progressinfo.location = 10;
1483 if (p_s_rtp_bridge) {
1484 message->param.progressinfo.rtpinfo.payload_type = p_s_rtp_payload_type;
1485 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1486 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1488 message_put(message);
1491 if (status < 100 || status > 199)
1493 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1499 if (status >= 200 && status <= 299) {
1500 PDEBUG(DEBUG_SIP, "do connect\n");
1501 nua_ack(nh, TAG_END());
1502 new_state(PORT_STATE_CONNECT);
1503 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1504 if (p_s_rtp_bridge) {
1505 message->param.connectinfo.rtpinfo.payload_type = p_s_rtp_payload_type;
1506 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1507 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1509 message_put(message);
1512 cause = status2cause(status);
1513 location = LOCATION_BEYOND;
1516 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1518 while(p_epointlist) {
1519 /* send setup message to endpoit */
1520 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1521 message->param.disconnectinfo.cause = cause;
1522 message->param.disconnectinfo.location = location;
1523 message_put(message);
1525 free_epointlist(p_epointlist);
1528 new_state(PORT_STATE_RELEASE);
1532 trigger_work(&p_s_delete);
1535 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[])
1537 struct sip_inst *inst = (struct sip_inst *) magic;
1539 class Psip *psip = NULL;
1541 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1545 /* create or find port instance */
1546 if (event == nua_i_invite)
1549 struct interface *interface = interface_first;
1551 /* create call instance */
1552 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1554 if (!strcmp(interface->name, inst->interface_name))
1556 interface = interface->next;
1559 PERROR("Cannot find interface %s.\n", inst->interface_name);
1562 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1563 FATAL("Cannot create Port instance.\n");
1567 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1568 psip = (class Psip *)port;
1569 if (psip->p_s_handle == nh) {
1577 PERROR("no SIP Port found for handel %p\n", nh);
1578 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1579 nua_handle_destroy(nh);
1584 case nua_r_set_params:
1585 PDEBUG(DEBUG_SIP, "setparam response\n");
1588 PDEBUG(DEBUG_SIP, "error received\n");
1591 PDEBUG(DEBUG_SIP, "state change received\n");
1593 case nua_i_register:
1594 PDEBUG(DEBUG_SIP, "register received\n");
1597 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1600 PDEBUG(DEBUG_SIP, "ack received\n");
1603 PDEBUG(DEBUG_SIP, "active received\n");
1606 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1609 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1612 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1615 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1618 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1620 case nua_i_terminated:
1621 PDEBUG(DEBUG_SIP, "terminated received\n");
1624 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1628 /* received shutdown due to termination of RTP */
1629 void Psip::rtp_shutdown(void)
1631 struct lcr_msg *message;
1633 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1635 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1638 nua_handle_destroy(p_s_handle);
1641 while(p_epointlist) {
1642 /* send setup message to endpoit */
1643 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1644 message->param.disconnectinfo.cause = 16;
1645 message->param.disconnectinfo.location = LOCATION_BEYOND;
1646 message_put(message);
1648 free_epointlist(p_epointlist);
1650 new_state(PORT_STATE_RELEASE);
1651 trigger_work(&p_s_delete);
1654 int sip_init_inst(struct interface *interface)
1656 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1658 interface->sip_inst = inst;
1659 SCPY(inst->interface_name, interface->name);
1660 SCPY(inst->local_ip, interface->sip_local_ip);
1661 SCPY(inst->remote_ip, interface->sip_remote_ip);
1663 /* init root object */
1664 inst->root = su_root_create(inst);
1666 PERROR("Failed to create SIP root\n");
1667 sip_exit_inst(interface);
1671 inst->nua = nua_create(inst->root, sip_callback, inst, TAG_NULL());
1673 PERROR("Failed to create SIP stack object\n");
1674 sip_exit_inst(interface);
1677 nua_set_params(inst->nua,
1678 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1679 NUTAG_APPL_METHOD("INVITE"),
1680 NUTAG_APPL_METHOD("ACK"),
1681 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1682 NUTAG_APPL_METHOD("CANCEL"),
1683 NUTAG_APPL_METHOD("OPTIONS"),
1684 NUTAG_APPL_METHOD("NOTIFY"),
1685 NUTAG_APPL_METHOD("INFO"),
1687 #ifdef NUTAG_AUTO100
1691 NUTAG_AUTOANSWER(0),
1694 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1699 void sip_exit_inst(struct interface *interface)
1701 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1706 su_root_destroy(inst->root);
1708 nua_destroy(inst->nua);
1710 FREE(inst, sizeof(*inst));
1711 interface->sip_inst = NULL;
1713 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1716 extern su_log_t su_log_default[];
1717 extern su_log_t nua_log[];
1718 //extern su_log_t soa_log[];
1724 /* init SOFIA lib */
1726 su_home_init(sip_home);
1728 if (options.deb & DEBUG_SIP) {
1729 su_log_set_level(su_log_default, 9);
1730 su_log_set_level(nua_log, 9);
1731 //su_log_set_level(soa_log, 9);
1734 for (i = 0; i < 256; i++)
1735 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);
1737 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1744 su_home_deinit(sip_home);
1747 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1750 void sip_handle(void)
1752 struct interface *interface = interface_first;
1753 struct sip_inst *inst;
1756 if (interface->sip_inst) {
1757 inst = (struct sip_inst *) interface->sip_inst;
1758 su_root_step(inst->root, 0);
1760 interface = interface->next;
1764 /* deletes when back in event loop */
1765 static int delete_event(struct lcr_work *work, void *instance, int index)
1767 class Psip *psip = (class Psip *)instance;