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