]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
vf_hwmap: Properly free a locally derived device
[ffmpeg] / libavformat / os_support.c
1 /*
2  * various OS-feature replacement utilities
3  * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4  * copyright (c) 2002 Francois Revol
5  *
6  * This file is part of Libav.
7  *
8  * Libav 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  * Libav 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 Libav; 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 "os_support.h"
29
30 #if CONFIG_NETWORK
31 #include <fcntl.h>
32 #if !HAVE_POLL_H
33 #if HAVE_SYS_TIME_H
34 #include <sys/time.h>
35 #endif /* HAVE_SYS_TIME_H */
36 #if HAVE_WINSOCK2_H
37 #include <winsock2.h>
38 #elif HAVE_SYS_SELECT_H
39 #include <sys/select.h>
40 #endif /* HAVE_WINSOCK2_H */
41 #endif /* !HAVE_POLL_H */
42
43 #include "network.h"
44
45 #if !HAVE_INET_ATON
46 #include <stdlib.h>
47
48 int ff_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)
56         return 0;
57
58     add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
59
60     return 1;
61 }
62 #else
63 int ff_inet_aton(const char *str, struct in_addr *add)
64 {
65     return inet_aton(str, add);
66 }
67 #endif /* !HAVE_INET_ATON */
68
69 #if !HAVE_GETADDRINFO
70 int ff_getaddrinfo(const char *node, const char *service,
71                    const struct addrinfo *hints, struct addrinfo **res)
72 {
73     struct hostent *h = NULL;
74     struct addrinfo *ai;
75     struct sockaddr_in *sin;
76
77     *res = NULL;
78     sin  = av_mallocz(sizeof(struct sockaddr_in));
79     if (!sin)
80         return EAI_FAIL;
81     sin->sin_family = AF_INET;
82
83     if (node) {
84         if (!ff_inet_aton(node, &sin->sin_addr)) {
85             if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
86                 av_free(sin);
87                 return EAI_FAIL;
88             }
89             h = gethostbyname(node);
90             if (!h) {
91                 av_free(sin);
92                 return EAI_FAIL;
93             }
94             memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
95         }
96     } else {
97         if (hints && (hints->ai_flags & AI_PASSIVE))
98             sin->sin_addr.s_addr = INADDR_ANY;
99         else
100             sin->sin_addr.s_addr = INADDR_LOOPBACK;
101     }
102
103     /* Note: getaddrinfo allows service to be a string, which
104      * should be looked up using getservbyname. */
105     if (service)
106         sin->sin_port = htons(atoi(service));
107
108     ai = av_mallocz(sizeof(struct addrinfo));
109     if (!ai) {
110         av_free(sin);
111         return EAI_FAIL;
112     }
113
114     *res            = ai;
115     ai->ai_family   = AF_INET;
116     ai->ai_socktype = hints ? hints->ai_socktype : 0;
117     switch (ai->ai_socktype) {
118     case SOCK_STREAM:
119         ai->ai_protocol = IPPROTO_TCP;
120         break;
121     case SOCK_DGRAM:
122         ai->ai_protocol = IPPROTO_UDP;
123         break;
124     default:
125         ai->ai_protocol = 0;
126         break;
127     }
128
129     ai->ai_addr    = (struct sockaddr *)sin;
130     ai->ai_addrlen = sizeof(struct sockaddr_in);
131     if (hints && (hints->ai_flags & AI_CANONNAME))
132         ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
133
134     ai->ai_next = NULL;
135     return 0;
136 }
137
138 void ff_freeaddrinfo(struct addrinfo *res)
139 {
140     av_free(res->ai_canonname);
141     av_free(res->ai_addr);
142     av_free(res);
143 }
144
145 int ff_getnameinfo(const struct sockaddr *sa, int salen,
146                    char *host, int hostlen,
147                    char *serv, int servlen, int flags)
148 {
149     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
150
151     if (sa->sa_family != AF_INET)
152         return EAI_FAMILY;
153     if (!host && !serv)
154         return EAI_NONAME;
155
156     if (host && hostlen > 0) {
157         struct hostent *ent = NULL;
158         uint32_t a;
159         if (!(flags & NI_NUMERICHOST))
160             ent = gethostbyaddr((const char *)&sin->sin_addr,
161                                 sizeof(sin->sin_addr), AF_INET);
162
163         if (ent) {
164             snprintf(host, hostlen, "%s", ent->h_name);
165         } else if (flags & NI_NAMERQD) {
166             return EAI_NONAME;
167         } else {
168             a = ntohl(sin->sin_addr.s_addr);
169             snprintf(host, hostlen, "%d.%d.%d.%d",
170                      ((a >> 24) & 0xff), ((a >> 16) & 0xff),
171                      ((a >>  8) & 0xff),  (a        & 0xff));
172         }
173     }
174
175     if (serv && servlen > 0) {
176         if (!(flags & NI_NUMERICSERV))
177             return EAI_FAIL;
178         snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
179     }
180
181     return 0;
182 }
183 #endif /* !HAVE_GETADDRINFO */
184
185 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
186 const char *ff_gai_strerror(int ecode)
187 {
188     switch (ecode) {
189     case EAI_AGAIN:
190         return "Temporary failure in name resolution";
191     case EAI_BADFLAGS:
192         return "Invalid flags for ai_flags";
193     case EAI_FAIL:
194         return "A non-recoverable error occurred";
195     case EAI_FAMILY:
196         return "The address family was not recognized or the address "
197                "length was invalid for the specified family";
198     case EAI_MEMORY:
199         return "Memory allocation failure";
200 #if EAI_NODATA != EAI_NONAME
201     case EAI_NODATA:
202         return "No address associated with hostname";
203 #endif /* EAI_NODATA != EAI_NONAME */
204     case EAI_NONAME:
205         return "The name does not resolve for the supplied parameters";
206     case EAI_SERVICE:
207         return "servname not supported for ai_socktype";
208     case EAI_SOCKTYPE:
209         return "ai_socktype not supported";
210     }
211
212     return "Unknown error";
213 }
214 #endif /* !HAVE_GETADDRINFO || HAVE_WINSOCK2_H */
215
216 int ff_socket_nonblock(int socket, int enable)
217 {
218 #if HAVE_WINSOCK2_H
219     u_long param = enable;
220     return ioctlsocket(socket, FIONBIO, &param);
221 #else
222     if (enable)
223         return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
224     else
225         return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
226 #endif /* HAVE_WINSOCK2_H */
227 }
228
229 #if !HAVE_POLL_H
230 int ff_poll(struct pollfd *fds, nfds_t numfds, int timeout)
231 {
232     fd_set read_set;
233     fd_set write_set;
234     fd_set exception_set;
235     nfds_t i;
236     int n;
237     int rc;
238
239 #if HAVE_WINSOCK2_H
240     if (numfds >= FD_SETSIZE) {
241         errno = EINVAL;
242         return -1;
243     }
244 #endif /* HAVE_WINSOCK2_H */
245
246     FD_ZERO(&read_set);
247     FD_ZERO(&write_set);
248     FD_ZERO(&exception_set);
249
250     n = 0;
251     for (i = 0; i < numfds; i++) {
252         if (fds[i].fd < 0)
253             continue;
254 #if !HAVE_WINSOCK2_H
255         if (fds[i].fd >= FD_SETSIZE) {
256             errno = EINVAL;
257             return -1;
258         }
259 #endif /* !HAVE_WINSOCK2_H */
260
261         if (fds[i].events & POLLIN)
262             FD_SET(fds[i].fd, &read_set);
263         if (fds[i].events & POLLOUT)
264             FD_SET(fds[i].fd, &write_set);
265         if (fds[i].events & POLLERR)
266             FD_SET(fds[i].fd, &exception_set);
267
268         if (fds[i].fd >= n)
269             n = fds[i].fd + 1;
270     }
271
272     if (n == 0)
273         /* Hey!? Nothing to poll, in fact!!! */
274         return 0;
275
276     if (timeout < 0) {
277         rc = select(n, &read_set, &write_set, &exception_set, NULL);
278     } else {
279         struct timeval tv;
280         tv.tv_sec  = timeout / 1000;
281         tv.tv_usec = 1000 * (timeout % 1000);
282         rc         = select(n, &read_set, &write_set, &exception_set, &tv);
283     }
284
285     if (rc < 0)
286         return rc;
287
288     for (i = 0; i < numfds; i++) {
289         fds[i].revents = 0;
290
291         if (FD_ISSET(fds[i].fd, &read_set))
292             fds[i].revents |= POLLIN;
293         if (FD_ISSET(fds[i].fd, &write_set))
294             fds[i].revents |= POLLOUT;
295         if (FD_ISSET(fds[i].fd, &exception_set))
296             fds[i].revents |= POLLERR;
297     }
298
299     return rc;
300 }
301 #endif /* !HAVE_POLL_H */
302
303 #endif /* CONFIG_NETWORK */