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
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
33 #include <sys/ioctl.h>
35 #ifdef HAVE_SYS_FILIO_H
36 #include <sys/filio.h>
43 #include <pulse/xmalloc.h>
45 #include <pulsecore/core-error.h>
46 #include <pulsecore/core-util.h>
47 #include <pulsecore/log.h>
48 #include <pulsecore/macro.h>
49 #include <pulsecore/arpa-inet.h>
54 #define MIME_TYPE "application/sdp"
56 pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
62 c->sdp_data = sdp_data;
63 c->msg_id_hash = (uint16_t) (rand()*rand());
68 void pa_sap_context_destroy(pa_sap_context *c) {
72 pa_xfree(c->sdp_data);
75 int pa_sap_send(pa_sap_context *c, pa_bool_t goodbye) {
77 struct sockaddr_storage sa_buf;
78 struct sockaddr *sa = (struct sockaddr*) &sa_buf;
79 socklen_t salen = sizeof(sa_buf);
84 if (getsockname(c->fd, sa, &salen) < 0) {
85 pa_log("getsockname() failed: %s\n", pa_cstrerror(errno));
90 pa_assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
92 pa_assert(sa->sa_family == AF_INET);
95 header = htonl(((uint32_t) 1 << 29) |
97 (sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
99 (goodbye ? (uint32_t) 1 << 26 : 0) |
102 iov[0].iov_base = &header;
103 iov[0].iov_len = sizeof(header);
105 if (sa->sa_family == AF_INET) {
106 iov[1].iov_base = (void*) &((struct sockaddr_in*) sa)->sin_addr;
110 iov[1].iov_base = (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
111 iov[1].iov_len = 16U;
115 iov[2].iov_base = (char*) MIME_TYPE;
116 iov[2].iov_len = sizeof(MIME_TYPE);
118 iov[3].iov_base = c->sdp_data;
119 iov[3].iov_len = strlen(c->sdp_data);
125 m.msg_control = NULL;
126 m.msg_controllen = 0;
129 if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
130 pa_log_warn("sendmsg() failed: %s\n", pa_cstrerror(errno));
135 pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
144 int pa_sap_recv(pa_sap_context *c, pa_bool_t *goodbye) {
148 char *buf = NULL, *e;
156 if (ioctl(c->fd, FIONREAD, &size) < 0) {
157 pa_log_warn("FIONREAD failed: %s", pa_cstrerror(errno));
161 buf = pa_xnew(char, (unsigned) size+1);
165 iov.iov_len = (size_t) size;
171 m.msg_control = NULL;
172 m.msg_controllen = 0;
175 if ((r = recvmsg(c->fd, &m, 0)) != size) {
176 pa_log_warn("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
181 pa_log_warn("SAP packet too short.");
185 memcpy(&header, buf, sizeof(uint32_t));
186 header = ntohl(header);
188 if (header >> 29 != 1) {
189 pa_log_warn("Unsupported SAP version.");
193 if ((header >> 25) & 1) {
194 pa_log_warn("Encrypted SAP not supported.");
198 if ((header >> 24) & 1) {
199 pa_log_warn("Compressed SAP not supported.");
203 six = (header >> 28) & 1U;
204 ac = (header >> 16) & 0xFFU;
206 k = 4 + (six ? 16U : 4U) + ac*4U;
207 if ((unsigned) size < k) {
208 pa_log_warn("SAP packet too short (AD).");
215 if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
216 e += sizeof(MIME_TYPE);
217 size -= (int) sizeof(MIME_TYPE);
218 } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
219 pa_log_warn("Invalid SDP header.");
224 pa_xfree(c->sdp_data);
226 c->sdp_data = pa_xstrndup(e, (unsigned) size);
229 *goodbye = !!((header >> 26) & 1);