masqmail-0.2

view src/connect.c @ 170:0f0e4e7cd762

added misc/list-versions This script helps to check if the versions numbers in the project are the same as the one for the release. This script is motivated by the 0.2.27 release in which masqmail introduces itself as being version 0.2.26.
author meillo@marmaro.de
date Mon, 19 Jul 2010 14:01:13 +0200
parents 26e34ae9a3e3
children
line source
1 /* MasqMail
2 Copyright (C) 1999 Oliver Kurth
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "masqmail.h"
20 static GList*
21 resolve_ip(GList * list, gchar * ip)
22 {
23 struct in_addr ia;
24 if (inet_aton(ip, &ia)) {
25 mxip_addr mxip;
27 mxip.name = g_strdup(ip);
28 mxip.pref = 0;
29 mxip.ip = (guint32) * (guint32 *) (&ia);
30 list = g_list_append(list, g_memdup(&mxip, sizeof(mxip)));
31 }
32 /* logwrite(LOG_ALERT, "invalid address '%s': inet_aton() failed\n", ip); */
33 return NULL;
34 }
36 mxip_addr*
37 connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list)
38 {
39 GList *addr_node;
40 struct sockaddr_in saddr;
42 DEBUG(5) debugf("connect_hostlist entered\n");
44 for (addr_node = g_list_first(addr_list); addr_node; addr_node = g_list_next(addr_node)) {
45 mxip_addr *addr = (mxip_addr *) (addr_node->data);
47 *psockfd = socket(PF_INET, SOCK_STREAM, 0);
49 memset(&saddr, 0, sizeof(saddr));
51 saddr.sin_family = AF_INET;
52 saddr.sin_port = htons(port);
54 /* clumsy, but makes compiler happy: */
55 saddr.sin_addr = *(struct in_addr *) (&(addr->ip));
56 DEBUG(5) debugf(" trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port);
57 if (connect(*psockfd, (struct sockaddr *) (&saddr), sizeof(saddr)) == 0) {
58 DEBUG(5) debugf(" connected to %s\n", inet_ntoa(saddr.sin_addr));
59 return addr;
60 } else {
61 int saved_errno = errno;
63 close(*psockfd);
65 logwrite(LOG_WARNING, "connection to %s failed: %s\n", inet_ntoa(saddr.sin_addr), strerror(errno));
67 errno = saved_errno;
69 if ((saved_errno != ECONNREFUSED)
70 && (saved_errno != ETIMEDOUT)
71 && (saved_errno != ENETUNREACH)
72 && (saved_errno != EHOSTUNREACH))
73 return NULL;
74 }
75 }
76 return NULL;
77 }
79 /* Given a list of resolver functions, this function
80 resolve the host and tries to connect to the addresses
81 returned. If a connection attemp is timed out or refused,
82 the next address is tried.
84 TODO: the resolver functions might return duplicate addresses,
85 if attempt failed for one it should not be tried again.
86 */
88 mxip_addr*
89 connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_func_list)
90 {
91 GList *res_node;
92 GList *addr_list;
94 DEBUG(5) debugf("connect_resolvelist entered\n");
96 h_errno = 0;
98 if (isdigit(host[0])) {
99 mxip_addr *addr;
101 addr_list = resolve_ip(NULL, host);
102 if (addr_list) {
103 addr = connect_hostlist(psockfd, host, port, addr_list);
104 g_list_free(addr_list);
105 return addr;
106 }
107 /* previous versions complained, until someone tried to use a hostname
108 out there that begins with a digit. eg. '3dwars.de'. */
109 }
111 if (res_func_list == NULL) {
112 logwrite(LOG_ALERT, "res_funcs == NULL !!!\n");
113 exit(EXIT_FAILURE);
114 }
116 foreach(res_func_list, res_node) {
117 resolve_func res_func;
118 DEBUG(6) debugf(" foreach() body\n");
119 res_func = (resolve_func) (res_node->data);
121 if (res_func == NULL) {
122 logwrite(LOG_ALERT, "res_func == NULL !!!\n");
123 exit(EXIT_FAILURE);
124 }
126 errno = 0;
127 if ((addr_list = res_func(NULL, host))) {
129 mxip_addr *addr;
130 if ((addr = connect_hostlist(psockfd, host, port, addr_list)))
131 return addr;
133 DEBUG(5) {
134 debugf("connect_hostlist failed: %s\n", strerror(errno));
135 }
137 g_list_free(addr_list);
138 } else {
139 if (!g_list_next(res_node)) {
140 logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno));
141 }
142 }
143 }
144 return NULL;
146 }