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