Mercurial > masqmail
comparison src/connect.c @ 0:08114f7dcc23 0.2.21
this is masqmail-0.2.21 from oliver kurth
author | meillo@marmaro.de |
---|---|
date | Fri, 26 Sep 2008 17:05:23 +0200 |
parents | |
children | 26e34ae9a3e3 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:08114f7dcc23 |
---|---|
1 /* MasqMail | |
2 Copyright (C) 1999 Oliver Kurth | |
3 | |
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. | |
8 | |
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. | |
13 | |
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" | |
19 | |
20 static | |
21 GList *resolve_ip(GList *list, gchar *ip) | |
22 { | |
23 struct in_addr ia; | |
24 if(inet_aton(ip, &ia)){ | |
25 mxip_addr mxip; | |
26 | |
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 } | |
35 | |
36 mxip_addr *connect_hostlist(int *psockfd, gchar *host, guint port, | |
37 GList *addr_list) | |
38 { | |
39 GList *addr_node; | |
40 struct sockaddr_in saddr; | |
41 | |
42 DEBUG(5) debugf("connect_hostlist entered\n"); | |
43 | |
44 for(addr_node = g_list_first(addr_list); | |
45 addr_node; | |
46 addr_node = g_list_next(addr_node)){ | |
47 mxip_addr *addr = (mxip_addr *)(addr_node->data); | |
48 | |
49 *psockfd = socket(PF_INET, SOCK_STREAM, 0); | |
50 | |
51 memset(&saddr, 0, sizeof(saddr)); | |
52 | |
53 saddr.sin_family = AF_INET; | |
54 saddr.sin_port = htons(port); | |
55 | |
56 /* clumsy, but makes compiler happy: */ | |
57 saddr.sin_addr = *(struct in_addr*)(&(addr->ip)); | |
58 DEBUG(5) debugf("trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port); | |
59 if(connect(*psockfd, (struct sockaddr *)(&saddr), sizeof(saddr)) == 0){ | |
60 DEBUG(5) debugf("connected to %s\n", inet_ntoa(saddr.sin_addr)); | |
61 return addr; | |
62 }else{ | |
63 int saved_errno = errno; | |
64 | |
65 close(*psockfd); | |
66 | |
67 logwrite(LOG_WARNING, "connection to %s failed: %s\n", | |
68 inet_ntoa(saddr.sin_addr), strerror(errno)); | |
69 | |
70 errno = saved_errno; | |
71 | |
72 if((saved_errno != ECONNREFUSED) && | |
73 (saved_errno != ETIMEDOUT) && | |
74 (saved_errno != ENETUNREACH) && | |
75 (saved_errno != EHOSTUNREACH)) | |
76 | |
77 return NULL; | |
78 } | |
79 } | |
80 return NULL; | |
81 } | |
82 | |
83 /* Given a list of resolver functions, this function | |
84 resolve the host and tries to connect to the addresses | |
85 returned. If a connection attemp is timed out or refused, | |
86 the next address is tried. | |
87 | |
88 TODO: the resolver functions might return duplicate addresses, | |
89 if attempt failed for one it should not be tried again. | |
90 */ | |
91 | |
92 mxip_addr *connect_resolvelist(int *psockfd, gchar *host, guint port, | |
93 GList *res_func_list) | |
94 { | |
95 GList *res_node; | |
96 GList *addr_list; | |
97 | |
98 DEBUG(5) debugf("connect_resolvelist entered\n"); | |
99 | |
100 h_errno = 0; | |
101 | |
102 if(isdigit(host[0])){ | |
103 mxip_addr *addr; | |
104 | |
105 addr_list = resolve_ip(NULL, host); | |
106 if(addr_list){ | |
107 addr = connect_hostlist(psockfd, host, port, addr_list); | |
108 g_list_free(addr_list); | |
109 return addr; | |
110 } | |
111 /* previous versions complained, until someone tried to use a hostname | |
112 out there that begins with a digit. eg. '3dwars.de'. */ | |
113 } | |
114 | |
115 if(res_func_list == NULL){ | |
116 logwrite(LOG_ALERT, "res_funcs == NULL !!!\n"); | |
117 exit(EXIT_FAILURE); | |
118 } | |
119 | |
120 foreach(res_func_list, res_node){ | |
121 resolve_func res_func; | |
122 DEBUG(6) debugf("connect_resolvelist 1a\n"); | |
123 res_func = (resolve_func)(res_node->data); | |
124 | |
125 if(res_func == NULL){ | |
126 logwrite(LOG_ALERT, "res_func == NULL !!!\n"); | |
127 exit(EXIT_FAILURE); | |
128 } | |
129 | |
130 errno = 0; | |
131 if((addr_list = res_func(NULL, host))){ | |
132 | |
133 mxip_addr *addr; | |
134 if((addr = connect_hostlist(psockfd, host, port, addr_list))) | |
135 return addr; | |
136 | |
137 DEBUG(5){ | |
138 debugf("connect_hostlist failed: %s\n", strerror(errno)); | |
139 } | |
140 | |
141 g_list_free(addr_list); | |
142 }else{ | |
143 if(!g_list_next(res_node)){ | |
144 logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno)); | |
145 } | |
146 } | |
147 } | |
148 return NULL; | |
149 | |
150 } | |
151 |