masqmail

annotate src/connect.c @ 61:bdef5f279fde

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