masqmail

annotate src/connect.c @ 394:c8e3d1a79313

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