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