24 #define _DEFAULT_SOURCE
39 #include <sys/select.h>
49 static int inet_aton(
const char *str,
struct in_addr *add)
51 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
53 if (sscanf(str,
"%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
56 if (!add1 || (add1 | add2 | add3 | add4) > 255)
59 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
68 struct hostent *
h =
NULL;
70 struct sockaddr_in *sin;
76 sin->sin_family = AF_INET;
79 if (!inet_aton(node, &sin->sin_addr)) {
84 h = gethostbyname(node);
89 memcpy(&sin->sin_addr,
h->h_addr_list[0],
sizeof(
struct in_addr));
93 sin->sin_addr.s_addr = INADDR_ANY;
101 sin->sin_port = htons(atoi(service));
124 ai->
ai_addr = (
struct sockaddr *)sin;
141 char *host,
int hostlen,
142 char *serv,
int servlen,
int flags)
144 const struct sockaddr_in *sin = (
const struct sockaddr_in *)sa;
146 if (sa->sa_family != AF_INET)
151 if (host && hostlen > 0) {
152 struct hostent *ent =
NULL;
155 ent = gethostbyaddr((
const char *)&sin->sin_addr,
156 sizeof(sin->sin_addr), AF_INET);
159 snprintf(host, hostlen,
"%s", ent->h_name);
163 a = ntohl(sin->sin_addr.s_addr);
164 snprintf(host, hostlen,
"%d.%d.%d.%d",
165 ((
a >> 24) & 0xff), ((
a >> 16) & 0xff),
166 ((
a >> 8) & 0xff), (
a & 0xff));
170 if (serv && servlen > 0) {
173 snprintf(serv, servlen,
"%d", ntohs(sin->sin_port));
180 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
185 return "Temporary failure in name resolution";
187 return "Invalid flags for ai_flags";
189 return "A non-recoverable error occurred";
191 return "The address family was not recognized or the address "
192 "length was invalid for the specified family";
194 return "Memory allocation failure";
195 #if EAI_NODATA != EAI_NONAME
197 return "No address associated with hostname";
200 return "The name does not resolve for the supplied parameters";
202 return "servname not supported for ai_socktype";
204 return "ai_socktype not supported";
207 return "Unknown error";
214 u_long param = enable;
215 return ioctlsocket(socket, FIONBIO, ¶m);
218 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
220 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
225 int ff_poll(
struct pollfd *fds, nfds_t numfds,
int timeout)
229 fd_set exception_set;
235 if (numfds >= FD_SETSIZE) {
243 FD_ZERO(&exception_set);
246 for (
i = 0;
i < numfds;
i++) {
250 if (fds[
i].fd >= FD_SETSIZE) {
256 if (fds[
i].events & POLLIN)
257 FD_SET(fds[
i].fd, &read_set);
258 if (fds[
i].events & POLLOUT)
259 FD_SET(fds[
i].fd, &write_set);
260 if (fds[
i].events & POLLERR)
261 FD_SET(fds[
i].fd, &exception_set);
272 rc = select(n, &read_set, &write_set, &exception_set,
NULL);
275 tv.tv_sec = timeout / 1000;
276 tv.tv_usec = 1000 * (timeout % 1000);
277 rc = select(n, &read_set, &write_set, &exception_set, &tv);
283 for (
i = 0;
i < numfds;
i++) {
286 if (FD_ISSET(fds[
i].fd, &read_set))
287 fds[
i].revents |= POLLIN;
288 if (FD_ISSET(fds[
i].fd, &write_set))
289 fds[
i].revents |= POLLOUT;
290 if (FD_ISSET(fds[
i].fd, &exception_set))
291 fds[
i].revents |= POLLERR;