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