]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
Merge commit '97bf7c03b1338a867da52c159a2afecbdedcfa88'
[ffmpeg] / libavformat / os_support.c
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 _SVID_SOURCE
25
26 #include "config.h"
27 #include "avformat.h"
28 #include "os_support.h"
29
30 #if defined(_WIN32) && !defined(__MINGW32CE__)
31 #undef open
32 #include <fcntl.h>
33 #include <io.h>
34 #include <windows.h>
35 #include <share.h>
36
37 int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
38 {
39     int fd;
40     int num_chars;
41     wchar_t *filename_w;
42
43     /* convert UTF-8 to wide chars */
44     num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
45     if (num_chars <= 0)
46         return -1;
47     filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
48     MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
49
50     fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
51     av_freep(&filename_w);
52
53     /* filename maybe be in CP_ACP */
54     if (fd == -1 && !(oflag & O_CREAT))
55         return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
56
57     return fd;
58 }
59 #endif
60
61 #if CONFIG_NETWORK
62 #include <fcntl.h>
63 #if !HAVE_POLL_H
64 #if HAVE_SYS_TIME_H
65 #include <sys/time.h>
66 #endif
67 #if HAVE_WINSOCK2_H
68 #include <winsock2.h>
69 #elif HAVE_SYS_SELECT_H
70 #include <sys/select.h>
71 #endif
72 #endif
73
74 #include "network.h"
75
76 #if !HAVE_INET_ATON
77 #include <stdlib.h>
78
79 int ff_inet_aton(const char *str, struct in_addr *add)
80 {
81     unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
82
83     if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
84         return 0;
85
86     if (!add1 || (add1 | add2 | add3 | add4) > 255)
87         return 0;
88
89     add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
90
91     return 1;
92 }
93 #else
94 int ff_inet_aton(const char *str, struct in_addr *add)
95 {
96     return inet_aton(str, add);
97 }
98 #endif /* !HAVE_INET_ATON */
99
100 #if !HAVE_GETADDRINFO
101 int ff_getaddrinfo(const char *node, const char *service,
102                    const struct addrinfo *hints, struct addrinfo **res)
103 {
104     struct hostent *h = NULL;
105     struct addrinfo *ai;
106     struct sockaddr_in *sin;
107
108 #if HAVE_WINSOCK2_H
109     int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
110                                   const struct addrinfo *hints,
111                                   struct addrinfo **res);
112     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
113     win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
114     if (win_getaddrinfo)
115         return win_getaddrinfo(node, service, hints, res);
116 #endif
117
118     *res = NULL;
119     sin  = av_mallocz(sizeof(struct sockaddr_in));
120     if (!sin)
121         return EAI_FAIL;
122     sin->sin_family = AF_INET;
123
124     if (node) {
125         if (!ff_inet_aton(node, &sin->sin_addr)) {
126             if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
127                 av_free(sin);
128                 return EAI_FAIL;
129             }
130             h = gethostbyname(node);
131             if (!h) {
132                 av_free(sin);
133                 return EAI_FAIL;
134             }
135             memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
136         }
137     } else {
138         if (hints && (hints->ai_flags & AI_PASSIVE))
139             sin->sin_addr.s_addr = INADDR_ANY;
140         else
141             sin->sin_addr.s_addr = INADDR_LOOPBACK;
142     }
143
144     /* Note: getaddrinfo allows service to be a string, which
145      * should be looked up using getservbyname. */
146     if (service)
147         sin->sin_port = htons(atoi(service));
148
149     ai = av_mallocz(sizeof(struct addrinfo));
150     if (!ai) {
151         av_free(sin);
152         return EAI_FAIL;
153     }
154
155     *res            = ai;
156     ai->ai_family   = AF_INET;
157     ai->ai_socktype = hints ? hints->ai_socktype : 0;
158     switch (ai->ai_socktype) {
159     case SOCK_STREAM:
160         ai->ai_protocol = IPPROTO_TCP;
161         break;
162     case SOCK_DGRAM:
163         ai->ai_protocol = IPPROTO_UDP;
164         break;
165     default:
166         ai->ai_protocol = 0;
167         break;
168     }
169
170     ai->ai_addr    = (struct sockaddr *)sin;
171     ai->ai_addrlen = sizeof(struct sockaddr_in);
172     if (hints && (hints->ai_flags & AI_CANONNAME))
173         ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
174
175     ai->ai_next = NULL;
176     return 0;
177 }
178
179 void ff_freeaddrinfo(struct addrinfo *res)
180 {
181 #if HAVE_WINSOCK2_H
182     void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
183     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
184     win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
185                        GetProcAddress(ws2mod, "freeaddrinfo");
186     if (win_freeaddrinfo) {
187         win_freeaddrinfo(res);
188         return;
189     }
190 #endif
191
192     av_free(res->ai_canonname);
193     av_free(res->ai_addr);
194     av_free(res);
195 }
196
197 int ff_getnameinfo(const struct sockaddr *sa, int salen,
198                    char *host, int hostlen,
199                    char *serv, int servlen, int flags)
200 {
201     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
202
203 #if HAVE_WINSOCK2_H
204     int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
205                                   char *host, DWORD hostlen,
206                                   char *serv, DWORD servlen, int flags);
207     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
208     win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
209     if (win_getnameinfo)
210         return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
211 #endif
212
213     if (sa->sa_family != AF_INET)
214         return EAI_FAMILY;
215     if (!host && !serv)
216         return EAI_NONAME;
217
218     if (host && hostlen > 0) {
219         struct hostent *ent = NULL;
220         uint32_t a;
221         if (!(flags & NI_NUMERICHOST))
222             ent = gethostbyaddr((const char *)&sin->sin_addr,
223                                 sizeof(sin->sin_addr), AF_INET);
224
225         if (ent) {
226             snprintf(host, hostlen, "%s", ent->h_name);
227         } else if (flags & NI_NAMERQD) {
228             return EAI_NONAME;
229         } else {
230             a = ntohl(sin->sin_addr.s_addr);
231             snprintf(host, hostlen, "%d.%d.%d.%d",
232                      ((a >> 24) & 0xff), ((a >> 16) & 0xff),
233                      ((a >>  8) & 0xff),  (a        & 0xff));
234         }
235     }
236
237     if (serv && servlen > 0) {
238         struct servent *ent = NULL;
239 #if HAVE_GETSERVBYPORT
240         if (!(flags & NI_NUMERICSERV))
241             ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
242 #endif
243
244         if (ent)
245             snprintf(serv, servlen, "%s", ent->s_name);
246         else
247             snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
248     }
249
250     return 0;
251 }
252 #endif /* !HAVE_GETADDRINFO */
253
254 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
255 const char *ff_gai_strerror(int ecode)
256 {
257     switch (ecode) {
258     case EAI_AGAIN:
259         return "Temporary failure in name resolution";
260     case EAI_BADFLAGS:
261         return "Invalid flags for ai_flags";
262     case EAI_FAIL:
263         return "A non-recoverable error occurred";
264     case EAI_FAMILY:
265         return "The address family was not recognized or the address "
266                "length was invalid for the specified family";
267     case EAI_MEMORY:
268         return "Memory allocation failure";
269 #if EAI_NODATA != EAI_NONAME
270     case EAI_NODATA:
271         return "No address associated with hostname";
272 #endif
273     case EAI_NONAME:
274         return "The name does not resolve for the supplied parameters";
275     case EAI_SERVICE:
276         return "servname not supported for ai_socktype";
277     case EAI_SOCKTYPE:
278         return "ai_socktype not supported";
279     }
280
281     return "Unknown error";
282 }
283 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
284
285 int ff_socket_nonblock(int socket, int enable)
286 {
287 #if HAVE_WINSOCK2_H
288     u_long param = enable;
289     return ioctlsocket(socket, FIONBIO, &param);
290 #else
291     if (enable)
292         return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
293     else
294         return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
295 #endif
296 }
297
298 #if !HAVE_POLL_H
299 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
300 {
301     fd_set read_set;
302     fd_set write_set;
303     fd_set exception_set;
304     nfds_t i;
305     int n;
306     int rc;
307
308 #if HAVE_WINSOCK2_H
309     if (numfds >= FD_SETSIZE) {
310         errno = EINVAL;
311         return -1;
312     }
313 #endif
314
315     FD_ZERO(&read_set);
316     FD_ZERO(&write_set);
317     FD_ZERO(&exception_set);
318
319     n = 0;
320     for (i = 0; i < numfds; i++) {
321         if (fds[i].fd < 0)
322             continue;
323 #if !HAVE_WINSOCK2_H
324         if (fds[i].fd >= FD_SETSIZE) {
325             errno = EINVAL;
326             return -1;
327         }
328 #endif
329
330         if (fds[i].events & POLLIN)
331             FD_SET(fds[i].fd, &read_set);
332         if (fds[i].events & POLLOUT)
333             FD_SET(fds[i].fd, &write_set);
334         if (fds[i].events & POLLERR)
335             FD_SET(fds[i].fd, &exception_set);
336
337         if (fds[i].fd >= n)
338             n = fds[i].fd + 1;
339     }
340
341     if (n == 0)
342         /* Hey!? Nothing to poll, in fact!!! */
343         return 0;
344
345     if (timeout < 0) {
346         rc = select(n, &read_set, &write_set, &exception_set, NULL);
347     } else {
348         struct timeval tv;
349         tv.tv_sec  = timeout / 1000;
350         tv.tv_usec = 1000 * (timeout % 1000);
351         rc         = select(n, &read_set, &write_set, &exception_set, &tv);
352     }
353
354     if (rc < 0)
355         return rc;
356
357     for (i = 0; i < numfds; i++) {
358         fds[i].revents = 0;
359
360         if (FD_ISSET(fds[i].fd, &read_set))
361             fds[i].revents |= POLLIN;
362         if (FD_ISSET(fds[i].fd, &write_set))
363             fds[i].revents |= POLLOUT;
364         if (FD_ISSET(fds[i].fd, &exception_set))
365             fds[i].revents |= POLLERR;
366     }
367
368     return rc;
369 }
370 #endif /* HAVE_POLL_H */
371 #endif /* CONFIG_NETWORK */