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
28 #include "libavutil/mem.h"
30 #include "os_support.h"
37 #endif /* HAVE_SYS_TIME_H */
39 #include <sys/select.h>
40 #endif /* HAVE_SYS_SELECT_H */
41 #endif /* !HAVE_POLL_H */
49 static int 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
);
63 #endif /* !HAVE_INET_ATON */
65 int ff_getaddrinfo(const char *node
, const char *service
,
66 const struct addrinfo
*hints
, struct addrinfo
**res
)
68 struct hostent
*h
= NULL
;
70 struct sockaddr_in
*sin
;
73 sin
= av_mallocz(sizeof(struct sockaddr_in
));
76 sin
->sin_family
= AF_INET
;
79 if (!inet_aton(node
, &sin
->sin_addr
)) {
80 if (hints
&& (hints
->ai_flags
& AI_NUMERICHOST
)) {
84 h
= gethostbyname(node
);
89 memcpy(&sin
->sin_addr
, h
->h_addr_list
[0], sizeof(struct in_addr
));
92 if (hints
&& (hints
->ai_flags
& AI_PASSIVE
))
93 sin
->sin_addr
.s_addr
= INADDR_ANY
;
95 sin
->sin_addr
.s_addr
= INADDR_LOOPBACK
;
98 /* Note: getaddrinfo allows service to be a string, which
99 * should be looked up using getservbyname. */
101 sin
->sin_port
= htons(atoi(service
));
103 ai
= av_mallocz(sizeof(struct addrinfo
));
110 ai
->ai_family
= AF_INET
;
111 ai
->ai_socktype
= hints
? hints
->ai_socktype
: 0;
112 switch (ai
->ai_socktype
) {
114 ai
->ai_protocol
= IPPROTO_TCP
;
117 ai
->ai_protocol
= IPPROTO_UDP
;
124 ai
->ai_addr
= (struct sockaddr
*)sin
;
125 ai
->ai_addrlen
= sizeof(struct sockaddr_in
);
126 if (hints
&& (hints
->ai_flags
& AI_CANONNAME
))
127 ai
->ai_canonname
= h
? av_strdup(h
->h_name
) : NULL
;
133 void ff_freeaddrinfo(struct addrinfo
*res
)
135 av_freep(&res
->ai_canonname
);
136 av_freep(&res
->ai_addr
);
140 int ff_getnameinfo(const struct sockaddr
*sa
, int salen
,
141 char *host
, int hostlen
,
142 char *serv
, int servlen
, int flags
)
144 const struct sockaddr_in
*sin
= (const struct sockaddr_in
*)sa
;
146 if (sa
->sa_family
!= AF_INET
)
151 if (host
&& hostlen
> 0) {
152 struct hostent
*ent
= NULL
;
154 if (!(flags
& NI_NUMERICHOST
))
155 ent
= gethostbyaddr((const char *)&sin
->sin_addr
,
156 sizeof(sin
->sin_addr
), AF_INET
);
159 snprintf(host
, hostlen
, "%s", ent
->h_name
);
160 } else if (flags
& NI_NAMERQD
) {
163 a
= ntohl(sin
->sin_addr
.s_addr
);
164 snprintf(host
, hostlen
, "%d.%d.%d.%d",
165 ((a
>> 24) & 0xff), ((a
>> 16) & 0xff),
166 ((a
>> 8) & 0xff), (a
& 0xff));
170 if (serv
&& servlen
> 0) {
171 if (!(flags
& NI_NUMERICSERV
))
173 snprintf(serv
, servlen
, "%d", ntohs(sin
->sin_port
));
178 #endif /* !HAVE_GETADDRINFO */
180 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
181 const char *ff_gai_strerror(int ecode
)
185 return "Temporary failure in name resolution";
187 return "Invalid flags for ai_flags";
189 return "A non-recoverable error occurred";
191 return "The address family was not recognized or the address "
192 "length was invalid for the specified family";
194 return "Memory allocation failure";
195 #if EAI_NODATA != EAI_NONAME
197 return "No address associated with hostname";
198 #endif /* EAI_NODATA != EAI_NONAME */
200 return "The name does not resolve for the supplied parameters";
202 return "servname not supported for ai_socktype";
204 return "ai_socktype not supported";
207 return "Unknown error";
209 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
211 int ff_socket_nonblock(int socket
, int enable
)
214 u_long param
= enable
;
215 return ioctlsocket(socket
, FIONBIO
, ¶m
);
218 return fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) | O_NONBLOCK
);
220 return fcntl(socket
, F_SETFL
, fcntl(socket
, F_GETFL
) & ~O_NONBLOCK
);
221 #endif /* HAVE_WINSOCK2_H */
225 int ff_poll(struct pollfd
*fds
, nfds_t numfds
, int timeout
)
229 fd_set exception_set
;
235 if (numfds
>= FD_SETSIZE
) {
239 #endif /* HAVE_WINSOCK2_H */
243 FD_ZERO(&exception_set
);
246 for (i
= 0; i
< numfds
; i
++) {
250 if (fds
[i
].fd
>= FD_SETSIZE
) {
255 if (fds
[i
].fd
== INVALID_SOCKET
)
257 #endif /* !HAVE_WINSOCK2_H */
259 if (fds
[i
].events
& POLLIN
)
260 FD_SET(fds
[i
].fd
, &read_set
);
261 if (fds
[i
].events
& POLLOUT
)
262 FD_SET(fds
[i
].fd
, &write_set
);
263 if (fds
[i
].events
& POLLERR
)
264 FD_SET(fds
[i
].fd
, &exception_set
);
271 /* Hey!? Nothing to poll, in fact!!! */
275 rc
= select(n
, &read_set
, &write_set
, &exception_set
, NULL
);
278 tv
.tv_sec
= timeout
/ 1000;
279 tv
.tv_usec
= 1000 * (timeout
% 1000);
280 rc
= select(n
, &read_set
, &write_set
, &exception_set
, &tv
);
286 for (i
= 0; i
< numfds
; i
++) {
289 if (FD_ISSET(fds
[i
].fd
, &read_set
))
290 fds
[i
].revents
|= POLLIN
;
291 if (FD_ISSET(fds
[i
].fd
, &write_set
))
292 fds
[i
].revents
|= POLLOUT
;
293 if (FD_ISSET(fds
[i
].fd
, &exception_set
))
294 fds
[i
].revents
|= POLLERR
;
299 #endif /* !HAVE_POLL_H */
301 #endif /* CONFIG_NETWORK */