Statistics
| Branch: | Revision:

ffmpeg / libavformat / network.h @ ebba2b3e

History | View | Annotate | Download (4.39 KB)

1
/*
2
 * Copyright (c) 2007 The Libav Project
3
 *
4
 * This file is part of Libav.
5
 *
6
 * Libav is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * Libav is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with Libav; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20

    
21
#ifndef AVFORMAT_NETWORK_H
22
#define AVFORMAT_NETWORK_H
23

    
24
#include "config.h"
25

    
26
#if HAVE_WINSOCK2_H
27
#include <winsock2.h>
28
#include <ws2tcpip.h>
29

    
30
#define EPROTONOSUPPORT WSAEPROTONOSUPPORT
31
#define ETIMEDOUT       WSAETIMEDOUT
32
#define ECONNREFUSED    WSAECONNREFUSED
33
#define EINPROGRESS     WSAEINPROGRESS
34

    
35
static inline int ff_neterrno() {
36
    int err = WSAGetLastError();
37
    switch (err) {
38
    case WSAEWOULDBLOCK:
39
        return AVERROR(EAGAIN);
40
    case WSAEINTR:
41
        return AVERROR(EINTR);
42
    }
43
    return -err;
44
}
45
#else
46
#include <sys/types.h>
47
#include <sys/socket.h>
48
#include <netinet/in.h>
49
#include <netdb.h>
50

    
51
#define ff_neterrno() AVERROR(errno)
52
#endif
53

    
54
#if HAVE_ARPA_INET_H
55
#include <arpa/inet.h>
56
#endif
57

    
58
#if HAVE_POLL_H
59
#include <poll.h>
60
#endif
61

    
62
int ff_socket_nonblock(int socket, int enable);
63

    
64
static inline int ff_network_init(void)
65
{
66
#if HAVE_WINSOCK2_H
67
    WSADATA wsaData;
68
    if (WSAStartup(MAKEWORD(1,1), &wsaData))
69
        return 0;
70
#endif
71
    return 1;
72
}
73

    
74
static inline int ff_network_wait_fd(int fd, int write)
75
{
76
    int ev = write ? POLLOUT : POLLIN;
77
    struct pollfd p = { .fd = fd, .events = ev, .revents = 0 };
78
    int ret;
79
    ret = poll(&p, 1, 100);
80
    return ret < 0 ? ff_neterrno() : p.revents & ev ? 0 : AVERROR(EAGAIN);
81
}
82

    
83
static inline void ff_network_close(void)
84
{
85
#if HAVE_WINSOCK2_H
86
    WSACleanup();
87
#endif
88
}
89

    
90
int ff_inet_aton (const char * str, struct in_addr * add);
91

    
92
#if !HAVE_STRUCT_SOCKADDR_STORAGE
93
struct sockaddr_storage {
94
#if HAVE_STRUCT_SOCKADDR_SA_LEN
95
    uint8_t ss_len;
96
    uint8_t ss_family;
97
#else
98
    uint16_t ss_family;
99
#endif
100
    char ss_pad1[6];
101
    int64_t ss_align;
102
    char ss_pad2[112];
103
};
104
#endif
105

    
106
#if !HAVE_STRUCT_ADDRINFO
107
struct addrinfo {
108
    int ai_flags;
109
    int ai_family;
110
    int ai_socktype;
111
    int ai_protocol;
112
    int ai_addrlen;
113
    struct sockaddr *ai_addr;
114
    char *ai_canonname;
115
    struct addrinfo *ai_next;
116
};
117
#endif
118

    
119
/* getaddrinfo constants */
120
#ifndef EAI_FAIL
121
#define EAI_FAIL 4
122
#endif
123

    
124
#ifndef EAI_FAMILY
125
#define EAI_FAMILY 5
126
#endif
127

    
128
#ifndef EAI_NONAME
129
#define EAI_NONAME 8
130
#endif
131

    
132
#ifndef AI_PASSIVE
133
#define AI_PASSIVE 1
134
#endif
135

    
136
#ifndef AI_CANONNAME
137
#define AI_CANONNAME 2
138
#endif
139

    
140
#ifndef AI_NUMERICHOST
141
#define AI_NUMERICHOST 4
142
#endif
143

    
144
#ifndef NI_NOFQDN
145
#define NI_NOFQDN 1
146
#endif
147

    
148
#ifndef NI_NUMERICHOST
149
#define NI_NUMERICHOST 2
150
#endif
151

    
152
#ifndef NI_NAMERQD
153
#define NI_NAMERQD 4
154
#endif
155

    
156
#ifndef NI_NUMERICSERV
157
#define NI_NUMERICSERV 8
158
#endif
159

    
160
#ifndef NI_DGRAM
161
#define NI_DGRAM 16
162
#endif
163

    
164
#if !HAVE_GETADDRINFO
165
int ff_getaddrinfo(const char *node, const char *service,
166
                   const struct addrinfo *hints, struct addrinfo **res);
167
void ff_freeaddrinfo(struct addrinfo *res);
168
int ff_getnameinfo(const struct sockaddr *sa, int salen,
169
                   char *host, int hostlen,
170
                   char *serv, int servlen, int flags);
171
const char *ff_gai_strerror(int ecode);
172
#define getaddrinfo ff_getaddrinfo
173
#define freeaddrinfo ff_freeaddrinfo
174
#define getnameinfo ff_getnameinfo
175
#define gai_strerror ff_gai_strerror
176
#endif
177

    
178
#ifndef INET6_ADDRSTRLEN
179
#define INET6_ADDRSTRLEN INET_ADDRSTRLEN
180
#endif
181

    
182
#ifndef IN_MULTICAST
183
#define IN_MULTICAST(a) ((((uint32_t)(a)) & 0xf0000000) == 0xe0000000)
184
#endif
185
#ifndef IN6_IS_ADDR_MULTICAST
186
#define IN6_IS_ADDR_MULTICAST(a) (((uint8_t *) (a))[0] == 0xff)
187
#endif
188

    
189
static inline int ff_is_multicast_address(struct sockaddr *addr)
190
{
191
    if (addr->sa_family == AF_INET) {
192
        return IN_MULTICAST(ntohl(((struct sockaddr_in *)addr)->sin_addr.s_addr));
193
    }
194
#if HAVE_STRUCT_SOCKADDR_IN6
195
    if (addr->sa_family == AF_INET6) {
196
        return IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *)addr)->sin6_addr);
197
    }
198
#endif
199

    
200
    return 0;
201
}
202

    
203
#endif /* AVFORMAT_NETWORK_H */