24 #define _DEFAULT_SOURCE
38 #include <sys/select.h>
48 static 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)
58 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
67 struct hostent *
h =
NULL;
69 struct sockaddr_in *sin;
75 sin->sin_family = AF_INET;
78 if (!inet_aton(node, &sin->sin_addr)) {
83 h = gethostbyname(node);
88 memcpy(&sin->sin_addr,
h->h_addr_list[0],
sizeof(
struct in_addr));
92 sin->sin_addr.s_addr = INADDR_ANY;
100 sin->sin_port = htons(atoi(service));
123 ai->
ai_addr = (
struct sockaddr *)sin;
140 char *host,
int hostlen,
141 char *serv,
int servlen,
int flags)
143 const struct sockaddr_in *sin = (
const struct sockaddr_in *)sa;
145 if (sa->sa_family != AF_INET)
150 if (host && hostlen > 0) {
151 struct hostent *ent =
NULL;
154 ent = gethostbyaddr((
const char *)&sin->sin_addr,
155 sizeof(sin->sin_addr), AF_INET);
158 snprintf(host, hostlen,
"%s", ent->h_name);
162 a = ntohl(sin->sin_addr.s_addr);
163 snprintf(host, hostlen,
"%d.%d.%d.%d",
164 ((
a >> 24) & 0xff), ((
a >> 16) & 0xff),
165 ((
a >> 8) & 0xff), (
a & 0xff));
169 if (serv && servlen > 0) {
172 snprintf(serv, servlen,
"%d", ntohs(sin->sin_port));
179 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
184 return "Temporary failure in name resolution";
186 return "Invalid flags for ai_flags";
188 return "A non-recoverable error occurred";
190 return "The address family was not recognized or the address "
191 "length was invalid for the specified family";
193 return "Memory allocation failure";
194 #if EAI_NODATA != EAI_NONAME
196 return "No address associated with hostname";
199 return "The name does not resolve for the supplied parameters";
201 return "servname not supported for ai_socktype";
203 return "ai_socktype not supported";
206 return "Unknown error";
213 u_long param = enable;
214 return ioctlsocket(socket, FIONBIO, ¶m);
217 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
219 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
224 int ff_poll(
struct pollfd *fds, nfds_t numfds,
int timeout)
228 fd_set exception_set;
234 if (numfds >= FD_SETSIZE) {
242 FD_ZERO(&exception_set);
245 for (
i = 0;
i < numfds;
i++) {
249 if (fds[
i].fd >= FD_SETSIZE) {
255 if (fds[
i].events & POLLIN)
256 FD_SET(fds[
i].fd, &read_set);
257 if (fds[
i].events & POLLOUT)
258 FD_SET(fds[
i].fd, &write_set);
259 if (fds[
i].events & POLLERR)
260 FD_SET(fds[
i].fd, &exception_set);
271 rc = select(n, &read_set, &write_set, &exception_set,
NULL);
274 tv.tv_sec = timeout / 1000;
275 tv.tv_usec = 1000 * (timeout % 1000);
276 rc = select(n, &read_set, &write_set, &exception_set, &tv);
282 for (
i = 0;
i < numfds;
i++) {
285 if (FD_ISSET(fds[
i].fd, &read_set))
286 fds[
i].revents |= POLLIN;
287 if (FD_ISSET(fds[
i].fd, &write_set))
288 fds[
i].revents |= POLLOUT;
289 if (FD_ISSET(fds[
i].fd, &exception_set))
290 fds[
i].revents |= POLLERR;