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