masqmail

view src/connect.c @ 400:6500db550a03

resolve_ip() appeared to be broken, in connect.c. I fixed it. In any case, NULL was returned, but if it is a valid IP address, a list should have been returned. This is fixed now.
author markus schnalke <meillo@marmaro.de>
date Tue, 21 Feb 2012 15:44:55 +0100
parents 19d57eb1b6a1
children 885e3d886199
line source
1 /*
2 ** MasqMail
3 ** Copyright (C) 1999 Oliver Kurth
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19 #include "masqmail.h"
21 static GList*
22 resolve_ip(gchar *ip)
23 {
24 struct in_addr ia;
25 mxip_addr mxip;
27 if (!inet_aton(ip, &ia)) {
28 /* No dots-and-numbers notation. */
29 return NULL;
30 }
31 mxip.name = g_strdup(ip);
32 mxip.pref = 0;
33 mxip.ip = (guint32) * (guint32 *) (&ia);
34 return g_list_append(NULL, g_memdup(&mxip, sizeof(mxip)));
35 }
37 mxip_addr*
38 connect_hostlist(int *psockfd, gchar *host, guint port, GList *addr_list)
39 {
40 GList *addr_node;
41 struct sockaddr_in saddr;
43 DEBUG(5) debugf("connect_hostlist entered\n");
45 for (addr_node = g_list_first(addr_list); addr_node; addr_node = g_list_next(addr_node)) {
46 mxip_addr *addr = (mxip_addr *) (addr_node->data);
48 *psockfd = socket(PF_INET, SOCK_STREAM, 0);
50 memset(&saddr, 0, sizeof(saddr));
52 saddr.sin_family = AF_INET;
53 saddr.sin_port = htons(port);
55 /* clumsy, but makes compiler happy: */
56 saddr.sin_addr = *(struct in_addr *) (&(addr->ip));
57 DEBUG(5) debugf(" trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port);
58 if (connect(*psockfd, (struct sockaddr *) (&saddr), sizeof(saddr)) == 0) {
59 DEBUG(5) debugf(" connected to %s\n", inet_ntoa(saddr.sin_addr));
60 return addr;
61 } else {
62 int saved_errno = errno;
64 close(*psockfd);
66 logwrite(LOG_WARNING, "connection to %s failed: %s\n", inet_ntoa(saddr.sin_addr), strerror(errno));
68 errno = saved_errno;
70 if ((saved_errno != ECONNREFUSED)
71 && (saved_errno != ETIMEDOUT)
72 && (saved_errno != ENETUNREACH)
73 && (saved_errno != EHOSTUNREACH))
74 return NULL;
75 }
76 }
77 return NULL;
78 }
80 /*
81 ** Given a list of resolver functions, this function
82 ** resolve the host and tries to connect to the addresses
83 ** returned. If a connection attemp is timed out or refused,
84 ** the next address is tried.
85 **
86 ** TODO: the resolver functions might return duplicate addresses,
87 ** if attempt failed for one it should not be tried again.
88 */
89 mxip_addr*
90 connect_resolvelist(int *psockfd, gchar *host, guint port, GList *res_func_list)
91 {
92 GList *res_node;
93 GList *addr_list;
95 DEBUG(5) debugf("connect_resolvelist entered\n");
97 h_errno = 0;
99 if (isdigit(*host)) {
100 mxip_addr *addr;
102 if ((addr_list = resolve_ip(host))) {
103 addr = connect_hostlist(psockfd, host, port, addr_list);
104 g_list_free(addr_list);
105 return addr;
106 }
107 /*
108 ** Probably a hostname that begins with a digit.
109 ** E.g. '3dwars.de'. Thus fall ...
110 */
111 }
113 if (res_func_list == NULL) {
114 logwrite(LOG_ALERT, "res_funcs == NULL !!!\n");
115 exit(1);
116 }
118 foreach(res_func_list, res_node) {
119 resolve_func res_func;
120 DEBUG(6) debugf(" foreach() body\n");
121 res_func = (resolve_func) (res_node->data);
123 if (res_func == NULL) {
124 logwrite(LOG_ALERT, "res_func == NULL !!!\n");
125 exit(1);
126 }
128 errno = 0;
129 if ((addr_list = res_func(NULL, host))) {
131 mxip_addr *addr;
132 if ((addr = connect_hostlist(psockfd, host, port, addr_list)))
133 return addr;
135 DEBUG(5) {
136 debugf("connect_hostlist failed: %s\n", strerror(errno));
137 }
139 g_list_free(addr_list);
140 } else if (!g_list_next(res_node)) {
141 logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno));
142 }
143 }
144 return NULL;
146 }