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