2
* Various utilities for ffmpeg system
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() */
25
#define _DARWIN_C_SOURCE
32
#include "os_support.h"
38
#elif HAVE_SYS_SELECT_H
39
#include <sys/select.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) return 0;
58
add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
63
int ff_inet_aton (const char * str, struct in_addr * add)
65
return inet_aton(str, add);
67
#endif /* !HAVE_INET_ATON */
70
int ff_getaddrinfo(const char *node, const char *service,
71
const struct addrinfo *hints, struct addrinfo **res)
73
struct hostent *h = NULL;
75
struct sockaddr_in *sin;
78
int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
79
const struct addrinfo *hints,
80
struct addrinfo **res);
81
HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
82
win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
84
return win_getaddrinfo(node, service, hints, res);
88
sin = av_mallocz(sizeof(struct sockaddr_in));
91
sin->sin_family = AF_INET;
94
if (!ff_inet_aton(node, &sin->sin_addr)) {
95
if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
99
h = gethostbyname(node);
104
memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
107
if (hints && (hints->ai_flags & AI_PASSIVE)) {
108
sin->sin_addr.s_addr = INADDR_ANY;
110
sin->sin_addr.s_addr = INADDR_LOOPBACK;
113
/* Note: getaddrinfo allows service to be a string, which
114
* should be looked up using getservbyname. */
116
sin->sin_port = htons(atoi(service));
118
ai = av_mallocz(sizeof(struct addrinfo));
125
ai->ai_family = AF_INET;
126
ai->ai_socktype = hints ? hints->ai_socktype : 0;
127
switch (ai->ai_socktype) {
128
case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
129
case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
130
default: ai->ai_protocol = 0; break;
133
ai->ai_addr = (struct sockaddr *)sin;
134
ai->ai_addrlen = sizeof(struct sockaddr_in);
135
if (hints && (hints->ai_flags & AI_CANONNAME))
136
ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
142
void ff_freeaddrinfo(struct addrinfo *res)
145
void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
146
HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
147
win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
148
GetProcAddress(ws2mod, "freeaddrinfo");
149
if (win_freeaddrinfo) {
150
win_freeaddrinfo(res);
155
av_free(res->ai_canonname);
156
av_free(res->ai_addr);
160
int ff_getnameinfo(const struct sockaddr *sa, int salen,
161
char *host, int hostlen,
162
char *serv, int servlen, int flags)
164
const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
167
int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
168
char *host, DWORD hostlen,
169
char *serv, DWORD servlen, int flags);
170
HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
171
win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
173
return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
176
if (sa->sa_family != AF_INET)
181
if (host && hostlen > 0) {
182
struct hostent *ent = NULL;
184
if (!(flags & NI_NUMERICHOST))
185
ent = gethostbyaddr((const char *)&sin->sin_addr,
186
sizeof(sin->sin_addr), AF_INET);
189
snprintf(host, hostlen, "%s", ent->h_name);
190
} else if (flags & NI_NAMERQD) {
193
a = ntohl(sin->sin_addr.s_addr);
194
snprintf(host, hostlen, "%d.%d.%d.%d",
195
((a >> 24) & 0xff), ((a >> 16) & 0xff),
196
((a >> 8) & 0xff), ( a & 0xff));
200
if (serv && servlen > 0) {
201
struct servent *ent = NULL;
202
if (!(flags & NI_NUMERICSERV))
203
ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
206
snprintf(serv, servlen, "%s", ent->s_name);
208
snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
214
const char *ff_gai_strerror(int ecode)
217
case EAI_FAIL : return "A non-recoverable error occurred";
218
case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
219
case EAI_NONAME : return "The name does not resolve for the supplied parameters";
222
return "Unknown error";
226
int ff_socket_nonblock(int socket, int enable)
229
return ioctlsocket(socket, FIONBIO, &enable);
232
return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
234
return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
237
#endif /* CONFIG_NETWORK */
241
int poll(struct pollfd *fds, nfds_t numfds, int timeout)
245
fd_set exception_set;
251
if (numfds >= FD_SETSIZE) {
259
FD_ZERO(&exception_set);
262
for(i = 0; i < numfds; i++) {
266
if (fds[i].fd >= FD_SETSIZE) {
272
if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
273
if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
274
if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
281
/* Hey!? Nothing to poll, in fact!!! */
285
rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
289
tv.tv_sec = timeout / 1000;
290
tv.tv_usec = 1000 * (timeout % 1000);
291
rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
297
for(i = 0; i < (nfds_t) n; i++) {
300
if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
301
if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
302
if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
307
#endif /* HAVE_POLL_H */
308
#endif /* CONFIG_FFSERVER */