2 * various OS-feature replacement utilities
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * copyright (c) 2002 Francois Revol
6 * This file is part of FFmpeg.
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.
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.
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
23 /* needed by inet_aton() */
24 #define _DEFAULT_SOURCE
29 #include "os_support.h"
36 #endif /* HAVE_SYS_TIME_H */
39 #elif HAVE_SYS_SELECT_H
40 #include <sys/select.h>
41 #endif /* HAVE_WINSOCK2_H */
42 #endif /* !HAVE_POLL_H */
49 int ff_inet_aton(const char *str, struct in_addr *add)
51 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
53 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
56 if (!add1 || (add1 | add2 | add3 | add4) > 255)
59 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
64 int ff_inet_aton(const char *str, struct in_addr *add)
66 return inet_aton(str, add);
68 #endif /* !HAVE_INET_ATON */
71 int ff_getaddrinfo(const char *node, const char *service,
72 const struct addrinfo *hints, struct addrinfo **res)
74 struct hostent *h = NULL;
76 struct sockaddr_in *sin;
79 sin = av_mallocz(sizeof(struct sockaddr_in));
82 sin->sin_family = AF_INET;
85 if (!ff_inet_aton(node, &sin->sin_addr)) {
86 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
90 h = gethostbyname(node);
95 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
98 if (hints && (hints->ai_flags & AI_PASSIVE))
99 sin->sin_addr.s_addr = INADDR_ANY;
101 sin->sin_addr.s_addr = INADDR_LOOPBACK;
104 /* Note: getaddrinfo allows service to be a string, which
105 * should be looked up using getservbyname. */
107 sin->sin_port = htons(atoi(service));
109 ai = av_mallocz(sizeof(struct addrinfo));
116 ai->ai_family = AF_INET;
117 ai->ai_socktype = hints ? hints->ai_socktype : 0;
118 switch (ai->ai_socktype) {
120 ai->ai_protocol = IPPROTO_TCP;
123 ai->ai_protocol = IPPROTO_UDP;
130 ai->ai_addr = (struct sockaddr *)sin;
131 ai->ai_addrlen = sizeof(struct sockaddr_in);
132 if (hints && (hints->ai_flags & AI_CANONNAME))
133 ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
139 void ff_freeaddrinfo(struct addrinfo *res)
141 av_freep(&res->ai_canonname);
142 av_freep(&res->ai_addr);
146 int ff_getnameinfo(const struct sockaddr *sa, int salen,
147 char *host, int hostlen,
148 char *serv, int servlen, int flags)
150 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
152 if (sa->sa_family != AF_INET)
157 if (host && hostlen > 0) {
158 struct hostent *ent = NULL;
160 if (!(flags & NI_NUMERICHOST))
161 ent = gethostbyaddr((const char *)&sin->sin_addr,
162 sizeof(sin->sin_addr), AF_INET);
165 snprintf(host, hostlen, "%s", ent->h_name);
166 } else if (flags & NI_NAMERQD) {
169 a = ntohl(sin->sin_addr.s_addr);
170 snprintf(host, hostlen, "%d.%d.%d.%d",
171 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
172 ((a >> 8) & 0xff), (a & 0xff));
176 if (serv && servlen > 0) {
177 if (!(flags & NI_NUMERICSERV))
179 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
184 #endif /* !HAVE_GETADDRINFO */
186 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
187 const char *ff_gai_strerror(int ecode)
191 return "Temporary failure in name resolution";
193 return "Invalid flags for ai_flags";
195 return "A non-recoverable error occurred";
197 return "The address family was not recognized or the address "
198 "length was invalid for the specified family";
200 return "Memory allocation failure";
201 #if EAI_NODATA != EAI_NONAME
203 return "No address associated with hostname";
204 #endif /* EAI_NODATA != EAI_NONAME */
206 return "The name does not resolve for the supplied parameters";
208 return "servname not supported for ai_socktype";
210 return "ai_socktype not supported";
213 return "Unknown error";
215 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
217 int ff_socket_nonblock(int socket, int enable)
220 u_long param = enable;
221 return ioctlsocket(socket, FIONBIO, ¶m);
224 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
226 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
227 #endif /* HAVE_WINSOCK2_H */
231 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
235 fd_set exception_set;
241 if (numfds >= FD_SETSIZE) {
245 #endif /* HAVE_WINSOCK2_H */
249 FD_ZERO(&exception_set);
252 for (i = 0; i < numfds; i++) {
256 if (fds[i].fd >= FD_SETSIZE) {
260 #endif /* !HAVE_WINSOCK2_H */
262 if (fds[i].events & POLLIN)
263 FD_SET(fds[i].fd, &read_set);
264 if (fds[i].events & POLLOUT)
265 FD_SET(fds[i].fd, &write_set);
266 if (fds[i].events & POLLERR)
267 FD_SET(fds[i].fd, &exception_set);
274 /* Hey!? Nothing to poll, in fact!!! */
278 rc = select(n, &read_set, &write_set, &exception_set, NULL);
281 tv.tv_sec = timeout / 1000;
282 tv.tv_usec = 1000 * (timeout % 1000);
283 rc = select(n, &read_set, &write_set, &exception_set, &tv);
289 for (i = 0; i < numfds; i++) {
292 if (FD_ISSET(fds[i].fd, &read_set))
293 fds[i].revents |= POLLIN;
294 if (FD_ISSET(fds[i].fd, &write_set))
295 fds[i].revents |= POLLOUT;
296 if (FD_ISSET(fds[i].fd, &exception_set))
297 fds[i].revents |= POLLERR;
302 #endif /* !HAVE_POLL_H */
304 #endif /* CONFIG_NETWORK */