annotate libsip/sdp_parse.c @ 0:35c0d9f03c0a

beginning with sipout-test-voice, a copy of sip-manual-out from themwi-system-sw
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Mar 2024 23:20:19 -0800
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * Here we implement the function that parses received SDP descriptions.
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 */
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 #include <sys/types.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/socket.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <netinet/in.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <arpa/inet.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <ctype.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <string.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <strings.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <stdlib.h>
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include "../include/tmgw_const.h"
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "sdp.h"
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 parse_incoming_sdp(body, bodylen, dist)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 char *body;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 unsigned bodylen;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 struct sdp_parse *dist;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 char *cp, *endp;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 int got_c = 0, got_m = 0, prefer_pcma = 0;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 unsigned codec;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 cp = body;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 endp = body + bodylen;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 while (cp < endp) {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (!islower(cp[0]) || cp[1] != '=')
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 switch (cp[0]) {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 case 'c':
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (got_c)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 got_c = 1;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 if (strncmp(cp + 2, "IN IP4 ", 7))
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 cp += 9;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 dist->ip_addr.s_addr = inet_addr(cp);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 if (dist->ip_addr.s_addr == INADDR_NONE)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 case 'm':
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 if (got_m)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 got_m = 1;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 if (strncmp(cp + 2, "audio ", 6))
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 cp += 8;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (!isdigit(*cp))
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 dist->audio_port = strtoul(cp, &cp, 10);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 if (strncmp(cp, " RTP/AVP", 8))
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 cp += 8;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 dist->codec_mask = 0;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 for (;;) {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 if (*cp == '\r' || *cp == '\n')
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 if (*cp++ != ' ')
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 codec = strtoul(cp, &cp, 10);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 switch (codec) {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 case PSTN_CODEC_PCMU:
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 dist->codec_mask |= SDP_CODEC_MASK_PCMU;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 case PSTN_CODEC_PCMA:
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (!dist->codec_mask)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 prefer_pcma = 1;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 dist->codec_mask |= SDP_CODEC_MASK_PCMA;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 if (dist->codec_mask == SDP_CODEC_MASK_BOTH &&
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 prefer_pcma)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 dist->codec_mask |= SDP_CODEC_MASK_PCMA_PREF;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 default:
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 cp += 2;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 for (;;) {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (cp >= endp)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 if (!*cp)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 if (*cp == '\n') {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86 cp++;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 if (*cp == '\r') {
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 cp++;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 if (cp >= endp)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 if (*cp++ != '\n')
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 break;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 cp++;
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 }
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (got_c && got_m)
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 return(0);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 else
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 return(-1);
35c0d9f03c0a beginning with sipout-test-voice,
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 }