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 struct interface *interface;
29 static int delete_event(struct lcr_work *work, void *instance, int index);
34 Psip::Psip(int type, struct mISDNport *mISDNport, char *portname, struct port_settings *settings, int channel, int exclusive, int mode, struct interface *interface) : PmISDN(type, mISDNport, portname, settings, channel, exclusive, mode)
36 p_m_s_sip_inst = interface->sip_inst;
37 memset(&p_m_s_delete, 0, sizeof(p_m_s_delete));
38 add_work(&p_m_s_delete, delete_event, this, 0);
41 memset(&p_m_s_rtp_fd, 0, sizeof(p_m_s_rtp_fd));
42 memset(&p_m_s_rtcp_fd, 0, sizeof(p_m_s_rtcp_fd));
43 memset(&p_m_s_rtp_sin_local, 0, sizeof(p_m_s_rtp_sin_local));
44 memset(&p_m_s_rtcp_sin_local, 0, sizeof(p_m_s_rtcp_sin_local));
45 memset(&p_m_s_rtp_sin_remote, 0, sizeof(p_m_s_rtp_sin_remote));
46 memset(&p_m_s_rtcp_sin_remote, 0, sizeof(p_m_s_rtcp_sin_remote));
47 p_m_s_rtp_ip_local = 0;
48 p_m_s_rtp_ip_remote = 0;
49 p_m_s_rtp_port_local = 0;
50 p_m_s_rtp_port_remote = 0;
55 p_m_s_rtp_tx_action = 0;
57 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
66 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
68 del_work(&p_m_s_delete);
70 /* close audio transfer socket */
71 if (p_m_s_b_sock > -1)
78 static void sip_trace_header(class Psip *sip, const char *message, int direction)
80 /* init trace with given values */
83 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
84 sip?sip->p_dialinginfo.id:NULL,
95 /* according to RFC 3550 */
97 #if __BYTE_ORDER == __LITTLE_ENDIAN
102 uint8_t payload_type:7,
104 #elif __BYTE_ORDER == __BIG_ENDIAN
115 } __attribute__((packed));
120 } __attribute__((packed));
122 #define RTP_VERSION 2
124 #define RTP_PT_ULAW 0
125 #define RTP_PT_ALAW 8
126 #define RTP_PT_GSM_FULL 3
127 #define RTP_PT_GSM_HALF 96
128 #define RTP_PT_GSM_EFR 97
129 #define RTP_PT_AMR 98
131 /* decode an rtp frame */
132 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
134 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
135 struct rtp_x_hdr *rtpxh;
141 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
144 if (rtph->version != RTP_VERSION) {
145 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
148 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
149 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
150 if (payload_len < 0) {
151 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
152 "csrc count = %d)\n", len, rtph->csrc_count);
155 if (rtph->extension) {
156 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
157 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
158 "extension header\n");
161 rtpxh = (struct rtp_x_hdr *)payload;
162 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
164 payload_len -= x_len;
165 if (payload_len < 0) {
166 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
167 "extension header exceeds frame length\n");
172 if (payload_len < 0) {
173 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
177 payload_len -= payload[payload_len - 1];
178 if (payload_len < 0) {
179 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
180 "greater than payload\n");
185 switch (rtph->payload_type) {
186 case RTP_PT_GSM_FULL:
187 if (payload_len != 33) {
188 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
189 "payload length != 33 (len = %d)\n",
195 if (payload_len != 31) {
196 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
197 "payload length != 31 (len = %d)\n",
203 if (options.law != 'a') {
204 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
209 if (options.law == 'a') {
210 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
215 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
216 "type %d\n", rtph->payload_type);
220 if (payload_len <= 0) {
221 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
225 psip->bchannel_send(PH_DATA_REQ, 0, payload, payload_len);
230 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
232 class Psip *psip = (class Psip *) instance;
234 unsigned char buffer[256];
237 if ((what & LCR_FD_READ)) {
238 len = read(fd->fd, &buffer, sizeof(buffer));
240 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
241 // psip->rtp_close();
242 // psip->rtp_shutdown();
245 rc = rtp_decode(psip, buffer, len);
251 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
253 // class Psip *psip = (class Psip *) instance;
255 unsigned char buffer[256];
257 if ((what & LCR_FD_READ)) {
258 len = read(fd->fd, &buffer, sizeof(buffer));
260 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
261 // psip->rtp_close();
262 // psip->rtp_shutdown();
265 PDEBUG(DEBUG_SIP, "rtcp!");
271 #define RTP_PORT_BASE 30000
272 static unsigned int next_udp_port = RTP_PORT_BASE;
274 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
277 socklen_t alen = sizeof(*sin_local);
279 sin_local->sin_family = AF_INET;
280 sin_local->sin_addr.s_addr = htonl(ip);
281 sin_local->sin_port = htons(port);
283 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
287 /* retrieve the address we actually bound to, in case we
288 * passed INADDR_ANY as IP address */
289 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
292 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
295 socklen_t alen = sizeof(*sin_local);
297 sin_remote->sin_family = AF_INET;
298 sin_remote->sin_addr.s_addr = htonl(ip);
299 sin_remote->sin_port = htons(port);
301 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
303 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
307 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
310 int Psip::rtp_open(void)
317 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
322 p_m_s_rtp_fd.fd = rc;
323 register_fd(&p_m_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
325 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
330 p_m_s_rtcp_fd.fd = rc;
331 register_fd(&p_m_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
334 ip = htonl(INADDR_ANY);
336 for (next_udp_port = next_udp_port % 0xffff;
337 next_udp_port < 0xffff; next_udp_port += 2) {
338 rc = rtp_sub_socket_bind(p_m_s_rtp_fd.fd, &p_m_s_rtp_sin_local, ip, next_udp_port);
342 rc = rtp_sub_socket_bind(p_m_s_rtcp_fd.fd, &p_m_s_rtcp_sin_local, ip, next_udp_port+1);
347 PDEBUG(DEBUG_SIP, "failed to find port\n");
351 p_m_s_rtp_port_local = next_udp_port;
352 p_m_s_rtp_ip_local = ntohl(p_m_s_rtp_sin_local.sin_addr.s_addr);
353 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_m_s_rtp_ip_local, p_m_s_rtp_port_local);
354 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_m_s_rtp_ip_remote, p_m_s_rtp_port_remote);
356 return p_m_s_rtp_port_local;
359 int Psip::rtp_connect(void)
364 ia.s_addr = htonl(p_m_s_rtp_ip_remote);
365 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_m_s_rtp_port_remote);
367 rc = rtp_sub_socket_connect(p_m_s_rtp_fd.fd, &p_m_s_rtp_sin_local, &p_m_s_rtp_sin_remote, p_m_s_rtp_ip_remote, p_m_s_rtp_port_remote);
371 rc = rtp_sub_socket_connect(p_m_s_rtcp_fd.fd, &p_m_s_rtcp_sin_local, &p_m_s_rtcp_sin_remote, p_m_s_rtp_ip_remote, p_m_s_rtp_port_remote + 1);
375 p_m_s_rtp_ip_local = ntohl(p_m_s_rtp_sin_local.sin_addr.s_addr);
376 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_m_s_rtp_ip_local, p_m_s_rtp_port_local);
377 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_m_s_rtp_ip_remote, p_m_s_rtp_port_remote);
378 p_m_s_rtp_is_connected = 1;
382 void Psip::rtp_close(void)
384 if (p_m_s_rtp_fd.fd > 0) {
385 unregister_fd(&p_m_s_rtp_fd);
386 close(p_m_s_rtp_fd.fd);
389 if (p_m_s_rtcp_fd.fd > 0) {
390 unregister_fd(&p_m_s_rtcp_fd);
391 close(p_m_s_rtcp_fd.fd);
392 p_m_s_rtcp_fd.fd = 0;
394 if (p_m_s_rtp_is_connected) {
395 PDEBUG(DEBUG_SIP, "rtp closed\n");
396 p_m_s_rtp_is_connected = 0;
401 void tv_difference(struct timeval *diff, const struct timeval *from,
402 const struct timeval *__to)
404 struct timeval _to = *__to, *to = &_to;
406 if (to->tv_usec < from->tv_usec) {
408 to->tv_usec += 1000000;
411 diff->tv_usec = to->tv_usec - from->tv_usec;
412 diff->tv_sec = to->tv_sec - from->tv_sec;
415 /* encode and send a rtp frame */
416 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, int payload_type)
418 struct rtp_hdr *rtph;
420 int duration; /* in samples */
421 unsigned char buffer[256];
423 if (!p_m_s_rtp_is_connected) {
428 if (!p_m_s_rtp_tx_action) {
429 /* initialize sequences */
430 p_m_s_rtp_tx_action = 1;
431 p_m_s_rtp_tx_ssrc = rand();
432 p_m_s_rtp_tx_sequence = random();
433 p_m_s_rtp_tx_timestamp = random();
434 memset(&p_m_s_rtp_tx_last_tv, 0, sizeof(p_m_s_rtp_tx_last_tv));
437 switch (payload_type) {
438 case RTP_PT_GSM_FULL:
452 PERROR("unsupported message type %d\n", payload_type);
458 struct timeval tv, tv_diff;
459 long int usec_diff, frame_diff;
461 gettimeofday(&tv, NULL);
462 tv_difference(&tv_diff, &p_m_s_rtp_tx_last_tv, &tv);
463 p_m_s_rtp_tx_last_tv = tv;
465 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
466 frame_diff = (usec_diff / 20000);
468 if (abs(frame_diff) > 1) {
469 long int frame_diff_excess = frame_diff - 1;
471 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
472 p_m_s_rtp_tx_sequence += frame_diff_excess;
473 p_m_s_rtp_tx_timestamp += frame_diff_excess * duration;
478 rtph = (struct rtp_hdr *) buffer;
479 rtph->version = RTP_VERSION;
482 rtph->csrc_count = 0;
484 rtph->payload_type = payload_type;
485 rtph->sequence = htons(p_m_s_rtp_tx_sequence++);
486 rtph->timestamp = htonl(p_m_s_rtp_tx_timestamp);
487 p_m_s_rtp_tx_timestamp += duration;
488 rtph->ssrc = htonl(p_m_s_rtp_tx_ssrc);
489 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
491 if (p_m_s_rtp_fd.fd > 0) {
492 len = write(p_m_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
493 if (len != sizeof(struct rtp_hdr) + payload_len) {
494 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
508 /* select free bchannel from loopback interface */
509 int Psip::hunt_bchannel(void)
511 return loop_hunt_bchannel(this, p_m_mISDNport);
514 /* close SIP side bchannel */
515 void Psip::bchannel_close(void)
517 if (p_m_s_b_sock > -1) {
518 unregister_fd(&p_m_s_b_fd);
526 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index);
528 /* open external side bchannel */
529 int Psip::bchannel_open(int index)
532 struct sockaddr_mISDN addr;
533 struct mISDNhead act;
535 if (p_m_s_b_sock > -1) {
536 PERROR("Socket already created for index %d\n", index);
541 ret = p_m_s_b_sock = socket(PF_ISDN, SOCK_DGRAM, ISDN_P_B_RAW);
543 PERROR("Failed to open bchannel-socket for index %d\n", index);
547 memset(&p_m_s_b_fd, 0, sizeof(p_m_s_b_fd));
548 p_m_s_b_fd.fd = p_m_s_b_sock;
549 register_fd(&p_m_s_b_fd, LCR_FD_READ, b_handler, this, 0);
552 /* bind socket to bchannel */
553 addr.family = AF_ISDN;
554 addr.dev = mISDNloop.port;
555 addr.channel = index+1+(index>15);
556 ret = bind(p_m_s_b_sock, (struct sockaddr *)&addr, sizeof(addr));
558 PERROR("Failed to bind bchannel-socket for index %d\n", index);
562 /* activate bchannel */
563 PDEBUG(DEBUG_SIP, "Activating SIP side channel index %i.\n", index);
564 act.prim = PH_ACTIVATE_REQ;
566 ret = sendto(p_m_s_b_sock, &act, MISDN_HEADER_LEN, 0, NULL, 0);
568 PERROR("Failed to activate index %d\n", index);
573 p_m_s_b_index = index;
578 /* receive from bchannel */
579 void Psip::bchannel_receive(struct mISDNhead *hh, unsigned char *data, int len)
581 /* write to rx buffer */
583 p_m_s_rxdata[p_m_s_rxpos++] = flip[*data++];
584 if (p_m_s_rxpos == 160) {
587 /* transmit data via rtp */
588 rtp_send_frame(p_m_s_rxdata, 160, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW);
593 /* transmit to bchannel */
594 void Psip::bchannel_send(unsigned int prim, unsigned int id, unsigned char *data, int len)
596 unsigned char buf[MISDN_HEADER_LEN+len];
597 struct mISDNhead *hh = (struct mISDNhead *)buf;
598 unsigned char *to = buf + MISDN_HEADER_LEN;
605 /* make and send frame */
609 *to++ = flip[*data++];
610 ret = sendto(p_m_s_b_sock, buf, MISDN_HEADER_LEN+len, 0, NULL, 0);
612 PERROR("Failed to send to socket index %d\n", p_m_s_b_index);
615 /* handle socket input */
616 static int b_handler(struct lcr_fd *fd, unsigned int what, void *instance, int index)
618 class Psip *psip = (class Psip *)instance;
620 unsigned char buffer[2048+MISDN_HEADER_LEN];
621 struct mISDNhead *hh = (struct mISDNhead *)buffer;
623 /* handle message from bchannel */
624 if (psip->p_m_s_b_sock > -1) {
625 ret = recv(psip->p_m_s_b_sock, buffer, sizeof(buffer), 0);
626 if (ret >= (int)MISDN_HEADER_LEN) {
628 /* we don't care about confirms, we use rx data to sync tx */
631 /* we receive audio data, we respond to it AND we send tones */
633 psip->bchannel_receive(hh, buffer+MISDN_HEADER_LEN, ret-MISDN_HEADER_LEN);
635 case PH_ACTIVATE_IND:
636 psip->p_m_s_b_active = 1;
638 case PH_DEACTIVATE_IND:
639 psip->p_m_s_b_active = 0;
643 if (ret < 0 && errno != EWOULDBLOCK)
644 PERROR("Read from GSM port, index %d failed with return code %d\n", ret);
651 /* taken from freeswitch */
652 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
653 static int status2cause(int status)
657 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
663 return 21; //SWITCH_CAUSE_CALL_REJECTED;
665 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
668 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
671 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
673 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
682 return 127; //SWITCH_CAUSE_INTERWORKING;
684 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
689 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
692 return 17; //SWITCH_CAUSE_USER_BUSY;
694 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
697 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
699 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
701 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
705 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
708 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
710 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
712 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
716 static int cause2status(int cause, int location, const char **st)
722 s = 404; *st = sip_404_Not_found;
725 s = 404; *st = sip_404_Not_found;
728 s = 404; *st = sip_404_Not_found;
731 s = 486; *st = sip_486_Busy_here;
734 s = 408; *st = sip_408_Request_timeout;
737 s = 480; *st = sip_480_Temporarily_unavailable;
740 s = 480; *st = sip_480_Temporarily_unavailable;
743 if (location == LOCATION_USER) {
744 s = 603; *st = sip_603_Decline;
746 s = 403; *st = sip_403_Forbidden;
750 //s = 301; *st = sip_301_Moved_permanently;
751 s = 410; *st = sip_410_Gone;
754 s = 410; *st = sip_410_Gone;
757 s = 502; *st = sip_502_Bad_gateway;
760 s = 484; *st = sip_484_Address_incomplete;
763 s = 501; *st = sip_501_Not_implemented;
766 s = 480; *st = sip_480_Temporarily_unavailable;
769 s = 503; *st = sip_503_Service_unavailable;
772 s = 503; *st = sip_503_Service_unavailable;
775 s = 503; *st = sip_503_Service_unavailable;
778 s = 503; *st = sip_503_Service_unavailable;
781 s = 503; *st = sip_503_Service_unavailable;
784 s = 403; *st = sip_403_Forbidden;
787 s = 403; *st = sip_403_Forbidden;
790 s = 503; *st = sip_503_Service_unavailable;
793 s = 488; *st = sip_488_Not_acceptable;
796 s = 501; *st = sip_501_Not_implemented;
799 s = 488; *st = sip_488_Not_acceptable;
802 s = 501; *st = sip_501_Not_implemented;
805 s = 403; *st = sip_403_Forbidden;
808 s = 503; *st = sip_503_Service_unavailable;
811 s = 504; *st = sip_504_Gateway_time_out;
814 s = 468; *st = sip_486_Busy_here;
821 * endpoint sends messages to the SIP port
824 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
828 struct lcr_msg *message;
830 if (rtp_connect() < 0) {
831 nua_cancel(p_m_s_handle, TAG_END());
832 nua_handle_destroy(p_m_s_handle);
834 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
835 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
837 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
838 message->param.disconnectinfo.cause = 41;
839 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
840 message_put(message);
841 new_state(PORT_STATE_RELEASE);
842 trigger_work(&p_m_s_delete);
845 ia.s_addr = htonl(p_m_s_rtp_ip_local);
849 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
853 "m=audio %d RTP/AVP %d\n"
854 "a=rtpmap:%d %s/8000\n"
855 , inet_ntoa(ia), inet_ntoa(ia), p_m_s_rtp_port_local, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW, (options.law=='a')?"PCMA":"PCMU");
856 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
858 nua_respond(p_m_s_handle, SIP_200_OK,
859 NUTAG_MEDIA_ENABLE(0),
860 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
861 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
862 new_state(PORT_STATE_CONNECT);
863 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
864 add_trace("respond", "value", "200 OK");
865 add_trace("reason", NULL, "call connected");
871 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
873 struct lcr_msg *message;
874 char cause_str[128] = "";
875 int cause = param->disconnectinfo.cause;
876 int location = param->disconnectinfo.cause;
878 const char *status_text;
880 if (cause > 0 && cause <= 127) {
881 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
885 case PORT_STATE_OUT_SETUP:
886 case PORT_STATE_OUT_PROCEEDING:
887 case PORT_STATE_OUT_ALERTING:
888 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
889 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
891 add_trace("cause", "value", "%d", cause);
893 nua_cancel(p_m_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
895 case PORT_STATE_IN_SETUP:
896 case PORT_STATE_IN_PROCEEDING:
897 case PORT_STATE_IN_ALERTING:
898 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
899 status = cause2status(cause, location, &status_text);
900 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
902 add_trace("cause", "value", "%d", cause);
903 add_trace("respond", "value", "%d %s", status, status_text);
905 nua_respond(p_m_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
906 nua_handle_destroy(p_m_s_handle);
908 trigger_work(&p_m_s_delete);
911 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
912 sip_trace_header(this, "BYE", DIRECTION_OUT);
914 add_trace("cause", "value", "%d", cause);
916 nua_bye(p_m_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
919 if (message_id == MESSAGE_DISCONNECT) {
920 while(p_epointlist) {
921 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
922 message->param.disconnectinfo.cause = CAUSE_NORMAL;
923 message->param.disconnectinfo.location = LOCATION_BEYOND;
924 message_put(message);
926 free_epointlist(p_epointlist);
930 new_state(PORT_STATE_RELEASE);
935 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
937 struct sip_inst *inst = (struct sip_inst *) p_m_s_sip_inst;
940 const char *local = inst->interface->sip_local_ip;
941 const char *remote = inst->interface->sip_remote_ip;
944 struct epoint_list *epointlist;
945 sip_cseq_t *cseq = NULL;
949 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
951 /* release if port is blocked */
952 if (p_m_mISDNport->ifport->block) {
953 struct lcr_msg *message;
955 sip_trace_header(this, "INVITE", DIRECTION_OUT);
956 add_trace("failure", NULL, "Port blocked.");
958 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
959 message->param.disconnectinfo.cause = 27; // temp. unavail.
960 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
961 message_put(message);
962 new_state(PORT_STATE_RELEASE);
963 trigger_work(&p_m_s_delete);
968 ret = channel = hunt_bchannel();
972 ret = seize_bchannel(channel, 1);
974 struct lcr_msg *message;
977 sip_trace_header(this, "INVITE", DIRECTION_OUT);
978 add_trace("failure", NULL, "No internal audio channel available.");
980 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
981 message->param.disconnectinfo.cause = 34;
982 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
983 message_put(message);
984 new_state(PORT_STATE_RELEASE);
985 trigger_work(&p_m_s_delete);
988 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
989 if (bchannel_open(p_m_b_index))
992 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
993 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
994 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
996 /* connect to remote RTP */
997 if (rtp_open() < 0) {
998 struct lcr_msg *message;
1000 PERROR("Failed to open RTP sockets\n");
1001 /* send release message to endpoit */
1002 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1003 message->param.disconnectinfo.cause = 41;
1004 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1005 message_put(message);
1006 new_state(PORT_STATE_RELEASE);
1007 trigger_work(&p_m_s_delete);
1010 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
1011 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
1013 sip_trace_header(this, "INVITE", DIRECTION_OUT);
1014 add_trace("from", "uri", "%s", from);
1015 add_trace("to", "uri", "%s", to);
1016 add_trace("rtp", "port", "%d,%d", p_m_s_rtp_port_local, p_m_s_rtp_port_local + 1);
1019 p_m_s_handle = nua_handle(inst->nua, NULL, TAG_END());
1020 if (!p_m_s_handle) {
1021 struct lcr_msg *message;
1023 PERROR("Failed to create handle\n");
1024 /* send release message to endpoit */
1025 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1026 message->param.disconnectinfo.cause = 41;
1027 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1028 message_put(message);
1029 new_state(PORT_STATE_RELEASE);
1030 trigger_work(&p_m_s_delete);
1034 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1035 add_trace("handle", "new", "0x%x", p_m_s_handle);
1038 inet_pton(AF_INET, local, &p_m_s_rtp_ip_local);
1039 p_m_s_rtp_ip_local = ntohl(p_m_s_rtp_ip_local);
1040 ia.s_addr = htonl(p_m_s_rtp_ip_local);
1043 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1047 "m=audio %d RTP/AVP %d\n"
1048 "a=rtpmap:%d %s/8000\n"
1049 , inet_ntoa(ia), inet_ntoa(ia), p_m_s_rtp_port_local, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW, (options.law=='a')?RTP_PT_ALAW:RTP_PT_ULAW, (options.law=='a')?"PCMA":"PCMU");
1050 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
1052 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
1054 nua_invite(p_m_s_handle,
1055 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1056 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1057 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1058 NUTAG_MEDIA_ENABLE(0),
1059 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1060 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1061 new_state(PORT_STATE_OUT_SETUP);
1063 /* attach only if not already */
1064 epointlist = p_epointlist;
1066 if (epointlist->epoint_id == epoint_id)
1068 epointlist = epointlist->next;
1071 epointlist_new(epoint_id);
1076 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1078 class Endpoint *epoint;
1080 if (PmISDN::message_epoint(epoint_id, message_id, param))
1083 epoint = find_epoint_id(epoint_id);
1085 PDEBUG(DEBUG_SIP, "PORT(%s) no endpoint object found where the message is from.\n", p_name);
1089 switch(message_id) {
1090 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1091 if (p_state != PORT_STATE_IN_SETUP
1092 && p_state != PORT_STATE_IN_PROCEEDING)
1094 nua_respond(p_m_s_handle, SIP_180_RINGING, TAG_END());
1095 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1096 add_trace("respond", "value", "180 Ringing");
1098 new_state(PORT_STATE_IN_ALERTING);
1101 case MESSAGE_CONNECT: /* call is connected on LCR side */
1102 if (p_state != PORT_STATE_IN_SETUP
1103 && p_state != PORT_STATE_IN_PROCEEDING
1104 && p_state != PORT_STATE_IN_ALERTING)
1106 message_connect(epoint_id, message_id, param);
1109 case MESSAGE_DISCONNECT: /* call has been disconnected */
1110 case MESSAGE_RELEASE: /* call has been released */
1111 message_release(epoint_id, message_id, param);
1114 case MESSAGE_SETUP: /* dial-out command received from epoint */
1115 message_setup(epoint_id, message_id, param);
1119 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1125 int Psip::parse_sdp(sip_t const *sip, unsigned int *ip, unsigned short *port)
1127 int codec_supported = 0;
1129 if (!sip->sip_payload) {
1130 PDEBUG(DEBUG_SIP, "no payload given\n");
1134 sdp_parser_t *parser;
1137 sdp_attribute_t *attr;
1139 sdp_connection_t *conn;
1141 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1143 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1147 if (!(sdp = sdp_session(parser))) {
1148 sdp_parser_free(parser);
1151 for (m = sdp->sdp_media; m; m = m->m_next) {
1152 if (m->m_proto != sdp_proto_rtp)
1154 if (m->m_type != sdp_media_audio)
1156 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1158 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1159 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1161 if (m->m_connections) {
1162 conn = m->m_connections;
1163 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1164 inet_pton(AF_INET, conn->c_address, ip);
1165 *ip = ntohl(p_m_s_rtp_ip_remote);
1167 char *p = sip->sip_payload->pl_data;
1170 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1171 p = strstr(p, "c=IN IP4 ");
1173 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1174 sdp_parser_free(parser);
1178 if ((p = strchr(addr, '\n'))) *p = '\0';
1179 if ((p = strchr(addr, '\r'))) *p = '\0';
1180 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1181 inet_pton(AF_INET, addr, ip);
1182 *ip = ntohl(p_m_s_rtp_ip_remote);
1184 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1185 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d'\n", map->rm_encoding, map->rm_rate);
1186 if (!strcmp(map->rm_encoding, (options.law=='a')?"PCMA":"PCMU") && map->rm_rate == 8000) {
1187 PDEBUG(DEBUG_SIP, "supported codec found\n");
1188 codec_supported = 1;
1195 sdp_parser_free(parser);
1197 if (!codec_supported)
1203 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[])
1205 const char *from = "", *to = "";
1207 class Endpoint *epoint;
1208 struct lcr_msg *message;
1211 if (sip->sip_from && sip->sip_from->a_url)
1212 from = sip->sip_from->a_url->url_user;
1213 if (sip->sip_to && sip->sip_to->a_url)
1214 to = sip->sip_to->a_url->url_user;
1215 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1217 ret = parse_sdp(sip, &p_m_s_rtp_ip_remote, &p_m_s_rtp_port_remote);
1220 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1222 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1223 nua_handle_destroy(nh);
1224 p_m_s_handle = NULL;
1225 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1227 add_trace("respond", "value", "415 Unsupported Media");
1229 add_trace("respond", "value", "400 Bad Request");
1230 add_trace("reason", NULL, "offered codec does not match");
1232 new_state(PORT_STATE_RELEASE);
1233 trigger_work(&p_m_s_delete);
1237 /* connect to remote RTP */
1238 if (rtp_open() < 0) {
1239 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1240 nua_handle_destroy(nh);
1241 p_m_s_handle = NULL;
1242 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1243 add_trace("respond", "value", "500 Internal Server Error");
1244 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1246 new_state(PORT_STATE_RELEASE);
1247 trigger_work(&p_m_s_delete);
1252 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1253 add_trace("handle", "new", "0x%x", nh);
1257 /* if blocked, release call */
1258 if (p_m_mISDNport->ifport->block) {
1259 nua_respond(nh, SIP_503_SERVICE_UNAVAILABLE, TAG_END());
1260 nua_handle_destroy(nh);
1261 p_m_s_handle = NULL;
1262 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1263 add_trace("respond", "value", "503 Service Unavailable");
1264 add_trace("reason", NULL, "port is blocked");
1266 new_state(PORT_STATE_RELEASE);
1267 trigger_work(&p_m_s_delete);
1270 sip_trace_header(this, "INVITE", DIRECTION_IN);
1271 add_trace("RTP", "port", "%d", p_m_s_rtp_port_remote);
1272 /* caller information */
1274 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1275 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1276 add_trace("calling", "present", "unavailable");
1278 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1279 add_trace("calling", "present", "allowed");
1280 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1281 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1282 SCPY(p_callerinfo.id, from);
1283 add_trace("calling", "number", "%s", from);
1285 p_callerinfo.isdn_port = p_m_portnum;
1286 SCPY(p_callerinfo.interface, p_m_mISDNport->ifport->interface->name);
1287 /* dialing information */
1289 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1290 SCAT(p_dialinginfo.id, to);
1291 add_trace("dialing", "number", "%s", to);
1294 /* bearer capability */
1295 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1296 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1297 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1298 add_trace("bearer", "capa", "speech");
1299 add_trace("bearer", "mode", "circuit");
1300 /* if packet mode works some day, see dss1.cpp for conditions */
1301 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1306 ret = channel = hunt_bchannel();
1311 ret = seize_bchannel(channel, 1);
1314 nua_respond(nh, SIP_480_TEMPORARILY_UNAVAILABLE, TAG_END());
1315 nua_handle_destroy(nh);
1316 p_m_s_handle = NULL;
1317 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1318 add_trace("respond", "value", "480 Temporarily Unavailable");
1319 add_trace("reason", NULL, "no channel");
1321 new_state(PORT_STATE_RELEASE);
1322 trigger_work(&p_m_s_delete);
1325 bchannel_event(p_m_mISDNport, p_m_b_index, B_EVENT_USE);
1326 if (bchannel_open(p_m_b_index))
1329 /* create endpoint */
1331 FATAL("Incoming call but already got an endpoint.\n");
1332 if (!(epoint = new Endpoint(p_serial, 0)))
1333 FATAL("No memory for Endpoint instance\n");
1334 if (!(epoint->ep_app = new DEFAULT_ENDPOINT_APP(epoint, 0))) //incoming
1335 FATAL("No memory for Endpoint Application instance\n");
1336 epointlist_new(epoint->ep_serial);
1338 /* send trying (proceeding) */
1339 nua_respond(nh, SIP_100_TRYING, TAG_END());
1340 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1341 add_trace("respond", "value", "100 Trying");
1344 new_state(PORT_STATE_IN_PROCEEDING);
1346 /* send setup message to endpoit */
1347 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1348 message->param.setup.isdn_port = p_m_portnum;
1349 message->param.setup.port_type = p_type;
1350 // message->param.setup.dtmf = 0;
1351 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1352 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1353 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1354 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1355 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1356 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1357 message_put(message);
1360 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[])
1362 struct lcr_msg *message;
1365 PDEBUG(DEBUG_SIP, "bye received\n");
1367 sip_trace_header(this, "BYE", DIRECTION_IN);
1368 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1369 cause = atoi(sip->sip_reason->re_cause);
1370 add_trace("cause", "value", "%d", cause);
1374 // let stack do bye automaticall, since it will not accept our response for some reason
1375 // nua_respond(nh, SIP_200_OK, TAG_END());
1376 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1377 add_trace("respond", "value", "200 OK");
1379 // nua_handle_destroy(nh);
1380 p_m_s_handle = NULL;
1384 while(p_epointlist) {
1385 /* send setup message to endpoit */
1386 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1387 message->param.disconnectinfo.cause = cause ? : 16;
1388 message->param.disconnectinfo.location = LOCATION_BEYOND;
1389 message_put(message);
1391 free_epointlist(p_epointlist);
1393 new_state(PORT_STATE_RELEASE);
1394 trigger_work(&p_m_s_delete);
1397 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[])
1399 struct lcr_msg *message;
1401 PDEBUG(DEBUG_SIP, "cancel received\n");
1403 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1406 nua_handle_destroy(nh);
1407 p_m_s_handle = NULL;
1411 while(p_epointlist) {
1412 /* send setup message to endpoit */
1413 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1414 message->param.disconnectinfo.cause = 16;
1415 message->param.disconnectinfo.location = LOCATION_BEYOND;
1416 message_put(message);
1418 free_epointlist(p_epointlist);
1420 new_state(PORT_STATE_RELEASE);
1421 trigger_work(&p_m_s_delete);
1424 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[])
1426 PDEBUG(DEBUG_SIP, "bye response received\n");
1428 nua_handle_destroy(nh);
1429 p_m_s_handle = NULL;
1433 trigger_work(&p_m_s_delete);
1436 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[])
1438 PDEBUG(DEBUG_SIP, "cancel response received\n");
1440 nua_handle_destroy(nh);
1441 p_m_s_handle = NULL;
1445 trigger_work(&p_m_s_delete);
1448 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[])
1450 struct lcr_msg *message;
1451 int cause = 0, location = 0;
1453 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1455 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1456 add_trace("respond", "value", "%d", status);
1462 PDEBUG(DEBUG_SIP, "do proceeding\n");
1463 new_state(PORT_STATE_OUT_PROCEEDING);
1464 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1465 message_put(message);
1468 PDEBUG(DEBUG_SIP, "do alerting\n");
1469 new_state(PORT_STATE_OUT_ALERTING);
1470 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1471 message_put(message);
1474 if (status < 100 || status > 199)
1476 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1482 if (status >= 200 && status <= 299) {
1485 ret = parse_sdp(sip, &p_m_s_rtp_ip_remote, &p_m_s_rtp_port_remote);
1488 nua_cancel(nh, TAG_END());
1490 nua_cancel(nh, TAG_END());
1491 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1492 add_trace("reason", NULL, "offered codec does not match");
1495 location = LOCATION_PRIVATE_LOCAL;
1496 goto release_with_cause;
1499 /* connect to remote RTP */
1500 if (rtp_connect() < 0) {
1501 nua_cancel(nh, TAG_END());
1502 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1503 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1506 location = LOCATION_PRIVATE_LOCAL;
1507 goto release_with_cause;
1510 PDEBUG(DEBUG_SIP, "do connect\n");
1511 nua_ack(nh, TAG_END());
1512 new_state(PORT_STATE_CONNECT);
1513 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1514 message_put(message);
1519 cause = status2cause(status);
1520 location = LOCATION_BEYOND;
1523 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1525 while(p_epointlist) {
1526 /* send setup message to endpoit */
1527 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1528 message->param.disconnectinfo.cause = cause;
1529 message->param.disconnectinfo.location = location;
1530 message_put(message);
1532 free_epointlist(p_epointlist);
1535 new_state(PORT_STATE_RELEASE);
1539 trigger_work(&p_m_s_delete);
1542 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[])
1544 struct sip_inst *inst = (struct sip_inst *) magic;
1546 class Psip *psip = NULL;
1548 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1552 /* create or find port instance */
1553 if (event == nua_i_invite)
1556 /* create call instance */
1557 SPRINT(name, "%s-%d-in", inst->interface->name, 0);
1558 if (!(psip = new Psip(PORT_TYPE_SIP_IN, (inst->interface->ifport) ? inst->interface->ifport->mISDNport : NULL, name, NULL, 0, 0, B_MODE_TRANSPARENT, inst->interface)))
1559 FATAL("Cannot create Port instance.\n");
1563 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1564 psip = (class Psip *)port;
1565 if (psip->p_m_s_handle == nh) {
1573 PERROR("no SIP Port found for handel %p\n", nh);
1574 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1575 nua_handle_destroy(nh);
1580 case nua_r_set_params:
1581 PDEBUG(DEBUG_SIP, "setparam response\n");
1584 PDEBUG(DEBUG_SIP, "error received\n");
1587 PDEBUG(DEBUG_SIP, "state change received\n");
1589 case nua_i_register:
1590 PDEBUG(DEBUG_SIP, "register received\n");
1593 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1596 PDEBUG(DEBUG_SIP, "ack received\n");
1599 PDEBUG(DEBUG_SIP, "active received\n");
1602 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1605 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1608 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1611 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1614 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1616 case nua_i_terminated:
1617 PDEBUG(DEBUG_SIP, "terminated received\n");
1620 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1624 /* received shutdown due to termination of RTP */
1625 void Psip::rtp_shutdown(void)
1627 struct lcr_msg *message;
1629 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1631 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1634 nua_handle_destroy(p_m_s_handle);
1635 p_m_s_handle = NULL;
1637 while(p_epointlist) {
1638 /* send setup message to endpoit */
1639 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1640 message->param.disconnectinfo.cause = 16;
1641 message->param.disconnectinfo.location = LOCATION_BEYOND;
1642 message_put(message);
1644 free_epointlist(p_epointlist);
1646 new_state(PORT_STATE_RELEASE);
1647 trigger_work(&p_m_s_delete);
1650 int sip_init_inst(struct interface *interface)
1652 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1654 interface->sip_inst = inst;
1655 inst->interface = interface;
1657 /* init root object */
1658 inst->root = su_root_create(inst);
1660 PERROR("Failed to create SIP root\n");
1661 sip_exit_inst(interface);
1665 inst->nua = nua_create(inst->root, sip_callback, inst, TAG_NULL());
1667 PERROR("Failed to create SIP stack object\n");
1668 sip_exit_inst(interface);
1671 nua_set_params(inst->nua,
1672 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1673 NUTAG_APPL_METHOD("INVITE"),
1674 NUTAG_APPL_METHOD("ACK"),
1675 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1676 NUTAG_APPL_METHOD("CANCEL"),
1677 NUTAG_APPL_METHOD("OPTIONS"),
1678 NUTAG_APPL_METHOD("NOTIFY"),
1679 NUTAG_APPL_METHOD("INFO"),
1683 NUTAG_AUTOANSWER(0),
1686 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1691 void sip_exit_inst(struct interface *interface)
1693 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1698 su_root_destroy(inst->root);
1700 nua_destroy(inst->nua);
1702 FREE(inst, sizeof(*inst));
1703 interface->sip_inst = NULL;
1705 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1708 extern su_log_t su_log_default[];
1709 extern su_log_t nua_log[];
1710 //extern su_log_t soa_log[];
1716 /* init SOFIA lib */
1718 su_home_init(sip_home);
1720 if (options.deb & DEBUG_SIP) {
1721 su_log_set_level(su_log_default, 9);
1722 su_log_set_level(nua_log, 9);
1723 //su_log_set_level(soa_log, 9);
1726 for (i = 0; i < 256; i++)
1727 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);
1729 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1736 su_home_deinit(sip_home);
1739 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1742 void sip_handle(void)
1744 struct interface *interface = interface_first;
1745 struct sip_inst *inst;
1748 if (interface->sip_inst) {
1749 inst = (struct sip_inst *) interface->sip_inst;
1750 su_root_step(inst->root, 0);
1752 interface = interface->next;
1756 /* deletes when back in event loop */
1757 static int delete_event(struct lcr_work *work, void *instance, int index)
1759 class Psip *psip = (class Psip *)instance;