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 */
38 #include <sys/select.h>
39 #endif /* HAVE_SYS_SELECT_H */
40 #endif /* !HAVE_POLL_H */
48 static int inet_aton(const char *str, struct in_addr *add)
50 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
52 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
55 if (!add1 || (add1 | add2 | add3 | add4) > 255)
58 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
62 #endif /* !HAVE_INET_ATON */
64 int ff_getaddrinfo(const char *node, const char *service,
65 const struct addrinfo *hints, struct addrinfo **res)
67 struct hostent *h = NULL;
69 struct sockaddr_in *sin;
72 sin = av_mallocz(sizeof(struct sockaddr_in));
75 sin->sin_family = AF_INET;
78 if (!inet_aton(node, &sin->sin_addr)) {
79 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
83 h = gethostbyname(node);
88 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
91 if (hints && (hints->ai_flags & AI_PASSIVE))
92 sin->sin_addr.s_addr = INADDR_ANY;
94 sin->sin_addr.s_addr = INADDR_LOOPBACK;
97 /* Note: getaddrinfo allows service to be a string, which
98 * should be looked up using getservbyname. */
100 sin->sin_port = htons(atoi(service));
102 ai = av_mallocz(sizeof(struct addrinfo));
109 ai->ai_family = AF_INET;
110 ai->ai_socktype = hints ? hints->ai_socktype : 0;
111 switch (ai->ai_socktype) {
113 ai->ai_protocol = IPPROTO_TCP;
116 ai->ai_protocol = IPPROTO_UDP;
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;
132 void ff_freeaddrinfo(struct addrinfo *res)
134 av_freep(&res->ai_canonname);
135 av_freep(&res->ai_addr);
139 int ff_getnameinfo(const struct sockaddr *sa, int salen,
140 char *host, int hostlen,
141 char *serv, int servlen, int flags)
143 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
145 if (sa->sa_family != AF_INET)
150 if (host && hostlen > 0) {
151 struct hostent *ent = NULL;
153 if (!(flags & NI_NUMERICHOST))
154 ent = gethostbyaddr((const char *)&sin->sin_addr,
155 sizeof(sin->sin_addr), AF_INET);
158 snprintf(host, hostlen, "%s", ent->h_name);
159 } else if (flags & NI_NAMERQD) {
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));
169 if (serv && servlen > 0) {
170 if (!(flags & NI_NUMERICSERV))
172 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
177 #endif /* !HAVE_GETADDRINFO */
179 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
180 const char *ff_gai_strerror(int ecode)
184 return "Temporary failure in name resolution";
186 return "Invalid flags for ai_flags";
188 return "A non-recoverable error occurred";
190 return "The address family was not recognized or the address "
191 "length was invalid for the specified family";
193 return "Memory allocation failure";
194 #if EAI_NODATA != EAI_NONAME
196 return "No address associated with hostname";
197 #endif /* EAI_NODATA != EAI_NONAME */
199 return "The name does not resolve for the supplied parameters";
201 return "servname not supported for ai_socktype";
203 return "ai_socktype not supported";
206 return "Unknown error";
208 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
210 int ff_socket_nonblock(int socket, int enable)
213 u_long param = enable;
214 return ioctlsocket(socket, FIONBIO, ¶m);
217 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
219 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
220 #endif /* HAVE_WINSOCK2_H */
224 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
228 fd_set exception_set;
234 if (numfds >= FD_SETSIZE) {
238 #endif /* HAVE_WINSOCK2_H */
242 FD_ZERO(&exception_set);
245 for (i = 0; i < numfds; i++) {
249 if (fds[i].fd >= FD_SETSIZE) {
253 #endif /* !HAVE_WINSOCK2_H */
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);
267 /* Hey!? Nothing to poll, in fact!!! */
271 rc = select(n, &read_set, &write_set, &exception_set, NULL);
274 tv.tv_sec = timeout / 1000;
275 tv.tv_usec = 1000 * (timeout % 1000);
276 rc = select(n, &read_set, &write_set, &exception_set, &tv);
282 for (i = 0; i < numfds; i++) {
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;
295 #endif /* !HAVE_POLL_H */
297 #endif /* CONFIG_NETWORK */