meillo@367: /* meillo@367: ** MasqMail meillo@367: ** Copyright (C) 1999 Oliver Kurth meillo@367: ** meillo@367: ** This program is free software; you can redistribute it and/or modify meillo@367: ** it under the terms of the GNU General Public License as published by meillo@367: ** the Free Software Foundation; either version 2 of the License, or meillo@367: ** (at your option) any later version. meillo@367: ** meillo@367: ** This program is distributed in the hope that it will be useful, meillo@367: ** but WITHOUT ANY WARRANTY; without even the implied warranty of meillo@367: ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the meillo@367: ** GNU General Public License for more details. meillo@367: ** meillo@367: ** You should have received a copy of the GNU General Public License meillo@367: ** along with this program; if not, write to the Free Software meillo@367: ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. meillo@0: */ meillo@0: #include "masqmail.h" meillo@0: meillo@10: static GList* meillo@400: resolve_ip(gchar *ip) meillo@0: { meillo@10: struct in_addr ia; meillo@400: mxip_addr mxip; meillo@10: meillo@400: if (!inet_aton(ip, &ia)) { meillo@400: /* No dots-and-numbers notation. */ meillo@400: return NULL; meillo@10: } meillo@400: mxip.name = g_strdup(ip); meillo@400: mxip.pref = 0; meillo@400: mxip.ip = (guint32) * (guint32 *) (&ia); meillo@400: return g_list_append(NULL, g_memdup(&mxip, sizeof(mxip))); meillo@0: } meillo@0: meillo@10: mxip_addr* meillo@366: connect_hostlist(int *psockfd, gchar *host, guint port, GList *addr_list) meillo@0: { meillo@10: GList *addr_node; meillo@10: struct sockaddr_in saddr; meillo@401: int saved_errno; meillo@0: meillo@10: DEBUG(5) debugf("connect_hostlist entered\n"); meillo@0: meillo@401: for (addr_node = g_list_first(addr_list); addr_node; meillo@401: addr_node = g_list_next(addr_node)) { meillo@10: mxip_addr *addr = (mxip_addr *) (addr_node->data); meillo@10: *psockfd = socket(PF_INET, SOCK_STREAM, 0); meillo@0: meillo@10: memset(&saddr, 0, sizeof(saddr)); meillo@10: saddr.sin_family = AF_INET; meillo@10: saddr.sin_port = htons(port); meillo@10: /* clumsy, but makes compiler happy: */ meillo@10: saddr.sin_addr = *(struct in_addr *) (&(addr->ip)); meillo@401: meillo@401: DEBUG(5) debugf(" trying ip %s port %d\n", meillo@401: inet_ntoa(saddr.sin_addr), port); meillo@401: meillo@401: if (connect(*psockfd, (struct sockaddr *) &saddr, meillo@401: sizeof(saddr))==0) { meillo@401: DEBUG(5) debugf(" connected to %s\n", meillo@401: inet_ntoa(saddr.sin_addr)); meillo@10: return addr; meillo@401: } meillo@0: meillo@401: saved_errno = errno; meillo@401: close(*psockfd); meillo@401: logwrite(LOG_WARNING, "connection to %s failed: %s\n", meillo@401: inet_ntoa(saddr.sin_addr), strerror(errno)); meillo@401: errno = saved_errno; meillo@0: meillo@401: if ((saved_errno != ECONNREFUSED) && meillo@401: (saved_errno != ETIMEDOUT) && meillo@401: (saved_errno != ENETUNREACH) && meillo@401: (saved_errno != EHOSTUNREACH)) { meillo@401: return NULL; meillo@10: } meillo@10: } meillo@0: return NULL; meillo@0: } meillo@0: meillo@367: /* meillo@367: ** Given a list of resolver functions, this function meillo@367: ** resolve the host and tries to connect to the addresses meillo@367: ** returned. If a connection attemp is timed out or refused, meillo@367: ** the next address is tried. meillo@367: ** meillo@367: ** TODO: the resolver functions might return duplicate addresses, meillo@367: ** if attempt failed for one it should not be tried again. meillo@0: */ meillo@10: mxip_addr* meillo@401: connect_resolvelist(int *psockfd, gchar *host, guint port, meillo@401: GList *res_func_list) meillo@0: { meillo@10: GList *res_node; meillo@10: GList *addr_list; meillo@0: meillo@10: DEBUG(5) debugf("connect_resolvelist entered\n"); meillo@0: meillo@10: h_errno = 0; meillo@400: if (isdigit(*host)) { meillo@10: mxip_addr *addr; meillo@0: meillo@400: if ((addr_list = resolve_ip(host))) { meillo@401: addr = connect_hostlist(psockfd, host, port, meillo@401: addr_list); meillo@10: g_list_free(addr_list); meillo@10: return addr; meillo@10: } meillo@367: /* meillo@400: ** Probably a hostname that begins with a digit. meillo@400: ** E.g. '3dwars.de'. Thus fall ... meillo@367: */ meillo@10: } meillo@0: meillo@401: if (!res_func_list) { meillo@401: logwrite(LOG_ALERT, "res_funcs not set!\n"); meillo@262: exit(1); meillo@10: } meillo@0: meillo@10: foreach(res_func_list, res_node) { meillo@10: resolve_func res_func; meillo@114: DEBUG(6) debugf(" foreach() body\n"); meillo@10: meillo@401: res_func = (resolve_func) res_node->data; meillo@401: if (!res_func) { meillo@401: logwrite(LOG_ALERT, "Empty res_func!\n"); meillo@262: exit(1); meillo@10: } meillo@10: meillo@10: errno = 0; meillo@10: if ((addr_list = res_func(NULL, host))) { meillo@10: meillo@10: mxip_addr *addr; meillo@401: if ((addr = connect_hostlist(psockfd, host, port, meillo@401: addr_list))) { meillo@10: return addr; meillo@10: } meillo@401: DEBUG(5) debugf("connect_hostlist failed: %s\n", meillo@401: strerror(errno)); meillo@10: g_list_free(addr_list); meillo@369: } else if (!g_list_next(res_node)) { meillo@401: logwrite(LOG_ALERT, "could not resolve %s: %s\n", meillo@401: host, hstrerror(h_errno)); meillo@10: } meillo@10: } meillo@10: return NULL; meillo@0: meillo@0: }