Statistics
| Branch: | Revision:

ffmpeg / libavformat / os_support.c @ 6023d84a

History | View | Annotate | Download (9.12 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 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
    sin = av_mallocz(sizeof(struct sockaddr_in));
82
    if (!sin)
83
        return EAI_FAIL;
84
    sin->sin_family = AF_INET;
85

    
86
    if (node) {
87
        if (!inet_aton(node, &sin->sin_addr)) {
88
            if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
89
                av_free(sin);
90
                return EAI_FAIL;
91
            }
92
            h = gethostbyname(node);
93
            if (!h) {
94
                av_free(sin);
95
                return EAI_FAIL;
96
            }
97
            memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
98
        }
99
    } else {
100
        if (hints && (hints->ai_flags & AI_PASSIVE)) {
101
            sin->sin_addr.s_addr = INADDR_ANY;
102
        } else
103
            sin->sin_addr.s_addr = INADDR_LOOPBACK;
104
    }
105

    
106
    /* Note: getaddrinfo allows service to be a string, which
107
     * should be looked up using getservbyname. */
108
    if (service)
109
        sin->sin_port = htons(atoi(service));
110

    
111
    ai = av_mallocz(sizeof(struct addrinfo));
112
    if (!ai) {
113
        av_free(sin);
114
        return EAI_FAIL;
115
    }
116

    
117
    *res = ai;
118
    ai->ai_family = AF_INET;
119
    ai->ai_socktype = hints ? hints->ai_socktype : 0;
120
    switch (ai->ai_socktype) {
121
    case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
122
    case SOCK_DGRAM:  ai->ai_protocol = IPPROTO_UDP; break;
123
    default:          ai->ai_protocol = 0;           break;
124
    }
125

    
126
    ai->ai_addr = (struct sockaddr *)sin;
127
    ai->ai_addrlen = sizeof(struct sockaddr_in);
128
    if (hints && (hints->ai_flags & AI_CANONNAME))
129
        ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
130

    
131
    ai->ai_next = NULL;
132
    return 0;
133
}
134

    
135
void ff_freeaddrinfo(struct addrinfo *res)
136
{
137
#if HAVE_WINSOCK2_H
138
    void (WSAAPI *win_freeaddrinfo)(struct addrinfo *res);
139
    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
140
    win_freeaddrinfo = (void (WSAAPI *)(struct addrinfo *res))
141
                       GetProcAddress(ws2mod, "freeaddrinfo");
142
    if (win_freeaddrinfo) {
143
        win_freeaddrinfo(res);
144
        return;
145
    }
146
#endif
147

    
148
    av_free(res->ai_canonname);
149
    av_free(res->ai_addr);
150
    av_free(res);
151
}
152

    
153
int ff_getnameinfo(const struct sockaddr *sa, int salen,
154
                   char *host, int hostlen,
155
                   char *serv, int servlen, int flags)
156
{
157
    const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
158

    
159
#if HAVE_WINSOCK2_H
160
    int (WSAAPI *win_getnameinfo)(const struct sockaddr *sa, socklen_t salen,
161
                                  char *host, DWORD hostlen,
162
                                  char *serv, DWORD servlen, int flags);
163
    HMODULE ws2mod = GetModuleHandle("ws2_32.dll");
164
    win_getnameinfo = GetProcAddress(ws2mod, "getnameinfo");
165
    if (win_getnameinfo)
166
        return win_getnameinfo(sa, salen, host, hostlen, serv, servlen, flags);
167
#endif
168

    
169
    if (sa->sa_family != AF_INET)
170
        return EAI_FAMILY;
171
    if (!host && !serv)
172
        return EAI_NONAME;
173

    
174
    if (host && hostlen > 0) {
175
        struct hostent *ent = NULL;
176
        uint32_t a;
177
        if (!(flags & NI_NUMERICHOST))
178
            ent = gethostbyaddr((const char *)&sin->sin_addr,
179
                                sizeof(sin->sin_addr), AF_INET);
180

    
181
        if (ent) {
182
            snprintf(host, hostlen, "%s", ent->h_name);
183
        } else if (flags & NI_NAMERQD) {
184
            return EAI_NONAME;
185
        } else {
186
            a = ntohl(sin->sin_addr.s_addr);
187
            snprintf(host, hostlen, "%d.%d.%d.%d",
188
                     ((a >> 24) & 0xff), ((a >> 16) & 0xff),
189
                     ((a >>  8) & 0xff), ( a        & 0xff));
190
        }
191
    }
192

    
193
    if (serv && servlen > 0) {
194
        struct servent *ent = NULL;
195
        if (!(flags & NI_NUMERICSERV))
196
            ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
197

    
198
        if (ent) {
199
            snprintf(serv, servlen, "%s", ent->s_name);
200
        } else
201
            snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
202
    }
203

    
204
    return 0;
205
}
206
#endif
207

    
208
/* resolve host with also IP address parsing */
209
int resolve_host(struct in_addr *sin_addr, const char *hostname)
210
{
211

    
212
    if (!inet_aton(hostname, sin_addr)) {
213
#if HAVE_GETADDRINFO
214
        struct addrinfo *ai, *cur;
215
        struct addrinfo hints;
216
        memset(&hints, 0, sizeof(hints));
217
        hints.ai_family = AF_INET;
218
        if (getaddrinfo(hostname, NULL, &hints, &ai))
219
            return -1;
220
        /* getaddrinfo returns a linked list of addrinfo structs.
221
         * Even if we set ai_family = AF_INET above, make sure
222
         * that the returned one actually is of the correct type. */
223
        for (cur = ai; cur; cur = cur->ai_next) {
224
            if (cur->ai_family == AF_INET) {
225
                *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
226
                freeaddrinfo(ai);
227
                return 0;
228
            }
229
        }
230
        freeaddrinfo(ai);
231
        return -1;
232
#else
233
        struct hostent *hp;
234
        hp = gethostbyname(hostname);
235
        if (!hp)
236
            return -1;
237
        memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
238
#endif
239
    }
240
    return 0;
241
}
242

    
243
int ff_socket_nonblock(int socket, int enable)
244
{
245
#if HAVE_WINSOCK2_H
246
   return ioctlsocket(socket, FIONBIO, &enable);
247
#else
248
   if (enable)
249
      return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
250
   else
251
      return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
252
#endif
253
}
254
#endif /* CONFIG_NETWORK */
255

    
256
#if CONFIG_FFSERVER
257
#if !HAVE_POLL_H
258
int poll(struct pollfd *fds, nfds_t numfds, int timeout)
259
{
260
    fd_set read_set;
261
    fd_set write_set;
262
    fd_set exception_set;
263
    nfds_t i;
264
    int n;
265
    int rc;
266

    
267
#if HAVE_WINSOCK2_H
268
    if (numfds >= FD_SETSIZE) {
269
        errno = EINVAL;
270
        return -1;
271
    }
272
#endif
273

    
274
    FD_ZERO(&read_set);
275
    FD_ZERO(&write_set);
276
    FD_ZERO(&exception_set);
277

    
278
    n = -1;
279
    for(i = 0; i < numfds; i++) {
280
        if (fds[i].fd < 0)
281
            continue;
282
#if !HAVE_WINSOCK2_H
283
        if (fds[i].fd >= FD_SETSIZE) {
284
            errno = EINVAL;
285
            return -1;
286
        }
287
#endif
288

    
289
        if (fds[i].events & POLLIN)  FD_SET(fds[i].fd, &read_set);
290
        if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
291
        if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
292

    
293
        if (fds[i].fd > n)
294
            n = fds[i].fd;
295
    };
296

    
297
    if (n == -1)
298
        /* Hey!? Nothing to poll, in fact!!! */
299
        return 0;
300

    
301
    if (timeout < 0)
302
        rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
303
    else {
304
        struct timeval    tv;
305

    
306
        tv.tv_sec = timeout / 1000;
307
        tv.tv_usec = 1000 * (timeout % 1000);
308
        rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
309
    };
310

    
311
    if (rc < 0)
312
        return rc;
313

    
314
    for(i = 0; i < (nfds_t) n; i++) {
315
        fds[i].revents = 0;
316

    
317
        if (FD_ISSET(fds[i].fd, &read_set))      fds[i].revents |= POLLIN;
318
        if (FD_ISSET(fds[i].fd, &write_set))     fds[i].revents |= POLLOUT;
319
        if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
320
    };
321

    
322
    return rc;
323
}
324
#endif /* HAVE_POLL_H */
325
#endif /* CONFIG_FFSERVER */
326