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