Use LGPL 2.1 on all files previously using LGPL 2
[pulseaudio-mirror.git] / src / modules / rtp / sap.c
blobadde16df2708db564e3097c86b3332bd7563454a
1 /***
2 This file is part of PulseAudio.
4 Copyright 2006 Lennart Poettering
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.
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.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <time.h>
27 #include <stdlib.h>
28 #include <sys/types.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
32 #include <errno.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_FILIO_H
38 #include <sys/filio.h>
39 #endif
41 #ifdef HAVE_SYS_UIO_H
42 #include <sys/uio.h>
43 #endif
45 #include <pulse/xmalloc.h>
47 #include <pulsecore/core-error.h>
48 #include <pulsecore/core-util.h>
49 #include <pulsecore/log.h>
50 #include <pulsecore/macro.h>
52 #include "sap.h"
53 #include "sdp.h"
55 #define MIME_TYPE "application/sdp"
57 pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
58 pa_assert(c);
59 pa_assert(fd >= 0);
60 pa_assert(sdp_data);
62 c->fd = fd;
63 c->sdp_data = sdp_data;
64 c->msg_id_hash = (uint16_t) (rand()*rand());
66 return c;
69 void pa_sap_context_destroy(pa_sap_context *c) {
70 pa_assert(c);
72 pa_close(c->fd);
73 pa_xfree(c->sdp_data);
76 int pa_sap_send(pa_sap_context *c, pa_bool_t goodbye) {
77 uint32_t header;
78 struct sockaddr_storage sa_buf;
79 struct sockaddr *sa = (struct sockaddr*) &sa_buf;
80 socklen_t salen = sizeof(sa_buf);
81 struct iovec iov[4];
82 struct msghdr m;
83 ssize_t k;
85 if (getsockname(c->fd, sa, &salen) < 0) {
86 pa_log("getsockname() failed: %s\n", pa_cstrerror(errno));
87 return -1;
90 #ifdef HAVE_IPV6
91 pa_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
92 #else
93 pa_assert(sa->sa_family == AF_INET);
94 #endif
96 header = htonl(((uint32_t) 1 << 29) |
97 #ifdef HAVE_IPV6
98 (sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
99 #endif
100 (goodbye ? (uint32_t) 1 << 26 : 0) |
101 (c->msg_id_hash));
103 iov[0].iov_base = &header;
104 iov[0].iov_len = sizeof(header);
106 if (sa->sa_family == AF_INET) {
107 iov[1].iov_base = (void*) &((struct sockaddr_in*) sa)->sin_addr;
108 iov[1].iov_len = 4U;
109 #ifdef HAVE_IPV6
110 } else {
111 iov[1].iov_base = (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
112 iov[1].iov_len = 16U;
113 #endif
116 iov[2].iov_base = (char*) MIME_TYPE;
117 iov[2].iov_len = sizeof(MIME_TYPE);
119 iov[3].iov_base = c->sdp_data;
120 iov[3].iov_len = strlen(c->sdp_data);
122 m.msg_name = NULL;
123 m.msg_namelen = 0;
124 m.msg_iov = iov;
125 m.msg_iovlen = 4;
126 m.msg_control = NULL;
127 m.msg_controllen = 0;
128 m.msg_flags = 0;
130 if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
131 pa_log_warn("sendmsg() failed: %s\n", pa_cstrerror(errno));
133 return (int) k;
136 pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
137 pa_assert(c);
138 pa_assert(fd >= 0);
140 c->fd = fd;
141 c->sdp_data = NULL;
142 return c;
145 int pa_sap_recv(pa_sap_context *c, pa_bool_t *goodbye) {
146 struct msghdr m;
147 struct iovec iov;
148 int size;
149 char *buf = NULL, *e;
150 uint32_t header;
151 unsigned six, ac, k;
152 ssize_t r;
154 pa_assert(c);
155 pa_assert(goodbye);
157 if (ioctl(c->fd, FIONREAD, &size) < 0) {
158 pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
159 goto fail;
162 buf = pa_xnew(char, (unsigned) size+1);
163 buf[size] = 0;
165 iov.iov_base = buf;
166 iov.iov_len = (size_t) size;
168 m.msg_name = NULL;
169 m.msg_namelen = 0;
170 m.msg_iov = &iov;
171 m.msg_iovlen = 1;
172 m.msg_control = NULL;
173 m.msg_controllen = 0;
174 m.msg_flags = 0;
176 if ((r = recvmsg(c->fd, &m, 0)) != size) {
177 pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
178 goto fail;
181 if (size < 4) {
182 pa_log_warn("SAP packet too short.");
183 goto fail;
186 memcpy(&header, buf, sizeof(uint32_t));
187 header = ntohl(header);
189 if (header >> 29 != 1) {
190 pa_log_warn("Unsupported SAP version.");
191 goto fail;
194 if ((header >> 25) & 1) {
195 pa_log_warn("Encrypted SAP not supported.");
196 goto fail;
199 if ((header >> 24) & 1) {
200 pa_log_warn("Compressed SAP not supported.");
201 goto fail;
204 six = (header >> 28) & 1U;
205 ac = (header >> 16) & 0xFFU;
207 k = 4 + (six ? 16U : 4U) + ac*4U;
208 if ((unsigned) size < k) {
209 pa_log_warn("SAP packet too short (AD).");
210 goto fail;
213 e = buf + k;
214 size -= (int) k;
216 if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
217 e += sizeof(MIME_TYPE);
218 size -= (int) sizeof(MIME_TYPE);
219 } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
220 pa_log_warn("Invalid SDP header.");
221 goto fail;
224 if (c->sdp_data)
225 pa_xfree(c->sdp_data);
227 c->sdp_data = pa_xstrndup(e, (unsigned) size);
228 pa_xfree(buf);
230 *goodbye = !!((header >> 26) & 1);
232 return 0;
234 fail:
235 pa_xfree(buf);
237 return -1;