OLD | NEW |
1 /* | 1 /* |
2 * Various utilities for ffmpeg system | 2 * Various utilities for ffmpeg system |
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard | 3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard |
4 * copyright (c) 2002 Francois Revol | 4 * copyright (c) 2002 Francois Revol |
5 * | 5 * |
6 * This file is part of FFmpeg. | 6 * This file is part of FFmpeg. |
7 * | 7 * |
8 * FFmpeg is free software; you can redistribute it and/or | 8 * FFmpeg is free software; you can redistribute it and/or |
9 * modify it under the terms of the GNU Lesser General Public | 9 * modify it under the terms of the GNU Lesser General Public |
10 * License as published by the Free Software Foundation; either | 10 * License as published by the Free Software Foundation; either |
11 * version 2.1 of the License, or (at your option) any later version. | 11 * version 2.1 of the License, or (at your option) any later version. |
12 * | 12 * |
13 * FFmpeg is distributed in the hope that it will be useful, | 13 * FFmpeg is distributed in the hope that it will be useful, |
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
16 * Lesser General Public License for more details. | 16 * Lesser General Public License for more details. |
17 * | 17 * |
18 * You should have received a copy of the GNU Lesser General Public | 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 | 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 | 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
21 */ | 21 */ |
22 | 22 |
23 /* needed by inet_aton() */ | 23 /* needed by inet_aton() */ |
24 #define _SVID_SOURCE | 24 #define _SVID_SOURCE |
| 25 #define _DARWIN_C_SOURCE |
25 | 26 |
26 #include "config.h" | 27 #include "config.h" |
27 #include "avformat.h" | 28 #include "avformat.h" |
28 #include <unistd.h> | 29 #include <unistd.h> |
29 #include <fcntl.h> | 30 #include <fcntl.h> |
30 #include <sys/time.h> | 31 #include <sys/time.h> |
31 #include "os_support.h" | 32 #include "os_support.h" |
32 | 33 |
33 #if CONFIG_NETWORK | 34 #if CONFIG_NETWORK |
34 #if !HAVE_POLL_H | 35 #if !HAVE_POLL_H |
35 #if HAVE_WINSOCK2_H | 36 #if HAVE_WINSOCK2_H |
36 #include <winsock2.h> | 37 #include <winsock2.h> |
37 #elif HAVE_SYS_SELECT_H | 38 #elif HAVE_SYS_SELECT_H |
38 #include <sys/select.h> | 39 #include <sys/select.h> |
39 #endif | 40 #endif |
40 #endif | 41 #endif |
41 | 42 |
42 #include "network.h" | 43 #include "network.h" |
43 | 44 |
44 #if !HAVE_INET_ATON | 45 #if !HAVE_INET_ATON |
45 #include <stdlib.h> | 46 #include <stdlib.h> |
46 #include <strings.h> | 47 #include <strings.h> |
47 | 48 |
48 int inet_aton (const char * str, struct in_addr * add) | 49 int ff_inet_aton (const char * str, struct in_addr * add) |
49 { | 50 { |
50 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0; | 51 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0; |
51 | 52 |
52 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4) | 53 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4) |
53 return 0; | 54 return 0; |
54 | 55 |
55 if (!add1 || (add1|add2|add3|add4) > 255) return 0; | 56 if (!add1 || (add1|add2|add3|add4) > 255) return 0; |
56 | 57 |
57 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4); | 58 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4); |
58 | 59 |
59 return 1; | 60 return 1; |
60 } | 61 } |
| 62 #else |
| 63 int ff_inet_aton (const char * str, struct in_addr * add) |
| 64 { |
| 65 return inet_aton(str, add); |
| 66 } |
61 #endif /* !HAVE_INET_ATON */ | 67 #endif /* !HAVE_INET_ATON */ |
62 | 68 |
63 #if !HAVE_GETADDRINFO | 69 #if !HAVE_GETADDRINFO |
64 int ff_getaddrinfo(const char *node, const char *service, | 70 int ff_getaddrinfo(const char *node, const char *service, |
65 const struct addrinfo *hints, struct addrinfo **res) | 71 const struct addrinfo *hints, struct addrinfo **res) |
66 { | 72 { |
67 struct hostent *h = NULL; | 73 struct hostent *h = NULL; |
68 struct addrinfo *ai; | 74 struct addrinfo *ai; |
69 struct sockaddr_in *sin; | 75 struct sockaddr_in *sin; |
70 | 76 |
71 #if HAVE_WINSOCK2_H | 77 #if HAVE_WINSOCK2_H |
72 int (WSAAPI *win_getaddrinfo)(const char *node, const char *service, | 78 int (WSAAPI *win_getaddrinfo)(const char *node, const char *service, |
73 const struct addrinfo *hints, | 79 const struct addrinfo *hints, |
74 struct addrinfo **res); | 80 struct addrinfo **res); |
75 HMODULE ws2mod = GetModuleHandle("ws2_32.dll"); | 81 HMODULE ws2mod = GetModuleHandle("ws2_32.dll"); |
76 win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo"); | 82 win_getaddrinfo = GetProcAddress(ws2mod, "getaddrinfo"); |
77 if (win_getaddrinfo) | 83 if (win_getaddrinfo) |
78 return win_getaddrinfo(node, service, hints, res); | 84 return win_getaddrinfo(node, service, hints, res); |
79 #endif | 85 #endif |
80 | 86 |
| 87 *res = NULL; |
81 sin = av_mallocz(sizeof(struct sockaddr_in)); | 88 sin = av_mallocz(sizeof(struct sockaddr_in)); |
82 if (!sin) | 89 if (!sin) |
83 return EAI_FAIL; | 90 return EAI_FAIL; |
84 sin->sin_family = AF_INET; | 91 sin->sin_family = AF_INET; |
85 | 92 |
86 if (node) { | 93 if (node) { |
87 if (!inet_aton(node, &sin->sin_addr)) { | 94 if (!ff_inet_aton(node, &sin->sin_addr)) { |
88 if (hints && (hints->ai_flags & AI_NUMERICHOST)) { | 95 if (hints && (hints->ai_flags & AI_NUMERICHOST)) { |
89 av_free(sin); | 96 av_free(sin); |
90 return EAI_FAIL; | 97 return EAI_FAIL; |
91 } | 98 } |
92 h = gethostbyname(node); | 99 h = gethostbyname(node); |
93 if (!h) { | 100 if (!h) { |
94 av_free(sin); | 101 av_free(sin); |
95 return EAI_FAIL; | 102 return EAI_FAIL; |
96 } | 103 } |
97 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr)); | 104 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr)); |
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
196 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp")
; | 203 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp")
; |
197 | 204 |
198 if (ent) { | 205 if (ent) { |
199 snprintf(serv, servlen, "%s", ent->s_name); | 206 snprintf(serv, servlen, "%s", ent->s_name); |
200 } else | 207 } else |
201 snprintf(serv, servlen, "%d", ntohs(sin->sin_port)); | 208 snprintf(serv, servlen, "%d", ntohs(sin->sin_port)); |
202 } | 209 } |
203 | 210 |
204 return 0; | 211 return 0; |
205 } | 212 } |
| 213 |
| 214 const char *ff_gai_strerror(int ecode) |
| 215 { |
| 216 switch(ecode) { |
| 217 case EAI_FAIL : return "A non-recoverable error occurred"; |
| 218 case EAI_FAMILY : return "The address family was not recognized or the addre
ss length was invalid for the specified family"; |
| 219 case EAI_NONAME : return "The name does not resolve for the supplied paramet
ers"; |
| 220 } |
| 221 |
| 222 return "Unknown error"; |
| 223 } |
206 #endif | 224 #endif |
207 | 225 |
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) | 226 int ff_socket_nonblock(int socket, int enable) |
244 { | 227 { |
245 #if HAVE_WINSOCK2_H | 228 #if HAVE_WINSOCK2_H |
246 return ioctlsocket(socket, FIONBIO, &enable); | 229 return ioctlsocket(socket, FIONBIO, &enable); |
247 #else | 230 #else |
248 if (enable) | 231 if (enable) |
249 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK); | 232 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK); |
250 else | 233 else |
251 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK); | 234 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK); |
252 #endif | 235 #endif |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
317 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN; | 300 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; | 301 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; | 302 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR; |
320 }; | 303 }; |
321 | 304 |
322 return rc; | 305 return rc; |
323 } | 306 } |
324 #endif /* HAVE_POLL_H */ | 307 #endif /* HAVE_POLL_H */ |
325 #endif /* CONFIG_FFSERVER */ | 308 #endif /* CONFIG_FFSERVER */ |
326 | 309 |
OLD | NEW |