configure: Check for sys/time.h
[libav.git] / libavformat / os_support.c
CommitLineData
f71869a4 1/*
124e2884 2 * various OS-feature replacement utilities
f71869a4 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
54eb77c0 4 * copyright (c) 2002 Francois Revol
f71869a4 5 *
2912e87a 6 * This file is part of Libav.
b78e7197 7 *
2912e87a 8 * Libav is free software; you can redistribute it and/or
f71869a4
FB
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
b78e7197 11 * version 2.1 of the License, or (at your option) any later version.
f71869a4 12 *
2912e87a 13 * Libav is distributed in the hope that it will be useful,
f71869a4
FB
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.
17 *
18 * You should have received a copy of the GNU Lesser General Public
2912e87a 19 * License along with Libav; if not, write to the Free Software
5509bffa 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
f71869a4 21 */
7246177d
AJ
22
23/* needed by inet_aton() */
24#define _SVID_SOURCE
25
17c22f57 26#include "config.h"
f71869a4 27#include "avformat.h"
087b3272 28#include "os_support.h"
36c07acd 29
b1ac139d
KG
30#if defined(_WIN32) && !defined(__MINGW32CE__)
31#include <windows.h>
32
33#undef open
34int ff_win32_open(const char *filename_utf8, int oflag, int pmode)
35{
36 int fd;
37 int num_chars;
38 wchar_t *filename_w;
39
40 /* convert UTF-8 to wide chars */
41 num_chars = MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, NULL, 0);
42 if (num_chars <= 0)
43 return -1;
44 filename_w = av_mallocz(sizeof(wchar_t) * num_chars);
45 MultiByteToWideChar(CP_UTF8, 0, filename_utf8, -1, filename_w, num_chars);
46
47 fd = _wopen(filename_w, oflag, pmode);
48 av_freep(&filename_w);
49
50 /* filename maybe be in CP_ACP */
51 if (fd == -1 && !(oflag & O_CREAT))
52 return open(filename_utf8, oflag, pmode);
53
54 return fd;
55}
56#endif
57
b250f9c6 58#if CONFIG_NETWORK
7fddac93 59#include <fcntl.h>
b250f9c6 60#if !HAVE_POLL_H
e64bceea 61#if HAVE_SYS_TIME_H
7fddac93 62#include <sys/time.h>
e64bceea 63#endif
b250f9c6 64#if HAVE_WINSOCK2_H
36c07acd 65#include <winsock2.h>
b250f9c6 66#elif HAVE_SYS_SELECT_H
b0c858d8
FR
67#include <sys/select.h>
68#endif
36c07acd 69#endif
f71869a4 70
383eda23
AB
71#include "network.h"
72
b250f9c6 73#if !HAVE_INET_ATON
eaa53b95 74#include <stdlib.h>
eaa53b95 75
ac11d562 76int ff_inet_aton (const char * str, struct in_addr * add)
54eb77c0 77{
54eb77c0
DB
78 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
79
d72e7d0f
BF
80 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
81 return 0;
54eb77c0 82
104d0418
JP
83 if (!add1 || (add1|add2|add3|add4) > 255) return 0;
84
b827f4eb 85 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
54eb77c0
DB
86
87 return 1;
88}
ac11d562
DC
89#else
90int ff_inet_aton (const char * str, struct in_addr * add)
91{
92 return inet_aton(str, add);
93}
b250f9c6 94#endif /* !HAVE_INET_ATON */
383eda23 95
fa053ca7
MS
96#if !HAVE_GETADDRINFO
97int ff_getaddrinfo(const char *node, const char *service,
98 const struct addrinfo *hints, struct addrinfo **res)
99{
100 struct hostent *h = NULL;
101 struct addrinfo *ai;
102 struct sockaddr_in *sin;
103
6023d84a
MS
104#if HAVE_WINSOCK2_H
105 int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
106 const struct addrinfo *hints,
107 struct addrinfo **res);
108 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
109 win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
110 if (win_getaddrinfo)
111 return win_getaddrinfo(node, service, hints, res);
112#endif
113
a34fc5e2 114 *res = NULL;
fa053ca7
MS
115 sin = av_mallocz(sizeof(struct sockaddr_in));
116 if (!sin)
117 return EAI_FAIL;
118 sin->sin_family = AF_INET;
119
120 if (node) {
ac11d562 121 if (!ff_inet_aton(node, &sin->sin_addr)) {
fa053ca7
MS
122 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
123 av_free(sin);
124 return EAI_FAIL;
125 }
126 h = gethostbyname(node);
127 if (!h) {
128 av_free(sin);
129 return EAI_FAIL;
130 }
131 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
132 }
133 } else {
134 if (hints && (hints->ai_flags & AI_PASSIVE)) {
135 sin->sin_addr.s_addr = INADDR_ANY;
136 } else
137 sin->sin_addr.s_addr = INADDR_LOOPBACK;
138 }
139
140 /* Note: getaddrinfo allows service to be a string, which
141 * should be looked up using getservbyname. */
142 if (service)
143 sin->sin_port = htons(atoi(service));
144
145 ai = av_mallocz(sizeof(struct addrinfo));
146 if (!ai) {
147 av_free(sin);
148 return EAI_FAIL;
149 }
150
151 *res = ai;
152 ai->ai_family = AF_INET;
153 ai->ai_socktype = hints ? hints->ai_socktype : 0;
154 switch (ai->ai_socktype) {
155 case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
156 case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
157 default: ai->ai_protocol = 0; break;
158 }
159
160 ai->ai_addr = (struct sockaddr *)sin;
161 ai->ai_addrlen = sizeof(struct sockaddr_in);
162 if (hints && (hints->ai_flags & AI_CANONNAME))
163 ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
164
165 ai->ai_next = NULL;
166 return 0;
167}
168
169void ff_freeaddrinfo(struct addrinfo *res)
170{
6023d84a
MS
171#if HAVE_WINSOCK2_H
172 void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
173 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
174 win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
175 GetProcAddress(ws2mod, "freeaddrinfo");
176 if (win_freeaddrinfo) {
177 win_freeaddrinfo(res);
178 return;
179 }
180#endif
181
fa053ca7
MS
182 av_free(res->ai_canonname);
183 av_free(res->ai_addr);
184 av_free(res);
185}
5d629b72
MS
186
187int ff_getnameinfo(const struct sockaddr *sa, int salen,
188 char *host, int hostlen,
189 char *serv, int servlen, int flags)
190{
191 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
192
6023d84a
MS
193#if HAVE_WINSOCK2_H
194 int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
195 char *host, DWORD hostlen,
196 char *serv, DWORD servlen, int flags);
197 HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
198 win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
199 if (win_getnameinfo)
200 return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
201#endif
202
5d629b72
MS
203 if (sa->sa_family != AF_INET)
204 return EAI_FAMILY;
205 if (!host && !serv)
206 return EAI_NONAME;
207
208 if (host && hostlen > 0) {
209 struct hostent *ent = NULL;
210 uint32_t a;
211 if (!(flags & NI_NUMERICHOST))
212 ent = gethostbyaddr((const char *)&sin->sin_addr,
213 sizeof(sin->sin_addr), AF_INET);
214
215 if (ent) {
216 snprintf(host, hostlen, "%s", ent->h_name);
217 } else if (flags & NI_NAMERQD) {
218 return EAI_NONAME;
219 } else {
220 a = ntohl(sin->sin_addr.s_addr);
221 snprintf(host, hostlen, "%d.%d.%d.%d",
222 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
223 ((a >> 8) & 0xff), ( a & 0xff));
224 }
225 }
226
227 if (serv && servlen > 0) {
228 struct servent *ent = NULL;
229 if (!(flags & NI_NUMERICSERV))
230 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
231
232 if (ent) {
233 snprintf(serv, servlen, "%s", ent->s_name);
234 } else
235 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
236 }
237
238 return 0;
239}
04a2b04b
RB
240
241const char *ff_gai_strerror(int ecode)
242{
243 switch(ecode) {
244 case EAI_FAIL : return "A non-recoverable error occurred";
245 case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
246 case EAI_NONAME : return "The name does not resolve for the supplied parameters";
247 }
248
249 return "Unknown error";
250}
fa053ca7
MS
251#endif
252
ba472aaf
AB
253int ff_socket_nonblock(int socket, int enable)
254{
b250f9c6 255#if HAVE_WINSOCK2_H
56168769
MS
256 u_long param = enable;
257 return ioctlsocket(socket, FIONBIO, &param);
5676d140 258#else
ba472aaf
AB
259 if (enable)
260 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
261 else
262 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
5676d140 263#endif
ba472aaf 264}
b0c858d8 265
b250f9c6 266#if !HAVE_POLL_H
b0c858d8
FR
267int poll(struct pollfd *fds, nfds_t numfds, int timeout)
268{
269 fd_set read_set;
270 fd_set write_set;
271 fd_set exception_set;
272 nfds_t i;
273 int n;
274 int rc;
275
b250f9c6 276#if HAVE_WINSOCK2_H
7235183d
AB
277 if (numfds >= FD_SETSIZE) {
278 errno = EINVAL;
279 return -1;
280 }
3e2e29fb 281#endif
7235183d 282
b0c858d8
FR
283 FD_ZERO(&read_set);
284 FD_ZERO(&write_set);
285 FD_ZERO(&exception_set);
286
287 n = -1;
288 for(i = 0; i < numfds; i++) {
289 if (fds[i].fd < 0)
290 continue;
b250f9c6 291#if !HAVE_WINSOCK2_H
3e2e29fb
AB
292 if (fds[i].fd >= FD_SETSIZE) {
293 errno = EINVAL;
294 return -1;
295 }
296#endif
b0c858d8
FR
297
298 if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
299 if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
300 if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
301
302 if (fds[i].fd > n)
303 n = fds[i].fd;
304 };
305
306 if (n == -1)
307 /* Hey!? Nothing to poll, in fact!!! */
308 return 0;
309
310 if (timeout < 0)
311 rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
312 else {
313 struct timeval tv;
314
315 tv.tv_sec = timeout / 1000;
316 tv.tv_usec = 1000 * (timeout % 1000);
317 rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
318 };
319
320 if (rc < 0)
321 return rc;
322
9ac2085d 323 for(i = 0; i < numfds; i++) {
b0c858d8
FR
324 fds[i].revents = 0;
325
326 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
327 if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
328 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
329 };
330
331 return rc;
332}
f8cda19e 333#endif /* HAVE_POLL_H */
362d8f7d 334#endif /* CONFIG_NETWORK */