]> Pileus Git - ~andy/fetchmail/blob - ipv6-connect.c
Sunil Shetye's latest cleanup patch.
[~andy/fetchmail] / ipv6-connect.c
1 /*
2 %%% copyright-cmetz-97
3
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:
7
8 1. All terms of the all other applicable copyrights and licenses must be
9    followed.
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.
18
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.
29
30 */
31
32
33 #include "config.h"
34 #ifdef INET6_ENABLE
35 #include <sys/types.h>
36 #include <stdio.h>
37 #ifdef HAVE_NET_SOCKET_H
38 #include <net/socket.h>
39 #else
40 #include <sys/socket.h>
41 #endif
42 #include <netinet/in.h>
43 #include <errno.h>
44 #include <netdb.h>
45 #include <signal.h>
46
47 /* This patch, supplying SA_LEN if it's undefined, is from Red Hat */
48 #ifndef SA_LEN
49 #define SA_LEN(sa)      sa_len(sa)
50
51 static size_t sa_len(struct sockaddr *sa)
52 {
53         switch(sa->sa_family) {
54 #if defined(AF_INET)
55                 case AF_INET:
56                         return sizeof(struct sockaddr_in);
57 #endif
58 #if defined(AF_INET6) && defined(INET6_ENABLE)
59                 case AF_INET6:
60                         return sizeof(struct sockaddr_in6);
61 #endif
62                 default:
63                         return sizeof(struct sockaddr);
64         }
65 }
66 #endif /* SA_LEN */
67
68 static int default_trying_callback(struct sockaddr *sa)
69 {
70   char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
71
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");
74     return -1;
75   };
76
77   fprintf(stderr, "Trying %s.%s...\n", hbuf, sbuf);
78   return 0;
79 };
80
81 static int default_error_callback(char *myname, char *message)
82 {
83   fprintf(stderr, "%s: %s\n", myname, message);
84   return 0;
85 };
86
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)
88 {
89   int fd;
90   char errorbuf[128];
91
92   if (!trying_callback)
93     trying_callback = default_trying_callback;
94
95   if (!error_callback)
96     error_callback = default_error_callback;
97
98   for (; ai; ai = ai->ai_next) {
99     if (trying_callback(ai->ai_addr))
100       continue;
101
102     if ((fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol)) < 0) {
103 #ifdef HAVE_SNPRINTF
104      snprintf(errorbuf, sizeof(errorbuf),
105 #else
106      sprintf(errorbuf,
107 #endif
108         "socket: %s(%d)", strerror(errno), errno);
109       error_callback(myname, errorbuf);
110       continue;
111     };
112
113     if (connect(fd, ai->ai_addr, ai->ai_addrlen) < 0) {
114 #ifdef HAVE_SNPRINTF
115      snprintf(errorbuf, sizeof(errorbuf),
116 #else
117      sprintf(errorbuf,
118 #endif
119          "connect: %s(%d)", strerror(errno), errno);
120       error_callback(myname, errorbuf);
121       close(fd);        /* just after a connect; no reads or writes yet */
122       continue;
123     }
124     break;
125   };
126
127   if (ai) {
128     if (pai)
129       *pai = ai;
130   } else {
131 #ifdef HAVE_SNPRINTF
132      snprintf(errorbuf, sizeof(errorbuf),
133 #else
134      sprintf(errorbuf,
135 #endif
136        "no connections result");
137     error_callback(myname, errorbuf);
138     fd = -1;
139   };
140
141   return fd;
142 };
143
144 #endif /* INET6_ENABLE */