1 /*****************************************************************************\
3 ** Linux Call Router **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg **
10 \*****************************************************************************/
13 #include <sofia-sip/sip_status.h>
14 #include <sofia-sip/su_log.h>
15 #include <sofia-sip/sdp.h>
16 #include <sofia-sip/sip_header.h>
20 unsigned char flip[256];
22 //pthread_mutex_t mutex_msg;
23 su_home_t sip_home[1];
26 char interface_name[64];
33 static int delete_event(struct lcr_work *work, void *instance, int index);
38 Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings)
41 if (interface->rtp_bridge)
43 p_s_sip_inst = interface->sip_inst;
44 memset(&p_s_delete, 0, sizeof(p_s_delete));
45 add_work(&p_s_delete, delete_event, this, 0);
48 memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
49 memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
50 memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
51 memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
52 memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
53 memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
55 p_s_rtp_ip_remote = 0;
56 p_s_rtp_port_local = 0;
57 p_s_rtp_port_remote = 0;
62 p_s_rtp_tx_action = 0;
64 PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
66 FATAL("No SIP instance for interface\n");
75 PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
77 del_work(&p_s_delete);
82 const char *media_type2name(uint8_t media_type) {
90 case MEDIA_TYPE_GSM_HR:
92 case MEDIA_TYPE_GSM_EFR:
101 static void sip_trace_header(class Psip *sip, const char *message, int direction)
103 /* init trace with given values */
106 sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
107 sip?sip->p_dialinginfo.id:NULL,
118 /* according to RFC 3550 */
120 #if __BYTE_ORDER == __LITTLE_ENDIAN
121 uint8_t csrc_count:4,
125 uint8_t payload_type:7,
127 #elif __BYTE_ORDER == __BIG_ENDIAN
138 } __attribute__((packed));
143 } __attribute__((packed));
145 #define RTP_VERSION 2
147 #define PAYLOAD_TYPE_ULAW 0
148 #define PAYLOAD_TYPE_ALAW 8
149 #define PAYLOAD_TYPE_GSM 3
151 /* decode an rtp frame */
152 static int rtp_decode(class Psip *psip, unsigned char *data, int len)
154 struct rtp_hdr *rtph = (struct rtp_hdr *)data;
155 struct rtp_x_hdr *rtpxh;
159 unsigned char *from, *to;
163 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
166 if (rtph->version != RTP_VERSION) {
167 PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
170 payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
171 payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
172 if (payload_len < 0) {
173 PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
174 "csrc count = %d)\n", len, rtph->csrc_count);
177 if (rtph->extension) {
178 if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
179 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
180 "extension header\n");
183 rtpxh = (struct rtp_x_hdr *)payload;
184 x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
186 payload_len -= x_len;
187 if (payload_len < 0) {
188 PDEBUG(DEBUG_SIP, "received RTP frame too short, "
189 "extension header exceeds frame length\n");
194 if (payload_len < 0) {
195 PDEBUG(DEBUG_SIP, "received RTP frame too short for "
199 payload_len -= payload[payload_len - 1];
200 if (payload_len < 0) {
201 PDEBUG(DEBUG_SIP, "received RTP frame with padding "
202 "greater than payload\n");
207 switch (rtph->payload_type) {
209 we only support alaw and ulaw!
210 case RTP_PT_GSM_FULL:
211 if (payload_len != 33) {
212 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
213 "payload length != 33 (len = %d)\n",
219 if (payload_len != 31) {
220 PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
221 "payload length != 31 (len = %d)\n",
226 case RTP_PT_GSM_HALF:
227 if (payload_len != 14) {
228 PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
229 "payload length != 14 (len = %d)\n",
235 case PAYLOAD_TYPE_ALAW:
236 if (options.law != 'a') {
237 PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
241 case PAYLOAD_TYPE_ULAW:
242 if (options.law == 'a') {
243 PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
248 PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
249 "type %d\n", rtph->payload_type);
253 if (payload_len <= 0) {
254 PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
261 if (psip->p_echotest) {
262 /* echo rtp data we just received */
263 psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
267 *to++ = flip[*from++];
268 psip->bridge_tx(payload, payload_len);
273 static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
275 class Psip *psip = (class Psip *) instance;
277 unsigned char buffer[256];
280 if ((what & LCR_FD_READ)) {
281 len = read(fd->fd, &buffer, sizeof(buffer));
283 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
284 // psip->rtp_close();
285 // psip->rtp_shutdown();
288 if (psip->p_s_rtp_is_connected)
289 rc = rtp_decode(psip, buffer, len);
295 static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
297 // class Psip *psip = (class Psip *) instance;
299 unsigned char buffer[256];
301 if ((what & LCR_FD_READ)) {
302 len = read(fd->fd, &buffer, sizeof(buffer));
304 PDEBUG(DEBUG_SIP, "read result=%d\n", len);
305 // psip->rtp_close();
306 // psip->rtp_shutdown();
309 PDEBUG(DEBUG_SIP, "rtcp!\n");
315 #define RTP_PORT_BASE 30000
316 static unsigned int next_udp_port = RTP_PORT_BASE;
318 static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
321 socklen_t alen = sizeof(*sin_local);
323 sin_local->sin_family = AF_INET;
324 sin_local->sin_addr.s_addr = htonl(ip);
325 sin_local->sin_port = htons(port);
327 rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
331 /* retrieve the address we actually bound to, in case we
332 * passed INADDR_ANY as IP address */
333 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
336 static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
339 socklen_t alen = sizeof(*sin_local);
341 sin_remote->sin_family = AF_INET;
342 sin_remote->sin_addr.s_addr = htonl(ip);
343 sin_remote->sin_port = htons(port);
345 rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
347 PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
351 return getsockname(fd, (struct sockaddr *) sin_local, &alen);
354 int Psip::rtp_open(void)
361 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
367 register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
369 rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
375 register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
378 ip = htonl(INADDR_ANY);
380 for (next_udp_port = next_udp_port % 0xffff;
381 next_udp_port < 0xffff; next_udp_port += 2) {
382 rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
386 rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port+1);
391 PDEBUG(DEBUG_SIP, "failed to find port\n");
395 p_s_rtp_port_local = next_udp_port;
396 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
397 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
398 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
400 return p_s_rtp_port_local;
403 int Psip::rtp_connect(void)
408 ia.s_addr = htonl(p_s_rtp_ip_remote);
409 PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
411 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);
415 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);
419 p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
420 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
421 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
422 p_s_rtp_is_connected = 1;
426 void Psip::rtp_close(void)
428 if (p_s_rtp_fd.fd > 0) {
429 unregister_fd(&p_s_rtp_fd);
430 close(p_s_rtp_fd.fd);
433 if (p_s_rtcp_fd.fd > 0) {
434 unregister_fd(&p_s_rtcp_fd);
435 close(p_s_rtcp_fd.fd);
438 if (p_s_rtp_is_connected) {
439 PDEBUG(DEBUG_SIP, "rtp closed\n");
440 p_s_rtp_is_connected = 0;
445 void tv_difference(struct timeval *diff, const struct timeval *from,
446 const struct timeval *__to)
448 struct timeval _to = *__to, *to = &_to;
450 if (to->tv_usec < from->tv_usec) {
452 to->tv_usec += 1000000;
455 diff->tv_usec = to->tv_usec - from->tv_usec;
456 diff->tv_sec = to->tv_sec - from->tv_sec;
459 /* encode and send a rtp frame */
460 int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
462 struct rtp_hdr *rtph;
464 int duration; /* in samples */
465 unsigned char buffer[256];
467 if (!p_s_rtp_is_connected) {
472 if (!p_s_rtp_tx_action) {
473 /* initialize sequences */
474 p_s_rtp_tx_action = 1;
475 p_s_rtp_tx_ssrc = rand();
476 p_s_rtp_tx_sequence = random();
477 p_s_rtp_tx_timestamp = random();
478 memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
481 switch (payload_type) {
483 we only support alaw and ulaw!
484 case RTP_PT_GSM_FULL:
492 case RTP_PT_GSM_HALF:
497 case PAYLOAD_TYPE_ALAW:
498 case PAYLOAD_TYPE_ULAW:
503 PERROR("unsupported message type %d\n", payload_type);
509 struct timeval tv, tv_diff;
510 long int usec_diff, frame_diff;
512 gettimeofday(&tv, NULL);
513 tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
514 p_s_rtp_tx_last_tv = tv;
516 usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
517 frame_diff = (usec_diff / 20000);
519 if (abs(frame_diff) > 1) {
520 long int frame_diff_excess = frame_diff - 1;
522 PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
523 p_s_rtp_tx_sequence += frame_diff_excess;
524 p_s_rtp_tx_timestamp += frame_diff_excess * duration;
529 rtph = (struct rtp_hdr *) buffer;
530 rtph->version = RTP_VERSION;
533 rtph->csrc_count = 0;
535 rtph->payload_type = payload_type;
536 rtph->sequence = htons(p_s_rtp_tx_sequence++);
537 rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
538 p_s_rtp_tx_timestamp += duration;
539 rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
540 memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
542 if (p_s_rtp_fd.fd > 0) {
543 len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
544 if (len != sizeof(struct rtp_hdr) + payload_len) {
545 PDEBUG(DEBUG_SIP, "write result=%d\n", len);
555 /* receive from remote */
556 int Psip::bridge_rx(unsigned char *data, int len)
558 /* write to rx buffer */
560 p_s_rxdata[p_s_rxpos++] = flip[*data++];
561 if (p_s_rxpos == 160) {
564 /* transmit data via rtp */
565 rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
572 /* taken from freeswitch */
573 /* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
574 static int status2cause(int status)
578 return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
584 return 21; //SWITCH_CAUSE_CALL_REJECTED;
586 return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
589 return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
592 return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
594 return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
603 return 127; //SWITCH_CAUSE_INTERWORKING;
605 return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
610 return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
613 return 17; //SWITCH_CAUSE_USER_BUSY;
615 return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
618 return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
620 return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
622 return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
626 return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
629 return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
631 return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
633 return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
637 static int cause2status(int cause, int location, const char **st)
643 s = 404; *st = sip_404_Not_found;
646 s = 404; *st = sip_404_Not_found;
649 s = 404; *st = sip_404_Not_found;
652 s = 486; *st = sip_486_Busy_here;
655 s = 408; *st = sip_408_Request_timeout;
658 s = 480; *st = sip_480_Temporarily_unavailable;
661 s = 480; *st = sip_480_Temporarily_unavailable;
664 if (location == LOCATION_USER) {
665 s = 603; *st = sip_603_Decline;
667 s = 403; *st = sip_403_Forbidden;
671 //s = 301; *st = sip_301_Moved_permanently;
672 s = 410; *st = sip_410_Gone;
675 s = 410; *st = sip_410_Gone;
678 s = 502; *st = sip_502_Bad_gateway;
681 s = 484; *st = sip_484_Address_incomplete;
684 s = 501; *st = sip_501_Not_implemented;
687 s = 480; *st = sip_480_Temporarily_unavailable;
690 s = 503; *st = sip_503_Service_unavailable;
693 s = 503; *st = sip_503_Service_unavailable;
696 s = 503; *st = sip_503_Service_unavailable;
699 s = 503; *st = sip_503_Service_unavailable;
702 s = 503; *st = sip_503_Service_unavailable;
705 s = 403; *st = sip_403_Forbidden;
708 s = 403; *st = sip_403_Forbidden;
711 s = 503; *st = sip_503_Service_unavailable;
714 s = 488; *st = sip_488_Not_acceptable;
717 s = 501; *st = sip_501_Not_implemented;
720 s = 488; *st = sip_488_Not_acceptable;
723 s = 501; *st = sip_501_Not_implemented;
726 s = 403; *st = sip_403_Forbidden;
729 s = 503; *st = sip_503_Service_unavailable;
732 s = 504; *st = sip_504_Gateway_time_out;
735 s = 468; *st = sip_486_Busy_here;
742 * endpoint sends messages to the SIP port
745 int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
749 struct lcr_msg *message;
751 unsigned char payload_type;
753 if (param->connectinfo.rtpinfo.port) {
754 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
756 media_type = param->connectinfo.rtpinfo.media_types[0];
757 payload_type = param->connectinfo.rtpinfo.payload_types[0];
758 p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
759 p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
760 PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
761 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
762 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
764 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
765 media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
766 payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
767 /* open local RTP peer (if not bridging) */
768 if (!p_s_rtp_is_connected && rtp_connect() < 0) {
769 nua_cancel(p_s_handle, TAG_END());
770 nua_handle_destroy(p_s_handle);
772 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
773 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
775 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
776 message->param.disconnectinfo.cause = 41;
777 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
778 message_put(message);
779 new_state(PORT_STATE_RELEASE);
780 trigger_work(&p_s_delete);
785 ia.s_addr = htonl(p_s_rtp_ip_local);
789 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
793 "m=audio %d RTP/AVP %d\n"
794 "a=rtpmap:%d %s/8000\n"
795 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
796 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
798 nua_respond(p_s_handle, SIP_200_OK,
799 NUTAG_MEDIA_ENABLE(0),
800 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
801 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
802 new_state(PORT_STATE_CONNECT);
803 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
804 add_trace("respond", "value", "200 OK");
805 add_trace("reason", NULL, "call connected");
806 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
807 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
808 add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
814 int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
816 struct lcr_msg *message;
817 char cause_str[128] = "";
818 int cause = param->disconnectinfo.cause;
819 int location = param->disconnectinfo.cause;
821 const char *status_text;
823 if (cause > 0 && cause <= 127) {
824 SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
828 case PORT_STATE_OUT_SETUP:
829 case PORT_STATE_OUT_PROCEEDING:
830 case PORT_STATE_OUT_ALERTING:
831 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
832 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
834 add_trace("cause", "value", "%d", cause);
836 nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
838 case PORT_STATE_IN_SETUP:
839 case PORT_STATE_IN_PROCEEDING:
840 case PORT_STATE_IN_ALERTING:
841 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
842 status = cause2status(cause, location, &status_text);
843 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
845 add_trace("cause", "value", "%d", cause);
846 add_trace("respond", "value", "%d %s", status, status_text);
848 nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
849 nua_handle_destroy(p_s_handle);
851 trigger_work(&p_s_delete);
854 PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
855 sip_trace_header(this, "BYE", DIRECTION_OUT);
857 add_trace("cause", "value", "%d", cause);
859 nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
862 if (message_id == MESSAGE_DISCONNECT) {
863 while(p_epointlist) {
864 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
865 message->param.disconnectinfo.cause = CAUSE_NORMAL;
866 message->param.disconnectinfo.location = LOCATION_BEYOND;
867 message_put(message);
869 free_epointlist(p_epointlist);
873 new_state(PORT_STATE_RELEASE);
878 int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
880 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
883 const char *local = inst->local_peer;
885 const char *remote = inst->remote_peer;
886 char sdp_str[512], pt_str[32];
888 struct epoint_list *epointlist;
889 sip_cseq_t *cseq = NULL;
890 struct lcr_msg *message;
891 int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
892 unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
894 unsigned char *payload_types;
898 PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
900 memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
901 memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
902 memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
904 if (param->setup.rtpinfo.port) {
905 PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
907 media_types = param->setup.rtpinfo.media_types;
908 payload_types = param->setup.rtpinfo.payload_types;
909 payloads = param->setup.rtpinfo.payloads;
910 p_s_rtp_ip_local = param->setup.rtpinfo.ip;
911 p_s_rtp_port_local = param->setup.rtpinfo.port;
912 PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
913 PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
915 PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
917 media_types = &lcr_media;
918 payload_types = &lcr_payload;
921 /* open local RTP peer (if not bridging) */
922 if (rtp_open() < 0) {
923 PERROR("Failed to open RTP sockets\n");
924 /* send release message to endpoit */
925 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
926 message->param.disconnectinfo.cause = 41;
927 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
928 message_put(message);
929 new_state(PORT_STATE_RELEASE);
930 trigger_work(&p_s_delete);
935 p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
937 PERROR("Failed to create handle\n");
938 /* send release message to endpoit */
939 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
940 message->param.disconnectinfo.cause = 41;
941 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
942 message_put(message);
943 new_state(PORT_STATE_RELEASE);
944 trigger_work(&p_s_delete);
948 sip_trace_header(this, "NEW handle", DIRECTION_IN);
949 add_trace("handle", "new", "0x%x", p_s_handle);
952 if (!p_s_rtp_ip_local) {
955 /* extract IP from local peer */
956 SCPY(local_ip, local);
957 p = strchr(local_ip, ':');
960 PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
961 inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
962 p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
964 ia.s_addr = htonl(p_s_rtp_ip_local);
967 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
972 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
973 for (i = 0; i < payloads; i++) {
974 SPRINT(pt_str, " %d", payload_types[i]);
975 SCAT(sdp_str, pt_str);
978 for (i = 0; i < payloads; i++) {
979 SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
980 SCAT(sdp_str, pt_str);
982 PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
984 SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
985 SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
987 sip_trace_header(this, "INVITE", DIRECTION_OUT);
988 add_trace("from", "uri", "%s", from);
989 add_trace("to", "uri", "%s", to);
990 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
991 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
992 for (i = 0; i < payloads; i++)
993 add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
996 // cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
998 nua_invite(p_s_handle,
999 TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1000 TAG_IF(to[0], SIPTAG_TO_STR(to)),
1001 TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1002 NUTAG_MEDIA_ENABLE(0),
1003 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1004 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1005 new_state(PORT_STATE_OUT_SETUP);
1008 PDEBUG(DEBUG_SIP, "do overlap\n");
1009 new_state(PORT_STATE_OUT_OVERLAP);
1010 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_OVERLAP);
1011 message_put(message);
1013 PDEBUG(DEBUG_SIP, "do proceeding\n");
1014 new_state(PORT_STATE_OUT_PROCEEDING);
1015 message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1016 message_put(message);
1019 /* attach only if not already */
1020 epointlist = p_epointlist;
1022 if (epointlist->epoint_id == epoint_id)
1024 epointlist = epointlist->next;
1027 epointlist_new(epoint_id);
1032 int Psip::message_notify(unsigned int epoint_id, int message_id, union parameter *param)
1034 // char sdp_str[256];
1035 // struct in_addr ia;
1037 switch (param->notifyinfo.notify) {
1038 case INFO_NOTIFY_REMOTE_HOLD:
1042 "o=LCR-Sofia-SIP 0 0 IN IP4 0.0.0.0\n"
1044 "c=IN IP4 0.0.0.0\n"
1047 PDEBUG(DEBUG_SIP, "Using SDP for hold: %s\n", sdp_str);
1048 nua_info(p_s_handle,
1049 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1050 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1051 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1052 NUTAG_MEDIA_ENABLE(0),
1053 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1054 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1057 case INFO_NOTIFY_REMOTE_RETRIEVAL:
1059 ia.s_addr = htonl(p_s_rtp_ip_local);
1062 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1066 "m=audio %d RTP/AVP %d\n"
1067 "a=rtpmap:%d %s/8000\n"
1068 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, p_s_rtp_payload_type, p_s_rtp_payload_type, media_type2name(p_s_rtp_media_type));
1069 PDEBUG(DEBUG_SIP, "Using SDP for rertieve: %s\n", sdp_str);
1070 nua_info(p_s_handle,
1071 // TAG_IF(from[0], SIPTAG_FROM_STR(from)),
1072 // TAG_IF(to[0], SIPTAG_TO_STR(to)),
1073 // TAG_IF(cseq, SIPTAG_CSEQ(cseq)),
1074 NUTAG_MEDIA_ENABLE(0),
1075 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1076 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1084 int Psip::message_dtmf(unsigned int epoint_id, int message_id, union parameter *param)
1088 /* prepare DTMF info payload */
1094 /* start invite to handle DTMF */
1095 nua_info(p_s_handle,
1096 NUTAG_MEDIA_ENABLE(0),
1097 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1098 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1103 /* NOTE: incomplete and not working */
1104 int Psip::message_information(unsigned int epoint_id, int message_id, union parameter *param)
1108 /* prepare DTMF info payload */
1112 , param->information.id);
1114 /* start invite to handle DTMF */
1115 nua_info(p_s_handle,
1116 NUTAG_MEDIA_ENABLE(0),
1117 SIPTAG_CONTENT_TYPE_STR("application/dtmf-relay"),
1118 SIPTAG_PAYLOAD_STR(dtmf_str), TAG_END());
1123 int Psip::message_epoint(unsigned int epoint_id, int message_id, union parameter *param)
1125 if (Port::message_epoint(epoint_id, message_id, param))
1128 switch(message_id) {
1129 case MESSAGE_ALERTING: /* call is ringing on LCR side */
1130 if (p_state != PORT_STATE_IN_SETUP
1131 && p_state != PORT_STATE_IN_PROCEEDING)
1133 nua_respond(p_s_handle, SIP_180_RINGING, TAG_END());
1134 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1135 add_trace("respond", "value", "180 Ringing");
1137 new_state(PORT_STATE_IN_ALERTING);
1140 case MESSAGE_CONNECT: /* call is connected on LCR side */
1141 if (p_state != PORT_STATE_IN_SETUP
1142 && p_state != PORT_STATE_IN_PROCEEDING
1143 && p_state != PORT_STATE_IN_ALERTING)
1145 message_connect(epoint_id, message_id, param);
1148 case MESSAGE_DISCONNECT: /* call has been disconnected */
1149 case MESSAGE_RELEASE: /* call has been released */
1150 message_release(epoint_id, message_id, param);
1153 case MESSAGE_SETUP: /* dial-out command received from epoint */
1154 message_setup(epoint_id, message_id, param);
1157 case MESSAGE_INFORMATION: /* overlap dialing */
1158 if (p_state != PORT_STATE_OUT_OVERLAP)
1160 message_information(epoint_id, message_id, param);
1163 case MESSAGE_DTMF: /* DTMF info to be transmitted via INFO transaction */
1164 if (p_state == PORT_STATE_CONNECT)
1165 message_dtmf(epoint_id, message_id, param);
1166 case MESSAGE_NOTIFY: /* notification about remote hold/retrieve */
1167 if (p_state == PORT_STATE_CONNECT)
1168 message_notify(epoint_id, message_id, param);
1172 PDEBUG(DEBUG_SIP, "PORT(%s) SP port with (caller id %s) received an unsupported message: %d\n", p_name, p_callerinfo.id, message_id);
1178 int Psip::parse_sdp(sip_t const *sip, unsigned int *ip, unsigned short *port, uint8_t *payload_types, int *media_types, int *payloads, int max_payloads)
1182 if (!sip->sip_payload) {
1183 PDEBUG(DEBUG_SIP, "no payload given\n");
1187 sdp_parser_t *parser;
1190 sdp_attribute_t *attr;
1192 sdp_connection_t *conn;
1194 PDEBUG(DEBUG_SIP, "payload given: %s\n", sip->sip_payload->pl_data);
1196 parser = sdp_parse(NULL, sip->sip_payload->pl_data, (int) strlen(sip->sip_payload->pl_data), 0);
1200 if (!(sdp = sdp_session(parser))) {
1201 sdp_parser_free(parser);
1204 for (m = sdp->sdp_media; m; m = m->m_next) {
1205 if (m->m_proto != sdp_proto_rtp)
1207 if (m->m_type != sdp_media_audio)
1209 PDEBUG(DEBUG_SIP, "RTP port:'%u'\n", m->m_port);
1211 for (attr = m->m_attributes; attr; attr = attr->a_next) {
1212 PDEBUG(DEBUG_SIP, "ATTR: name:'%s' value='%s'\n", attr->a_name, attr->a_value);
1214 if (m->m_connections) {
1215 conn = m->m_connections;
1216 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", conn->c_address);
1217 inet_pton(AF_INET, conn->c_address, ip);
1218 *ip = ntohl(p_s_rtp_ip_remote);
1220 char *p = sip->sip_payload->pl_data;
1223 PDEBUG(DEBUG_SIP, "sofia cannot find connection tag, so we try ourself\n");
1224 p = strstr(p, "c=IN IP4 ");
1226 PDEBUG(DEBUG_SIP, "missing c-tag with internet address\n");
1227 sdp_parser_free(parser);
1231 if ((p = strchr(addr, '\n'))) *p = '\0';
1232 if ((p = strchr(addr, '\r'))) *p = '\0';
1233 PDEBUG(DEBUG_SIP, "CONN: address:'%s'\n", addr);
1234 inet_pton(AF_INET, addr, ip);
1235 *ip = ntohl(p_s_rtp_ip_remote);
1237 for (map = m->m_rtpmaps; map; map = map->rm_next) {
1240 PDEBUG(DEBUG_SIP, "RTPMAP: coding:'%s' rate='%d' pt='%d'\n", map->rm_encoding, map->rm_rate, map->rm_pt);
1241 /* append to payload list, if there is space */
1242 add_trace("rtp", "payload", "%s:%d", map->rm_encoding, map->rm_pt);
1243 if (map->rm_pt == PAYLOAD_TYPE_ALAW)
1244 media_type = MEDIA_TYPE_ALAW;
1245 else if (map->rm_pt == PAYLOAD_TYPE_ULAW)
1246 media_type = MEDIA_TYPE_ULAW;
1247 else if (map->rm_pt == PAYLOAD_TYPE_GSM)
1248 media_type = MEDIA_TYPE_GSM;
1249 else if (!strcmp(map->rm_encoding, "GSM-EFR"))
1250 media_type = MEDIA_TYPE_GSM_EFR;
1251 else if (!strcmp(map->rm_encoding, "AMR"))
1252 media_type = MEDIA_TYPE_AMR;
1253 else if (!strcmp(map->rm_encoding, "GSM-HR"))
1254 media_type = MEDIA_TYPE_GSM_HR;
1255 if (media_type && *payloads <= max_payloads) {
1256 *payload_types++ = map->rm_pt;
1257 *media_types++ = media_type;
1263 sdp_parser_free(parser);
1268 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[])
1270 struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
1271 const char *from = "", *to = "";
1273 class Endpoint *epoint;
1274 struct lcr_msg *message;
1275 struct interface *interface;
1276 int media_types[32];
1277 uint8_t payload_types[32];
1280 interface = getinterfacebyname(inst->interface_name);
1282 PERROR("Cannot find interface %s.\n", inst->interface_name);
1286 if (sip->sip_from && sip->sip_from->a_url)
1287 from = sip->sip_from->a_url->url_user;
1288 if (sip->sip_to && sip->sip_to->a_url)
1289 to = sip->sip_to->a_url->url_user;
1290 PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
1292 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1293 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1295 /* if no RTP bridge, we must support LAW codec, otherwise we forward what we have */
1296 if (!p_s_rtp_bridge) {
1299 /* check if supported payload type exists */
1300 for (i = 0; i < payloads; i++) {
1301 if (media_types[i] == ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW))
1304 if (i == payloads) {
1305 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1313 nua_respond(nh, SIP_400_BAD_REQUEST, TAG_END());
1315 nua_respond(nh, SIP_415_UNSUPPORTED_MEDIA, TAG_END());
1316 nua_handle_destroy(nh);
1318 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1320 add_trace("respond", "value", "415 Unsupported Media");
1322 add_trace("respond", "value", "400 Bad Request");
1323 add_trace("reason", NULL, "offered codec does not match");
1325 new_state(PORT_STATE_RELEASE);
1326 trigger_work(&p_s_delete);
1330 /* open local RTP peer (if not bridging) */
1331 if (!p_s_rtp_bridge && rtp_open() < 0) {
1332 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1333 nua_handle_destroy(nh);
1335 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1336 add_trace("respond", "value", "500 Internal Server Error");
1337 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1339 new_state(PORT_STATE_RELEASE);
1340 trigger_work(&p_s_delete);
1345 sip_trace_header(this, "NEW handle", DIRECTION_IN);
1346 add_trace("handle", "new", "0x%x", nh);
1350 sip_trace_header(this, "INVITE", DIRECTION_IN);
1351 add_trace("rtp", "port", "%d", p_s_rtp_port_remote);
1352 /* caller information */
1354 p_callerinfo.present = INFO_PRESENT_NOTAVAIL;
1355 p_callerinfo.ntype = INFO_NTYPE_NOTPRESENT;
1356 add_trace("calling", "present", "unavailable");
1358 p_callerinfo.present = INFO_PRESENT_ALLOWED;
1359 add_trace("calling", "present", "allowed");
1360 p_callerinfo.screen = INFO_SCREEN_NETWORK;
1361 p_callerinfo.ntype = INFO_NTYPE_UNKNOWN;
1362 SCPY(p_callerinfo.id, from);
1363 add_trace("calling", "number", "%s", from);
1365 SCPY(p_callerinfo.interface, inst->interface_name);
1366 /* dialing information */
1368 p_dialinginfo.ntype = INFO_NTYPE_UNKNOWN;
1369 SCAT(p_dialinginfo.id, to);
1370 add_trace("dialing", "number", "%s", to);
1373 /* bearer capability */
1374 p_capainfo.bearer_capa = INFO_BC_SPEECH;
1375 p_capainfo.bearer_info1 = (options.law=='a')?3:2;
1376 p_capainfo.bearer_mode = INFO_BMODE_CIRCUIT;
1377 add_trace("bearer", "capa", "speech");
1378 add_trace("bearer", "mode", "circuit");
1379 /* if packet mode works some day, see dss1.cpp for conditions */
1380 p_capainfo.source_mode = B_MODE_TRANSPARENT;
1384 /* create endpoint */
1386 FATAL("Incoming call but already got an endpoint.\n");
1387 if (!(epoint = new Endpoint(p_serial, 0)))
1388 FATAL("No memory for Endpoint instance\n");
1389 epoint->ep_app = new_endpointapp(epoint, 0, interface->app); //incoming
1390 epointlist_new(epoint->ep_serial);
1392 #ifdef NUTAG_AUTO100
1393 /* send trying (proceeding) */
1394 nua_respond(nh, SIP_100_TRYING, TAG_END());
1395 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1396 add_trace("respond", "value", "100 Trying");
1400 new_state(PORT_STATE_IN_PROCEEDING);
1402 /* send setup message to endpoit */
1403 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_SETUP);
1404 message->param.setup.port_type = p_type;
1405 // message->param.setup.dtmf = 0;
1406 memcpy(&message->param.setup.dialinginfo, &p_dialinginfo, sizeof(struct dialing_info));
1407 memcpy(&message->param.setup.callerinfo, &p_callerinfo, sizeof(struct caller_info));
1408 memcpy(&message->param.setup.capainfo, &p_capainfo, sizeof(struct capa_info));
1409 // SCPY((char *)message->param.setup.useruser.data, useruser.info);
1410 // message->param.setup.useruser.len = strlen(mncc->useruser.info);
1411 // message->param.setup.useruser.protocol = mncc->useruser.proto;
1412 if (p_s_rtp_bridge) {
1415 PDEBUG(DEBUG_SIP, "sending setup with RTP info\n");
1416 message->param.setup.rtpinfo.ip = p_s_rtp_ip_remote;
1417 message->param.setup.rtpinfo.port = p_s_rtp_port_remote;
1418 /* add codecs to setup message */
1419 for (i = 0; i < payloads; i++) {
1420 message->param.setup.rtpinfo.media_types[i] = media_types[i];
1421 message->param.setup.rtpinfo.payload_types[i] = payload_types[i];
1422 if (i == sizeof(message->param.setup.rtpinfo.payload_types))
1425 message->param.setup.rtpinfo.payloads = i;
1427 message_put(message);
1431 - send tones that are clocked with timer, unless data is received from bridge
1432 /* send progress, if tones are available and if we don't bridge */
1433 if (!p_s_rtp_bridge && interface->is_tones == IS_YES) {
1436 unsigned char payload_type;
1438 PDEBUG(DEBUG_SIP, "Connecting audio, since we have tones available\n");
1439 payload_type = (options.law=='a') ? RTP_PT_ALAW : RTP_PT_ULAW;
1440 /* open local RTP peer (if not bridging) */
1441 if (rtp_connect() < 0) {
1442 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1443 nua_handle_destroy(nh);
1445 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1446 add_trace("respond", "value", "500 Internal Server Error");
1447 add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
1449 new_state(PORT_STATE_RELEASE);
1450 trigger_work(&p_s_delete);
1451 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_RELEASE);
1452 message->param.disconnectinfo.cause = 41;
1453 message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
1454 message_put(message);
1455 new_state(PORT_STATE_RELEASE);
1456 trigger_work(&p_s_delete);
1460 ia.s_addr = htonl(p_s_rtp_ip_local);
1464 "o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
1468 "m=audio %d RTP/AVP %d\n"
1469 "a=rtpmap:%d %s/8000\n"
1470 , inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, payload_type2name(payload_type));
1471 PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
1473 nua_respond(p_s_handle, SIP_183_SESSION_PROGRESS,
1474 NUTAG_MEDIA_ENABLE(0),
1475 SIPTAG_CONTENT_TYPE_STR("application/sdp"),
1476 SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
1477 new_state(PORT_STATE_CONNECT);
1478 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1479 add_trace("respond", "value", "183 SESSION PROGRESS");
1480 add_trace("reason", NULL, "audio available");
1481 add_trace("rtp", "ip", "%s", inet_ntoa(ia));
1482 add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
1483 add_trace("rtp", "payload", "%d", payload_type);
1489 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[])
1491 struct lcr_msg *message;
1494 PDEBUG(DEBUG_SIP, "bye received\n");
1496 sip_trace_header(this, "BYE", DIRECTION_IN);
1497 if (sip->sip_reason && sip->sip_reason->re_protocol && !strcasecmp(sip->sip_reason->re_protocol, "Q.850") && sip->sip_reason->re_cause) {
1498 cause = atoi(sip->sip_reason->re_cause);
1499 add_trace("cause", "value", "%d", cause);
1503 // let stack do bye automaticall, since it will not accept our response for some reason
1504 // nua_respond(nh, SIP_200_OK, TAG_END());
1505 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1506 add_trace("respond", "value", "200 OK");
1508 // nua_handle_destroy(nh);
1513 while(p_epointlist) {
1514 /* send setup message to endpoit */
1515 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1516 message->param.disconnectinfo.cause = cause ? : 16;
1517 message->param.disconnectinfo.location = LOCATION_BEYOND;
1518 message_put(message);
1520 free_epointlist(p_epointlist);
1522 new_state(PORT_STATE_RELEASE);
1523 trigger_work(&p_s_delete);
1526 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[])
1528 struct lcr_msg *message;
1530 PDEBUG(DEBUG_SIP, "cancel received\n");
1532 sip_trace_header(this, "CANCEL", DIRECTION_IN);
1535 nua_handle_destroy(nh);
1540 while(p_epointlist) {
1541 /* send setup message to endpoit */
1542 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1543 message->param.disconnectinfo.cause = 16;
1544 message->param.disconnectinfo.location = LOCATION_BEYOND;
1545 message_put(message);
1547 free_epointlist(p_epointlist);
1549 new_state(PORT_STATE_RELEASE);
1550 trigger_work(&p_s_delete);
1553 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[])
1555 PDEBUG(DEBUG_SIP, "bye response received\n");
1557 nua_handle_destroy(nh);
1562 trigger_work(&p_s_delete);
1565 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[])
1567 PDEBUG(DEBUG_SIP, "cancel response received\n");
1569 nua_handle_destroy(nh);
1574 trigger_work(&p_s_delete);
1577 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[])
1579 struct lcr_msg *message;
1580 int cause = 0, location = 0;
1581 int media_types[32];
1582 uint8_t payload_types[32];
1585 PDEBUG(DEBUG_SIP, "response to invite received (status = %d)\n", status);
1587 sip_trace_header(this, "RESPOND", DIRECTION_OUT);
1588 add_trace("respond", "value", "%d", status);
1592 if (status == 183 || (status >= 200 && status <= 299)) {
1595 sip_trace_header(this, "Payload received", DIRECTION_NONE);
1596 ret = parse_sdp(sip, &p_s_rtp_ip_remote, &p_s_rtp_port_remote, payload_types, media_types, &payloads, sizeof(payload_types));
1600 else if (!p_s_rtp_bridge) {
1601 if (media_types[0] != ((options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW)) {
1602 add_trace("error", NULL, "Expected LAW payload type (not bridged)");
1609 nua_cancel(nh, TAG_END());
1610 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1611 add_trace("reason", NULL, "accepted codec does not match");
1614 location = LOCATION_PRIVATE_LOCAL;
1615 goto release_with_cause;
1618 /* connect to remote RTP (if not bridging) */
1619 if (!p_s_rtp_bridge && rtp_connect() < 0) {
1620 nua_cancel(nh, TAG_END());
1621 sip_trace_header(this, "CANCEL", DIRECTION_OUT);
1622 add_trace("reason", NULL, "failed to open RTP/RTCP sockts");
1625 location = LOCATION_PRIVATE_LOCAL;
1626 goto release_with_cause;
1634 PDEBUG(DEBUG_SIP, "do proceeding\n");
1635 new_state(PORT_STATE_OUT_PROCEEDING);
1636 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROCEEDING);
1637 message_put(message);
1641 PDEBUG(DEBUG_SIP, "do alerting\n");
1642 new_state(PORT_STATE_OUT_ALERTING);
1643 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_ALERTING);
1644 message_put(message);
1647 PDEBUG(DEBUG_SIP, "do progress\n");
1648 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_PROGRESS);
1649 message->param.progressinfo.progress = 8;
1650 message->param.progressinfo.location = 10;
1651 if (p_s_rtp_bridge) {
1652 message->param.progressinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1653 message->param.progressinfo.rtpinfo.port = p_s_rtp_port_remote;
1654 message->param.progressinfo.rtpinfo.media_types[0] = media_types[0];
1655 message->param.progressinfo.rtpinfo.payload_types[0] = payload_types[0];
1656 message->param.progressinfo.rtpinfo.payloads = 1;
1658 message_put(message);
1661 if (status < 100 || status > 199)
1663 PDEBUG(DEBUG_SIP, "skipping 1xx message\n");
1669 if (status >= 200 && status <= 299) {
1670 PDEBUG(DEBUG_SIP, "do connect\n");
1671 nua_ack(nh, TAG_END());
1672 new_state(PORT_STATE_CONNECT);
1673 message = message_create(p_serial, ACTIVE_EPOINT(p_epointlist), PORT_TO_EPOINT, MESSAGE_CONNECT);
1674 if (p_s_rtp_bridge) {
1675 message->param.connectinfo.rtpinfo.ip = p_s_rtp_ip_remote;
1676 message->param.connectinfo.rtpinfo.port = p_s_rtp_port_remote;
1677 message->param.connectinfo.rtpinfo.media_types[0] = media_types[0];
1678 message->param.connectinfo.rtpinfo.payload_types[0] = payload_types[0];
1679 message->param.connectinfo.rtpinfo.payloads = 1;
1681 message_put(message);
1684 cause = status2cause(status);
1685 location = LOCATION_BEYOND;
1688 PDEBUG(DEBUG_SIP, "do release (cause %d)\n", cause);
1690 while(p_epointlist) {
1691 /* send setup message to endpoit */
1692 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1693 message->param.disconnectinfo.cause = cause;
1694 message->param.disconnectinfo.location = location;
1695 message_put(message);
1697 free_epointlist(p_epointlist);
1700 new_state(PORT_STATE_RELEASE);
1704 trigger_work(&p_s_delete);
1707 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[])
1709 struct sip_inst *inst = (struct sip_inst *) magic;
1711 class Psip *psip = NULL;
1713 PDEBUG(DEBUG_SIP, "Event %d from stack received (handle=%p)\n", event, nh);
1717 /* create or find port instance */
1718 if (event == nua_i_invite)
1721 struct interface *interface = interface_first;
1723 /* create call instance */
1724 SPRINT(name, "%s-%d-in", inst->interface_name, 0);
1726 if (!strcmp(interface->name, inst->interface_name))
1728 interface = interface->next;
1731 PERROR("Cannot find interface %s.\n", inst->interface_name);
1734 if (!(psip = new Psip(PORT_TYPE_SIP_IN, name, NULL, interface)))
1735 FATAL("Cannot create Port instance.\n");
1739 if ((port->p_type & PORT_CLASS_mISDN_MASK) == PORT_CLASS_SIP) {
1740 psip = (class Psip *)port;
1741 if (psip->p_s_handle == nh) {
1749 PERROR("no SIP Port found for handel %p\n", nh);
1750 nua_respond(nh, SIP_500_INTERNAL_SERVER_ERROR, TAG_END());
1751 nua_handle_destroy(nh);
1756 case nua_r_set_params:
1757 PDEBUG(DEBUG_SIP, "setparam response\n");
1760 PDEBUG(DEBUG_SIP, "error received\n");
1763 PDEBUG(DEBUG_SIP, "state change received\n");
1765 case nua_i_register:
1766 PDEBUG(DEBUG_SIP, "register received\n");
1769 psip->i_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1772 PDEBUG(DEBUG_SIP, "ack received\n");
1775 PDEBUG(DEBUG_SIP, "active received\n");
1778 psip->i_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1781 psip->i_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1784 psip->r_bye(status, phrase, nua, magic, nh, hmagic, sip, tags);
1787 psip->r_cancel(status, phrase, nua, magic, nh, hmagic, sip, tags);
1790 psip->r_invite(status, phrase, nua, magic, nh, hmagic, sip, tags);
1792 case nua_i_terminated:
1793 PDEBUG(DEBUG_SIP, "terminated received\n");
1796 PDEBUG(DEBUG_SIP, "Event %d not handled\n", event);
1800 /* received shutdown due to termination of RTP */
1801 void Psip::rtp_shutdown(void)
1803 struct lcr_msg *message;
1805 PDEBUG(DEBUG_SIP, "RTP stream terminated\n");
1807 sip_trace_header(this, "RTP terminated", DIRECTION_IN);
1810 nua_handle_destroy(p_s_handle);
1813 while(p_epointlist) {
1814 /* send setup message to endpoit */
1815 message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
1816 message->param.disconnectinfo.cause = 16;
1817 message->param.disconnectinfo.location = LOCATION_BEYOND;
1818 message_put(message);
1820 free_epointlist(p_epointlist);
1822 new_state(PORT_STATE_RELEASE);
1823 trigger_work(&p_s_delete);
1826 int sip_init_inst(struct interface *interface)
1828 struct sip_inst *inst = (struct sip_inst *) MALLOC(sizeof(*inst));
1831 interface->sip_inst = inst;
1832 SCPY(inst->interface_name, interface->name);
1833 SCPY(inst->local_peer, interface->sip_local_peer);
1834 SCPY(inst->remote_peer, interface->sip_remote_peer);
1836 /* init root object */
1837 inst->root = su_root_create(inst);
1839 PERROR("Failed to create SIP root\n");
1840 sip_exit_inst(interface);
1844 SPRINT(local, "sip:%s",inst->local_peer);
1845 if (!strchr(inst->local_peer, ':'))
1846 SCAT(local, ":5060");
1847 inst->nua = nua_create(inst->root, sip_callback, inst, NUTAG_URL(local), TAG_END());
1849 PERROR("Failed to create SIP stack object\n");
1850 sip_exit_inst(interface);
1853 nua_set_params(inst->nua,
1854 SIPTAG_ALLOW_STR("INVITE,ACK,BYE,CANCEL,OPTIONS,NOTIFY,INFO"),
1855 NUTAG_APPL_METHOD("INVITE"),
1856 NUTAG_APPL_METHOD("ACK"),
1857 // NUTAG_APPL_METHOD("BYE"), /* we must reply to BYE */
1858 NUTAG_APPL_METHOD("CANCEL"),
1859 NUTAG_APPL_METHOD("OPTIONS"),
1860 NUTAG_APPL_METHOD("NOTIFY"),
1861 NUTAG_APPL_METHOD("INFO"),
1863 #ifdef NUTAG_AUTO100
1867 NUTAG_AUTOANSWER(0),
1870 PDEBUG(DEBUG_SIP, "SIP interface created (inst=%p)\n", inst);
1875 void sip_exit_inst(struct interface *interface)
1877 struct sip_inst *inst = (struct sip_inst *) interface->sip_inst;
1882 su_root_destroy(inst->root);
1884 nua_destroy(inst->nua);
1886 FREE(inst, sizeof(*inst));
1887 interface->sip_inst = NULL;
1889 PDEBUG(DEBUG_SIP, "SIP interface removed\n");
1892 extern su_log_t su_log_default[];
1893 extern su_log_t nua_log[];
1894 //extern su_log_t soa_log[];
1900 /* init SOFIA lib */
1902 su_home_init(sip_home);
1904 if (options.deb & DEBUG_SIP) {
1905 su_log_set_level(su_log_default, 9);
1906 su_log_set_level(nua_log, 9);
1907 //su_log_set_level(soa_log, 9);
1910 for (i = 0; i < 256; i++)
1911 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);
1913 PDEBUG(DEBUG_SIP, "SIP globals initialized\n");
1920 su_home_deinit(sip_home);
1923 PDEBUG(DEBUG_SIP, "SIP globals de-initialized\n");
1926 void sip_handle(void)
1928 struct interface *interface = interface_first;
1929 struct sip_inst *inst;
1932 if (interface->sip_inst) {
1933 inst = (struct sip_inst *) interface->sip_inst;
1934 su_root_step(inst->root, 0);
1936 interface = interface->next;
1940 /* deletes when back in event loop */
1941 static int delete_event(struct lcr_work *work, void *instance, int index)
1943 class Psip *psip = (class Psip *)instance;