]> git.sesse.net Git - ffmpeg/blob - libavformat/os_support.c
Add symbol versioning for shared libraries
[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     sin = av_mallocz(sizeof(struct sockaddr_in));
72     if (!sin)
73         return EAI_FAIL;
74     sin->sin_family = AF_INET;
75
76     if (node) {
77         if (!inet_aton(node, &sin->sin_addr)) {
78             if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
79                 av_free(sin);
80                 return EAI_FAIL;
81             }
82             h = gethostbyname(node);
83             if (!h) {
84                 av_free(sin);
85                 return EAI_FAIL;
86             }
87             memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
88         }
89     } else {
90         if (hints && (hints->ai_flags & AI_PASSIVE)) {
91             sin->sin_addr.s_addr = INADDR_ANY;
92         } else
93             sin->sin_addr.s_addr = INADDR_LOOPBACK;
94     }
95
96     /* Note: getaddrinfo allows service to be a string, which
97      * should be looked up using getservbyname. */
98     if (service)
99         sin->sin_port = htons(atoi(service));
100
101     ai = av_mallocz(sizeof(struct addrinfo));
102     if (!ai) {
103         av_free(sin);
104         return EAI_FAIL;
105     }
106
107     *res = ai;
108     ai->ai_family = AF_INET;
109     ai->ai_socktype = hints ? hints->ai_socktype : 0;
110     switch (ai->ai_socktype) {
111     case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
112     case SOCK_DGRAM:  ai->ai_protocol = IPPROTO_UDP; break;
113     default:          ai->ai_protocol = 0;           break;
114     }
115
116     ai->ai_addr = (struct sockaddr *)sin;
117     ai->ai_addrlen = sizeof(struct sockaddr_in);
118     if (hints && (hints->ai_flags & AI_CANONNAME))
119         ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
120
121     ai->ai_next = NULL;
122     return 0;
123 }
124
125 void ff_freeaddrinfo(struct addrinfo *res)
126 {
127     av_free(res->ai_canonname);
128     av_free(res->ai_addr);
129     av_free(res);
130 }
131
132 int ff_getnameinfo(const struct sockaddr *sa, int salen,
133                    char *host, int hostlen,
134                    char *serv, int servlen, int flags)
135 {
136     const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
137
138     if (sa->sa_family != AF_INET)
139         return EAI_FAMILY;
140     if (!host && !serv)
141         return EAI_NONAME;
142
143     if (host && hostlen > 0) {
144         struct hostent *ent = NULL;
145         uint32_t a;
146         if (!(flags & NI_NUMERICHOST))
147             ent = gethostbyaddr((const char *)&sin->sin_addr,
148                                 sizeof(sin->sin_addr), AF_INET);
149
150         if (ent) {
151             snprintf(host, hostlen, "%s", ent->h_name);
152         } else if (flags & NI_NAMERQD) {
153             return EAI_NONAME;
154         } else {
155             a = ntohl(sin->sin_addr.s_addr);
156             snprintf(host, hostlen, "%d.%d.%d.%d",
157                      ((a >> 24) & 0xff), ((a >> 16) & 0xff),
158                      ((a >>  8) & 0xff), ( a        & 0xff));
159         }
160     }
161
162     if (serv && servlen > 0) {
163         struct servent *ent = NULL;
164         if (!(flags & NI_NUMERICSERV))
165             ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
166
167         if (ent) {
168             snprintf(serv, servlen, "%s", ent->s_name);
169         } else
170             snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
171     }
172
173     return 0;
174 }
175 #endif
176
177 /* resolve host with also IP address parsing */
178 int resolve_host(struct in_addr *sin_addr, const char *hostname)
179 {
180
181     if (!inet_aton(hostname, sin_addr)) {
182 #if HAVE_GETADDRINFO
183         struct addrinfo *ai, *cur;
184         struct addrinfo hints;
185         memset(&hints, 0, sizeof(hints));
186         hints.ai_family = AF_INET;
187         if (getaddrinfo(hostname, NULL, &hints, &ai))
188             return -1;
189         /* getaddrinfo returns a linked list of addrinfo structs.
190          * Even if we set ai_family = AF_INET above, make sure
191          * that the returned one actually is of the correct type. */
192         for (cur = ai; cur; cur = cur->ai_next) {
193             if (cur->ai_family == AF_INET) {
194                 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
195                 freeaddrinfo(ai);
196                 return 0;
197             }
198         }
199         freeaddrinfo(ai);
200         return -1;
201 #else
202         struct hostent *hp;
203         hp = gethostbyname(hostname);
204         if (!hp)
205             return -1;
206         memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
207 #endif
208     }
209     return 0;
210 }
211
212 int ff_socket_nonblock(int socket, int enable)
213 {
214 #if HAVE_WINSOCK2_H
215    return ioctlsocket(socket, FIONBIO, &enable);
216 #else
217    if (enable)
218       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
219    else
220       return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
221 #endif
222 }
223 #endif /* CONFIG_NETWORK */
224
225 #if CONFIG_FFSERVER
226 #if !HAVE_POLL_H
227 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
228 {
229     fd_set read_set;
230     fd_set write_set;
231     fd_set exception_set;
232     nfds_t i;
233     int n;
234     int rc;
235
236 #if HAVE_WINSOCK2_H
237     if (numfds >= FD_SETSIZE) {
238         errno = EINVAL;
239         return -1;
240     }
241 #endif
242
243     FD_ZERO(&read_set);
244     FD_ZERO(&write_set);
245     FD_ZERO(&exception_set);
246
247     n = -1;
248     for(i = 0; i < numfds; i++) {
249         if (fds[i].fd < 0)
250             continue;
251 #if !HAVE_WINSOCK2_H
252         if (fds[i].fd >= FD_SETSIZE) {
253             errno = EINVAL;
254             return -1;
255         }
256 #endif
257
258         if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
259         if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
260         if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
261
262         if (fds[i].fd > n)
263             n = fds[i].fd;
264     };
265
266     if (n == -1)
267         /* Hey!? Nothing to poll, in fact!!! */
268         return 0;
269
270     if (timeout < 0)
271         rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
272     else {
273         struct timeval    tv;
274
275         tv.tv_sec = timeout / 1000;
276         tv.tv_usec = 1000 * (timeout % 1000);
277         rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
278     };
279
280     if (rc < 0)
281         return rc;
282
283     for(i = 0; i < (nfds_t) n; i++) {
284         fds[i].revents = 0;
285
286         if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
287         if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
288         if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
289     };
290
291     return rc;
292 }
293 #endif /* HAVE_POLL_H */
294 #endif /* CONFIG_FFSERVER */
295