]>
code.delx.au - pulseaudio/blob - src/modules/rtp/sap.c
4 This file is part of polypaudio.
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.
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.
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
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <arpa/inet.h>
35 #include <sys/ioctl.h>
37 #ifdef HAVE_SYS_FILIO_H
38 #include <sys/filio.h>
41 #include <polyp/error.h>
42 #include <polyp/xmalloc.h>
44 #include <polypcore/core-util.h>
45 #include <polypcore/log.h>
50 #define MIME_TYPE "application/sdp"
52 pa_sap_context
* pa_sap_context_init_send(pa_sap_context
*c
, int fd
, char *sdp_data
) {
58 c
->sdp_data
= sdp_data
;
59 c
->msg_id_hash
= (uint16_t) (rand()*rand());
64 void pa_sap_context_destroy(pa_sap_context
*c
) {
68 pa_xfree(c
->sdp_data
);
71 int pa_sap_send(pa_sap_context
*c
, int goodbye
) {
73 struct sockaddr_storage sa_buf
;
74 struct sockaddr
*sa
= (struct sockaddr
*) &sa_buf
;
75 socklen_t salen
= sizeof(sa_buf
);
80 if (getsockname(c
->fd
, sa
, &salen
) < 0) {
81 pa_log("getsockname() failed: %s\n", pa_cstrerror(errno
));
85 assert(sa
->sa_family
== AF_INET
|| sa
->sa_family
== AF_INET6
);
87 header
= htonl(((uint32_t) 1 << 29) |
88 (sa
->sa_family
== AF_INET6
? (uint32_t) 1 << 28 : 0) |
89 (goodbye
? (uint32_t) 1 << 26 : 0) |
92 iov
[0].iov_base
= &header
;
93 iov
[0].iov_len
= sizeof(header
);
95 iov
[1].iov_base
= sa
->sa_family
== AF_INET
? (void*) &((struct sockaddr_in
*) sa
)->sin_addr
: (void*) &((struct sockaddr_in6
*) sa
)->sin6_addr
;
96 iov
[1].iov_len
= sa
->sa_family
== AF_INET
? 4 : 16;
98 iov
[2].iov_base
= (char*) MIME_TYPE
;
99 iov
[2].iov_len
= sizeof(MIME_TYPE
);
101 iov
[3].iov_base
= c
->sdp_data
;
102 iov
[3].iov_len
= strlen(c
->sdp_data
);
108 m
.msg_control
= NULL
;
109 m
.msg_controllen
= 0;
112 if ((k
= sendmsg(c
->fd
, &m
, MSG_DONTWAIT
)) < 0)
113 pa_log("sendmsg() failed: %s\n", pa_cstrerror(errno
));
118 pa_sap_context
* pa_sap_context_init_recv(pa_sap_context
*c
, int fd
) {
127 int pa_sap_recv(pa_sap_context
*c
, int *goodbye
) {
131 char *buf
= NULL
, *e
;
139 if (ioctl(c
->fd
, FIONREAD
, &size
) < 0) {
140 pa_log(__FILE__
": FIONREAD failed: %s", pa_cstrerror(errno
));
147 buf
= pa_xnew(char, size
+1);
157 m
.msg_control
= NULL
;
158 m
.msg_controllen
= 0;
161 if ((r
= recvmsg(c
->fd
, &m
, 0)) != size
) {
162 pa_log(__FILE__
": recvmsg() failed: %s", r
< 0 ? pa_cstrerror(errno
) : "size mismatch");
167 pa_log(__FILE__
": SAP packet too short.");
171 memcpy(&header
, buf
, sizeof(uint32_t));
172 header
= ntohl(header
);
174 if (header
>> 29 != 1) {
175 pa_log(__FILE__
": Unsupported SAP version.");
179 if ((header
>> 25) & 1) {
180 pa_log(__FILE__
": Encrypted SAP not supported.");
184 if ((header
>> 24) & 1) {
185 pa_log(__FILE__
": Compressed SAP not supported.");
189 six
= (header
>> 28) & 1;
190 ac
= (header
>> 16) & 0xFF;
192 k
= 4 + (six
? 16 : 4) + ac
*4;
194 pa_log(__FILE__
": SAP packet too short (AD).");
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(__FILE__
": Invalid SDP header.");
210 pa_xfree(c
->sdp_data
);
212 c
->sdp_data
= pa_xstrndup(e
, size
);
215 *goodbye
= !!((header
>> 26) & 1);