-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsip.cpp
1949 lines (1730 loc) · 53.6 KB
/
sip.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*****************************************************************************\
** **
** Linux Call Router **
** **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg **
** **
** SIP port **
** **
\*****************************************************************************/
#include "main.h"
#include <sofia-sip/sip_status.h>
#include <sofia-sip/su_log.h>
#include <sofia-sip/sdp.h>
#include <sofia-sip/sip_header.h>
#undef NUTAG_AUTO100
unsigned char flip[256];
//pthread_mutex_t mutex_msg;
su_home_t sip_home[1];
struct sip_inst {
char interface_name[64];
char local_peer[32];
char remote_peer[32];
su_root_t *root;
nua_t *nua;
};
static int delete_event(struct lcr_work *work, void *instance, int index);
/*
* initialize SIP port
*/
Psip::Psip(int type, char *portname, struct port_settings *settings, struct interface *interface) : Port(type, portname, settings)
{
p_s_rtp_bridge = 0;
if (interface->rtp_bridge)
p_s_rtp_bridge = 1;
p_s_sip_inst = interface->sip_inst;
memset(&p_s_delete, 0, sizeof(p_s_delete));
add_work(&p_s_delete, delete_event, this, 0);
p_s_handle = 0;
p_s_magic = 0;
memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
memset(&p_s_rtcp_sin_local, 0, sizeof(p_s_rtcp_sin_local));
memset(&p_s_rtp_sin_remote, 0, sizeof(p_s_rtp_sin_remote));
memset(&p_s_rtcp_sin_remote, 0, sizeof(p_s_rtcp_sin_remote));
p_s_rtp_ip_local = 0;
p_s_rtp_ip_remote = 0;
p_s_rtp_port_local = 0;
p_s_rtp_port_remote = 0;
p_s_b_sock = -1;
p_s_b_index = -1;
p_s_b_active = 0;
p_s_rxpos = 0;
p_s_rtp_tx_action = 0;
PDEBUG(DEBUG_SIP, "Created new Psip(%s).\n", portname);
if (!p_s_sip_inst)
FATAL("No SIP instance for interface\n");
}
/*
* destructor
*/
Psip::~Psip()
{
PDEBUG(DEBUG_SIP, "Destroyed SIP process(%s).\n", p_name);
del_work(&p_s_delete);
rtp_close();
}
static const char *media_type2name(uint8_t media_type) {
switch (media_type) {
case MEDIA_TYPE_ULAW:
return "PCMU";
case MEDIA_TYPE_ALAW:
return "PCMA";
case MEDIA_TYPE_GSM:
return "GSM";
case MEDIA_TYPE_GSM_HR:
return "GSM-HR";
case MEDIA_TYPE_GSM_EFR:
return "GSM-EFR";
case MEDIA_TYPE_AMR:
return "AMR";
}
return "UKN";
}
static void sip_trace_header(class Psip *sip, const char *message, int direction)
{
/* init trace with given values */
start_trace(-1,
NULL,
sip?numberrize_callerinfo(sip->p_callerinfo.id, sip->p_callerinfo.ntype, options.national, options.international):NULL,
sip?sip->p_dialinginfo.id:NULL,
direction,
CATEGORY_CH,
sip?sip->p_serial:0,
message);
}
/*
* RTP
*/
/* according to RFC 3550 */
struct rtp_hdr {
#if __BYTE_ORDER == __LITTLE_ENDIAN
uint8_t csrc_count:4,
extension:1,
padding:1,
version:2;
uint8_t payload_type:7,
marker:1;
#elif __BYTE_ORDER == __BIG_ENDIAN
uint8_t version:2,
padding:1,
extension:1,
csrc_count:4;
uint8_t marker:1,
payload_type:7;
#endif
uint16_t sequence;
uint32_t timestamp;
uint32_t ssrc;
} __attribute__((packed));
struct rtp_x_hdr {
uint16_t by_profile;
uint16_t length;
} __attribute__((packed));
#define RTP_VERSION 2
#define PAYLOAD_TYPE_ULAW 0
#define PAYLOAD_TYPE_ALAW 8
#define PAYLOAD_TYPE_GSM 3
/* decode an rtp frame */
static int rtp_decode(class Psip *psip, unsigned char *data, int len)
{
struct rtp_hdr *rtph = (struct rtp_hdr *)data;
struct rtp_x_hdr *rtpxh;
uint8_t *payload;
int payload_len;
int x_len;
unsigned char *from, *to;
int n;
if (len < 12) {
PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d)\n", len);
return -EINVAL;
}
if (rtph->version != RTP_VERSION) {
PDEBUG(DEBUG_SIP, "received RTP version %d not supported.\n", rtph->version);
return -EINVAL;
}
payload = data + sizeof(struct rtp_hdr) + (rtph->csrc_count << 2);
payload_len = len - sizeof(struct rtp_hdr) - (rtph->csrc_count << 2);
if (payload_len < 0) {
PDEBUG(DEBUG_SIP, "received RTP frame too short (len = %d, "
"csrc count = %d)\n", len, rtph->csrc_count);
return -EINVAL;
}
if (rtph->extension) {
if (payload_len < (int)sizeof(struct rtp_x_hdr)) {
PDEBUG(DEBUG_SIP, "received RTP frame too short for "
"extension header\n");
return -EINVAL;
}
rtpxh = (struct rtp_x_hdr *)payload;
x_len = ntohs(rtpxh->length) * 4 + sizeof(struct rtp_x_hdr);
payload += x_len;
payload_len -= x_len;
if (payload_len < 0) {
PDEBUG(DEBUG_SIP, "received RTP frame too short, "
"extension header exceeds frame length\n");
return -EINVAL;
}
}
if (rtph->padding) {
if (payload_len < 0) {
PDEBUG(DEBUG_SIP, "received RTP frame too short for "
"padding length\n");
return -EINVAL;
}
payload_len -= payload[payload_len - 1];
if (payload_len < 0) {
PDEBUG(DEBUG_SIP, "received RTP frame with padding "
"greater than payload\n");
return -EINVAL;
}
}
switch (rtph->payload_type) {
#if 0
we only support alaw and ulaw!
case RTP_PT_GSM_FULL:
if (payload_len != 33) {
PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
"payload length != 33 (len = %d)\n",
payload_len);
return -EINVAL;
}
break;
case RTP_PT_GSM_EFR:
if (payload_len != 31) {
PDEBUG(DEBUG_SIP, "received RTP full rate frame with "
"payload length != 31 (len = %d)\n",
payload_len);
return -EINVAL;
}
break;
case RTP_PT_GSM_HALF:
if (payload_len != 14) {
PDEBUG(DEBUG_SIP, "received RTP half rate frame with "
"payload length != 14 (len = %d)\n",
payload_len);
return -EINVAL;
}
break;
#endif
case PAYLOAD_TYPE_ALAW:
if (options.law != 'a') {
PDEBUG(DEBUG_SIP, "received Alaw, but we don't do Alaw\n");
return -EINVAL;
}
break;
case PAYLOAD_TYPE_ULAW:
if (options.law == 'a') {
PDEBUG(DEBUG_SIP, "received Ulaw, but we don't do Ulaw\n");
return -EINVAL;
}
break;
default:
PDEBUG(DEBUG_SIP, "received RTP frame with unknown payload "
"type %d\n", rtph->payload_type);
return -EINVAL;
}
if (payload_len <= 0) {
PDEBUG(DEBUG_SIP, "received RTP payload is too small: %d\n", payload_len);
return 0;
}
n = payload_len;
from = payload;
to = payload;
if (psip->p_echotest) {
/* echo rtp data we just received */
psip->rtp_send_frame(from, n, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
return 0;
}
while(n--)
*to++ = flip[*from++];
psip->bridge_tx(payload, payload_len);
return 0;
}
static int rtp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
{
class Psip *psip = (class Psip *) instance;
int len;
unsigned char buffer[256];
int rc = 0;
if ((what & LCR_FD_READ)) {
len = read(fd->fd, &buffer, sizeof(buffer));
if (len <= 0) {
PDEBUG(DEBUG_SIP, "read result=%d\n", len);
// psip->rtp_close();
// psip->rtp_shutdown();
return len;
}
if (psip->p_s_rtp_is_connected)
rc = rtp_decode(psip, buffer, len);
}
return rc;
}
static int rtcp_sock_callback(struct lcr_fd *fd, unsigned int what, void *instance, int index)
{
// class Psip *psip = (class Psip *) instance;
int len;
unsigned char buffer[256];
if ((what & LCR_FD_READ)) {
len = read(fd->fd, &buffer, sizeof(buffer));
if (len <= 0) {
PDEBUG(DEBUG_SIP, "read result=%d\n", len);
// psip->rtp_close();
// psip->rtp_shutdown();
return len;
}
PDEBUG(DEBUG_SIP, "rtcp!\n");
}
return 0;
}
#define RTP_PORT_BASE 30000
static unsigned int next_udp_port = RTP_PORT_BASE;
static int rtp_sub_socket_bind(int fd, struct sockaddr_in *sin_local, uint32_t ip, uint16_t port)
{
int rc;
socklen_t alen = sizeof(*sin_local);
sin_local->sin_family = AF_INET;
sin_local->sin_addr.s_addr = htonl(ip);
sin_local->sin_port = htons(port);
rc = bind(fd, (struct sockaddr *) sin_local, sizeof(*sin_local));
if (rc < 0)
return rc;
/* retrieve the address we actually bound to, in case we
* passed INADDR_ANY as IP address */
return getsockname(fd, (struct sockaddr *) sin_local, &alen);
}
static int rtp_sub_socket_connect(int fd, struct sockaddr_in *sin_local, struct sockaddr_in *sin_remote, uint32_t ip, uint16_t port)
{
int rc;
socklen_t alen = sizeof(*sin_local);
sin_remote->sin_family = AF_INET;
sin_remote->sin_addr.s_addr = htonl(ip);
sin_remote->sin_port = htons(port);
rc = connect(fd, (struct sockaddr *) sin_remote, sizeof(*sin_remote));
if (rc < 0) {
PERROR("failed to connect to ip %08x port %d rc=%d\n", ip, port, rc);
return rc;
}
return getsockname(fd, (struct sockaddr *) sin_local, &alen);
}
int Psip::rtp_open(void)
{
int rc;
struct in_addr ia;
unsigned int ip;
/* create socket */
rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!rc) {
rtp_close();
return -EIO;
}
p_s_rtp_fd.fd = rc;
register_fd(&p_s_rtp_fd, LCR_FD_READ, rtp_sock_callback, this, 0);
rc = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (!rc) {
rtp_close();
return -EIO;
}
p_s_rtcp_fd.fd = rc;
register_fd(&p_s_rtcp_fd, LCR_FD_READ, rtcp_sock_callback, this, 0);
/* bind socket */
ip = htonl(INADDR_ANY);
ia.s_addr = ip;
for (next_udp_port = next_udp_port % 0xffff;
next_udp_port < 0xffff; next_udp_port += 2) {
rc = rtp_sub_socket_bind(p_s_rtp_fd.fd, &p_s_rtp_sin_local, ip, next_udp_port);
if (rc != 0)
continue;
rc = rtp_sub_socket_bind(p_s_rtcp_fd.fd, &p_s_rtcp_sin_local, ip, next_udp_port+1);
if (rc == 0)
break;
}
if (rc < 0) {
PDEBUG(DEBUG_SIP, "failed to find port\n");
rtp_close();
return rc;
}
p_s_rtp_port_local = next_udp_port;
p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
return p_s_rtp_port_local;
}
int Psip::rtp_connect(void)
{
int rc;
struct in_addr ia;
ia.s_addr = htonl(p_s_rtp_ip_remote);
PDEBUG(DEBUG_SIP, "rtp_connect(ip=%s, port=%u)\n", inet_ntoa(ia), p_s_rtp_port_remote);
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);
if (rc < 0)
return rc;
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);
if (rc < 0)
return rc;
p_s_rtp_ip_local = ntohl(p_s_rtp_sin_local.sin_addr.s_addr);
PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
p_s_rtp_is_connected = 1;
return 0;
}
void Psip::rtp_close(void)
{
if (p_s_rtp_fd.fd > 0) {
unregister_fd(&p_s_rtp_fd);
close(p_s_rtp_fd.fd);
p_s_rtp_fd.fd = 0;
}
if (p_s_rtcp_fd.fd > 0) {
unregister_fd(&p_s_rtcp_fd);
close(p_s_rtcp_fd.fd);
p_s_rtcp_fd.fd = 0;
}
if (p_s_rtp_is_connected) {
PDEBUG(DEBUG_SIP, "rtp closed\n");
p_s_rtp_is_connected = 0;
}
}
/* "to - from" */
void tv_difference(struct timeval *diff, const struct timeval *from,
const struct timeval *__to)
{
struct timeval _to = *__to, *to = &_to;
if (to->tv_usec < from->tv_usec) {
to->tv_sec -= 1;
to->tv_usec += 1000000;
}
diff->tv_usec = to->tv_usec - from->tv_usec;
diff->tv_sec = to->tv_sec - from->tv_sec;
}
/* encode and send a rtp frame */
int Psip::rtp_send_frame(unsigned char *data, unsigned int len, uint8_t payload_type)
{
struct rtp_hdr *rtph;
int payload_len;
int duration; /* in samples */
unsigned char buffer[256];
if (!p_s_rtp_is_connected) {
/* drop silently */
return 0;
}
if (!p_s_rtp_tx_action) {
/* initialize sequences */
p_s_rtp_tx_action = 1;
p_s_rtp_tx_ssrc = rand();
p_s_rtp_tx_sequence = random();
p_s_rtp_tx_timestamp = random();
memset(&p_s_rtp_tx_last_tv, 0, sizeof(p_s_rtp_tx_last_tv));
}
switch (payload_type) {
#if 0
we only support alaw and ulaw!
case RTP_PT_GSM_FULL:
payload_len = 33;
duration = 160;
break;
case RTP_PT_GSM_EFR:
payload_len = 31;
duration = 160;
break;
case RTP_PT_GSM_HALF:
payload_len = 14;
duration = 160;
break;
#endif
case PAYLOAD_TYPE_ALAW:
case PAYLOAD_TYPE_ULAW:
payload_len = len;
duration = len;
break;
default:
PERROR("unsupported message type %d\n", payload_type);
return -EINVAL;
}
#if 0
{
struct timeval tv, tv_diff;
long int usec_diff, frame_diff;
gettimeofday(&tv, NULL);
tv_difference(&tv_diff, &p_s_rtp_tx_last_tv, &tv);
p_s_rtp_tx_last_tv = tv;
usec_diff = tv_diff.tv_sec * 1000000 + tv_diff.tv_usec;
frame_diff = (usec_diff / 20000);
if (abs(frame_diff) > 1) {
long int frame_diff_excess = frame_diff - 1;
PDEBUG(DEBUG_SIP, "Correcting frame difference of %ld frames\n", frame_diff_excess);
p_s_rtp_tx_sequence += frame_diff_excess;
p_s_rtp_tx_timestamp += frame_diff_excess * duration;
}
}
#endif
rtph = (struct rtp_hdr *) buffer;
rtph->version = RTP_VERSION;
rtph->padding = 0;
rtph->extension = 0;
rtph->csrc_count = 0;
rtph->marker = 0;
rtph->payload_type = payload_type;
rtph->sequence = htons(p_s_rtp_tx_sequence++);
rtph->timestamp = htonl(p_s_rtp_tx_timestamp);
p_s_rtp_tx_timestamp += duration;
rtph->ssrc = htonl(p_s_rtp_tx_ssrc);
memcpy(buffer + sizeof(struct rtp_hdr), data, payload_len);
if (p_s_rtp_fd.fd > 0) {
len = write(p_s_rtp_fd.fd, &buffer, sizeof(struct rtp_hdr) + payload_len);
if (len != sizeof(struct rtp_hdr) + payload_len) {
PDEBUG(DEBUG_SIP, "write result=%d\n", len);
// rtp_close();
// rtp_shutdown();
return -EIO;
}
}
return 0;
}
/* receive from remote */
int Psip::bridge_rx(unsigned char *data, int len)
{
/* write to rx buffer */
while(len--) {
p_s_rxdata[p_s_rxpos++] = flip[*data++];
if (p_s_rxpos == 160) {
p_s_rxpos = 0;
/* transmit data via rtp */
rtp_send_frame(p_s_rxdata, 160, (options.law=='a')?PAYLOAD_TYPE_ALAW:PAYLOAD_TYPE_ULAW);
}
}
return 0;
}
/* taken from freeswitch */
/* map sip responses to QSIG cause codes ala RFC4497 section 8.4.4 */
static int status2cause(int status)
{
switch (status) {
case 200:
return 16; //SWITCH_CAUSE_NORMAL_CLEARING;
case 401:
case 402:
case 403:
case 407:
case 603:
return 21; //SWITCH_CAUSE_CALL_REJECTED;
case 404:
return 1; //SWITCH_CAUSE_UNALLOCATED_NUMBER;
case 485:
case 604:
return 3; //SWITCH_CAUSE_NO_ROUTE_DESTINATION;
case 408:
case 504:
return 102; //SWITCH_CAUSE_RECOVERY_ON_TIMER_EXPIRE;
case 410:
return 22; //SWITCH_CAUSE_NUMBER_CHANGED;
case 413:
case 414:
case 416:
case 420:
case 421:
case 423:
case 505:
case 513:
return 127; //SWITCH_CAUSE_INTERWORKING;
case 480:
return 180; //SWITCH_CAUSE_NO_USER_RESPONSE;
case 400:
case 481:
case 500:
case 503:
return 41; //SWITCH_CAUSE_NORMAL_TEMPORARY_FAILURE;
case 486:
case 600:
return 17; //SWITCH_CAUSE_USER_BUSY;
case 484:
return 28; //SWITCH_CAUSE_INVALID_NUMBER_FORMAT;
case 488:
case 606:
return 88; //SWITCH_CAUSE_INCOMPATIBLE_DESTINATION;
case 502:
return 38; //SWITCH_CAUSE_NETWORK_OUT_OF_ORDER;
case 405:
return 63; //SWITCH_CAUSE_SERVICE_UNAVAILABLE;
case 406:
case 415:
case 501:
return 79; //SWITCH_CAUSE_SERVICE_NOT_IMPLEMENTED;
case 482:
case 483:
return 25; //SWITCH_CAUSE_EXCHANGE_ROUTING_ERROR;
case 487:
return 31; //??? SWITCH_CAUSE_ORIGINATOR_CANCEL;
default:
return 31; //SWITCH_CAUSE_NORMAL_UNSPECIFIED;
}
}
static int cause2status(int cause, int location, const char **st)
{
int s;
switch (cause) {
case 1:
s = 404; *st = sip_404_Not_found;
break;
case 2:
s = 404; *st = sip_404_Not_found;
break;
case 3:
s = 404; *st = sip_404_Not_found;
break;
case 17:
s = 486; *st = sip_486_Busy_here;
break;
case 18:
s = 408; *st = sip_408_Request_timeout;
break;
case 19:
s = 480; *st = sip_480_Temporarily_unavailable;
break;
case 20:
s = 480; *st = sip_480_Temporarily_unavailable;
break;
case 21:
if (location == LOCATION_USER) {
s = 603; *st = sip_603_Decline;
} else {
s = 403; *st = sip_403_Forbidden;
}
break;
case 22:
//s = 301; *st = sip_301_Moved_permanently;
s = 410; *st = sip_410_Gone;
break;
case 23:
s = 410; *st = sip_410_Gone;
break;
case 27:
s = 502; *st = sip_502_Bad_gateway;
break;
case 28:
s = 484; *st = sip_484_Address_incomplete;
break;
case 29:
s = 501; *st = sip_501_Not_implemented;
break;
case 31:
s = 480; *st = sip_480_Temporarily_unavailable;
break;
case 34:
s = 503; *st = sip_503_Service_unavailable;
break;
case 38:
s = 503; *st = sip_503_Service_unavailable;
break;
case 41:
s = 503; *st = sip_503_Service_unavailable;
break;
case 42:
s = 503; *st = sip_503_Service_unavailable;
break;
case 47:
s = 503; *st = sip_503_Service_unavailable;
break;
case 55:
s = 403; *st = sip_403_Forbidden;
break;
case 57:
s = 403; *st = sip_403_Forbidden;
break;
case 58:
s = 503; *st = sip_503_Service_unavailable;
break;
case 65:
s = 488; *st = sip_488_Not_acceptable;
break;
case 69:
s = 501; *st = sip_501_Not_implemented;
break;
case 70:
s = 488; *st = sip_488_Not_acceptable;
break;
case 79:
s = 501; *st = sip_501_Not_implemented;
break;
case 87:
s = 403; *st = sip_403_Forbidden;
break;
case 88:
s = 503; *st = sip_503_Service_unavailable;
break;
case 102:
s = 504; *st = sip_504_Gateway_time_out;
break;
default:
s = 468; *st = sip_486_Busy_here;
}
return s;
}
/*
* endpoint sends messages to the SIP port
*/
int Psip::message_connect(unsigned int epoint_id, int message_id, union parameter *param)
{
char sdp_str[256];
struct in_addr ia;
struct lcr_msg *message;
int media_type;
unsigned char payload_type;
if (param->connectinfo.rtpinfo.port) {
PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
p_s_rtp_bridge = 1;
media_type = param->connectinfo.rtpinfo.media_types[0];
payload_type = param->connectinfo.rtpinfo.payload_types[0];
p_s_rtp_ip_local = param->connectinfo.rtpinfo.ip;
p_s_rtp_port_local = param->connectinfo.rtpinfo.port;
PDEBUG(DEBUG_SIP, "payload type %d\n", payload_type);
PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
} else {
PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
media_type = (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW;
payload_type = (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW;
/* open local RTP peer (if not bridging) */
if (!p_s_rtp_is_connected && rtp_connect() < 0) {
nua_cancel(p_s_handle, TAG_END());
nua_handle_destroy(p_s_handle);
p_s_handle = NULL;
sip_trace_header(this, "CANCEL", DIRECTION_OUT);
add_trace("reason", NULL, "failed to connect RTP/RTCP sockts");
end_trace();
message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
message->param.disconnectinfo.cause = 41;
message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
message_put(message);
new_state(PORT_STATE_RELEASE);
trigger_work(&p_s_delete);
return 0;
}
}
ia.s_addr = htonl(p_s_rtp_ip_local);
SPRINT(sdp_str,
"v=0\n"
"o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
"s=SIP Call\n"
"c=IN IP4 %s\n"
"t=0 0\n"
"m=audio %d RTP/AVP %d\n"
"a=rtpmap:%d %s/8000\n"
, inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local, payload_type, payload_type, media_type2name(media_type));
PDEBUG(DEBUG_SIP, "Using SDP response: %s\n", sdp_str);
nua_respond(p_s_handle, SIP_200_OK,
NUTAG_MEDIA_ENABLE(0),
SIPTAG_CONTENT_TYPE_STR("application/sdp"),
SIPTAG_PAYLOAD_STR(sdp_str), TAG_END());
new_state(PORT_STATE_CONNECT);
sip_trace_header(this, "RESPOND", DIRECTION_OUT);
add_trace("respond", "value", "200 OK");
add_trace("reason", NULL, "call connected");
add_trace("rtp", "ip", "%s", inet_ntoa(ia));
add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
add_trace("rtp", "payload", "%s:%d", media_type2name(media_type), payload_type);
end_trace();
return 0;
}
int Psip::message_release(unsigned int epoint_id, int message_id, union parameter *param)
{
struct lcr_msg *message;
char cause_str[128] = "";
int cause = param->disconnectinfo.cause;
int location = param->disconnectinfo.cause;
int status;
const char *status_text;
if (cause > 0 && cause <= 127) {
SPRINT(cause_str, "Q.850;cause=%d;text=\"%s\"", cause, isdn_cause[cause].english);
}
switch (p_state) {
case PORT_STATE_OUT_SETUP:
case PORT_STATE_OUT_PROCEEDING:
case PORT_STATE_OUT_ALERTING:
PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will cancel\n");
sip_trace_header(this, "CANCEL", DIRECTION_OUT);
if (cause_str[0])
add_trace("cause", "value", "%d", cause);
end_trace();
nua_cancel(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
break;
case PORT_STATE_IN_SETUP:
case PORT_STATE_IN_PROCEEDING:
case PORT_STATE_IN_ALERTING:
PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will respond\n");
status = cause2status(cause, location, &status_text);
sip_trace_header(this, "RESPOND", DIRECTION_OUT);
if (cause_str[0])
add_trace("cause", "value", "%d", cause);
add_trace("respond", "value", "%d %s", status, status_text);
end_trace();
nua_respond(p_s_handle, status, status_text, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
nua_handle_destroy(p_s_handle);
p_s_handle = NULL;
trigger_work(&p_s_delete);
break;
default:
PDEBUG(DEBUG_SIP, "RELEASE/DISCONNECT will perform nua_bye\n");
sip_trace_header(this, "BYE", DIRECTION_OUT);
if (cause_str[0])
add_trace("cause", "value", "%d", cause);
end_trace();
nua_bye(p_s_handle, TAG_IF(cause_str[0], SIPTAG_REASON_STR(cause_str)), TAG_END());
}
if (message_id == MESSAGE_DISCONNECT) {
while(p_epointlist) {
message = message_create(p_serial, p_epointlist->epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
message->param.disconnectinfo.cause = CAUSE_NORMAL;
message->param.disconnectinfo.location = LOCATION_BEYOND;
message_put(message);
/* remove epoint */
free_epointlist(p_epointlist);
}
}
new_state(PORT_STATE_RELEASE);
return(0);
}
int Psip::message_setup(unsigned int epoint_id, int message_id, union parameter *param)
{
struct sip_inst *inst = (struct sip_inst *) p_s_sip_inst;
char from[128];
char to[128];
const char *local = inst->local_peer;
char local_ip[16];
const char *remote = inst->remote_peer;
char sdp_str[512], pt_str[32];
struct in_addr ia;
struct epoint_list *epointlist;
sip_cseq_t *cseq = NULL;
struct lcr_msg *message;
int lcr_media = { (options.law=='a') ? MEDIA_TYPE_ALAW : MEDIA_TYPE_ULAW };
unsigned char lcr_payload = { (options.law=='a') ? PAYLOAD_TYPE_ALAW : PAYLOAD_TYPE_ULAW };
int *media_types;
unsigned char *payload_types;
int payloads = 0;
int i;
PDEBUG(DEBUG_SIP, "Doing Setup (inst %p)\n", inst);
memcpy(&p_dialinginfo, ¶m->setup.dialinginfo, sizeof(p_dialinginfo));
memcpy(&p_callerinfo, ¶m->setup.callerinfo, sizeof(p_callerinfo));
memcpy(&p_redirinfo, ¶m->setup.redirinfo, sizeof(p_redirinfo));
if (param->setup.rtpinfo.port) {
PDEBUG(DEBUG_SIP, "RTP info given by remote, forward that\n");
p_s_rtp_bridge = 1;
media_types = param->setup.rtpinfo.media_types;
payload_types = param->setup.rtpinfo.payload_types;
payloads = param->setup.rtpinfo.payloads;
p_s_rtp_ip_local = param->setup.rtpinfo.ip;
p_s_rtp_port_local = param->setup.rtpinfo.port;
PDEBUG(DEBUG_SIP, "local ip %08x port %d\n", p_s_rtp_ip_local, p_s_rtp_port_local);
PDEBUG(DEBUG_SIP, "remote ip %08x port %d\n", p_s_rtp_ip_remote, p_s_rtp_port_remote);
} else {
PDEBUG(DEBUG_SIP, "RTP info not given by remote, so we do our own RTP\n");
p_s_rtp_bridge = 0;
media_types = &lcr_media;
payload_types = &lcr_payload;
payloads = 1;
/* open local RTP peer (if not bridging) */
if (rtp_open() < 0) {
PERROR("Failed to open RTP sockets\n");
/* send release message to endpoit */
message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
message->param.disconnectinfo.cause = 41;
message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
message_put(message);
new_state(PORT_STATE_RELEASE);
trigger_work(&p_s_delete);
return 0;
}
}
p_s_handle = nua_handle(inst->nua, NULL, TAG_END());
if (!p_s_handle) {
PERROR("Failed to create handle\n");
/* send release message to endpoit */
message = message_create(p_serial, epoint_id, PORT_TO_EPOINT, MESSAGE_RELEASE);
message->param.disconnectinfo.cause = 41;
message->param.disconnectinfo.location = LOCATION_PRIVATE_LOCAL;
message_put(message);
new_state(PORT_STATE_RELEASE);
trigger_work(&p_s_delete);
return 0;
}
/* apply handle */
sip_trace_header(this, "NEW handle", DIRECTION_IN);
add_trace("handle", "new", "0x%x", p_s_handle);
end_trace();
if (!p_s_rtp_ip_local) {
char *p;
/* extract IP from local peer */
SCPY(local_ip, local);
p = strchr(local_ip, ':');
if (p)
*p = '\0';
PDEBUG(DEBUG_SIP, "RTP local IP not known, so we use our local SIP ip %s\n", local_ip);
inet_pton(AF_INET, local_ip, &p_s_rtp_ip_local);
p_s_rtp_ip_local = ntohl(p_s_rtp_ip_local);
}
ia.s_addr = htonl(p_s_rtp_ip_local);
SPRINT(sdp_str,
"v=0\n"
"o=LCR-Sofia-SIP 0 0 IN IP4 %s\n"
"s=SIP Call\n"
"c=IN IP4 %s\n"
"t=0 0\n"
"m=audio %d RTP/AVP"
, inet_ntoa(ia), inet_ntoa(ia), p_s_rtp_port_local);
for (i = 0; i < payloads; i++) {
SPRINT(pt_str, " %d", payload_types[i]);
SCAT(sdp_str, pt_str);
}
SCAT(sdp_str, "\n");
for (i = 0; i < payloads; i++) {
SPRINT(pt_str, "a=rtpmap:%d %s/8000\n", payload_types[i], media_type2name(media_types[i]));
SCAT(sdp_str, pt_str);
}
PDEBUG(DEBUG_SIP, "Using SDP for invite: %s\n", sdp_str);
SPRINT(from, "sip:%s@%s", param->setup.callerinfo.id, local);
SPRINT(to, "sip:%s@%s", param->setup.dialinginfo.id, remote);
sip_trace_header(this, "INVITE", DIRECTION_OUT);
add_trace("from", "uri", "%s", from);
add_trace("to", "uri", "%s", to);
add_trace("rtp", "ip", "%s", inet_ntoa(ia));
add_trace("rtp", "port", "%d,%d", p_s_rtp_port_local, p_s_rtp_port_local + 1);
for (i = 0; i < payloads; i++)
add_trace("rtp", "payload", "%s:%d", media_type2name(media_types[i]), payload_types[i]);
end_trace();
// cseq = sip_cseq_create(sip_home, 123, SIP_METHOD_INVITE);
nua_invite(p_s_handle,
TAG_IF(from[0], SIPTAG_FROM_STR(from)),
TAG_IF(to[0], SIPTAG_TO_STR(to)),