7 #include <sys/socket.h>
14 #include "fastestmirror.h"
17 findfastest(char **urls, int nurls)
24 struct addrinfo hints, *result;;
26 fds = solv_calloc(nurls, sizeof(*fds));
27 socks = solv_calloc(nurls, sizeof(*socks));
28 for (i = 0; i < nurls; i++)
31 p = strchr(urls[i], '/');
44 if ((p2 = strchr(p, '@')) != 0)
47 if (!strncmp("https:", urls[i], 6))
49 else if (!strncmp("ftp:", urls[i], 4))
51 if ((p2 = strrchr(p, ':')) != 0)
60 sprintf(portstr, "%d", port);
61 memset(&hints, 0, sizeof(struct addrinfo));
62 hints.ai_family = AF_UNSPEC;
63 hints.ai_socktype = SOCK_STREAM;
64 hints.ai_flags = AI_NUMERICSERV;
66 if (!getaddrinfo(p, portstr, &hints, &result))
68 socks[i] = socket(result->ai_family, result->ai_socktype, result->ai_protocol);
71 fcntl(socks[i], F_SETFL, O_NONBLOCK);
72 if (connect(socks[i], result->ai_addr, result->ai_addrlen) == -1)
74 if (errno != EINPROGRESS)
88 for (i = j = 0; i < nurls; i++)
93 fds[j].events = POLLOUT;
101 if (poll(fds, j, 10000) <= 0)
103 i = -1; /* something is wrong */
106 for (i = 0; i < j; i++)
107 if ((fds[i].revents & POLLOUT) != 0)
110 socklen_t soel = sizeof(int);
111 if (getsockopt(fds[i].fd, SOL_SOCKET, SO_ERROR, &soe, &soel) == -1 || soe != 0)
113 /* connect failed, kill socket */
114 for (j = 0; j < nurls; j++)
115 if (socks[j] == fds[i].fd)
128 i = -1; /* something is wrong, no bit was set */
131 /* now i contains the fastest fd index */
134 for (j = 0; j < nurls; j++)
135 if (socks[j] == fds[i].fd)
139 char *url0 = urls[0];
144 for (i = j = 0; i < nurls; i++)