3 This software is Copyright 1997-1998 by Craig Metz, All Rights Reserved.
4 The Inner Net License Version 2 applies to this software.
5 You should have received a copy of the license with this software. If
6 you didn't get a copy, you may request one from <license@inner.net>.
13 #include <sys/types.h>
15 #ifdef HAVE_NET_SOCKET_H
16 #include <net/socket.h>
18 #include <sys/socket.h>
20 #include <netinet/in.h>
25 /* This patch, supplying SA_LEN if it's undefined, is from Red Hat */
27 #define SA_LEN(sa) sa_len(sa)
29 static size_t sa_len(struct sockaddr *sa)
31 switch(sa->sa_family) {
34 return sizeof(struct sockaddr_in);
36 #if defined(AF_INET6) && defined(INET6_ENABLE)
38 return sizeof(struct sockaddr_in6);
41 return sizeof(struct sockaddr);
46 static int default_trying_callback(struct sockaddr *sa)
48 char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
50 if (getnameinfo(sa, SA_LEN(sa), hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV)) {
51 fprintf(stderr, "inner_getstream: getnameinfo failed\n");
55 fprintf(stderr, "Trying %s.%s...\n", hbuf, sbuf);
59 static int default_error_callback(char *myname, char *message)
61 fprintf(stderr, "%s: %s\n", myname, message);
65 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)
71 trying_callback = default_trying_callback;
74 error_callback = default_error_callback;
76 for (; ai; ai = ai->ai_next) {
77 if (trying_callback(ai->ai_addr))
80 if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) {
82 snprintf(errorbuf, sizeof(errorbuf),
86 "socket: %s(%d)", strerror(errno), errno);
87 error_callback(myname, errorbuf);
91 if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
93 snprintf(errorbuf, sizeof(errorbuf),
97 "connect: %s(%d)", strerror(errno), errno);
98 error_callback(myname, errorbuf);
99 close(fd); /* just after a connect; no reads or writes yet */
110 snprintf(errorbuf, sizeof(errorbuf),
114 "no connections result");
115 error_callback(myname, errorbuf);
122 #endif /* INET6_ENABLE */