4 This file is part of PulseAudio.
6 Copyright 2006 Lennart Poettering
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.
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.
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
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
38 #include <sys/ioctl.h>
40 #ifdef HAVE_SYS_FILIO_H
41 #include <sys/filio.h>
44 #include <pulse/xmalloc.h>
46 #include <pulsecore/core-error.h>
47 #include <pulsecore/core-util.h>
48 #include <pulsecore/log.h>
53 #define MIME_TYPE "application/sdp"
55 pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
61 c->sdp_data = sdp_data;
62 c->msg_id_hash = (uint16_t) (rand()*rand());
67 void pa_sap_context_destroy(pa_sap_context *c) {
71 pa_xfree(c->sdp_data);
74 int pa_sap_send(pa_sap_context *c, int goodbye) {
76 struct sockaddr_storage sa_buf;
77 struct sockaddr *sa = (struct sockaddr*) &sa_buf;
78 socklen_t salen = sizeof(sa_buf);
83 if (getsockname(c->fd, sa, &salen) < 0) {
84 pa_log("getsockname() failed: %s\n", pa_cstrerror(errno));
88 assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
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) |
95 iov[0].iov_base = &header;
96 iov[0].iov_len = sizeof(header);
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;
101 iov[2].iov_base = (char*) MIME_TYPE;
102 iov[2].iov_len = sizeof(MIME_TYPE);
104 iov[3].iov_base = c->sdp_data;
105 iov[3].iov_len = strlen(c->sdp_data);
111 m.msg_control = NULL;
112 m.msg_controllen = 0;
115 if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
116 pa_log("sendmsg() failed: %s\n", pa_cstrerror(errno));
121 pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
130 int pa_sap_recv(pa_sap_context *c, int *goodbye) {
134 char *buf = NULL, *e;
142 if (ioctl(c->fd, FIONREAD, &size) < 0) {
143 pa_log("FIONREAD failed: %s", pa_cstrerror(errno));
150 buf = pa_xnew(char, size+1);
160 m.msg_control = NULL;
161 m.msg_controllen = 0;
164 if ((r = recvmsg(c->fd, &m, 0)) != size) {
165 pa_log("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
170 pa_log("SAP packet too short.");
174 memcpy(&header, buf, sizeof(uint32_t));
175 header = ntohl(header);
177 if (header >> 29 != 1) {
178 pa_log("Unsupported SAP version.");
182 if ((header >> 25) & 1) {
183 pa_log("Encrypted SAP not supported.");
187 if ((header >> 24) & 1) {
188 pa_log("Compressed SAP not supported.");
192 six = (header >> 28) & 1;
193 ac = (header >> 16) & 0xFF;
195 k = 4 + (six ? 16 : 4) + ac*4;
197 pa_log("SAP packet too short (AD).");
204 if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
205 e += sizeof(MIME_TYPE);
206 size -= sizeof(MIME_TYPE);
207 } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
208 pa_log("Invalid SDP header.");
213 pa_xfree(c->sdp_data);
215 c->sdp_data = pa_xstrndup(e, size);
218 *goodbye = !!((header >> 26) & 1);