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