Statistics
| Branch: | Revision:

ffmpeg / libavformat / os_support.c @ ac11d562

History | View | Annotate | Download (9.61 KB)

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 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) return 0;
56

    
57
    add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
58

    
59
    return 1;
60
}
61
#else
62
int ff_inet_aton (const char * str, struct in_addr * add)
63
{
64
    return inet_aton(str, add);
65
}
66
#endif /* !HAVE_INET_ATON */
67

    
68
#if !HAVE_GETADDRINFO
69
int ff_getaddrinfo(const char *node, const char *service,
70
                const struct addrinfo *hints, struct addrinfo **res)
71
{
72
    struct hostent *h = NULL;
73
    struct addrinfo *ai;
74
    struct sockaddr_in *sin;
75

    
76
#if HAVE_WINSOCK2_H
77
    int (WSAAPI *win_getaddrinfo)(const char *node, const char *service,
78
                                  const struct addrinfo *hints,
79
                                  struct addrinfo **res);
80
    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
81
    win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo");
82
    if (win_getaddrinfo)
83
        return win_getaddrinfo(node, service, hints, res);
84
#endif
85

    
86
    *res = NULL;
87
    sin = av_mallocz(sizeof(struct sockaddr_in));
88
    if (!sin)
89
        return EAI_FAIL;
90
    sin->sin_family = AF_INET;
91

    
92
    if (node) {
93
        if (!ff_inet_aton(node, &sin->sin_addr)) {
94
            if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
95
                av_free(sin);
96
                return EAI_FAIL;
97
            }
98
            h = gethostbyname(node);
99
            if (!h) {
100
                av_free(sin);
101
                return EAI_FAIL;
102
            }
103
            memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
104
        }
105
    } else {
106
        if (hints && (hints->ai_flags & AI_PASSIVE)) {
107
            sin->sin_addr.s_addr = INADDR_ANY;
108
        } else
109
            sin->sin_addr.s_addr = INADDR_LOOPBACK;
110
    }
111

    
112
    /* Note: getaddrinfo allows service to be a string, which
113
     * should be looked up using getservbyname. */
114
    if (service)
115
        sin->sin_port = htons(atoi(service));
116

    
117
    ai = av_mallocz(sizeof(struct addrinfo));
118
    if (!ai) {
119
        av_free(sin);
120
        return EAI_FAIL;
121
    }
122

    
123
    *res = ai;
124
    ai->ai_family = AF_INET;
125
    ai->ai_socktype = hints ? hints->ai_socktype : 0;
126
    switch (ai->ai_socktype) {
127
    case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
128
    case SOCK_DGRAM:  ai->ai_protocol = IPPROTO_UDP; break;
129
    default:          ai->ai_protocol = 0;           break;
130
    }
131

    
132
    ai->ai_addr = (struct sockaddr *)sin;
133
    ai->ai_addrlen = sizeof(struct sockaddr_in);
134
    if (hints && (hints->ai_flags & AI_CANONNAME))
135
        ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
136

    
137
    ai->ai_next = NULL;
138
    return 0;
139
}
140

    
141
void ff_freeaddrinfo(struct addrinfo *res)
142
{
143
#if HAVE_WINSOCK2_H
144
    void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
145
    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
146
    win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
147
                       GetProcAddress(ws2mod, "freeaddrinfo");
148
    if (win_freeaddrinfo) {
149
        win_freeaddrinfo(res);
150
        return;
151
    }
152
#endif
153

    
154
    av_free(res->ai_canonname);
155
    av_free(res->ai_addr);
156
    av_free(res);
157
}
158

    
159
int ff_getnameinfo(const struct sockaddr *sa, int salen,
160
                   char *host, int hostlen,
161
                   char *serv, int servlen, int flags)
162
{
163
    const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
164

    
165
#if HAVE_WINSOCK2_H
166
    int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
167
                                  char *host, DWORD hostlen,
168
                                  char *serv, DWORD servlen, int flags);
169
    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
170
    win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
171
    if (win_getnameinfo)
172
        return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
173
#endif
174

    
175
    if (sa->sa_family != AF_INET)
176
        return EAI_FAMILY;
177
    if (!host && !serv)
178
        return EAI_NONAME;
179

    
180
    if (host && hostlen > 0) {
181
        struct hostent *ent = NULL;
182
        uint32_t a;
183
        if (!(flags & NI_NUMERICHOST))
184
            ent = gethostbyaddr((const char *)&sin->sin_addr,
185
                                sizeof(sin->sin_addr), AF_INET);
186

    
187
        if (ent) {
188
            snprintf(host, hostlen, "%s", ent->h_name);
189
        } else if (flags & NI_NAMERQD) {
190
            return EAI_NONAME;
191
        } else {
192
            a = ntohl(sin->sin_addr.s_addr);
193
            snprintf(host, hostlen, "%d.%d.%d.%d",
194
                     ((a >> 24) & 0xff), ((a >> 16) & 0xff),
195
                     ((a >>  8) & 0xff), ( a        & 0xff));
196
        }
197
    }
198

    
199
    if (serv && servlen > 0) {
200
        struct servent *ent = NULL;
201
        if (!(flags & NI_NUMERICSERV))
202
            ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
203

    
204
        if (ent) {
205
            snprintf(serv, servlen, "%s", ent->s_name);
206
        } else
207
            snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
208
    }
209

    
210
    return 0;
211
}
212

    
213
const char *ff_gai_strerror(int ecode)
214
{
215
    switch(ecode) {
216
    case EAI_FAIL   : return "A non-recoverable error occurred";
217
    case EAI_FAMILY : return "The address family was not recognized or the address length was invalid for the specified family";
218
    case EAI_NONAME : return "The name does not resolve for the supplied parameters";
219
    }
220

    
221
    return "Unknown error";
222
}
223
#endif
224

    
225
/* resolve host with also IP address parsing */
226
int resolve_host(struct in_addr *sin_addr, const char *hostname)
227
{
228

    
229
    if (!ff_inet_aton(hostname, sin_addr)) {
230
#if HAVE_GETADDRINFO
231
        struct addrinfo *ai, *cur;
232
        struct addrinfo hints;
233
        memset(&hints, 0, sizeof(hints));
234
        hints.ai_family = AF_INET;
235
        if (getaddrinfo(hostname, NULL, &hints, &ai))
236
            return -1;
237
        /* getaddrinfo returns a linked list of addrinfo structs.
238
         * Even if we set ai_family = AF_INET above, make sure
239
         * that the returned one actually is of the correct type. */
240
        for (cur = ai; cur; cur = cur->ai_next) {
241
            if (cur->ai_family == AF_INET) {
242
                *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
243
                freeaddrinfo(ai);
244
                return 0;
245
            }
246
        }
247
        freeaddrinfo(ai);
248
        return -1;
249
#else
250
        struct hostent *hp;
251
        hp = gethostbyname(hostname);
252
        if (!hp)
253
            return -1;
254
        memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
255
#endif
256
    }
257
    return 0;
258
}
259

    
260
int ff_socket_nonblock(int socket, int enable)
261
{
262
#if HAVE_WINSOCK2_H
263
   return ioctlsocket(socket, FIONBIO, &enable);
264
#else
265
   if (enable)
266
      return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
267
   else
268
      return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
269
#endif
270
}
271
#endif /* CONFIG_NETWORK */
272

    
273
#if CONFIG_FFSERVER
274
#if !HAVE_POLL_H
275
int poll(struct pollfd *fds, nfds_t numfds, int timeout)
276
{
277
    fd_set read_set;
278
    fd_set write_set;
279
    fd_set exception_set;
280
    nfds_t i;
281
    int n;
282
    int rc;
283

    
284
#if HAVE_WINSOCK2_H
285
    if (numfds >= FD_SETSIZE) {
286
        errno = EINVAL;
287
        return -1;
288
    }
289
#endif
290

    
291
    FD_ZERO(&read_set);
292
    FD_ZERO(&write_set);
293
    FD_ZERO(&exception_set);
294

    
295
    n = -1;
296
    for(i = 0; i < numfds; i++) {
297
        if (fds[i].fd < 0)
298
            continue;
299
#if !HAVE_WINSOCK2_H
300
        if (fds[i].fd >= FD_SETSIZE) {
301
            errno = EINVAL;
302
            return -1;
303
        }
304
#endif
305

    
306
        if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
307
        if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
308
        if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
309

    
310
        if (fds[i].fd > n)
311
            n = fds[i].fd;
312
    };
313

    
314
    if (n == -1)
315
        /* Hey!? Nothing to poll, in fact!!! */
316
        return 0;
317

    
318
    if (timeout < 0)
319
        rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
320
    else {
321
        struct timeval    tv;
322

    
323
        tv.tv_sec = timeout / 1000;
324
        tv.tv_usec = 1000 * (timeout % 1000);
325
        rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
326
    };
327

    
328
    if (rc < 0)
329
        return rc;
330

    
331
    for(i = 0; i < (nfds_t) n; i++) {
332
        fds[i].revents = 0;
333

    
334
        if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
335
        if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
336
        if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
337
    };
338

    
339
    return rc;
340
}
341
#endif /* HAVE_POLL_H */
342
#endif /* CONFIG_FFSERVER */
343