]> code.delx.au - pulseaudio/blob - src/modules/rtp/sap.c
More fixes caused by Sun's complete inability to follow any standard
[pulseaudio] / src / modules / rtp / sap.c
1 /* $Id$ */
2
3 /***
4 This file is part of polypaudio.
5
6 polypaudio 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 of the License,
9 or (at your option) any later version.
10
11 polypaudio 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 polypaudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <assert.h>
27 #include <time.h>
28 #include <stdlib.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/ioctl.h>
36
37 #ifdef HAVE_SYS_FILIO_H
38 #include <sys/filio.h>
39 #endif
40
41 #include <polypcore/util.h>
42 #include <polypcore/log.h>
43 #include <polypcore/xmalloc.h>
44
45 #include "sap.h"
46 #include "sdp.h"
47
48 #define MIME_TYPE "application/sdp"
49
50 pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
51 assert(c);
52 assert(fd >= 0);
53 assert(sdp_data);
54
55 c->fd = fd;
56 c->sdp_data = sdp_data;
57 c->msg_id_hash = (uint16_t) (rand()*rand());
58
59 return c;
60 }
61
62 void pa_sap_context_destroy(pa_sap_context *c) {
63 assert(c);
64
65 close(c->fd);
66 pa_xfree(c->sdp_data);
67 }
68
69 int pa_sap_send(pa_sap_context *c, int goodbye) {
70 uint32_t header;
71 struct sockaddr_storage sa_buf;
72 struct sockaddr *sa = (struct sockaddr*) &sa_buf;
73 socklen_t salen = sizeof(sa_buf);
74 struct iovec iov[4];
75 struct msghdr m;
76 int k;
77
78 if (getsockname(c->fd, sa, &salen) < 0) {
79 pa_log("getsockname() failed: %s\n", strerror(errno));
80 return -1;
81 }
82
83 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
84
85 header = htonl(((uint32_t) 1 << 29) |
86 (sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
87 (goodbye ? (uint32_t) 1 << 26 : 0) |
88 (c->msg_id_hash));
89
90 iov[0].iov_base = &header;
91 iov[0].iov_len = sizeof(header);
92
93 iov[1].iov_base = sa->sa_family == AF_INET ? (void*) &((struct sockaddr_in*) sa)->sin_addr : (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
94 iov[1].iov_len = sa->sa_family == AF_INET ? 4 : 16;
95
96 iov[2].iov_base = (char*) MIME_TYPE;
97 iov[2].iov_len = sizeof(MIME_TYPE);
98
99 iov[3].iov_base = c->sdp_data;
100 iov[3].iov_len = strlen(c->sdp_data);
101
102 m.msg_name = NULL;
103 m.msg_namelen = 0;
104 m.msg_iov = iov;
105 m.msg_iovlen = 4;
106 m.msg_control = NULL;
107 m.msg_controllen = 0;
108 m.msg_flags = 0;
109
110 if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
111 pa_log("sendmsg() failed: %s\n", strerror(errno));
112
113 return k;
114 }
115
116 pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
117 assert(c);
118 assert(fd >= 0);
119
120 c->fd = fd;
121 c->sdp_data = NULL;
122 return c;
123 }
124
125 int pa_sap_recv(pa_sap_context *c, int *goodbye) {
126 struct msghdr m;
127 struct iovec iov;
128 int size, k;
129 char *buf = NULL, *e;
130 uint32_t header;
131 int six, ac;
132 ssize_t r;
133
134 assert(c);
135 assert(goodbye);
136
137 if (ioctl(c->fd, FIONREAD, &size) < 0) {
138 pa_log(__FILE__": FIONREAD failed: %s", strerror(errno));
139 goto fail;
140 }
141
142 if (!size)
143 return 0;
144
145 buf = pa_xnew(char, size+1);
146 buf[size] = 0;
147
148 iov.iov_base = buf;
149 iov.iov_len = size;
150
151 m.msg_name = NULL;
152 m.msg_namelen = 0;
153 m.msg_iov = &iov;
154 m.msg_iovlen = 1;
155 m.msg_control = NULL;
156 m.msg_controllen = 0;
157 m.msg_flags = 0;
158
159 if ((r = recvmsg(c->fd, &m, 0)) != size) {
160 pa_log(__FILE__": recvmsg() failed: %s", r < 0 ? strerror(errno) : "size mismatch");
161 goto fail;
162 }
163
164 if (size < 4) {
165 pa_log(__FILE__": SAP packet too short.");
166 goto fail;
167 }
168
169 memcpy(&header, buf, sizeof(uint32_t));
170 header = ntohl(header);
171
172 if (header >> 29 != 1) {
173 pa_log(__FILE__": Unsupported SAP version.");
174 goto fail;
175 }
176
177 if ((header >> 25) & 1) {
178 pa_log(__FILE__": Encrypted SAP not supported.");
179 goto fail;
180 }
181
182 if ((header >> 24) & 1) {
183 pa_log(__FILE__": Compressed SAP not supported.");
184 goto fail;
185 }
186
187 six = (header >> 28) & 1;
188 ac = (header >> 16) & 0xFF;
189
190 k = 4 + (six ? 16 : 4) + ac*4;
191 if (size < k) {
192 pa_log(__FILE__": SAP packet too short (AD).");
193 goto fail;
194 }
195
196 e = buf + k;
197 size -= k;
198
199 if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
200 e += sizeof(MIME_TYPE);
201 size -= sizeof(MIME_TYPE);
202 } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
203 pa_log(__FILE__": Invalid SDP header.");
204 goto fail;
205 }
206
207 if (c->sdp_data)
208 pa_xfree(c->sdp_data);
209
210 c->sdp_data = pa_xstrndup(e, size);
211 pa_xfree(buf);
212
213 *goodbye = !!((header >> 26) & 1);
214
215 return 0;
216
217 fail:
218 pa_xfree(buf);
219
220 return -1;
221 }