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)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) {
43 uint32_t ntp;
44 char buf_src[64], buf_dst[64], un[64];
45 const char *u, *f;
46
47 pa_assert(src);
48 pa_assert(dst);
49
50 #ifdef HAVE_IPV6
51 pa_assert(af == AF_INET || af == AF_INET6);
52 #else
53 pa_assert(af == AF_INET);
54 #endif
55
56 pa_assert_se(f = pa_rtp_format_to_string(ss->format));
57
58 if (!(u = pa_get_user_name(un, sizeof(un))))
59 u = "-";
60
61 ntp = (uint32_t) time(NULL) + 2208988800U;
62
63 pa_assert_se(inet_ntop(af, src, buf_src, sizeof(buf_src)));
64 pa_assert_se(inet_ntop(af, dst, buf_dst, sizeof(buf_dst)));
65
66 return pa_sprintf_malloc(
67 PA_SDP_HEADER
68 "o=%s %lu 0 IN %s %s\n"
69 "s=%s\n"
70 "c=IN %s %s\n"
71 "t=%lu 0\n"
72 "a=recvonly\n"
73 "m=audio %u RTP/AVP %i\n"
74 "a=rtpmap:%i %s/%u/%u\n"
75 "a=type:broadcast\n",
76 u, (unsigned long) ntp, af == AF_INET ? "IP4" : "IP6", buf_src,
77 name,
78 af == AF_INET ? "IP4" : "IP6", buf_dst,
79 (unsigned long) ntp,
80 port, payload,
81 payload, f, ss->rate, ss->channels);
82 }
83
parse_sdp_sample_spec(pa_sample_spec * ss,char * c)84 static pa_sample_spec *parse_sdp_sample_spec(pa_sample_spec *ss, char *c) {
85 unsigned rate, channels;
86 pa_assert(ss);
87 pa_assert(c);
88
89 if (pa_startswith(c, "L16/")) {
90 ss->format = PA_SAMPLE_S16BE;
91 c += 4;
92 } else
93 return NULL;
94
95 if (sscanf(c, "%u/%u", &rate, &channels) == 2) {
96 ss->rate = (uint32_t) rate;
97 ss->channels = (uint8_t) channels;
98 } else if (sscanf(c, "%u", &rate) == 2) {
99 ss->rate = (uint32_t) rate;
100 ss->channels = 1;
101 } else
102 return NULL;
103
104 if (!pa_sample_spec_valid(ss))
105 return NULL;
106
107 return ss;
108 }
109
pa_sdp_parse(const char * t,pa_sdp_info * i,int is_goodbye)110 pa_sdp_info *pa_sdp_parse(const char *t, pa_sdp_info *i, int is_goodbye) {
111 uint16_t port = 0;
112 bool ss_valid = false;
113
114 pa_assert(t);
115 pa_assert(i);
116
117 i->origin = i->session_name = NULL;
118 i->salen = 0;
119 i->payload = 255;
120
121 if (!pa_startswith(t, PA_SDP_HEADER)) {
122 pa_log("Failed to parse SDP data: invalid header.");
123 goto fail;
124 }
125
126 t += sizeof(PA_SDP_HEADER)-1;
127
128 while (*t) {
129 size_t l;
130
131 l = strcspn(t, "\n");
132
133 if (l <= 2) {
134 pa_log("Failed to parse SDP data: line too short: >%s<.", t);
135 goto fail;
136 }
137
138 if (pa_startswith(t, "o="))
139 i->origin = pa_xstrndup(t+2, l-2);
140 else if (pa_startswith(t, "s="))
141 i->session_name = pa_xstrndup(t+2, l-2);
142 else if (pa_startswith(t, "c=IN IP4 ")) {
143 char a[64];
144 size_t k;
145
146 k = l-8 > sizeof(a) ? sizeof(a) : l-8;
147
148 pa_strlcpy(a, t+9, k);
149 a[strcspn(a, "/")] = 0;
150
151 if (inet_pton(AF_INET, a, &((struct sockaddr_in*) &i->sa)->sin_addr) <= 0) {
152 pa_log("Failed to parse SDP data: bad address: >%s<.", a);
153 goto fail;
154 }
155
156 ((struct sockaddr_in*) &i->sa)->sin_family = AF_INET;
157 ((struct sockaddr_in*) &i->sa)->sin_port = 0;
158 i->salen = sizeof(struct sockaddr_in);
159 #ifdef HAVE_IPV6
160 } else if (pa_startswith(t, "c=IN IP6 ")) {
161 char a[64];
162 size_t k;
163
164 k = l-8 > sizeof(a) ? sizeof(a) : l-8;
165
166 pa_strlcpy(a, t+9, k);
167 a[strcspn(a, "/")] = 0;
168
169 if (inet_pton(AF_INET6, a, &((struct sockaddr_in6*) &i->sa)->sin6_addr) <= 0) {
170 pa_log("Failed to parse SDP data: bad address: >%s<.", a);
171 goto fail;
172 }
173
174 ((struct sockaddr_in6*) &i->sa)->sin6_family = AF_INET6;
175 ((struct sockaddr_in6*) &i->sa)->sin6_port = 0;
176 i->salen = sizeof(struct sockaddr_in6);
177 #endif
178 } else if (pa_startswith(t, "m=audio ")) {
179
180 if (i->payload > 127) {
181 int _port, _payload;
182
183 if (sscanf(t+8, "%i RTP/AVP %i", &_port, &_payload) == 2) {
184
185 if (_port <= 0 || _port > 0xFFFF) {
186 pa_log("Failed to parse SDP data: invalid port %i.", _port);
187 goto fail;
188 }
189
190 if (_payload < 0 || _payload > 127) {
191 pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
192 goto fail;
193 }
194
195 port = (uint16_t) _port;
196 i->payload = (uint8_t) _payload;
197
198 if (pa_rtp_sample_spec_from_payload(i->payload, &i->sample_spec))
199 ss_valid = true;
200 }
201 }
202 } else if (pa_startswith(t, "a=rtpmap:")) {
203
204 if (i->payload <= 127) {
205 char c[64];
206 int _payload;
207 int len;
208
209 if (sscanf(t + 9, "%i %n", &_payload, &len) == 1) {
210 if (_payload < 0 || _payload > 127) {
211 pa_log("Failed to parse SDP data: invalid payload %i.", _payload);
212 goto fail;
213 }
214 if (_payload == i->payload) {
215 strncpy(c, t + 9 + len, 63);
216 c[63] = 0;
217 c[strcspn(c, "\n")] = 0;
218
219 if (parse_sdp_sample_spec(&i->sample_spec, c))
220 ss_valid = true;
221 }
222 }
223 }
224 }
225
226 t += l;
227
228 if (*t == '\n')
229 t++;
230 }
231
232 if (!i->origin || (!is_goodbye && (!i->salen || i->payload > 127 || !ss_valid || port == 0))) {
233 pa_log("Failed to parse SDP data: missing data.");
234 goto fail;
235 }
236
237 if (((struct sockaddr*) &i->sa)->sa_family == AF_INET)
238 ((struct sockaddr_in*) &i->sa)->sin_port = htons(port);
239 else
240 ((struct sockaddr_in6*) &i->sa)->sin6_port = htons(port);
241
242 return i;
243
244 fail:
245 pa_xfree(i->origin);
246 pa_xfree(i->session_name);
247
248 return NULL;
249 }
250
pa_sdp_info_destroy(pa_sdp_info * i)251 void pa_sdp_info_destroy(pa_sdp_info *i) {
252 pa_assert(i);
253
254 pa_xfree(i->origin);
255 pa_xfree(i->session_name);
256 }
257