2 * Various utilities for ffmpeg system
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 * copyright (c) 2002 Francois Revol
6 * This file is part of FFmpeg.
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.
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.
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
23 /* needed by inet_aton() */
31 #include "os_support.h"
37 #elif HAVE_SYS_SELECT_H
38 #include <sys/select.h>
48 int inet_aton (const char * str, struct in_addr * add)
50 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
52 if (sscanf(str, "%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
55 if (!add1 || (add1|add2|add3|add4) > 255) return 0;
57 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
61 #endif /* !HAVE_INET_ATON */
64 int ff_getaddrinfo(const char *node, const char *service,
65 const struct addrinfo *hints, struct addrinfo **res)
67 struct hostent *h = NULL;
69 struct sockaddr_in *sin;
71 sin = av_mallocz(sizeof(struct sockaddr_in));
74 sin->sin_family = AF_INET;
77 if (!inet_aton(node, &sin->sin_addr)) {
78 if (hints && (hints->ai_flags & AI_NUMERICHOST)) {
82 h = gethostbyname(node);
87 memcpy(&sin->sin_addr, h->h_addr_list[0], sizeof(struct in_addr));
90 if (hints && (hints->ai_flags & AI_PASSIVE)) {
91 sin->sin_addr.s_addr = INADDR_ANY;
93 sin->sin_addr.s_addr = INADDR_LOOPBACK;
96 /* Note: getaddrinfo allows service to be a string, which
97 * should be looked up using getservbyname. */
99 sin->sin_port = htons(atoi(service));
101 ai = av_mallocz(sizeof(struct addrinfo));
108 ai->ai_family = AF_INET;
109 ai->ai_socktype = hints ? hints->ai_socktype : 0;
110 switch (ai->ai_socktype) {
111 case SOCK_STREAM: ai->ai_protocol = IPPROTO_TCP; break;
112 case SOCK_DGRAM: ai->ai_protocol = IPPROTO_UDP; break;
113 default: ai->ai_protocol = 0; break;
116 ai->ai_addr = (struct sockaddr *)sin;
117 ai->ai_addrlen = sizeof(struct sockaddr_in);
118 if (hints && (hints->ai_flags & AI_CANONNAME))
119 ai->ai_canonname = h ? av_strdup(h->h_name) : NULL;
125 void ff_freeaddrinfo(struct addrinfo *res)
127 av_free(res->ai_canonname);
128 av_free(res->ai_addr);
132 int ff_getnameinfo(const struct sockaddr *sa, int salen,
133 char *host, int hostlen,
134 char *serv, int servlen, int flags)
136 const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
138 if (sa->sa_family != AF_INET)
143 if (host && hostlen > 0) {
144 struct hostent *ent = NULL;
146 if (!(flags & NI_NUMERICHOST))
147 ent = gethostbyaddr((const char *)&sin->sin_addr,
148 sizeof(sin->sin_addr), AF_INET);
151 snprintf(host, hostlen, "%s", ent->h_name);
152 } else if (flags & NI_NAMERQD) {
155 a = ntohl(sin->sin_addr.s_addr);
156 snprintf(host, hostlen, "%d.%d.%d.%d",
157 ((a >> 24) & 0xff), ((a >> 16) & 0xff),
158 ((a >> 8) & 0xff), ( a & 0xff));
162 if (serv && servlen > 0) {
163 struct servent *ent = NULL;
164 if (!(flags & NI_NUMERICSERV))
165 ent = getservbyport(sin->sin_port, flags & NI_DGRAM ? "udp" : "tcp");
168 snprintf(serv, servlen, "%s", ent->s_name);
170 snprintf(serv, servlen, "%d", ntohs(sin->sin_port));
177 /* resolve host with also IP address parsing */
178 int resolve_host(struct in_addr *sin_addr, const char *hostname)
181 if (!inet_aton(hostname, sin_addr)) {
183 struct addrinfo *ai, *cur;
184 struct addrinfo hints;
185 memset(&hints, 0, sizeof(hints));
186 hints.ai_family = AF_INET;
187 if (getaddrinfo(hostname, NULL, &hints, &ai))
189 /* getaddrinfo returns a linked list of addrinfo structs.
190 * Even if we set ai_family = AF_INET above, make sure
191 * that the returned one actually is of the correct type. */
192 for (cur = ai; cur; cur = cur->ai_next) {
193 if (cur->ai_family == AF_INET) {
194 *sin_addr = ((struct sockaddr_in *)cur->ai_addr)->sin_addr;
203 hp = gethostbyname(hostname);
206 memcpy(sin_addr, hp->h_addr_list[0], sizeof(struct in_addr));
212 int ff_socket_nonblock(int socket, int enable)
215 return ioctlsocket(socket, FIONBIO, &enable);
218 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
220 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
223 #endif /* CONFIG_NETWORK */
227 int poll(struct pollfd *fds, nfds_t numfds, int timeout)
231 fd_set exception_set;
237 if (numfds >= FD_SETSIZE) {
245 FD_ZERO(&exception_set);
248 for(i = 0; i < numfds; i++) {
252 if (fds[i].fd >= FD_SETSIZE) {
258 if (fds[i].events & POLLIN) FD_SET(fds[i].fd, &read_set);
259 if (fds[i].events & POLLOUT) FD_SET(fds[i].fd, &write_set);
260 if (fds[i].events & POLLERR) FD_SET(fds[i].fd, &exception_set);
267 /* Hey!? Nothing to poll, in fact!!! */
271 rc = select(n+1, &read_set, &write_set, &exception_set, NULL);
275 tv.tv_sec = timeout / 1000;
276 tv.tv_usec = 1000 * (timeout % 1000);
277 rc = select(n+1, &read_set, &write_set, &exception_set, &tv);
283 for(i = 0; i < (nfds_t) n; i++) {
286 if (FD_ISSET(fds[i].fd, &read_set)) fds[i].revents |= POLLIN;
287 if (FD_ISSET(fds[i].fd, &write_set)) fds[i].revents |= POLLOUT;
288 if (FD_ISSET(fds[i].fd, &exception_set)) fds[i].revents |= POLLERR;
293 #endif /* HAVE_POLL_H */
294 #endif /* CONFIG_FFSERVER */