24 #define _DEFAULT_SOURCE
39 #elif HAVE_SYS_SELECT_H
40 #include <sys/select.h>
50 static int inet_aton(
const char *
str,
struct in_addr *
add)
52 unsigned int add1 = 0, add2 = 0, add3 = 0, add4 = 0;
54 if (sscanf(
str,
"%d.%d.%d.%d", &add1, &add2, &add3, &add4) != 4)
57 if (!add1 || (add1 | add2 | add3 | add4) > 255)
60 add->s_addr = htonl((add1 << 24) + (add2 << 16) + (add3 << 8) + add4);
69 struct hostent *
h =
NULL;
71 struct sockaddr_in *sin;
77 sin->sin_family = AF_INET;
80 if (!inet_aton(node, &sin->sin_addr)) {
85 h = gethostbyname(node);
90 memcpy(&sin->sin_addr,
h->h_addr_list[0],
sizeof(
struct in_addr));
94 sin->sin_addr.s_addr = INADDR_ANY;
102 sin->sin_port = htons(atoi(service));
125 ai->
ai_addr = (
struct sockaddr *)sin;
142 char *host,
int hostlen,
143 char *serv,
int servlen,
int flags)
145 const struct sockaddr_in *sin = (
const struct sockaddr_in *)sa;
147 if (sa->sa_family != AF_INET)
152 if (host && hostlen > 0) {
153 struct hostent *ent =
NULL;
156 ent = gethostbyaddr((
const char *)&sin->sin_addr,
157 sizeof(sin->sin_addr), AF_INET);
160 snprintf(host, hostlen,
"%s", ent->h_name);
164 a = ntohl(sin->sin_addr.s_addr);
165 snprintf(host, hostlen,
"%d.%d.%d.%d",
166 ((
a >> 24) & 0xff), ((
a >> 16) & 0xff),
167 ((
a >> 8) & 0xff), (
a & 0xff));
171 if (serv && servlen > 0) {
174 snprintf(serv, servlen,
"%d", ntohs(sin->sin_port));
181 #if !HAVE_GETADDRINFO || HAVE_WINSOCK2_H
186 return "Temporary failure in name resolution";
188 return "Invalid flags for ai_flags";
190 return "A non-recoverable error occurred";
192 return "The address family was not recognized or the address "
193 "length was invalid for the specified family";
195 return "Memory allocation failure";
196 #if EAI_NODATA != EAI_NONAME
198 return "No address associated with hostname";
201 return "The name does not resolve for the supplied parameters";
203 return "servname not supported for ai_socktype";
205 return "ai_socktype not supported";
208 return "Unknown error";
215 u_long param = enable;
216 return ioctlsocket(socket, FIONBIO, ¶m);
219 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) | O_NONBLOCK);
221 return fcntl(socket, F_SETFL, fcntl(socket, F_GETFL) & ~O_NONBLOCK);
226 int ff_poll(
struct pollfd *fds, nfds_t numfds,
int timeout)
230 fd_set exception_set;
236 if (numfds >= FD_SETSIZE) {
244 FD_ZERO(&exception_set);
247 for (
i = 0;
i < numfds;
i++) {
251 if (fds[
i].fd >= FD_SETSIZE) {
257 if (fds[
i].events & POLLIN)
258 FD_SET(fds[
i].fd, &read_set);
259 if (fds[
i].events & POLLOUT)
260 FD_SET(fds[
i].fd, &write_set);
261 if (fds[
i].events & POLLERR)
262 FD_SET(fds[
i].fd, &exception_set);
273 rc = select(n, &read_set, &write_set, &exception_set,
NULL);
276 tv.tv_sec = timeout / 1000;
277 tv.tv_usec = 1000 * (timeout % 1000);
278 rc = select(n, &read_set, &write_set, &exception_set, &tv);
284 for (
i = 0;
i < numfds;
i++) {
287 if (FD_ISSET(fds[
i].fd, &read_set))
288 fds[
i].revents |= POLLIN;
289 if (FD_ISSET(fds[
i].fd, &write_set))
290 fds[
i].revents |= POLLOUT;
291 if (FD_ISSET(fds[
i].fd, &exception_set))
292 fds[
i].revents |= POLLERR;