Mercurial > libavformat.hg
annotate os_support.c @ 5515:fa3115c9b043 libavformat
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
Storsj <$firstname()$firstname,st>.
author | rbultje |
---|---|
date | Mon, 11 Jan 2010 17:14:16 +0000 |
parents | 166bc62c1d57 |
children | afe4a96b6832 |
rev | line source |
---|---|
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
1 /* |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
2 * Various utilities for ffmpeg system |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard |
1506 | 4 * copyright (c) 2002 Francois Revol |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
5 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1171
diff
changeset
|
6 * This file is part of FFmpeg. |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1171
diff
changeset
|
7 * |
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1171
diff
changeset
|
8 * FFmpeg is free software; you can redistribute it and/or |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
9 * modify it under the terms of the GNU Lesser General Public |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
10 * License as published by the Free Software Foundation; either |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1171
diff
changeset
|
11 * version 2.1 of the License, or (at your option) any later version. |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
12 * |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1171
diff
changeset
|
13 * FFmpeg is distributed in the hope that it will be useful, |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
16 * Lesser General Public License for more details. |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
17 * |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
18 * You should have received a copy of the GNU Lesser General Public |
1358
0899bfe4105c
Change license headers to say 'FFmpeg' instead of 'this program/this library'
diego
parents:
1171
diff
changeset
|
19 * License along with FFmpeg; if not, write to the Free Software |
896
edbe5c3717f9
Update licensing information: The FSF changed postal address.
diego
parents:
885
diff
changeset
|
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
21 */ |
3722
68749aaea50f
ensure we get explicit definition of various _XOPEN_SOURCE functions we use
aurel
parents:
3039
diff
changeset
|
22 |
68749aaea50f
ensure we get explicit definition of various _XOPEN_SOURCE functions we use
aurel
parents:
3039
diff
changeset
|
23 /* needed by inet_aton() */ |
68749aaea50f
ensure we get explicit definition of various _XOPEN_SOURCE functions we use
aurel
parents:
3039
diff
changeset
|
24 #define _SVID_SOURCE |
68749aaea50f
ensure we get explicit definition of various _XOPEN_SOURCE functions we use
aurel
parents:
3039
diff
changeset
|
25 |
443
afbe3a5aae02
libavformat/os_support.c HAVE_LOCALTIME_R patch by ("Steven M. Schultz" <sms at 2BSD dot COM>)
michael
parents:
230
diff
changeset
|
26 #include "config.h" |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
27 #include "avformat.h" |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
28 #include <unistd.h> |
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
29 #include <fcntl.h> |
3732 | 30 #include <sys/time.h> |
2773 | 31 #include "os_support.h" |
1798
d1e61f4d02cd
MinGW patch by <koyama AT infocity DOT co DOT jp>, reworked. Removed placeholder for winsock.
mmu_man
parents:
1797
diff
changeset
|
32 |
4206 | 33 #if CONFIG_NETWORK |
34 #if !HAVE_POLL_H | |
35 #if HAVE_WINSOCK2_H | |
1798
d1e61f4d02cd
MinGW patch by <koyama AT infocity DOT co DOT jp>, reworked. Removed placeholder for winsock.
mmu_man
parents:
1797
diff
changeset
|
36 #include <winsock2.h> |
4206 | 37 #elif HAVE_SYS_SELECT_H |
1797 | 38 #include <sys/select.h> |
39 #endif | |
1798
d1e61f4d02cd
MinGW patch by <koyama AT infocity DOT co DOT jp>, reworked. Removed placeholder for winsock.
mmu_man
parents:
1797
diff
changeset
|
40 #endif |
230
9f4f4ca9f7b5
simpler strptime - added os_support.[ch] - moved localtime_r to os_support.c
bellard
parents:
diff
changeset
|
41 |
2050
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
42 #include "network.h" |
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
43 |
4206 | 44 #if !HAVE_INET_ATON |
1511
6d9dd5b5e764
MinGW compilation fix, inet_aton and #includes depend on networking support.
diego
parents:
1506
diff
changeset
|
45 #include <stdlib.h> |
6d9dd5b5e764
MinGW compilation fix, inet_aton and #includes depend on networking support.
diego
parents:
1506
diff
changeset
|
46 #include <strings.h> |
6d9dd5b5e764
MinGW compilation fix, inet_aton and #includes depend on networking support.
diego
parents:
1506
diff
changeset
|
47 |
1506 | 48 int inet_aton (const char * str, struct in_addr * add) |
49 { | |
50 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0; | |
51 | |
2271 | 52 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4) |
53 return 0; | |
1506 | 54 |
2269
dfccc156e185
fix emulated inet_aton so that it fails for invalid addresses
benoit
parents:
2267
diff
changeset
|
55 if (!add1 || (add1|add2|add3|add4) > 255) return 0; |
dfccc156e185
fix emulated inet_aton so that it fails for invalid addresses
benoit
parents:
2267
diff
changeset
|
56 |
5505
166bc62c1d57
Currently, the replacement which is used if inet_aton isn't available,
rbultje
parents:
4206
diff
changeset
|
57 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4); |
1506 | 58 |
59 return 1; | |
60 } | |
4206 | 61 #endif /* !HAVE_INET_ATON */ |
2050
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
62 |
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
63 /* resolve host with also IP address parsing */ |
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
64 int resolve_host(struct in_addr *sin_addr, const char *hostname) |
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
65 { |
2052 | 66 |
2050
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
67 if (!inet_aton(hostname, sin_addr)) { |
5515
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
68 #if HAVE_GETADDRINFO |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
69 struct addrinfo *ai, *cur; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
70 struct addrinfo hints; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
71 memset(&hints, 0, sizeof(hints)); |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
72 hints.ai_family = AF_INET; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
73 if (getaddrinfo(hostname, NULL, &hints, &ai)) |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
74 return -1; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
75 /* getaddrinfo returns a linked list of addrinfo structs. |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
76 * Even if we set ai_family = AF_INET above, make sure |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
77 * that the returned one actually is of the correct type. */ |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
78 for (cur = ai; cur; cur = cur->ai_next) { |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
79 if (cur->ai_family == AF_INET) { |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
80 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
81 freeaddrinfo(ai); |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
82 return 0; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
83 } |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
84 } |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
85 freeaddrinfo(ai); |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
86 return -1; |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
87 #else |
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
88 struct hostent *hp; |
2052 | 89 hp = gethostbyname(hostname); |
90 if (!hp) | |
91 return -1; | |
3723
81726233f9b3
use h_addr_list[0] instead of h_addr as this is the posix hostent field name
aurel
parents:
3722
diff
changeset
|
92 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr)); |
5515
fa3115c9b043
Use getaddrinfo(), if available, in resolve_host(). Patch by Martin
rbultje
parents:
5505
diff
changeset
|
93 #endif |
2050
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
94 } |
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
95 return 0; |
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
96 } |
2057
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
97 |
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
98 int ff_socket_nonblock(int socket, int enable) |
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
99 { |
4206 | 100 #if HAVE_WINSOCK2_H |
2085 | 101 return ioctlsocket(socket, FIONBIO, &enable); |
102 #else | |
2057
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
103 if (enable) |
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
104 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK); |
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
105 else |
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
106 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK); |
2085 | 107 #endif |
2057
857fbfeb2fa0
implement ff_socket_nonblock and use it in networking code
alex
parents:
2052
diff
changeset
|
108 } |
2050
612d19cb9550
move resolve_host from tcp.c to os_support.c as it is used widely
alex
parents:
2025
diff
changeset
|
109 #endif /* CONFIG_NETWORK */ |
1797 | 110 |
4206 | 111 #if CONFIG_FFSERVER |
112 #if !HAVE_POLL_H | |
1797 | 113 int poll(struct pollfd *fds, nfds_t numfds, int timeout) |
114 { | |
115 fd_set read_set; | |
116 fd_set write_set; | |
117 fd_set exception_set; | |
118 nfds_t i; | |
119 int n; | |
120 int rc; | |
121 | |
4206 | 122 #if HAVE_WINSOCK2_H |
2092 | 123 if (numfds >= FD_SETSIZE) { |
124 errno = EINVAL; | |
125 return -1; | |
126 } | |
2093 | 127 #endif |
2092 | 128 |
1797 | 129 FD_ZERO(&read_set); |
130 FD_ZERO(&write_set); | |
131 FD_ZERO(&exception_set); | |
132 | |
133 n = -1; | |
134 for(i = 0; i < numfds; i++) { | |
135 if (fds[i].fd < 0) | |
136 continue; | |
4206 | 137 #if !HAVE_WINSOCK2_H |
2093 | 138 if (fds[i].fd >= FD_SETSIZE) { |
139 errno = EINVAL; | |
140 return -1; | |
141 } | |
142 #endif | |
1797 | 143 |
144 if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set); | |
145 if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set); | |
146 if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set); | |
147 | |
148 if (fds[i].fd > n) | |
149 n = fds[i].fd; | |
150 }; | |
151 | |
152 if (n == -1) | |
153 /* Hey!? Nothing to poll, in fact!!! */ | |
154 return 0; | |
155 | |
156 if (timeout < 0) | |
157 rc = select(n+1, &read_set, &write_set, &exception_set, NULL); | |
158 else { | |
159 struct timeval tv; | |
160 | |
161 tv.tv_sec = timeout / 1000; | |
162 tv.tv_usec = 1000 * (timeout % 1000); | |
163 rc = select(n+1, &read_set, &write_set, &exception_set, &tv); | |
164 }; | |
165 | |
166 if (rc < 0) | |
167 return rc; | |
168 | |
169 for(i = 0; i < (nfds_t) n; i++) { | |
170 fds[i].revents = 0; | |
171 | |
172 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN; | |
173 if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT; | |
174 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR; | |
175 }; | |
176 | |
177 return rc; | |
178 } | |
3039 | 179 #endif /* HAVE_POLL_H */ |
1799
0dc3a767943d
Make sure we don't try to emulate poll() when it's there if --disable-ffserver... 10l
mmu_man
parents:
1798
diff
changeset
|
180 #endif /* CONFIG_FFSERVER */ |
1797 | 181 |