1 /***
2 This file is part of PulseAudio.
3
4 Copyright 2006 Lennart Poettering
5
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
10
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <time.h>
25 #include <stdlib.h>
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <string.h>
30
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33
34 #include <pulsecore/core-util.h>
35 #include <pulsecore/log.h>
36 #include <pulsecore/macro.h>
37 #include <pulsecore/arpa-inet.h>
38
39 #include "sdp.h"
40 #include "rtp.h"
41
pa_sdp_build(int af,const void * src,const void * dst,const char * name,uint16_t port,uint8_t payload,const pa_sample_spec * ss,bool enable_opus)42 char *pa_sdp_build(int af, const void *src, const void *dst, const char *name, uint16_t port, uint8_t payload, const pa_sample_spec *ss, bool enable_opus) {
43 uint32_t ntp;
44 uint32_t rate, channels;
45 char buf_src[64], buf_dst[64], un[64];
46 const char *u, *f;
47
48 pa_assert(src);
49 pa_assert(dst);
50
51 #ifdef HAVE_IPV6
52 pa_assert(af == AF_INET || af == AF_INET6);
53 #else
54 pa_assert(af == AF_INET);
55 #endif
56
57 if (enable_opus) {
58 f = "OPUS";
59 rate = 48000;
60 channels = 2;
61 } else {
62 pa_assert_se(f = pa_rtp_format_to_string(ss->format));
63 rate = ss->rate;
64 channels = ss->channels;
65 }
66
67 if (!(u = pa_get_user_name(un, sizeof(un))))
68 u = "-";
69
70 ntp = (uint32_t) time(NULL) + 2208988800U;
71
72 pa_assert_se(inet_ntop(af, src, buf_src, sizeof(buf_src)));
73 pa_assert_se(inet_ntop(af, dst, buf_dst, sizeof(buf_dst)));
74
75 return pa_sprintf_malloc(
76 PA_SDP_HEADER "\n"
77 "o=%s %lu 0 IN %s %s\n"
78 "s=%s\n"
79 "c=IN %s %s\n"
80 "t=%lu 0\n"
81 "a=recvonly\n"
82 "m=audio %u RTP/AVP %i\n"
83 "a=rtpmap:%i %s/%u/%u\n"
84 "a=type:broadcast\n",
85 u, (unsigned long) ntp, af == AF_INET ? "IP4" : "IP6", buf_src,
86 name,
87 af == AF_INET ? "IP4" : "IP6", buf_dst,
88 (unsigned long) ntp,
89 port, payload,
90 payload, f, rate, channels);
91 }
92
parse_sdp_sample_spec(pa_sample_spec * ss,char * c)93 static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
94 unsigned rate, channels;
95 pa_assert(ss);
96 pa_assert(c);
97
98 if (pa_startswith(c, "L16/")) {
99 ss->format = PA_SAMPLE_S16BE;
100 c += 4;
101 } else if (pa_startswith(c, "OPUS/")) {
102 ss->format = PA_SAMPLE_S16LE;
103 c += 5;
104 } else
105 return NULL;
106
107 if (sscanf(c, "%u/%u", &rate, &channels) == 2) {
108 ss->rate = (uint32_t) rate;
109 ss->channels = (uint8_t) channels;
110 } else if (sscanf(c, "%u", &rate) == 2) {
111 ss->rate = (uint32_t) rate;
112 ss->channels = 1;
113 } else
114 return NULL;
115
116 if (!pa_sample_spec_valid(ss))
117 return NULL;
118
119 return ss;
120 }
121
pa_sdp_parse(const char * t,pa_sdp_info * i,int is_goodbye)122 pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
123 uint16_t port = 0;
124 bool ss_valid = false;
125
126 pa_assert(t);
127 pa_assert(i);
128
129 i->origin = i->session_name = NULL;
130 i->salen = 0;
131 i->payload = 255;
132 i->enable_opus = false;
133
134 if (pa_startswith(t, PA_SDP_HEADER)) {
135 t += sizeof(PA_SDP_HEADER) - 1;
136
137 /* CR delimiter is optional */
138 if (*t == '\r')
139 t++;
140
141 /* LF delimiter is mandatory */
142 if (*t == '\n')
143 t++;
144 else {
145 pa_log("Failed to parse SDP data: missing header record terminator LF.");
146 goto fail;
147 }
148 } else {
149 pa_log("Failed to parse SDP data: invalid header.");
150 goto fail;
151 }
152
153 while (*t) {
154 size_t l;
155
156 l = strcspn(t, "\r\n");
157
158 if (l <= 2) {
159 pa_log("Failed to parse SDP data: line too short: >%s<.", t);
160 goto fail;
161 }
162
163 if (pa_startswith(t, "o="))
164 i->origin = pa_xstrndup(t+2, l-2);
165 else if (pa_startswith(t, "s="))
166 i->session_name = pa_xstrndup(t+2, l-2);
167 else if (pa_startswith(t, "c=IN IP4 ")) {
168 char a[64];
169 size_t k;
170
171 k = l-8 > sizeof(a) ? sizeof(a) : l-8;
172
173 pa_strlcpy(a, t+9, k);
174 a[strcspn(a, "/")] = 0;
175
176 if (inet_pton(AF_INET, a, &((struct sockaddr_in*) &i->sa)->sin_addr) <= 0) {
177 pa_log("Failed to parse SDP data: bad address: >%s<.", a);
178 goto fail;
179 }
180
181 ((struct sockaddr_in*) &i->sa)->sin_family = AF_INET;
182 ((struct sockaddr_in*) &i->sa)->sin_port = 0;
183 i->salen = sizeof(struct sockaddr_in);
184 #ifdef HAVE_IPV6
185 } else if (pa_startswith(t, "c=IN IP6 ")) {
186 char a[64];
187 size_t k;
188
189 k = l-8 > sizeof(a) ? sizeof(a) : l-8;
190
191 pa_strlcpy(a, t+9, k);
192 a[strcspn(a, "/")] = 0;
193
194 if (inet_pton(AF_INET6, a, &((struct sockaddr_in6*) &i->sa)->sin6_addr) <= 0) {
195 pa_log("Failed to parse SDP data: bad address: >%s<.", a);
196 goto fail;
197 }
198
199 ((struct sockaddr_in6*) &i->sa)->sin6_family = AF_INET6;
200 ((struct sockaddr_in6*) &i->sa)->sin6_port = 0;
201 i->salen = sizeof(struct sockaddr_in6);
202 #endif
203 } else if (pa_startswith(t, "m=audio ")) {
204
205 if (i->payload > 127) {
206 int _port, _payload;
207
208 if (sscanf(t+8, "%i RTP/AVP %i", &_port, &_payload) == 2) {
209
210 if (_port <= 0 || _port > 0xFFFF) {
211 pa_log("Failed to parse SDP data: invalid port %i.", _port);
212 goto fail;
213 }
214
215 if (_payload < 0 || _payload > 127) {
216 pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
217 goto fail;
218 }
219
220 port = (uint16_t) _port;
221 i->payload = (uint8_t) _payload;
222
223 if (pa_rtp_sample_spec_from_payload(i->payload, &i->sample_spec))
224 ss_valid = true;
225 }
226 }
227 } else if (pa_startswith(t, "a=rtpmap:")) {
228
229 if (i->payload <= 127) {
230 char c[64];
231 int _payload;
232 int len;
233
234 if (sscanf(t + 9, "%i %n", &_payload, &len) == 1) {
235 if (_payload < 0 || _payload > 127) {
236 pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
237 goto fail;
238 }
239 if (_payload == i->payload) {
240 strncpy(c, t + 9 + len, 63);
241 c[63] = 0;
242 c[strcspn(c, "\n")] = 0;
243
244 if (parse_sdp_sample_spec(&i->sample_spec, c))
245 ss_valid = true;
246
247 if (pa_startswith(c, "OPUS/"))
248 i->enable_opus = true;
249 }
250 }
251 }
252 }
253
254 t += l;
255
256 /* CR delimiter is optional */
257 if (*t == '\r')
258 t++;
259
260 /* LF delimiter is mandatory */
261 if (*t == '\n')
262 t++;
263 else {
264 pa_log("Failed to parse SDP data: missing record terminator LF.");
265 goto fail;
266 }
267 }
268
269 if (!i->origin || (!is_goodbye && (!i->salen || i->payload > 127 || !ss_valid || port == 0))) {
270 pa_log("Failed to parse SDP data: missing data.");
271 goto fail;
272 }
273
274 if (((struct sockaddr*) &i->sa)->sa_family == AF_INET)
275 ((struct sockaddr_in*) &i->sa)->sin_port = htons(port);
276 else
277 ((struct sockaddr_in6*) &i->sa)->sin6_port = htons(port);
278
279 return i;
280
281 fail:
282 pa_xfree(i->origin);
283 pa_xfree(i->session_name);
284
285 return NULL;
286 }
287
pa_sdp_info_destroy(pa_sdp_info * i)288 void pa_sdp_info_destroy(pa_sdp_info *i) {
289 pa_assert(i);
290
291 pa_xfree(i->origin);
292 pa_xfree(i->session_name);
293 }
294