]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
4bf2e268cf03e3197ca7312aa525fe18ed94df3e
[ffmpeg] / libavformat / os_support.c
1 /*
2  * Various utilities for ffmpeg system
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * copyright (c) 2002 Francois Revol
5  *
6  * This file is part of FFmpeg.
7  *
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.
12  *
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.
17  *
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
21  */
22
23 /* needed by inet_aton() */
24 #define _SVID_SOURCE
25
26 #include "config.h"
27 #include "avformat.h"
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <sys/time.h>
31 #include "os_support.h"
32
33 #if CONFIG_NETWORK
34 #if !HAVE_POLL_H
35 #if HAVE_WINSOCK2_H
36 #include <winsock2.h>
37 #elif HAVE_SYS_SELECT_H
38 #include <sys/select.h>
39 #endif
40 #endif
41
42 #include "network.h"
43
44 #if !HAVE_INET_ATON
45 #include <stdlib.h>
46 #include <strings.h>
47
48 int inet_aton (const char * str, struct in_addr * add)
49 {
50     unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
51
52     if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
53         return 0;
54
55     if (!add1 || (add1|add2|add3|add4) > 255) return 0;
56
57     add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
58
59     return 1;
60 }
61 #endif /* !HAVE_INET_ATON */
62
63 #if !HAVE_GETADDRINFO
64 int ff_getaddrinfo(const char *node, const char *service,
65                 const struct addrinfo *hints, struct addrinfo **res)
66 {
67     struct hostent *h = NULL;
68     struct addrinfo *ai;
69     struct sockaddr_in *sin;
70
71 #if HAVE_WINSOCK2_H
72     int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
73                                   const struct addrinfo *hints,
74                                   struct addrinfo **res);
75     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
76     win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
77     if (win_getaddrinfo)
78         return win_getaddrinfo(node, service, hints, res);
79 #endif
80
81     *res = NULL;
82     sin = av_mallocz(sizeof(struct sockaddr_in));
83     if (!sin)
84         return EAI_FAIL;
85     sin->sin_family = AF_INET;
86
87     if (node) {
88         if (!inet_aton(node, &sin->sin_addr)) {
89             if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
90                 av_free(sin);
91                 return EAI_FAIL;
92             }
93             h = gethostbyname(node);
94             if (!h) {
95                 av_free(sin);
96                 return EAI_FAIL;
97             }
98             memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
99         }
100     } else {
101         if (hints && (hints->ai_flags & AI_PASSIVE)) {
102             sin->sin_addr.s_addr = INADDR_ANY;
103         } else
104             sin->sin_addr.s_addr = INADDR_LOOPBACK;
105     }
106
107     /* Note: getaddrinfo allows service to be a string, which
108      * should be looked up using getservbyname. */
109     if (service)
110         sin->sin_port = htons(atoi(service));
111
112     ai = av_mallocz(sizeof(struct addrinfo));
113     if (!ai) {
114         av_free(sin);
115         return EAI_FAIL;
116     }
117
118     *res = ai;
119     ai->ai_family = AF_INET;
120     ai->ai_socktype = hints ? hints->ai_socktype : 0;
121     switch (ai->ai_socktype) {
122     case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
123     case SOCK_DGRAM:  ai->ai_protocol = IPPROTO_UDP; break;
124     default:          ai->ai_protocol = 0;           break;
125     }
126
127     ai->ai_addr = (struct sockaddr *)sin;
128     ai->ai_addrlen = sizeof(struct sockaddr_in);
129     if (hints && (hints->ai_flags & AI_CANONNAME))
130         ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
131
132     ai->ai_next = NULL;
133     return 0;
134 }
135
136 void ff_freeaddrinfo(struct addrinfo *res)
137 {
138 #if HAVE_WINSOCK2_H
139     void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
140     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
141     win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
142                        GetProcAddress(ws2mod, "freeaddrinfo");
143     if (win_freeaddrinfo) {
144         win_freeaddrinfo(res);
145         return;
146     }
147 #endif
148
149     av_free(res->ai_canonname);
150     av_free(res->ai_addr);
151     av_free(res);
152 }
153
154 int ff_getnameinfo(const struct sockaddr *sa, int salen,
155                    char *host, int hostlen,
156                    char *serv, int servlen, int flags)
157 {
158     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
159
160 #if HAVE_WINSOCK2_H
161     int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
162                                   char *host, DWORD hostlen,
163                                   char *serv, DWORD servlen, int flags);
164     HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
165     win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
166     if (win_getnameinfo)
167         return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
168 #endif
169
170     if (sa->sa_family != AF_INET)
171         return EAI_FAMILY;
172     if (!host && !serv)
173         return EAI_NONAME;
174
175     if (host && hostlen > 0) {
176         struct hostent *ent = NULL;
177         uint32_t a;
178         if (!(flags & NI_NUMERICHOST))
179             ent = gethostbyaddr((const char *)&sin->sin_addr,
180                                 sizeof(sin->sin_addr), AF_INET);
181
182         if (ent) {
183             snprintf(host, hostlen, "%s", ent->h_name);
184         } else if (flags & NI_NAMERQD) {
185             return EAI_NONAME;
186         } else {
187             a = ntohl(sin->sin_addr.s_addr);
188             snprintf(host, hostlen, "%d.%d.%d.%d",
189                      ((a >> 24) & 0xff), ((a >> 16) & 0xff),
190                      ((a >>  8) & 0xff), ( a        & 0xff));
191         }
192     }
193
194     if (serv && servlen > 0) {
195         struct servent *ent = NULL;
196         if (!(flags & NI_NUMERICSERV))
197             ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
198
199         if (ent) {
200             snprintf(serv, servlen, "%s", ent->s_name);
201         } else
202             snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
203     }
204
205     return 0;
206 }
207
208 const char *ff_gai_strerror(int ecode)
209 {
210     switch(ecode) {
211     case EAI_FAIL   : return "A non-recoverable error occurred";
212     case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
213     case EAI_NONAME : return "The name does not resolve for the supplied parameters";
214     }
215
216     return "Unknown error";
217 }
218 #endif
219
220 /* resolve host with also IP address parsing */
221 int resolve_host(struct in_addr *sin_addr, const char *hostname)
222 {
223
224     if (!inet_aton(hostname, sin_addr)) {
225 #if HAVE_GETADDRINFO
226         struct addrinfo *ai, *cur;
227         struct addrinfo hints;
228         memset(&hints, 0, sizeof(hints));
229         hints.ai_family = AF_INET;
230         if (getaddrinfo(hostname, NULL, &hints, &ai))
231             return -1;
232         /* getaddrinfo returns a linked list of addrinfo structs.
233          * Even if we set ai_family = AF_INET above, make sure
234          * that the returned one actually is of the correct type. */
235         for (cur = ai; cur; cur = cur->ai_next) {
236             if (cur->ai_family == AF_INET) {
237                 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
238                 freeaddrinfo(ai);
239                 return 0;
240             }
241         }
242         freeaddrinfo(ai);
243         return -1;
244 #else
245         struct hostent *hp;
246         hp = gethostbyname(hostname);
247         if (!hp)
248             return -1;
249         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
250 #endif
251     }
252     return 0;
253 }
254
255 int ff_socket_nonblock(int socket, int enable)
256 {
257 #if HAVE_WINSOCK2_H
258    return ioctlsocket(socket, FIONBIO, &enable);
259 #else
260    if (enable)
261       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
262    else
263       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
264 #endif
265 }
266 #endif /* CONFIG_NETWORK */
267
268 #if CONFIG_FFSERVER
269 #if !HAVE_POLL_H
270 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
271 {
272     fd_set read_set;
273     fd_set write_set;
274     fd_set exception_set;
275     nfds_t i;
276     int n;
277     int rc;
278
279 #if HAVE_WINSOCK2_H
280     if (numfds >= FD_SETSIZE) {
281         errno = EINVAL;
282         return -1;
283     }
284 #endif
285
286     FD_ZERO(&read_set);
287     FD_ZERO(&write_set);
288     FD_ZERO(&exception_set);
289
290     n = -1;
291     for(i = 0; i < numfds; i++) {
292         if (fds[i].fd < 0)
293             continue;
294 #if !HAVE_WINSOCK2_H
295         if (fds[i].fd >= FD_SETSIZE) {
296             errno = EINVAL;
297             return -1;
298         }
299 #endif
300
301         if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
302         if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
303         if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
304
305         if (fds[i].fd > n)
306             n = fds[i].fd;
307     };
308
309     if (n == -1)
310         /* Hey!? Nothing to poll, in fact!!! */
311         return 0;
312
313     if (timeout < 0)
314         rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
315     else {
316         struct timeval    tv;
317
318         tv.tv_sec = timeout / 1000;
319         tv.tv_usec = 1000 * (timeout % 1000);
320         rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
321     };
322
323     if (rc < 0)
324         return rc;
325
326     for(i = 0; i < (nfds_t) n; i++) {
327         fds[i].revents = 0;
328
329         if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
330         if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
331         if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
332     };
333
334     return rc;
335 }
336 #endif /* HAVE_POLL_H */
337 #endif /* CONFIG_FFSERVER */
338