Debian Codegraph @

frontend/rtp.c

raw
1 /*
2 * rtp socket communication functions
3 *
4 * initially contributed by Felix von Leitner
5 *
6 * Copyright (c) 2000 Mark Taylor
7 * 2010 Robert Hegemann
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public
11 * License as published by the Free Software Foundation; either
12 * version 2 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Library General Public License for more details.
18 *
19 * You should have received a copy of the GNU Library General Public
20 * License along with this library; if not, write to the
21 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 * Boston, MA 02111-1307, USA.
23 */
24
25 /* $Id$ */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #ifdef HAVE_STDINT_H
32 # include <stdint.h>
33 #endif
34
35 struct rtpbits {
36 int sequence:16; /* sequence number: random */
37 int pt:7; /* payload type: 14 for MPEG audio */
38 int m:1; /* marker: 0 */
39 int cc:4; /* number of CSRC identifiers: 0 */
40 int x:1; /* number of extension headers: 0 */
41 int p:1; /* is there padding appended: 0 */
42 int v:2; /* version: 2 */
43 };
44
45 struct rtpheader { /* in network byte order */
46 struct rtpbits b;
47 int timestamp; /* start: random */
48 int ssrc; /* random */
49 int iAudioHeader; /* =0?! */
50 };
51
52
53 #if !defined( _WIN32 ) && !defined(__MINGW32__)
54
55 #ifdef STDC_HEADERS
56 # include <stdio.h>
57 # include <stdarg.h>
58 # include <stdlib.h>
59 # include <string.h>
60 #else
61 # ifndef HAVE_MEMCPY
62 # define memcpy(d, s, n) bcopy ((s), (d), (n))
63 # define memmove(d, s, n) bcopy ((s), (d), (n))
64 # endif
65 #endif
66
67 #ifdef HAVE_UNISTD_H
68 # include <unistd.h>
69 #endif
70
71 #include <sys/types.h>
72 #include <sys/socket.h>
73 #ifdef __int8_t_defined
74 #undef uint8_t
75 #undef uint16_t
76 #undef uint32_t
77 #undef uint64_t
78 #endif
79 #include <netinet/in.h>
80 #include <arpa/inet.h>
81
82 #ifdef WITH_DMALLOC
83 #include <dmalloc.h>
84 #endif
85
86 #include "rtp.h"
87 #include "console.h"
88
89 #ifdef IPV6
90 # include <netdb.h>
91 # define MAX_PORT_LENGTH 6
92 #endif
93
94 typedef int SOCKET;
95
96 struct rtpheader RTPheader;
97 SOCKET rtpsocket;
98
99
100 /* create a sender socket. */
101 int
102 rtp_socket(char const *address, unsigned int port, unsigned int TTL)
103 {
104 int iRet, iLoop = 1;
105 int iSocket = -1;
106 #ifdef IPV6
107 char cPortStr[MAX_PORT_LENGTH];
108 struct addrinfo hint, *multicastAddr = NULL;
109 int iTtl = TTL;
110 unsigned int iMulticastLoop = 0;
111 #else
112 struct sockaddr_in sin;
113 unsigned char cTtl = TTL;
114 char cLoop = 0;
115 unsigned int tempaddr;
116 #endif
117
118 if(port == 0 || port > 0xffff){
119 error_printf("rtp_socket() Invalid port number.\n");
120 goto err_cleanup;
121 }
122
123 #ifdef IPV6
124 snprintf(cPortStr,MAX_PORT_LENGTH,"%d",port);
125 memset(&hint, 0, sizeof(hint));
126 hint.ai_family = AF_UNSPEC;
127 hint.ai_socktype = SOCK_DGRAM;
128 iRet = getaddrinfo(address, cPortStr, &hint, &multicastAddr);
129 if (iRet){
130 error_printf("getaddrinfo() failed.\n");
131 goto err_cleanup;
132 }
133
134 iSocket = socket(multicastAddr->ai_family, multicastAddr->ai_socktype, 0);
135 if (iSocket < 0) {
136 error_printf("socket() failed.\n");
137 goto err_cleanup;
138 }
139
140 #else
141 iSocket = socket(AF_INET, SOCK_DGRAM, 0);
142 if (iSocket < 0) {
143 error_printf("socket() failed.\n");
144 goto err_cleanup;
145 }
146
147 memset(&sin, 0, sizeof(sin));
148 tempaddr = inet_addr(address);
149 sin.sin_family = AF_INET;
150 sin.sin_port = htons(port);
151 sin.sin_addr.s_addr = tempaddr;
152 #endif
153
154 iRet = setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, &iLoop, sizeof(int));
155 if (iRet < 0) {
156 error_printf("setsockopt SO_REUSEADDR failed\n");
157 close(iSocket);
158 goto err_cleanup;
159 }
160
161 #ifdef IPV6
162 if(((multicastAddr->ai_family == PF_INET6)
163 ?(((struct sockaddr_in6*)multicastAddr->ai_addr)->sin6_addr.s6_addr[0] == 0xFF)
164 :(((unsigned)ntohl(((struct sockaddr_in*)multicastAddr->ai_addr)->sin_addr.s_addr)) >> 28 == 0xE)))
165 {
166 /* only set multicast parameters for multicast destination IPs */
167 iRet = setsockopt(iSocket,
168 multicastAddr->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
169 multicastAddr->ai_family == PF_INET6 ? IPV6_MULTICAST_HOPS : IP_MULTICAST_TTL,
170 (char*) &iTtl, sizeof(iTtl));
171
172 if (iRet < 0) {
173 error_printf("setsockopt IPV6_MULTICAST_HOPS failed. multicast in kernel?\n");
174 goto err_cleanup;
175 }
176
177 iMulticastLoop = 1;
178 iRet = setsockopt(iSocket,
179 multicastAddr->ai_family == PF_INET6 ? IPPROTO_IPV6 : IPPROTO_IP,
180 multicastAddr->ai_family == PF_INET6 ? IPV6_MULTICAST_LOOP : IP_MULTICAST_LOOP,
181 (char*) &iMulticastLoop, sizeof(iMulticastLoop));
182
183 if (iRet < 0) {
184 error_printf("setsockopt IPV6_MULTICAST_LOOP failed. multicast in kernel?\n");
185 goto err_cleanup;
186 }
187 }
188 iRet = connect(iSocket, multicastAddr->ai_addr, multicastAddr->ai_addrlen);
189 if (iRet < 0) {
190 error_printf("connect IPV6_MULTICAST_LOOP failed. multicast in kernel?\n");
191 goto err_cleanup;
192 }
193 freeaddrinfo(multicastAddr);
194 #else
195 if ((ntohl(tempaddr) >> 28) == 0xe) {
196 /* only set multicast parameters for multicast destination IPs */
197 iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_TTL, &cTtl, sizeof(char));
198 if (iRet < 0) {
199 error_printf("setsockopt IP_MULTICAST_TTL failed. multicast in kernel?\n");
200 close(iSocket);
201 goto err_cleanup;
202 }
203
204 cLoop = 1; /* !? */
205 iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &cLoop, sizeof(char));
206 if (iRet < 0) {
207 error_printf("setsockopt IP_MULTICAST_LOOP failed. multicast in kernel?\n");
208 close(iSocket);
209 goto err_cleanup;
210 }
211 }
212 iRet = connect(iSocket, (struct sockaddr *) &sin, sizeof(struct sockaddr_in));
213 if (iRet < 0) {
214 error_printf("connect IP_MULTICAST_LOOP failed. multicast in kernel?\n");
215 close(iSocket);
216 goto err_cleanup;
217 }
218 #endif
219
220 rtpsocket = iSocket;
221
222 return 0;
223
224 err_cleanup:
225 if(iSocket >= 0)
226 close(iSocket);
227 #ifdef IPV6
228 if(multicastAddr)
229 freeaddrinfo(multicastAddr);
230 #endif
231 return 1;
232 }
233
234
235 static void
236 rtp_initialization_extra(void)
237 {
238 }
239
240 static void
241 rtp_close_extra(void)
242 {
243 }
244
245 #else
246
247 #include <Winsock2.h>
248 #ifndef IP_MULTICAST_TTL
249 #define IP_MULTICAST_TTL 3
250 #endif
251 #include <stdio.h>
252 #include <stdarg.h>
253
254 #include "rtp.h"
255 #include "console.h"
256
257
258 struct rtpheader RTPheader;
259 SOCKET rtpsocket;
260
261 static char *
262 last_error_message(int err_code)
263 {
264 char *msg;
265 void *p_msg_buf;
266 FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
267 (void *) 0,
268 (DWORD) err_code,
269 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) & p_msg_buf, 0, NULL);
270 msg = strdup(p_msg_buf);
271 LocalFree(p_msg_buf);
272 return msg;
273 }
274
275 static int
276 print_socket_error(int error)
277 {
278 char *err_txt = last_error_message(error);
279 error_printf("error %d\n%s\n", error, err_txt);
280 free(err_txt);
281 return error;
282 }
283
284 static int
285 on_socket_error(SOCKET s)
286 {
287 int error = WSAGetLastError();
288 print_socket_error(error);
289 if (s != INVALID_SOCKET) {
290 closesocket(s);
291 }
292 return error;
293 }
294
295 /* create a sender socket. */
296 int
297 rtp_socket(char const *address, unsigned int port, unsigned int TTL)
298 {
299 char const True = 1;
300 char const *c = "";
301 int error;
302 UINT ip;
303 PHOSTENT host;
304 SOCKET s;
305 SOCKADDR_IN source, dest;
306
307 source.sin_family = AF_INET;
308 source.sin_addr.s_addr = htonl(INADDR_ANY);
309 source.sin_port = htons(0);
310
311 dest.sin_family = AF_INET;
312 dest.sin_addr.s_addr = inet_addr(address);
313
314 if (!strcmp(address, "255.255.255.255")) {
315 }
316 else if (dest.sin_addr.s_addr == INADDR_NONE) {
317 host = gethostbyname(address);
318
319 if (host) {
320 dest.sin_addr = *(PIN_ADDR) host->h_addr;
321 }
322 else {
323 error_printf("Unknown host %s\r\n", address);
324 return 1;
325 }
326 }
327
328 dest.sin_port = htons((u_short) port);
329
330 ip = ntohl(dest.sin_addr.s_addr);
331
332 if (IN_CLASSA(ip))
333 c = "class A";
334 if (IN_CLASSB(ip))
335 c = "class B";
336 if (IN_CLASSC(ip))
337 c = "class C";
338 if (IN_CLASSD(ip))
339 c = "class D";
340 if (ip == INADDR_LOOPBACK)
341 c = "loopback";
342 if (ip == INADDR_BROADCAST)
343 c = "broadcast";
344
345 s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
346 if (s == INVALID_SOCKET) {
347 error_printf("socket () ");
348 return on_socket_error(s);
349 }
350 error = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &True, sizeof(True));
351 error = bind(s, (struct sockaddr *) &source, sizeof(source));
352 if (error == SOCKET_ERROR) {
353 error_printf("bind () ");
354 return on_socket_error(s);
355 }
356 if (ip == INADDR_BROADCAST) {
357 error_printf("broadcast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
358 error = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &True, sizeof(True));
359 if (error == SOCKET_ERROR) {
360 error_printf("setsockopt (%u, SOL_SOCKET, SO_BROADCAST, ...) ", s);
361 return on_socket_error(s);
362 }
363 }
364 if (IN_CLASSD(ip)) {
365 error_printf("multicast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
366 error = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &TTL, sizeof(TTL));
367 if (error == SOCKET_ERROR) {
368 error_printf("setsockopt (%u, IPPROTO_IP, IP_MULTICAST_TTL, ...) ", s);
369 return on_socket_error(s);
370 }
371 }
372 error = connect(s, (PSOCKADDR) & dest, sizeof(SOCKADDR_IN));
373 if (error == SOCKET_ERROR) {
374 error_printf("connect: ");
375 return on_socket_error(s);
376 }
377 rtpsocket = s;
378 return 0;
379 }
380
381 static void
382 rtp_initialization_extra(void)
383 {
384 WSADATA wsaData;
385 int rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
386 if (rc != 0) {
387 print_socket_error(rc);
388 }
389 }
390
391 static void
392 rtp_close_extra(void)
393 {
394 WSACleanup();
395 }
396
397 #endif
398
399
400 static int
401 rtp_send(unsigned char const *data, int len)
402 {
403 SOCKET s = rtpsocket;
404 struct rtpheader *foo = &RTPheader;
405 char *buffer = malloc(len + sizeof(struct rtpheader));
406 int *cast = (int *) foo;
407 int *outcast = (int *) buffer;
408 int count, size;
409
410 outcast[0] = htonl(cast[0]);
411 outcast[1] = htonl(cast[1]);
412 outcast[2] = htonl(cast[2]);
413 outcast[3] = htonl(cast[3]);
414 memmove(buffer + sizeof(struct rtpheader), data, len);
415 size = len + sizeof(*foo);
416 count = send(s, buffer, size, 0);
417 free(buffer);
418
419 return count != size;
420 }
421
422 void
423 rtp_output(unsigned char const *mp3buffer, int mp3size)
424 {
425 rtp_send(mp3buffer, mp3size);
426 RTPheader.timestamp += 5;
427 RTPheader.b.sequence++;
428 }
429
430 void
431 rtp_initialization(void)
432 {
433 struct rtpheader *foo = &RTPheader;
434 foo->b.v = 2;
435 foo->b.p = 0;
436 foo->b.x = 0;
437 foo->b.cc = 0;
438 foo->b.m = 0;
439 foo->b.pt = 14; /* MPEG Audio */
440 foo->b.sequence = rand() & 65535;
441 foo->timestamp = rand();
442 foo->ssrc = rand();
443 foo->iAudioHeader = 0;
444 rtp_initialization_extra();
445 }
446
447 void
448 rtp_deinitialization(void)
449 {
450 rtp_close_extra();
451 }