FFmpeg
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 "libavutil/mem.h"
29 #include "avformat.h"
30 #include "os_support.h"
31 
32 #if CONFIG_NETWORK
33 #include <fcntl.h>
34 #if !HAVE_POLL_H
35 #if HAVE_SYS_TIME_H
36 #include <sys/time.h>
37 #endif /* HAVE_SYS_TIME_H */
38 #if HAVE_SYS_SELECT_H
39 #include <sys/select.h>
40 #endif /* HAVE_SYS_SELECT_H */
41 #endif /* !HAVE_POLL_H */
42 
43 #include "network.h"
44 
45 #if !HAVE_GETADDRINFO
46 #if !HAVE_INET_ATON
47 #include <stdlib.h>
48 
49 static int 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 #endif /* !HAVE_INET_ATON */
64 
65 int ff_getaddrinfo(const char *node, const char *service,
66  const struct addrinfo *hints, struct addrinfo **res)
67 {
68  struct hostent *h = NULL;
69  struct addrinfo *ai;
70  struct sockaddr_in *sin;
71 
72  *res = NULL;
73  sin = av_mallocz(sizeof(struct sockaddr_in));
74  if (!sin)
75  return EAI_FAIL;
76  sin->sin_family = AF_INET;
77 
78  if (node) {
79  if (!inet_aton(node, &sin->sin_addr)) {
80  if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
81  av_free(sin);
82  return EAI_FAIL;
83  }
84  h = gethostbyname(node);
85  if (!h) {
86  av_free(sin);
87  return EAI_FAIL;
88  }
89  memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
90  }
91  } else {
92  if (hints && (hints->ai_flags & AI_PASSIVE))
93  sin->sin_addr.s_addr = INADDR_ANY;
94  else
95  sin->sin_addr.s_addr = INADDR_LOOPBACK;
96  }
97 
98  /* Note: getaddrinfo allows service to be a string, which
99  * should be looked up using getservbyname. */
100  if (service)
101  sin->sin_port = htons(atoi(service));
102 
103  ai = av_mallocz(sizeof(struct addrinfo));
104  if (!ai) {
105  av_free(sin);
106  return EAI_FAIL;
107  }
108 
109  *res = ai;
110  ai->ai_family = AF_INET;
111  ai->ai_socktype = hints ? hints->ai_socktype : 0;
112  switch (ai->ai_socktype) {
113  case SOCK_STREAM:
114  ai->ai_protocol = IPPROTO_TCP;
115  break;
116  case SOCK_DGRAM:
117  ai->ai_protocol = IPPROTO_UDP;
118  break;
119  default:
120  ai->ai_protocol = 0;
121  break;
122  }
123 
124  ai->ai_addr = (struct sockaddr *)sin;
125  ai->ai_addrlen = sizeof(struct sockaddr_in);
126  if (hints && (hints->ai_flags & AI_CANONNAME))
127  ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
128 
129  ai->ai_next = NULL;
130  return 0;
131 }
132 
133 void ff_freeaddrinfo(struct addrinfo *res)
134 {
135  av_freep(&res->ai_canonname);
136  av_freep(&res->ai_addr);
137  av_freep(&res);
138 }
139 
140 int ff_getnameinfo(const struct sockaddr *sa, int salen,
141  char *host, int hostlen,
142  char *serv, int servlen, int flags)
143 {
144  const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
145 
146  if (sa->sa_family != AF_INET)
147  return EAI_FAMILY;
148  if (!host && !serv)
149  return EAI_NONAME;
150 
151  if (host && hostlen > 0) {
152  struct hostent *ent = NULL;
153  uint32_t a;
154  if (!(flags & NI_NUMERICHOST))
155  ent = gethostbyaddr((const char *)&sin->sin_addr,
156  sizeof(sin->sin_addr), AF_INET);
157 
158  if (ent) {
159  snprintf(host, hostlen, "%s", ent->h_name);
160  } else if (flags & NI_NAMERQD) {
161  return EAI_NONAME;
162  } else {
163  a = ntohl(sin->sin_addr.s_addr);
164  snprintf(host, hostlen, "%d.%d.%d.%d",
165  ((a >> 24) & 0xff), ((a >> 16) & 0xff),
166  ((a >> 8) & 0xff), (a & 0xff));
167  }
168  }
169 
170  if (serv && servlen > 0) {
171  if (!(flags & NI_NUMERICSERV))
172  return EAI_FAIL;
173  snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
174  }
175 
176  return 0;
177 }
178 #endif /* !HAVE_GETADDRINFO */
179 
180 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
181 const char *ff_gai_strerror(int ecode)
182 {
183  switch (ecode) {
184  case EAI_AGAIN:
185  return "Temporary failure in name resolution";
186  case EAI_BADFLAGS:
187  return "Invalid flags for ai_flags";
188  case EAI_FAIL:
189  return "A non-recoverable error occurred";
190  case EAI_FAMILY:
191  return "The address family was not recognized or the address "
192  "length was invalid for the specified family";
193  case EAI_MEMORY:
194  return "Memory allocation failure";
195 #if EAI_NODATA != EAI_NONAME
196  case EAI_NODATA:
197  return "No address associated with hostname";
198 #endif /* EAI_NODATA != EAI_NONAME */
199  case EAI_NONAME:
200  return "The name does not resolve for the supplied parameters";
201  case EAI_SERVICE:
202  return "servname not supported for ai_socktype";
203  case EAI_SOCKTYPE:
204  return "ai_socktype not supported";
205  }
206 
207  return "Unknown error";
208 }
209 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
210 
211 int ff_socket_nonblock(int socket, int enable)
212 {
213 #if HAVE_WINSOCK2_H
214  u_long param = enable;
215  return ioctlsocket(socket, FIONBIO, &param);
216 #else
217  if (enable)
218  return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
219  else
220  return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
221 #endif /* HAVE_WINSOCK2_H */
222 }
223 
224 #if !HAVE_POLL_H
225 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
226 {
227  fd_set read_set;
228  fd_set write_set;
229  fd_set exception_set;
230  nfds_t i;
231  int n;
232  int rc;
233 
234 #if HAVE_WINSOCK2_H
235  if (numfds >= FD_SETSIZE) {
236  errno = EINVAL;
237  return -1;
238  }
239 #endif /* HAVE_WINSOCK2_H */
240 
241  FD_ZERO(&read_set);
242  FD_ZERO(&write_set);
243  FD_ZERO(&exception_set);
244 
245  n = 0;
246  for (i = 0; i < numfds; i++) {
247  if (fds[i].fd < 0)
248  continue;
249 #if !HAVE_WINSOCK2_H
250  if (fds[i].fd >= FD_SETSIZE) {
251  errno = EINVAL;
252  return -1;
253  }
254 #endif /* !HAVE_WINSOCK2_H */
255 
256  if (fds[i].events & POLLIN)
257  FD_SET(fds[i].fd, &read_set);
258  if (fds[i].events & POLLOUT)
259  FD_SET(fds[i].fd, &write_set);
260  if (fds[i].events & POLLERR)
261  FD_SET(fds[i].fd, &exception_set);
262 
263  if (fds[i].fd >= n)
264  n = fds[i].fd + 1;
265  }
266 
267  if (n == 0)
268  /* Hey!? Nothing to poll, in fact!!! */
269  return 0;
270 
271  if (timeout < 0) {
272  rc = select(n, &read_set, &write_set, &exception_set, NULL);
273  } else {
274  struct timeval tv;
275  tv.tv_sec = timeout / 1000;
276  tv.tv_usec = 1000 * (timeout % 1000);
277  rc = select(n, &read_set, &write_set, &exception_set, &tv);
278  }
279 
280  if (rc < 0)
281  return rc;
282 
283  for (i = 0; i < numfds; i++) {
284  fds[i].revents = 0;
285 
286  if (FD_ISSET(fds[i].fd, &read_set))
287  fds[i].revents |= POLLIN;
288  if (FD_ISSET(fds[i].fd, &write_set))
289  fds[i].revents |= POLLOUT;
290  if (FD_ISSET(fds[i].fd, &exception_set))
291  fds[i].revents |= POLLERR;
292  }
293 
294  return rc;
295 }
296 #endif /* !HAVE_POLL_H */
297 
298 #endif /* CONFIG_NETWORK */
ff_getaddrinfo
int ff_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res)
EAI_NODATA
#define EAI_NODATA
Definition: network.h:166
NI_NUMERICSERV
#define NI_NUMERICSERV
Definition: network.h:203
os_support.h
EAI_MEMORY
#define EAI_MEMORY
Definition: network.h:163
EAI_BADFLAGS
#define EAI_BADFLAGS
Definition: network.h:154
ff_getnameinfo
int ff_getnameinfo(const struct sockaddr *sa, int salen, char *host, int hostlen, char *serv, int servlen, int flags)
ff_freeaddrinfo
void ff_freeaddrinfo(struct addrinfo *res)
AI_NUMERICHOST
#define AI_NUMERICHOST
Definition: network.h:187
NI_NAMERQD
#define NI_NAMERQD
Definition: network.h:199
EAI_FAMILY
#define EAI_FAMILY
Definition: network.h:160
NULL
#define NULL
Definition: coverity.c:32
ff_gai_strerror
const char * ff_gai_strerror(int ecode)
addrinfo::ai_canonname
char * ai_canonname
Definition: network.h:144
addrinfo::ai_addr
struct sockaddr * ai_addr
Definition: network.h:143
INADDR_LOOPBACK
#define INADDR_LOOPBACK
Definition: network.h:229
addrinfo::ai_family
int ai_family
Definition: network.h:139
NI_NUMERICHOST
#define NI_NUMERICHOST
Definition: network.h:195
addrinfo::ai_protocol
int ai_protocol
Definition: network.h:141
ff_socket_nonblock
int ff_socket_nonblock(int socket, int enable)
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
AI_CANONNAME
#define AI_CANONNAME
Definition: network.h:183
addrinfo::ai_next
struct addrinfo * ai_next
Definition: network.h:145
addrinfo::ai_addrlen
int ai_addrlen
Definition: network.h:142
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
av_mallocz
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:256
EAI_NONAME
#define EAI_NONAME
Definition: network.h:169
EAI_SERVICE
#define EAI_SERVICE
Definition: network.h:172
addrinfo::ai_socktype
int ai_socktype
Definition: network.h:140
avformat.h
network.h
addrinfo::ai_flags
int ai_flags
Definition: network.h:138
EAI_AGAIN
#define EAI_AGAIN
Definition: network.h:151
av_strdup
char * av_strdup(const char *s)
Duplicate a string.
Definition: mem.c:272
mem.h
av_free
#define av_free(p)
Definition: tableprint_vlc.h:33
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
flags
#define flags(name, subs,...)
Definition: cbs_av1.c:474
h
h
Definition: vp9dsp_template.c:2038
addrinfo
Definition: network.h:137
EAI_FAIL
#define EAI_FAIL
Definition: network.h:157
snprintf
#define snprintf
Definition: snprintf.h:34
EAI_SOCKTYPE
#define EAI_SOCKTYPE
Definition: network.h:175
AI_PASSIVE
#define AI_PASSIVE
Definition: network.h:179