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_free(res
->ai_canonname
);
163 av_free(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 struct servent
*ent
= NULL
;
209 #if HAVE_GETSERVBYPORT
210 if (!(flags
& NI_NUMERICSERV
))
211 ent
= getservbyport(sin
->sin_port
, flags
& NI_DGRAM
? "udp" : "tcp");
212 #endif /* HAVE_GETSERVBYPORT */
215 snprintf(serv
, servlen
, "%s", ent
->s_name
);
217 snprintf(serv
, servlen
, "%d", ntohs(sin
->sin_port
));
222 #endif /* !HAVE_GETADDRINFO */
224 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
225 const char *ff_gai_strerror(int ecode
)
229 return "Temporary failure in name resolution";
231 return "Invalid flags for ai_flags";
233 return "A non-recoverable error occurred";
235 return "The address family was not recognized or the address "
236 "length was invalid for the specified family";
238 return "Memory allocation failure";
239 #if EAI_NODATA != EAI_NONAME
241 return "No address associated with hostname";
242 #endif /* EAI_NODATA != EAI_NONAME */
244 return "The name does not resolve for the supplied parameters";
246 return "servname not supported for ai_socktype";
248 return "ai_socktype not supported";
251 return "Unknown error";
253 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
255 int ff_socket_nonblock(int socket
, int enable
)
258 u_long param
= enable
;
259 return ioctlsocket(socket
, FIONBIO
, ¶m
);
262 return fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) | O_NONBLOCK
);
264 return fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) & ~O_NONBLOCK
);
265 #endif /* HAVE_WINSOCK2_H */
269 int ff_poll(struct pollfd
*fds
, nfds_t numfds
, int timeout
)
273 fd_set exception_set
;
279 if (numfds
>= FD_SETSIZE
) {
283 #endif /* HAVE_WINSOCK2_H */
287 FD_ZERO(&exception_set
);
290 for (i
= 0; i
< numfds
; i
++) {
294 if (fds
[i
].fd
>= FD_SETSIZE
) {
298 #endif /* !HAVE_WINSOCK2_H */
300 if (fds
[i
].events
& POLLIN
)
301 FD_SET(fds
[i
].fd
, &read_set
);
302 if (fds
[i
].events
& POLLOUT
)
303 FD_SET(fds
[i
].fd
, &write_set
);
304 if (fds
[i
].events
& POLLERR
)
305 FD_SET(fds
[i
].fd
, &exception_set
);
312 /* Hey!? Nothing to poll, in fact!!! */
316 rc
= select(n
, &read_set
, &write_set
, &exception_set
, NULL
);
319 tv
.tv_sec
= timeout
/ 1000;
320 tv
.tv_usec
= 1000 * (timeout
% 1000);
321 rc
= select(n
, &read_set
, &write_set
, &exception_set
, &tv
);
327 for (i
= 0; i
< numfds
; i
++) {
330 if (FD_ISSET(fds
[i
].fd
, &read_set
))
331 fds
[i
].revents
|= POLLIN
;
332 if (FD_ISSET(fds
[i
].fd
, &write_set
))
333 fds
[i
].revents
|= POLLOUT
;
334 if (FD_ISSET(fds
[i
].fd
, &exception_set
))
335 fds
[i
].revents
|= POLLERR
;
340 #endif /* !HAVE_POLL_H */
342 #endif /* CONFIG_NETWORK */