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: rtp.c,v 1.24 2011/05/07 16:05:17 rbrito Exp $ */
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 typedef int SOCKET;
90 
91 struct rtpheader RTPheader;
92 SOCKET  rtpsocket;
93 
94 
95 /* create a sender socket. */
96 int
rtp_socket(char const * address,unsigned int port,unsigned int TTL)97 rtp_socket(char const *address, unsigned int port, unsigned int TTL)
98 {
99     int     iRet, iLoop = 1;
100     struct sockaddr_in sin;
101     unsigned char cTtl = TTL;
102     char    cLoop = 0;
103     unsigned int tempaddr;
104 
105     int     iSocket = socket(AF_INET, SOCK_DGRAM, 0);
106     if (iSocket < 0) {
107         error_printf("socket() failed.\n");
108         return 1;
109     }
110 
111     memset(&sin, 0, sizeof(sin));
112     tempaddr = inet_addr(address);
113     sin.sin_family = AF_INET;
114     sin.sin_port = htons(port);
115     sin.sin_addr.s_addr = tempaddr;
116 
117     iRet = setsockopt(iSocket, SOL_SOCKET, SO_REUSEADDR, &iLoop, sizeof(int));
118     if (iRet < 0) {
119         error_printf("setsockopt SO_REUSEADDR failed\n");
120         return 1;
121     }
122 
123     if ((ntohl(tempaddr) >> 28) == 0xe) {
124         /* only set multicast parameters for multicast destination IPs */
125         iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_TTL, &cTtl, sizeof(char));
126         if (iRet < 0) {
127             error_printf("setsockopt IP_MULTICAST_TTL failed.  multicast in kernel?\n");
128             return 1;
129         }
130 
131         cLoop = 1;      /* !? */
132         iRet = setsockopt(iSocket, IPPROTO_IP, IP_MULTICAST_LOOP, &cLoop, sizeof(char));
133         if (iRet < 0) {
134             error_printf("setsockopt IP_MULTICAST_LOOP failed.  multicast in kernel?\n");
135             return 1;
136         }
137     }
138     iRet = connect(iSocket, (struct sockaddr *) &sin, sizeof(struct sockaddr_in));
139     if (iRet < 0) {
140         error_printf("connect IP_MULTICAST_LOOP failed.  multicast in kernel?\n");
141         return 1;
142     }
143 
144     rtpsocket = iSocket;
145 
146     return 0;
147 }
148 
149 
150 static void
rtp_initialization_extra(void)151 rtp_initialization_extra(void)
152 {
153 }
154 
155 static void
rtp_close_extra(void)156 rtp_close_extra(void)
157 {
158 }
159 
160 #else
161 
162 #include <Winsock2.h>
163 #ifndef IP_MULTICAST_TTL
164 #define IP_MULTICAST_TTL 3
165 #endif
166 #include <stdio.h>
167 #include <stdarg.h>
168 
169 #include "rtp.h"
170 #include "console.h"
171 
172 
173 struct rtpheader RTPheader;
174 SOCKET  rtpsocket;
175 
176 static char *
last_error_message(int err_code)177 last_error_message(int err_code)
178 {
179     char   *msg;
180     void   *p_msg_buf;
181     FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER,
182                    (void *) 0,
183                    (DWORD) err_code,
184                    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR) & p_msg_buf, 0, NULL);
185     msg = strdup(p_msg_buf);
186     LocalFree(p_msg_buf);
187     return msg;
188 }
189 
190 static int
print_socket_error(int error)191 print_socket_error(int error)
192 {
193     char   *err_txt = last_error_message(error);
194     error_printf("error %d\n%s\n", error, err_txt);
195     free(err_txt);
196     return error;
197 }
198 
199 static int
on_socket_error(SOCKET s)200 on_socket_error(SOCKET s)
201 {
202     int     error = WSAGetLastError();
203     print_socket_error(error);
204     if (s != INVALID_SOCKET) {
205         closesocket(s);
206     }
207     return error;
208 }
209 
210 /* create a sender socket. */
211 int
rtp_socket(char const * address,unsigned int port,unsigned int TTL)212 rtp_socket(char const *address, unsigned int port, unsigned int TTL)
213 {
214     char const True = 1;
215     char const *c = "";
216     int     error;
217     UINT    ip;
218     PHOSTENT host;
219     SOCKET  s;
220     SOCKADDR_IN source, dest;
221 
222     source.sin_family = AF_INET;
223     source.sin_addr.s_addr = htonl(INADDR_ANY);
224     source.sin_port = htons(0);
225 
226     dest.sin_family = AF_INET;
227     dest.sin_addr.s_addr = inet_addr(address);
228 
229     if (!strcmp(address, "255.255.255.255")) {
230     }
231     else if (dest.sin_addr.s_addr == INADDR_NONE) {
232         host = gethostbyname(address);
233 
234         if (host) {
235             dest.sin_addr = *(PIN_ADDR) host->h_addr;
236         }
237         else {
238             error_printf("Unknown host %s\r\n", address);
239             return 1;
240         }
241     }
242 
243     dest.sin_port = htons((u_short) port);
244 
245     ip = ntohl(dest.sin_addr.s_addr);
246 
247     if (IN_CLASSA(ip))
248         c = "class A";
249     if (IN_CLASSB(ip))
250         c = "class B";
251     if (IN_CLASSC(ip))
252         c = "class C";
253     if (IN_CLASSD(ip))
254         c = "class D";
255     if (ip == INADDR_LOOPBACK)
256         c = "loopback";
257     if (ip == INADDR_BROADCAST)
258         c = "broadcast";
259 
260     s = socket(AF_INET, SOCK_DGRAM, PF_UNSPEC);
261     if (s == INVALID_SOCKET) {
262         error_printf("socket () ");
263         return on_socket_error(s);
264     }
265     error = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &True, sizeof(True));
266     error = bind(s, (struct sockaddr *) &source, sizeof(source));
267     if (error == SOCKET_ERROR) {
268         error_printf("bind () ");
269         return on_socket_error(s);
270     }
271     if (ip == INADDR_BROADCAST) {
272         error_printf("broadcast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
273         error = setsockopt(s, SOL_SOCKET, SO_BROADCAST, &True, sizeof(True));
274         if (error == SOCKET_ERROR) {
275             error_printf("setsockopt (%u, SOL_SOCKET, SO_BROADCAST, ...) ", s);
276             return on_socket_error(s);
277         }
278     }
279     if (IN_CLASSD(ip)) {
280         error_printf("multicast %s:%u %s\r\n", inet_ntoa(dest.sin_addr), ntohs(dest.sin_port), c);
281         error = setsockopt(s, IPPROTO_IP, IP_MULTICAST_TTL, (const char *) &TTL, sizeof(TTL));
282         if (error == SOCKET_ERROR) {
283             error_printf("setsockopt (%u, IPPROTO_IP, IP_MULTICAST_TTL, ...) ", s);
284             return on_socket_error(s);
285         }
286     }
287     error = connect(s, (PSOCKADDR) & dest, sizeof(SOCKADDR_IN));
288     if (error == SOCKET_ERROR) {
289         error_printf("connect: ");
290         return on_socket_error(s);
291     }
292     rtpsocket = s;
293     return 0;
294 }
295 
296 static void
rtp_initialization_extra(void)297 rtp_initialization_extra(void)
298 {
299     WSADATA wsaData;
300     int     rc = WSAStartup(MAKEWORD(2, 2), &wsaData);
301     if (rc != 0) {
302         print_socket_error(rc);
303     }
304 }
305 
306 static void
rtp_close_extra(void)307 rtp_close_extra(void)
308 {
309     WSACleanup();
310 }
311 
312 #endif
313 
314 
315 static int
rtp_send(unsigned char const * data,int len)316 rtp_send(unsigned char const *data, int len)
317 {
318     SOCKET  s = rtpsocket;
319     struct rtpheader *foo = &RTPheader;
320     char   *buffer = malloc(len + sizeof(struct rtpheader));
321     int    *cast = (int *) foo;
322     int    *outcast = (int *) buffer;
323     int     count, size;
324 
325     outcast[0] = htonl(cast[0]);
326     outcast[1] = htonl(cast[1]);
327     outcast[2] = htonl(cast[2]);
328     outcast[3] = htonl(cast[3]);
329     memmove(buffer + sizeof(struct rtpheader), data, len);
330     size = len + sizeof(*foo);
331     count = send(s, buffer, size, 0);
332     free(buffer);
333 
334     return count != size;
335 }
336 
337 void
rtp_output(unsigned char const * mp3buffer,int mp3size)338 rtp_output(unsigned char const *mp3buffer, int mp3size)
339 {
340     rtp_send(mp3buffer, mp3size);
341     RTPheader.timestamp += 5;
342     RTPheader.b.sequence++;
343 }
344 
345 void
rtp_initialization(void)346 rtp_initialization(void)
347 {
348     struct rtpheader *foo = &RTPheader;
349     foo->b.v = 2;
350     foo->b.p = 0;
351     foo->b.x = 0;
352     foo->b.cc = 0;
353     foo->b.m = 0;
354     foo->b.pt = 14;     /* MPEG Audio */
355     foo->b.sequence = rand() & 65535;
356     foo->timestamp = rand();
357     foo->ssrc = rand();
358     foo->iAudioHeader = 0;
359     rtp_initialization_extra();
360 }
361 
362 void
rtp_deinitialization(void)363 rtp_deinitialization(void)
364 {
365     rtp_close_extra();
366 }
367