1 /*
2 * various OS-feature replacement utilities
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * copyright (c) 2002 Francois Revol
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /* needed by inet_aton() */
24 #define _DEFAULT_SOURCE
25 #define _SVID_SOURCE
26
27 #ifdef __AROS__
28 #include <proto/socket.h>
29 #endif
30
31 #include "config.h"
32
33 #include "avformat.h"
34 #include "os_support.h"
35
36 #if CONFIG_NETWORK
37 #include <fcntl.h>
38 #if !HAVE_POLL_H
39 #if HAVE_SYS_TIME_H
40 #include <sys/time.h>
41 #endif /* HAVE_SYS_TIME_H */
42 #if HAVE_WINSOCK2_H
43 #include <winsock2.h>
44 #elif HAVE_SYS_SELECT_H
45 #include <sys/select.h>
46 #endif /* HAVE_WINSOCK2_H */
47 #endif /* !HAVE_POLL_H */
48
49 #include "network.h"
50
51 #if !HAVE_INET_ATON
52 #include <stdlib.h>
53
ff_inet_aton(const char * str,struct in_addr * add)54 int ff_inet_aton(const char *str, struct in_addr *add)
55 {
56 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
57
58 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
59 return 0;
60
61 if (!add1 || (add1 | add2 | add3 | add4) > 255)
62 return 0;
63
64 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
65
66 return 1;
67 }
68 #else
ff_inet_aton(const char * str,struct in_addr * add)69 int ff_inet_aton(const char *str, struct in_addr *add)
70 {
71 return inet_aton(str, add);
72 }
73 #endif /* !HAVE_INET_ATON */
74
75 #if !HAVE_GETADDRINFO
ff_getaddrinfo(const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** res)76 int ff_getaddrinfo(const char *node, const char *service,
77 const struct addrinfo *hints, struct addrinfo **res)
78 {
79 struct hostent *h = NULL;
80 struct addrinfo *ai;
81 struct sockaddr_in *sin;
82
83 #if HAVE_WINSOCK2_H
84 int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
85 const struct addrinfo *hints,
86 struct addrinfo **res);
87 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
88 win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
89 if (win_getaddrinfo)
90 return win_getaddrinfo(node, service, hints, res);
91 #endif /* HAVE_WINSOCK2_H */
92
93 *res = NULL;
94 sin = av_mallocz(sizeof(struct sockaddr_in));
95 if (!sin)
96 return EAI_FAIL;
97 sin->sin_family = AF_INET;
98
99 if (node) {
100 if (!ff_inet_aton(node, &sin->sin_addr)) {
101 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
102 av_free(sin);
103 return EAI_FAIL;
104 }
105 h = gethostbyname(node);
106 if (!h) {
107 av_free(sin);
108 return EAI_FAIL;
109 }
110 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
111 }
112 } else {
113 if (hints && (hints->ai_flags & AI_PASSIVE))
114 sin->sin_addr.s_addr = INADDR_ANY;
115 else
116 sin->sin_addr.s_addr = INADDR_LOOPBACK;
117 }
118
119 /* Note: getaddrinfo allows service to be a string, which
120 * should be looked up using getservbyname. */
121 if (service)
122 sin->sin_port = htons(atoi(service));
123
124 ai = av_mallocz(sizeof(struct addrinfo));
125 if (!ai) {
126 av_free(sin);
127 return EAI_FAIL;
128 }
129
130 *res = ai;
131 ai->ai_family = AF_INET;
132 ai->ai_socktype = hints ? hints->ai_socktype : 0;
133 switch (ai->ai_socktype) {
134 case SOCK_STREAM:
135 ai->ai_protocol = IPPROTO_TCP;
136 break;
137 case SOCK_DGRAM:
138 ai->ai_protocol = IPPROTO_UDP;
139 break;
140 default:
141 ai->ai_protocol = 0;
142 break;
143 }
144
145 ai->ai_addr = (struct sockaddr *)sin;
146 ai->ai_addrlen = sizeof(struct sockaddr_in);
147 if (hints && (hints->ai_flags & AI_CANONNAME))
148 ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
149
150 ai->ai_next = NULL;
151 return 0;
152 }
153
ff_freeaddrinfo(struct addrinfo * res)154 void ff_freeaddrinfo(struct addrinfo *res)
155 {
156 #if HAVE_WINSOCK2_H
157 void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
158 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
159 win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
160 GetProcAddress(ws2mod, "freeaddrinfo");
161 if (win_freeaddrinfo) {
162 win_freeaddrinfo(res);
163 return;
164 }
165 #endif /* HAVE_WINSOCK2_H */
166
167 av_free(res->ai_canonname);
168 av_free(res->ai_addr);
169 av_free(res);
170 }
171
ff_getnameinfo(const struct sockaddr * sa,int salen,char * host,int hostlen,char * serv,int servlen,int flags)172 int ff_getnameinfo(const struct sockaddr *sa, int salen,
173 char *host, int hostlen,
174 char *serv, int servlen, int flags)
175 {
176 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
177
178 #if HAVE_WINSOCK2_H
179 int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
180 char *host, DWORD hostlen,
181 char *serv, DWORD servlen, int flags);
182 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
183 win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
184 if (win_getnameinfo)
185 return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
186 #endif /* HAVE_WINSOCK2_H */
187
188 if (sa->sa_family != AF_INET)
189 return EAI_FAMILY;
190 if (!host && !serv)
191 return EAI_NONAME;
192
193 if (host && hostlen > 0) {
194 struct hostent *ent = NULL;
195 uint32_t a;
196 if (!(flags & NI_NUMERICHOST))
197 ent = gethostbyaddr((const char *)&sin->sin_addr,
198 sizeof(sin->sin_addr), AF_INET);
199
200 if (ent) {
201 snprintf(host, hostlen, "%s", ent->h_name);
202 } else if (flags & NI_NAMERQD) {
203 return EAI_NONAME;
204 } else {
205 a = ntohl(sin->sin_addr.s_addr);
206 snprintf(host, hostlen, "%d.%d.%d.%d",
207 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
208 ((a >> 8) & 0xff), (a & 0xff));
209 }
210 }
211
212 if (serv && servlen > 0) {
213 struct servent *ent = NULL;
214 #if HAVE_GETSERVBYPORT
215 if (!(flags & NI_NUMERICSERV))
216 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
217 #endif /* HAVE_GETSERVBYPORT */
218
219 if (ent)
220 snprintf(serv, servlen, "%s", ent->s_name);
221 else
222 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
223 }
224
225 return 0;
226 }
227 #endif /* !HAVE_GETADDRINFO */
228
229 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
ff_gai_strerror(int ecode)230 const char *ff_gai_strerror(int ecode)
231 {
232 switch (ecode) {
233 case EAI_AGAIN:
234 return "Temporary failure in name resolution";
235 case EAI_BADFLAGS:
236 return "Invalid flags for ai_flags";
237 case EAI_FAIL:
238 return "A non-recoverable error occurred";
239 case EAI_FAMILY:
240 return "The address family was not recognized or the address "
241 "length was invalid for the specified family";
242 case EAI_MEMORY:
243 return "Memory allocation failure";
244 #if EAI_NODATA != EAI_NONAME
245 case EAI_NODATA:
246 return "No address associated with hostname";
247 #endif /* EAI_NODATA != EAI_NONAME */
248 case EAI_NONAME:
249 return "The name does not resolve for the supplied parameters";
250 case EAI_SERVICE:
251 return "servname not supported for ai_socktype";
252 case EAI_SOCKTYPE:
253 return "ai_socktype not supported";
254 }
255
256 return "Unknown error";
257 }
258 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
259
ff_socket_nonblock(int socket,int enable)260 int ff_socket_nonblock(int socket, int enable)
261 {
262 #if HAVE_WINSOCK2_H
263 u_long param = enable;
264 return ioctlsocket(socket, FIONBIO, ¶m);
265 #else
266 if (enable)
267 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
268 else
269 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
270 #endif /* HAVE_WINSOCK2_H */
271 }
272
273 #if !HAVE_POLL_H
ff_poll(struct pollfd * fds,nfds_t numfds,int timeout)274 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
275 {
276 fd_set read_set;
277 fd_set write_set;
278 fd_set exception_set;
279 nfds_t i;
280 int n;
281 int rc;
282
283 #if HAVE_WINSOCK2_H
284 if (numfds >= FD_SETSIZE) {
285 errno = EINVAL;
286 return -1;
287 }
288 #endif /* HAVE_WINSOCK2_H */
289
290 FD_ZERO(&read_set);
291 FD_ZERO(&write_set);
292 FD_ZERO(&exception_set);
293
294 n = 0;
295 for (i = 0; i < numfds; i++) {
296 if (fds[i].fd < 0)
297 continue;
298 #if !HAVE_WINSOCK2_H
299 if (fds[i].fd >= FD_SETSIZE) {
300 errno = EINVAL;
301 return -1;
302 }
303 #endif /* !HAVE_WINSOCK2_H */
304
305 if (fds[i].events & POLLIN)
306 FD_SET(fds[i].fd, &read_set);
307 if (fds[i].events & POLLOUT)
308 FD_SET(fds[i].fd, &write_set);
309 if (fds[i].events & POLLERR)
310 FD_SET(fds[i].fd, &exception_set);
311
312 if (fds[i].fd >= n)
313 n = fds[i].fd + 1;
314 }
315
316 if (n == 0)
317 /* Hey!? Nothing to poll, in fact!!! */
318 return 0;
319
320 if (timeout < 0) {
321 rc = select(n, &read_set, &write_set, &exception_set, NULL);
322 } else {
323 struct timeval tv;
324 tv.tv_sec = timeout / 1000;
325 tv.tv_usec = 1000 * (timeout % 1000);
326 rc = select(n, &read_set, &write_set, &exception_set, &tv);
327 }
328
329 if (rc < 0)
330 return rc;
331
332 for (i = 0; i < numfds; i++) {
333 fds[i].revents = 0;
334
335 if (FD_ISSET(fds[i].fd, &read_set))
336 fds[i].revents |= POLLIN;
337 if (FD_ISSET(fds[i].fd, &write_set))
338 fds[i].revents |= POLLOUT;
339 if (FD_ISSET(fds[i].fd, &exception_set))
340 fds[i].revents |= POLLERR;
341 }
342
343 return rc;
344 }
345 #endif /* !HAVE_POLL_H */
346
347 #endif /* CONFIG_NETWORK */
348
349 #ifdef __AROS__
vice_ffmpeg_accept(int socket,struct sockaddr * address,socklen_t * len)350 int vice_ffmpeg_accept(int socket, struct sockaddr *address, socklen_t *len)
351 {
352 return accept(socket, address, len);
353 }
354
vice_ffmpeg_connect(int socket,struct sockaddr * address,socklen_t * len)355 int vice_ffmpeg_connect(int socket, struct sockaddr *address, socklen_t *len)
356 {
357 return connect(socket, address, len);
358 }
359
vice_ffmpeg_recv(int socket,void * buffer,size_t len,int flags)360 ssize_t vice_ffmpeg_recv(int socket, void *buffer, size_t len, int flags)
361 {
362 return recv(socket, buffer, len, flags);
363 }
364
vice_ffmpeg_send(int socket,const void * buffer,size_t len,int flags)365 ssize_t vice_ffmpeg_send(int socket, const void *buffer, size_t len, int flags)
366 {
367 return send(socket, buffer, len, flags);
368 }
369
vice_ffmpeg_sendto(int socket,const void * message,size_t len,int flags,const struct sockaddr * dest_addr,socklen_t dest_len)370 ssize_t vice_ffmpeg_sendto(int socket, const void *message, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t dest_len)
371 {
372 return sendto(socket, message, len, flags, dest_addr, dest_len);
373 }
374 #endif
375
376