~diwic/ubuntu/lucid/pulseaudio/bugfixes

« back to all changes in this revision

Viewing changes to src/modules/rtp/sap.c

  • Committer: Bazaar Package Importer
  • Author(s): Oliver Grawert
  • Date: 2006-11-12 20:00:18 UTC
  • Revision ID: james.westby@ubuntu.com-20061112200018-oji9njq7rr3te53k
Tags: upstream-0.9.5
ImportĀ upstreamĀ versionĀ 0.9.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: sap.c 1272 2006-08-18 21:38:40Z lennart $ */
 
2
 
 
3
/***
 
4
  This file is part of PulseAudio.
 
5
 
 
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 of the License,
 
9
  or (at your option) any later version.
 
10
 
 
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.
 
15
 
 
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
***/
 
21
 
 
22
#ifdef HAVE_CONFIG_H
 
23
#include <config.h>
 
24
#endif
 
25
 
 
26
#include <assert.h>
 
27
#include <time.h>
 
28
#include <stdlib.h>
 
29
#include <sys/types.h>
 
30
#include <sys/socket.h>
 
31
#include <netinet/in.h>
 
32
#include <arpa/inet.h>
 
33
#include <errno.h>
 
34
#include <string.h>
 
35
#include <unistd.h>
 
36
#include <sys/ioctl.h>
 
37
 
 
38
#ifdef HAVE_SYS_FILIO_H
 
39
#include <sys/filio.h>
 
40
#endif
 
41
 
 
42
#include <pulse/xmalloc.h>
 
43
 
 
44
#include <pulsecore/core-error.h>
 
45
#include <pulsecore/core-util.h>
 
46
#include <pulsecore/log.h>
 
47
 
 
48
#include "sap.h"
 
49
#include "sdp.h"
 
50
 
 
51
#define MIME_TYPE "application/sdp"
 
52
 
 
53
pa_sap_context* pa_sap_context_init_send(pa_sap_context *c, int fd, char *sdp_data) {
 
54
    assert(c);
 
55
    assert(fd >= 0);
 
56
    assert(sdp_data);
 
57
 
 
58
    c->fd = fd;
 
59
    c->sdp_data = sdp_data;
 
60
    c->msg_id_hash = (uint16_t) (rand()*rand());
 
61
    
 
62
    return c;    
 
63
}
 
64
 
 
65
void pa_sap_context_destroy(pa_sap_context *c) {
 
66
    assert(c);
 
67
 
 
68
    close(c->fd);
 
69
    pa_xfree(c->sdp_data);
 
70
}
 
71
 
 
72
int pa_sap_send(pa_sap_context *c, int goodbye) {
 
73
    uint32_t header;
 
74
    struct sockaddr_storage sa_buf;
 
75
    struct sockaddr *sa = (struct sockaddr*) &sa_buf;
 
76
    socklen_t salen = sizeof(sa_buf);
 
77
    struct iovec iov[4];
 
78
    struct msghdr m;
 
79
    int k;
 
80
 
 
81
    if (getsockname(c->fd, sa, &salen) < 0) {
 
82
        pa_log("getsockname() failed: %s\n", pa_cstrerror(errno));
 
83
        return -1;
 
84
    }
 
85
 
 
86
    assert(sa->sa_family == AF_INET || sa->sa_family == AF_INET6);
 
87
    
 
88
    header = htonl(((uint32_t) 1 << 29) |
 
89
                   (sa->sa_family == AF_INET6 ? (uint32_t) 1 << 28 : 0) |
 
90
                   (goodbye ? (uint32_t) 1 << 26 : 0) |
 
91
                   (c->msg_id_hash));
 
92
 
 
93
    iov[0].iov_base = &header;
 
94
    iov[0].iov_len = sizeof(header);
 
95
 
 
96
    iov[1].iov_base = sa->sa_family == AF_INET ? (void*) &((struct sockaddr_in*) sa)->sin_addr : (void*) &((struct sockaddr_in6*) sa)->sin6_addr;
 
97
    iov[1].iov_len = sa->sa_family == AF_INET ? 4 : 16;
 
98
 
 
99
    iov[2].iov_base = (char*) MIME_TYPE;
 
100
    iov[2].iov_len = sizeof(MIME_TYPE);
 
101
 
 
102
    iov[3].iov_base = c->sdp_data;
 
103
    iov[3].iov_len = strlen(c->sdp_data);
 
104
                   
 
105
    m.msg_name = NULL;
 
106
    m.msg_namelen = 0;
 
107
    m.msg_iov = iov;
 
108
    m.msg_iovlen = 4;
 
109
    m.msg_control = NULL;
 
110
    m.msg_controllen = 0;
 
111
    m.msg_flags = 0;
 
112
    
 
113
    if ((k = sendmsg(c->fd, &m, MSG_DONTWAIT)) < 0)
 
114
        pa_log("sendmsg() failed: %s\n", pa_cstrerror(errno));
 
115
 
 
116
    return k;
 
117
}
 
118
 
 
119
pa_sap_context* pa_sap_context_init_recv(pa_sap_context *c, int fd) {
 
120
    assert(c);
 
121
    assert(fd >= 0);
 
122
 
 
123
    c->fd = fd;
 
124
    c->sdp_data = NULL;
 
125
    return c;
 
126
}
 
127
 
 
128
int pa_sap_recv(pa_sap_context *c, int *goodbye) {
 
129
    struct msghdr m;
 
130
    struct iovec iov;
 
131
    int size, k;
 
132
    char *buf = NULL, *e;
 
133
    uint32_t header;
 
134
    int six, ac;
 
135
    ssize_t r;
 
136
    
 
137
    assert(c);
 
138
    assert(goodbye);
 
139
 
 
140
    if (ioctl(c->fd, FIONREAD, &size) < 0) {
 
141
        pa_log("FIONREAD failed: %s", pa_cstrerror(errno));
 
142
        goto fail;
 
143
    }
 
144
 
 
145
    if (!size)
 
146
        return 0;
 
147
 
 
148
    buf = pa_xnew(char, size+1);
 
149
    buf[size] = 0;
 
150
    
 
151
    iov.iov_base = buf;
 
152
    iov.iov_len = size;
 
153
 
 
154
    m.msg_name = NULL;
 
155
    m.msg_namelen = 0;
 
156
    m.msg_iov = &iov;
 
157
    m.msg_iovlen = 1;
 
158
    m.msg_control = NULL;
 
159
    m.msg_controllen = 0;
 
160
    m.msg_flags = 0;
 
161
    
 
162
    if ((r = recvmsg(c->fd, &m, 0)) != size) {
 
163
        pa_log("recvmsg() failed: %s", r < 0 ? pa_cstrerror(errno) : "size mismatch");
 
164
        goto fail;
 
165
    }
 
166
 
 
167
    if (size < 4) {
 
168
        pa_log("SAP packet too short.");
 
169
        goto fail;
 
170
    }
 
171
 
 
172
    memcpy(&header, buf, sizeof(uint32_t));
 
173
    header = ntohl(header);
 
174
 
 
175
    if (header >> 29 != 1) {
 
176
        pa_log("Unsupported SAP version.");
 
177
        goto fail;
 
178
    }
 
179
 
 
180
    if ((header >> 25) & 1) {
 
181
        pa_log("Encrypted SAP not supported.");
 
182
        goto fail;
 
183
    }
 
184
 
 
185
    if ((header >> 24) & 1) {
 
186
        pa_log("Compressed SAP not supported.");
 
187
        goto fail;
 
188
    }
 
189
 
 
190
    six = (header >> 28) & 1;
 
191
    ac = (header >> 16) & 0xFF;
 
192
 
 
193
    k = 4 + (six ? 16 : 4) + ac*4;
 
194
    if (size < k) {
 
195
        pa_log("SAP packet too short (AD).");
 
196
        goto fail;
 
197
    }
 
198
 
 
199
    e = buf + k;
 
200
    size -= k;
 
201
 
 
202
    if ((unsigned) size >= sizeof(MIME_TYPE) && !strcmp(e, MIME_TYPE)) {
 
203
        e += sizeof(MIME_TYPE);
 
204
        size -= sizeof(MIME_TYPE);
 
205
    } else if ((unsigned) size < sizeof(PA_SDP_HEADER)-1 || strncmp(e, PA_SDP_HEADER, sizeof(PA_SDP_HEADER)-1)) {
 
206
        pa_log("Invalid SDP header.");
 
207
        goto fail;
 
208
    }
 
209
 
 
210
    if (c->sdp_data)
 
211
        pa_xfree(c->sdp_data);
 
212
    
 
213
    c->sdp_data = pa_xstrndup(e, size);
 
214
    pa_xfree(buf);
 
215
    
 
216
    *goodbye = !!((header >> 26) & 1);
 
217
    
 
218
    return 0;
 
219
 
 
220
fail:
 
221
    pa_xfree(buf);
 
222
 
 
223
    return -1;
 
224
}