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 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");
85 return win_getaddrinfo(node, service, hints, res);
86 #endif /* HAVE_WINSOCK2_H */
89 sin = av_mallocz(sizeof(struct sockaddr_in));
92 sin->sin_family = AF_INET;
95 if (!ff_inet_aton(node, &sin->sin_addr)) {
96 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
100 h = gethostbyname(node);
105 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
108 if (hints && (hints->ai_flags & AI_PASSIVE))
109 sin->sin_addr.s_addr = INADDR_ANY;
111 sin->sin_addr.s_addr = INADDR_LOOPBACK;
114 /* Note: getaddrinfo allows service to be a string, which
115 * should be looked up using getservbyname. */
117 sin->sin_port = htons(atoi(service));
119 ai = av_mallocz(sizeof(struct addrinfo));
126 ai->ai_family = AF_INET;
127 ai->ai_socktype = hints ? hints->ai_socktype : 0;
128 switch (ai->ai_socktype) {
130 ai->ai_protocol = IPPROTO_TCP;
133 ai->ai_protocol = IPPROTO_UDP;
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;
149 void ff_freeaddrinfo(struct addrinfo *res)
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);
160 #endif /* HAVE_WINSOCK2_H */
162 av_freep(&res->ai_canonname);
163 av_freep(&res->ai_addr);
167 int ff_getnameinfo(const struct sockaddr *sa, int salen,
168 char *host, int hostlen,
169 char *serv, int servlen, int flags)
171 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
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");
180 return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
181 #endif /* HAVE_WINSOCK2_H */
183 if (sa->sa_family != AF_INET)
188 if (host && hostlen > 0) {
189 struct hostent *ent = NULL;
191 if (!(flags & NI_NUMERICHOST))
192 ent = gethostbyaddr((const char *)&sin->sin_addr,
193 sizeof(sin->sin_addr), AF_INET);
196 snprintf(host, hostlen, "%s", ent->h_name);
197 } else if (flags & NI_NAMERQD) {
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));
207 if (serv && servlen > 0) {
208 if (!(flags & NI_NUMERICSERV))
210 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
215 #endif /* !HAVE_GETADDRINFO */
217 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
218 const char *ff_gai_strerror(int ecode)
222 return "Temporary failure in name resolution";
224 return "Invalid flags for ai_flags";
226 return "A non-recoverable error occurred";
228 return "The address family was not recognized or the address "
229 "length was invalid for the specified family";
231 return "Memory allocation failure";
232 #if EAI_NODATA != EAI_NONAME
234 return "No address associated with hostname";
235 #endif /* EAI_NODATA != EAI_NONAME */
237 return "The name does not resolve for the supplied parameters";
239 return "servname not supported for ai_socktype";
241 return "ai_socktype not supported";
244 return "Unknown error";
246 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
248 int ff_socket_nonblock(int socket, int enable)
251 u_long param = enable;
252 return ioctlsocket(socket, FIONBIO, ¶m);
255 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
257 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
258 #endif /* HAVE_WINSOCK2_H */
262 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
266 fd_set exception_set;
272 if (numfds >= FD_SETSIZE) {
276 #endif /* HAVE_WINSOCK2_H */
280 FD_ZERO(&exception_set);
283 for (i = 0; i < numfds; i++) {
287 if (fds[i].fd >= FD_SETSIZE) {
291 #endif /* !HAVE_WINSOCK2_H */
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);
305 /* Hey!? Nothing to poll, in fact!!! */
309 rc = select(n, &read_set, &write_set, &exception_set, NULL);
312 tv.tv_sec = timeout / 1000;
313 tv.tv_usec = 1000 * (timeout % 1000);
314 rc = select(n, &read_set, &write_set, &exception_set, &tv);
320 for (i = 0; i < numfds; i++) {
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;
333 #endif /* !HAVE_POLL_H */
335 #endif /* CONFIG_NETWORK */