-int connect_to_socket4(const char *ip, int port, struct timeval *timeout) {
- int sock;
-
- debug(D_LISTENER, "IPv4 connecting to ip '%s' port %d", ip, port);
-
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if(sock < 0) {
- error("IPv4 socket() on ip '%s' port %d failed.", ip, port);
- return -1;
- }
-
- if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
- error("Failed to set timeout on the socket to ip '%s' port %d", ip, port);
-
- struct sockaddr_in name;
- memset(&name, 0, sizeof(struct sockaddr_in));
- name.sin_family = AF_INET;
- name.sin_port = htons(port);
-
- int ret = inet_pton(AF_INET, ip, (void *)&name.sin_addr.s_addr);
- if(ret != 1) {
- error("Failed to convert '%s' to a valid IPv4 address.", ip);
- close(sock);
- return -1;
- }
-
- if(connect(sock, (struct sockaddr *) &name, sizeof(name)) < 0) {
- close(sock);
- error("IPv4 failed to connect to '%s', port %d", ip, port);
- return -1;
- }
-
- debug(D_LISTENER, "Connected to IPv4 ip '%s' port %d", ip, port);
- return sock;
-}
-
-int connect_to_socket6(const char *ip, int port, struct timeval *timeout) {
- int sock = -1;
- int ipv6only = 1;
-
- debug(D_LISTENER, "IPv6 connecting to ip '%s' port %d", ip, port);
-
- sock = socket(AF_INET6, SOCK_STREAM, 0);
- if (sock < 0) {
- error("IPv6 socket() on ip '%s' port %d failed.", ip, port);
- return -1;
- }
-
- if(setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, (char *)timeout, sizeof(struct timeval)) < 0)
- error("Failed to set timeout on the socket to ip '%s' port %d", ip, port);
-
- /* IPv6 only */
- if(setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (void*)&ipv6only, sizeof(ipv6only)) != 0)
- error("Cannot set IPV6_V6ONLY on ip '%s' port's %d.", ip, port);
-
- struct sockaddr_in6 name;
- memset(&name, 0, sizeof(struct sockaddr_in6));
- name.sin6_family = AF_INET6;
- name.sin6_port = htons ((uint16_t) port);
-
- int ret = inet_pton(AF_INET6, ip, (void *)&name.sin6_addr.s6_addr);
- if(ret != 1) {
- error("Failed to convert IP '%s' to a valid IPv6 address.", ip);
- close(sock);
- return -1;
- }
-
- name.sin6_scope_id = 0;
-
- if(connect(sock, (struct sockaddr *)&name, sizeof(name)) < 0) {
- close(sock);
- error("IPv6 failed to connect to '%s', port %d", ip, port);
- return -1;
- }
-
- debug(D_LISTENER, "Connected to IPv6 ip '%s' port %d", ip, port);
- return sock;
-}
-
-
-static inline int connect_to_one(const char *definition, int default_port, struct timeval *timeout) {
- struct addrinfo hints;
- struct addrinfo *result = NULL, *rp = NULL;
-
- char buffer[strlen(definition) + 1];
- strcpy(buffer, definition);
-
- char buffer2[10 + 1];
- snprintfz(buffer2, 10, "%d", default_port);
-
- char *ip = buffer, *port = buffer2;
-
- char *e = ip;
- if(*e == '[') {
- e = ++ip;
- while(*e && *e != ']') e++;
- if(*e == ']') {
- *e = '\0';
- e++;
- }
- }
- else {
- while(*e && *e != ':') e++;
- }
-
- if(*e == ':') {
- port = e + 1;
- *e = '\0';
- }
-
- if(!*ip)
- return -1;
-
- if(!*port)
- port = buffer2;
-
- memset(&hints, 0, sizeof(struct addrinfo));
- hints.ai_family = AF_UNSPEC; /* Allow IPv4 or IPv6 */
- hints.ai_socktype = SOCK_DGRAM; /* Datagram socket */
- hints.ai_flags = AI_PASSIVE; /* For wildcard IP address */
- hints.ai_protocol = 0; /* Any protocol */
- hints.ai_canonname = NULL;
- hints.ai_addr = NULL;
- hints.ai_next = NULL;
-
- int r = getaddrinfo(ip, port, &hints, &result);
- if (r != 0) {
- error("Cannot resolve host '%s', port '%s': %s\n", ip, port, gai_strerror(r));
- return -1;
- }
-
- int fd = -1;
- for (rp = result; rp != NULL && fd == -1; rp = rp->ai_next) {
- char rip[INET_ADDRSTRLEN + INET6_ADDRSTRLEN] = "INVALID";
- int rport;
-
- switch (rp->ai_addr->sa_family) {
- case AF_INET: {
- struct sockaddr_in *sin = (struct sockaddr_in *) rp->ai_addr;
- inet_ntop(AF_INET, &sin->sin_addr, rip, INET_ADDRSTRLEN);
- rport = ntohs(sin->sin_port);
- fd = connect_to_socket4(rip, rport, timeout);
- break;
- }