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() */
28 #include "os_support.h"
30 #if defined(_WIN32) && !defined(__MINGW32CE__)
35 int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
41 /* convert UTF-8 to wide chars */
42 num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
45 filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
46 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
48 fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
49 av_freep(&filename_w);
51 /* filename maybe be in CP_ACP */
52 if (fd == -1 && !(oflag & O_CREAT))
53 return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
67 #elif HAVE_SYS_SELECT_H
68 #include <sys/select.h>
77 int ff_inet_aton (const char * str, struct in_addr * add)
79 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
81 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
84 if (!add1 || (add1|add2|add3|add4) > 255) return 0;
86 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
91 int ff_inet_aton (const char * str, struct in_addr * add)
93 return inet_aton(str, add);
95 #endif /* !HAVE_INET_ATON */
98 int ff_getaddrinfo(const char *node, const char *service,
99 const struct addrinfo *hints, struct addrinfo **res)
101 struct hostent *h = NULL;
103 struct sockaddr_in *sin;
106 int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
107 const struct addrinfo *hints,
108 struct addrinfo **res);
109 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
110 win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
112 return win_getaddrinfo(node, service, hints, res);
116 sin = av_mallocz(sizeof(struct sockaddr_in));
119 sin->sin_family = AF_INET;
122 if (!ff_inet_aton(node, &sin->sin_addr)) {
123 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
127 h = gethostbyname(node);
132 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
135 if (hints && (hints->ai_flags & AI_PASSIVE)) {
136 sin->sin_addr.s_addr = INADDR_ANY;
138 sin->sin_addr.s_addr = INADDR_LOOPBACK;
141 /* Note: getaddrinfo allows service to be a string, which
142 * should be looked up using getservbyname. */
144 sin->sin_port = htons(atoi(service));
146 ai = av_mallocz(sizeof(struct addrinfo));
153 ai->ai_family = AF_INET;
154 ai->ai_socktype = hints ? hints->ai_socktype : 0;
155 switch (ai->ai_socktype) {
156 case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
157 case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
158 default: ai->ai_protocol = 0; break;
161 ai->ai_addr = (struct sockaddr *)sin;
162 ai->ai_addrlen = sizeof(struct sockaddr_in);
163 if (hints && (hints->ai_flags & AI_CANONNAME))
164 ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
170 void ff_freeaddrinfo(struct addrinfo *res)
173 void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
174 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
175 win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
176 GetProcAddress(ws2mod, "freeaddrinfo");
177 if (win_freeaddrinfo) {
178 win_freeaddrinfo(res);
183 av_free(res->ai_canonname);
184 av_free(res->ai_addr);
188 int ff_getnameinfo(const struct sockaddr *sa, int salen,
189 char *host, int hostlen,
190 char *serv, int servlen, int flags)
192 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
195 int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
196 char *host, DWORD hostlen,
197 char *serv, DWORD servlen, int flags);
198 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
199 win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
201 return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
204 if (sa->sa_family != AF_INET)
209 if (host && hostlen > 0) {
210 struct hostent *ent = NULL;
212 if (!(flags & NI_NUMERICHOST))
213 ent = gethostbyaddr((const char *)&sin->sin_addr,
214 sizeof(sin->sin_addr), AF_INET);
217 snprintf(host, hostlen, "%s", ent->h_name);
218 } else if (flags & NI_NAMERQD) {
221 a = ntohl(sin->sin_addr.s_addr);
222 snprintf(host, hostlen, "%d.%d.%d.%d",
223 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
224 ((a >> 8) & 0xff), ( a & 0xff));
228 if (serv && servlen > 0) {
229 struct servent *ent = NULL;
230 if (!(flags & NI_NUMERICSERV))
231 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
234 snprintf(serv, servlen, "%s", ent->s_name);
236 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
242 const char *ff_gai_strerror(int ecode)
245 case EAI_FAIL : return "A non-recoverable error occurred";
246 case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
247 case EAI_NONAME : return "The name does not resolve for the supplied parameters";
250 return "Unknown error";
254 int ff_socket_nonblock(int socket, int enable)
257 u_long param = enable;
258 return ioctlsocket(socket, FIONBIO, ¶m);
261 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
263 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
268 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
272 fd_set exception_set;
278 if (numfds >= FD_SETSIZE) {
286 FD_ZERO(&exception_set);
289 for(i = 0; i < numfds; i++) {
293 if (fds[i].fd >= FD_SETSIZE) {
299 if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
300 if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
301 if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
308 /* Hey!? Nothing to poll, in fact!!! */
312 rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
316 tv.tv_sec = timeout / 1000;
317 tv.tv_usec = 1000 * (timeout % 1000);
318 rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
324 for(i = 0; i < numfds; i++) {
327 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
328 if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
329 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
334 #endif /* HAVE_POLL_H */
335 #endif /* CONFIG_NETWORK */