Mercurial > masqmail
annotate src/connect.c @ 434:f2a7271746d1 default tip
Removes Freshmeat.net from the docs
The site, which was later renamed to freecode.com, is no longer
maintained (contains only a static copy).
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 07 Feb 2015 11:45:07 +0100 |
parents | 885e3d886199 |
children |
rev | line source |
---|---|
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
1 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
2 ** MasqMail |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
3 ** Copyright (C) 1999 Oliver Kurth |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
4 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
5 ** This program is free software; you can redistribute it and/or modify |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
6 ** it under the terms of the GNU General Public License as published by |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
7 ** the Free Software Foundation; either version 2 of the License, or |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
8 ** (at your option) any later version. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
9 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
10 ** This program is distributed in the hope that it will be useful, |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
13 ** GNU General Public License for more details. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
14 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
15 ** You should have received a copy of the GNU General Public License |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
16 ** along with this program; if not, write to the Free Software |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
17 ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
0 | 18 */ |
19 #include "masqmail.h" | |
20 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
21 static GList* |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
22 resolve_ip(gchar *ip) |
0 | 23 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
24 struct in_addr ia; |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
25 mxip_addr mxip; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
26 |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
27 if (!inet_aton(ip, &ia)) { |
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
28 /* No dots-and-numbers notation. */ |
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
29 return NULL; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
30 } |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
31 mxip.name = g_strdup(ip); |
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
32 mxip.pref = 0; |
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
33 mxip.ip = (guint32) * (guint32 *) (&ia); |
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
34 return g_list_append(NULL, g_memdup(&mxip, sizeof(mxip))); |
0 | 35 } |
36 | |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
37 mxip_addr* |
366
41958685480d
Switched to `type *name' style
markus schnalke <meillo@marmaro.de>
parents:
262
diff
changeset
|
38 connect_hostlist(int *psockfd, gchar *host, guint port, GList *addr_list) |
0 | 39 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
40 GList *addr_node; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
41 struct sockaddr_in saddr; |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
42 int saved_errno; |
0 | 43 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
44 DEBUG(5) debugf("connect_hostlist entered\n"); |
0 | 45 |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
46 for (addr_node = g_list_first(addr_list); addr_node; |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
47 addr_node = g_list_next(addr_node)) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
48 mxip_addr *addr = (mxip_addr *) (addr_node->data); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
49 *psockfd = socket(PF_INET, SOCK_STREAM, 0); |
0 | 50 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
51 memset(&saddr, 0, sizeof(saddr)); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
52 saddr.sin_family = AF_INET; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
53 saddr.sin_port = htons(port); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
54 /* clumsy, but makes compiler happy: */ |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
55 saddr.sin_addr = *(struct in_addr *) (&(addr->ip)); |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
56 |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
57 DEBUG(5) debugf(" trying ip %s port %d\n", |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
58 inet_ntoa(saddr.sin_addr), port); |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
59 |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
60 if (connect(*psockfd, (struct sockaddr *) &saddr, |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
61 sizeof(saddr))==0) { |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
62 DEBUG(5) debugf(" connected to %s\n", |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
63 inet_ntoa(saddr.sin_addr)); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
64 return addr; |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
65 } |
0 | 66 |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
67 saved_errno = errno; |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
68 close(*psockfd); |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
69 logwrite(LOG_WARNING, "connection to %s failed: %s\n", |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
70 inet_ntoa(saddr.sin_addr), strerror(errno)); |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
71 errno = saved_errno; |
0 | 72 |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
73 if ((saved_errno != ECONNREFUSED) && |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
74 (saved_errno != ETIMEDOUT) && |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
75 (saved_errno != ENETUNREACH) && |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
76 (saved_errno != EHOSTUNREACH)) { |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
77 return NULL; |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
78 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
79 } |
0 | 80 return NULL; |
81 } | |
82 | |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
83 /* |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
84 ** Given a list of resolver functions, this function |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
85 ** resolve the host and tries to connect to the addresses |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
86 ** returned. If a connection attemp is timed out or refused, |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
87 ** the next address is tried. |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
88 ** |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
89 ** TODO: the resolver functions might return duplicate addresses, |
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
90 ** if attempt failed for one it should not be tried again. |
0 | 91 */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
92 mxip_addr* |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
93 connect_resolvelist(int *psockfd, gchar *host, guint port, |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
94 GList *res_func_list) |
0 | 95 { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
96 GList *res_node; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
97 GList *addr_list; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
98 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
99 DEBUG(5) debugf("connect_resolvelist entered\n"); |
0 | 100 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
101 h_errno = 0; |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
102 if (isdigit(*host)) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
103 mxip_addr *addr; |
0 | 104 |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
105 if ((addr_list = resolve_ip(host))) { |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
106 addr = connect_hostlist(psockfd, host, port, |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
107 addr_list); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
108 g_list_free(addr_list); |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
109 return addr; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
110 } |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
111 /* |
400
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
112 ** Probably a hostname that begins with a digit. |
6500db550a03
resolve_ip() appeared to be broken, in connect.c. I fixed it.
markus schnalke <meillo@marmaro.de>
parents:
369
diff
changeset
|
113 ** E.g. '3dwars.de'. Thus fall ... |
367
b27f66555ba8
Reformated multiline comments to have leading asterisks on each line
markus schnalke <meillo@marmaro.de>
parents:
366
diff
changeset
|
114 */ |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
115 } |
0 | 116 |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
117 if (!res_func_list) { |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
118 logwrite(LOG_ALERT, "res_funcs not set!\n"); |
262
fc1c6425c024
s/EXIT_SUCCESS/0/ && s/EXIT_FAILURE/1/
markus schnalke <meillo@marmaro.de>
parents:
114
diff
changeset
|
119 exit(1); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
120 } |
0 | 121 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
122 foreach(res_func_list, res_node) { |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
123 resolve_func res_func; |
114 | 124 DEBUG(6) debugf(" foreach() body\n"); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
125 |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
126 res_func = (resolve_func) res_node->data; |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
127 if (!res_func) { |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
128 logwrite(LOG_ALERT, "Empty res_func!\n"); |
262
fc1c6425c024
s/EXIT_SUCCESS/0/ && s/EXIT_FAILURE/1/
markus schnalke <meillo@marmaro.de>
parents:
114
diff
changeset
|
129 exit(1); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
130 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
131 |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
132 errno = 0; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
133 if ((addr_list = res_func(NULL, host))) { |
0 | 134 |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
135 mxip_addr *addr; |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
136 if ((addr = connect_hostlist(psockfd, host, port, |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
137 addr_list))) { |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
138 return addr; |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
139 } |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
140 DEBUG(5) debugf("connect_hostlist failed: %s\n", |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
141 strerror(errno)); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
142 g_list_free(addr_list); |
369
19d57eb1b6a1
Minor refactoring change (else-if).
markus schnalke <meillo@marmaro.de>
parents:
367
diff
changeset
|
143 } else if (!g_list_next(res_node)) { |
401
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
144 logwrite(LOG_ALERT, "could not resolve %s: %s\n", |
885e3d886199
Various minor refactoring.
markus schnalke <meillo@marmaro.de>
parents:
400
diff
changeset
|
145 host, hstrerror(h_errno)); |
10
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
146 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
147 } |
26e34ae9a3e3
changed indention and line wrapping to a more consistent style
meillo@marmaro.de
parents:
0
diff
changeset
|
148 return NULL; |
0 | 149 |
150 } |