30 #if defined(_WIN32) && !defined(__MINGW32CE__)
37 int ff_win32_open(
const char *filename_utf8,
int oflag,
int pmode)
44 num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1,
NULL, 0);
47 filename_w =
av_mallocz(
sizeof(
wchar_t) * num_chars);
48 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
50 fd = _wsopen(filename_w, oflag, SH_DENYNO, pmode);
54 if (fd == -1 && !(oflag & O_CREAT))
55 return _sopen(filename_utf8, oflag, SH_DENYNO, pmode);
69 #elif HAVE_SYS_SELECT_H
70 #include <sys/select.h>
81 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
83 if (sscanf(str,
"%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
86 if (!add1 || (add1 | add2 | add3 | add4) > 255)
89 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
96 return inet_aton(str, add);
100 #if !HAVE_GETADDRINFO
104 struct hostent *h =
NULL;
106 struct sockaddr_in *sin;
109 int (WSAAPI *win_getaddrinfo)(
const char *node,
const char *service,
112 HMODULE ws2mod = GetModuleHandle(
"ws2_32.dll");
113 win_getaddrinfo = GetProcAddress(ws2mod,
"getaddrinfo");
115 return win_getaddrinfo(node, service, hints, res);
122 sin->sin_family = AF_INET;
130 h = gethostbyname(node);
135 memcpy(&sin->sin_addr, h->h_addr_list[0],
sizeof(
struct in_addr));
139 sin->sin_addr.s_addr = INADDR_ANY;
147 sin->sin_port = htons(atoi(service));
170 ai->
ai_addr = (
struct sockaddr *)sin;
183 HMODULE ws2mod = GetModuleHandle(
"ws2_32.dll");
184 win_freeaddrinfo = (
void (WSAAPI *)(
struct addrinfo *res))
185 GetProcAddress(ws2mod,
"freeaddrinfo");
186 if (win_freeaddrinfo) {
187 win_freeaddrinfo(res);
198 char *host,
int hostlen,
199 char *serv,
int servlen,
int flags)
201 const struct sockaddr_in *sin = (
const struct sockaddr_in *)sa;
204 int (WSAAPI *win_getnameinfo)(
const struct sockaddr *sa, socklen_t salen,
205 char *host, DWORD hostlen,
206 char *serv, DWORD servlen,
int flags);
207 HMODULE ws2mod = GetModuleHandle(
"ws2_32.dll");
208 win_getnameinfo = GetProcAddress(ws2mod,
"getnameinfo");
210 return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
213 if (sa->sa_family != AF_INET)
218 if (host && hostlen > 0) {
219 struct hostent *ent =
NULL;
222 ent = gethostbyaddr((
const char *)&sin->sin_addr,
223 sizeof(sin->sin_addr), AF_INET);
226 snprintf(host, hostlen,
"%s", ent->h_name);
230 a = ntohl(sin->sin_addr.s_addr);
231 snprintf(host, hostlen,
"%d.%d.%d.%d",
232 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
233 ((a >> 8) & 0xff), (a & 0xff));
237 if (serv && servlen > 0) {
238 struct servent *ent =
NULL;
239 #if HAVE_GETSERVBYPORT
241 ent = getservbyport(sin->sin_port, flags &
NI_DGRAM ?
"udp" :
"tcp");
245 snprintf(serv, servlen,
"%s", ent->s_name);
247 snprintf(serv, servlen,
"%d", ntohs(sin->sin_port));
254 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
259 return "Temporary failure in name resolution";
261 return "Invalid flags for ai_flags";
263 return "A non-recoverable error occurred";
265 return "The address family was not recognized or the address "
266 "length was invalid for the specified family";
268 return "Memory allocation failure";
269 #if EAI_NODATA != EAI_NONAME
271 return "No address associated with hostname";
274 return "The name does not resolve for the supplied parameters";
276 return "servname not supported for ai_socktype";
278 return "ai_socktype not supported";
281 return "Unknown error";
288 u_long param = enable;
289 return ioctlsocket(socket, FIONBIO, ¶m);
292 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
294 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
299 int ff_poll(
struct pollfd *fds, nfds_t numfds,
int timeout)
303 fd_set exception_set;
309 if (numfds >= FD_SETSIZE) {
317 FD_ZERO(&exception_set);
320 for (i = 0; i < numfds; i++) {
324 if (fds[i].fd >= FD_SETSIZE) {
330 if (fds[i].events & POLLIN)
331 FD_SET(fds[i].fd, &read_set);
332 if (fds[i].events & POLLOUT)
333 FD_SET(fds[i].fd, &write_set);
334 if (fds[i].events & POLLERR)
335 FD_SET(fds[i].fd, &exception_set);
346 rc = select(n, &read_set, &write_set, &exception_set,
NULL);
349 tv.tv_sec = timeout / 1000;
350 tv.tv_usec = 1000 * (timeout % 1000);
351 rc = select(n, &read_set, &write_set, &exception_set, &tv);
357 for (i = 0; i < numfds; i++) {
360 if (FD_ISSET(fds[i].fd, &read_set))
361 fds[i].revents |= POLLIN;
362 if (FD_ISSET(fds[i].fd, &write_set))
363 fds[i].revents |= POLLOUT;
364 if (FD_ISSET(fds[i].fd, &exception_set))
365 fds[i].revents |= POLLERR;