4 The author(s) grant permission for redistribution and use in source and
5 binary forms, with or without modification, of the software and documentation
6 provided that the following conditions are met:
8 1. All terms of the all other applicable copyrights and licenses must be
10 2. Redistributions of source code must retain the authors' copyright
11 notice(s), this list of conditions, and the following disclaimer.
12 3. Redistributions in binary form must reproduce the authors' copyright
13 notice(s), this list of conditions, and the following disclaimer in the
14 documentation and/or other materials provided with the distribution.
15 4. Neither the name(s) of the author(s) nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY ITS AUTHORS AND CONTRIBUTORS ``AS IS'' AND ANY
20 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY
23 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26 ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/types.h>
37 #ifdef HAVE_NET_SOCKET_H
38 #include <net/socket.h>
40 #include <sys/socket.h>
42 #include <netinet/in.h>
47 /* This patch, supplying SA_LEN if it's undefined, is from Red Hat */
49 #define SA_LEN(sa) sa_len(sa)
51 static size_t sa_len(struct sockaddr *sa)
53 switch(sa->sa_family) {
56 return sizeof(struct sockaddr_in);
58 #if defined(AF_INET6) && defined(INET6_ENABLE)
60 return sizeof(struct sockaddr_in6);
63 return sizeof(struct sockaddr);
68 static int default_trying_callback(struct sockaddr *sa)
70 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
72 if (getnameinfo(sa, SA_LEN(sa), hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
73 fprintf(stderr, "inner_getstream: getnameinfo failed\n");
77 fprintf(stderr, "Trying %s.%s...\n", hbuf, sbuf);
81 static int default_error_callback(char *myname, char *message)
83 fprintf(stderr, "%s: %s\n", myname, message);
87 int inner_connect(struct addrinfo *ai, void *request, int requestlen, int (*trying_callback)(struct sockaddr *sa), int (*error_callback)(char *myname, char *message), char *myname, struct addrinfo **pai)
93 trying_callback = default_trying_callback;
96 error_callback = default_error_callback;
98 for (; ai; ai = ai->ai_next) {
99 if (trying_callback(ai->ai_addr))
102 if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) {
103 snprintf(errorbuf, sizeof(errorbuf), "socket: %s(%d)",
104 strerror(errno), errno);
105 error_callback(myname, errorbuf);
109 if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
110 snprintf(errorbuf, sizeof(errorbuf), "connect: %s(%d)",
111 strerror(errno), errno);
112 error_callback(myname, errorbuf);
113 close(fd); /* just after a connect; no reads or writes yet */
123 snprintf(errorbuf, sizeof(errorbuf), "no connections result");
124 error_callback(myname, errorbuf);
131 #endif /* INET6_ENABLE */