merge glitch-free branch back into trunk
[profile/ivi/pulseaudio.git] / src / modules / rtp / sap.c
1 /* $Id$ */
2
3 /***
4   This file is part of PulseAudio.
5
6   Copyright 2006 Lennart Poettering
7
8   PulseAudio is free software; you can redistribute it and/or modify
9   it under the terms of the GNU Lesser General Public License as published
10   by the Free Software Foundation; either version 2 of the License,
11   or (at your option) any later version.
12
13   PulseAudio is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with PulseAudio; if not, write to the Free Software
20   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
21   USA.
22 ***/
23
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
27
28 #include <time.h>
29 #include <stdlib.h>
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
33 #include <arpa/inet.h>
34 #include <errno.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <sys/ioctl.h>
38
39 #ifdef HAVE_SYS_FILIO_H
40 #include <sys/filio.h>
41 #endif
42
43 #include <pulse/xmalloc.h>
44
45 #include <pulsecore/core-error.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/macro.h>
49
50 #include "sap.h"
51 #include "sdp.h"
52
53 #define MIME_TYPE "application/sdp"
54
55 pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
56     pa_assert(c);
57     pa_assert(fd >= 0);
58     pa_assert(sdp_data);
59
60     c->fd = fd;
61     c->sdp_data = sdp_data;
62     c->msg_id_hash = (uint16_t) (rand()*rand());
63
64     return c;
65 }
66
67 void pa_sap_context_destroy(pa_sap_context *c) {
68     pa_assert(c);
69
70     pa_close(c->fd);
71     pa_xfree(c->sdp_data);
72 }
73
74 int pa_sap_send(pa_sap_context *c, pa_bool_t goodbye) {
75     uint32_t header;
76     struct sockaddr_storage sa_buf;
77     struct sockaddr *sa = (struct sockaddr*) &sa_buf;
78     socklen_t salen = sizeof(sa_buf);
79     struct iovec iov[4];
80     struct msghdr m;
81     int k;
82
83     if (getsockname(c->fd, sa, &salen) < 0) {
84         pa_log("getsockname() failed: %s\n", pa_cstrerror(errno));
85         return -1;
86     }
87
88     pa_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
89
90     header = htonl(((uint32_t) 1 << 29) |
91                    (sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
92                    (goodbye ? (uint32_t) 1 << 26 : 0) |
93                    (c->msg_id_hash));
94
95     iov[0].iov_base = &header;
96     iov[0].iov_len = sizeof(header);
97
98     iov[1].iov_base = sa->sa_family == AF_INET ? (void*) &((struct sockaddr_in*) sa)->sin_addr : (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
99     iov[1].iov_len = sa->sa_family == AF_INET ? 4 : 16;
100
101     iov[2].iov_base = (char*) MIME_TYPE;
102     iov[2].iov_len = sizeof(MIME_TYPE);
103
104     iov[3].iov_base = c->sdp_data;
105     iov[3].iov_len = strlen(c->sdp_data);
106
107     m.msg_name = NULL;
108     m.msg_namelen = 0;
109     m.msg_iov = iov;
110     m.msg_iovlen = 4;
111     m.msg_control = NULL;
112     m.msg_controllen = 0;
113     m.msg_flags = 0;
114
115     if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
116         pa_log_warn("sendmsg() failed: %s\n", pa_cstrerror(errno));
117
118     return k;
119 }
120
121 pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
122     pa_assert(c);
123     pa_assert(fd >= 0);
124
125     c->fd = fd;
126     c->sdp_data = NULL;
127     return c;
128 }
129
130 int pa_sap_recv(pa_sap_context *c, pa_bool_t *goodbye) {
131     struct msghdr m;
132     struct iovec iov;
133     int size, k;
134     char *buf = NULL, *e;
135     uint32_t header;
136     int six, ac;
137     ssize_t r;
138
139     pa_assert(c);
140     pa_assert(goodbye);
141
142     if (ioctl(c->fd, FIONREAD, &size) < 0) {
143         pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
144         goto fail;
145     }
146
147     buf = pa_xnew(char, size+1);
148     buf[size] = 0;
149
150     iov.iov_base = buf;
151     iov.iov_len = size;
152
153     m.msg_name = NULL;
154     m.msg_namelen = 0;
155     m.msg_iov = &iov;
156     m.msg_iovlen = 1;
157     m.msg_control = NULL;
158     m.msg_controllen = 0;
159     m.msg_flags = 0;
160
161     if ((r = recvmsg(c->fd, &m, 0)) != size) {
162         pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
163         goto fail;
164     }
165
166     if (size < 4) {
167         pa_log_warn("SAP packet too short.");
168         goto fail;
169     }
170
171     memcpy(&header, buf, sizeof(uint32_t));
172     header = ntohl(header);
173
174     if (header >> 29 != 1) {
175         pa_log_warn("Unsupported SAP version.");
176         goto fail;
177     }
178
179     if ((header >> 25) & 1) {
180         pa_log_warn("Encrypted SAP not supported.");
181         goto fail;
182     }
183
184     if ((header >> 24) & 1) {
185         pa_log_warn("Compressed SAP not supported.");
186         goto fail;
187     }
188
189     six = (header >> 28) & 1;
190     ac = (header >> 16) & 0xFF;
191
192     k = 4 + (six ? 16 : 4) + ac*4;
193     if (size < k) {
194         pa_log_warn("SAP packet too short (AD).");
195         goto fail;
196     }
197
198     e = buf + k;
199     size -= k;
200
201     if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
202         e += sizeof(MIME_TYPE);
203         size -= sizeof(MIME_TYPE);
204     } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
205         pa_log_warn("Invalid SDP header.");
206         goto fail;
207     }
208
209     if (c->sdp_data)
210         pa_xfree(c->sdp_data);
211
212     c->sdp_data = pa_xstrndup(e, size);
213     pa_xfree(buf);
214
215     *goodbye = !!((header >> 26) & 1);
216
217     return 0;
218
219 fail:
220     pa_xfree(buf);
221
222     return -1;
223 }