masqmail-0.2
diff src/connect.c @ 10:26e34ae9a3e3
changed indention and line wrapping to a more consistent style
author | meillo@marmaro.de |
---|---|
date | Mon, 27 Oct 2008 16:23:10 +0100 |
parents | 08114f7dcc23 |
children | a80ebfa16cd5 |
line diff
1.1 --- a/src/connect.c Mon Oct 27 16:21:27 2008 +0100 1.2 +++ b/src/connect.c Mon Oct 27 16:23:10 2008 +0100 1.3 @@ -17,67 +17,63 @@ 1.4 */ 1.5 #include "masqmail.h" 1.6 1.7 -static 1.8 -GList *resolve_ip(GList *list, gchar *ip) 1.9 +static GList* 1.10 +resolve_ip(GList * list, gchar * ip) 1.11 { 1.12 - struct in_addr ia; 1.13 - if(inet_aton(ip, &ia)){ 1.14 - mxip_addr mxip; 1.15 - 1.16 - mxip.name = g_strdup(ip); 1.17 - mxip.pref = 0; 1.18 - mxip.ip = (guint32) *(guint32 *)(&ia); 1.19 - list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.20 - } 1.21 - /* logwrite(LOG_ALERT, "invalid address '%s': inet_aton() failed\n", ip);*/ 1.22 - return NULL; 1.23 + struct in_addr ia; 1.24 + if (inet_aton(ip, &ia)) { 1.25 + mxip_addr mxip; 1.26 + 1.27 + mxip.name = g_strdup(ip); 1.28 + mxip.pref = 0; 1.29 + mxip.ip = (guint32) * (guint32 *) (&ia); 1.30 + list = g_list_append(list, g_memdup(&mxip, sizeof(mxip))); 1.31 + } 1.32 + /* logwrite(LOG_ALERT, "invalid address '%s': inet_aton() failed\n", ip); */ 1.33 + return NULL; 1.34 } 1.35 1.36 -mxip_addr *connect_hostlist(int *psockfd, gchar *host, guint port, 1.37 - GList *addr_list) 1.38 +mxip_addr* 1.39 +connect_hostlist(int *psockfd, gchar * host, guint port, GList * addr_list) 1.40 { 1.41 - GList *addr_node; 1.42 - struct sockaddr_in saddr; 1.43 + GList *addr_node; 1.44 + struct sockaddr_in saddr; 1.45 1.46 - DEBUG(5) debugf("connect_hostlist entered\n"); 1.47 + DEBUG(5) debugf("connect_hostlist entered\n"); 1.48 1.49 - for(addr_node = g_list_first(addr_list); 1.50 - addr_node; 1.51 - addr_node = g_list_next(addr_node)){ 1.52 - mxip_addr *addr = (mxip_addr *)(addr_node->data); 1.53 + for (addr_node = g_list_first(addr_list); addr_node; addr_node = g_list_next(addr_node)) { 1.54 + mxip_addr *addr = (mxip_addr *) (addr_node->data); 1.55 1.56 - *psockfd = socket(PF_INET, SOCK_STREAM, 0); 1.57 + *psockfd = socket(PF_INET, SOCK_STREAM, 0); 1.58 1.59 - memset(&saddr, 0, sizeof(saddr)); 1.60 + memset(&saddr, 0, sizeof(saddr)); 1.61 1.62 - saddr.sin_family = AF_INET; 1.63 - saddr.sin_port = htons(port); 1.64 + saddr.sin_family = AF_INET; 1.65 + saddr.sin_port = htons(port); 1.66 1.67 - /* clumsy, but makes compiler happy: */ 1.68 - saddr.sin_addr = *(struct in_addr*)(&(addr->ip)); 1.69 - DEBUG(5) debugf("trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port); 1.70 - if(connect(*psockfd, (struct sockaddr *)(&saddr), sizeof(saddr)) == 0){ 1.71 - DEBUG(5) debugf("connected to %s\n", inet_ntoa(saddr.sin_addr)); 1.72 - return addr; 1.73 - }else{ 1.74 - int saved_errno = errno; 1.75 + /* clumsy, but makes compiler happy: */ 1.76 + saddr.sin_addr = *(struct in_addr *) (&(addr->ip)); 1.77 + DEBUG(5) debugf("trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port); 1.78 + if (connect(*psockfd, (struct sockaddr *) (&saddr), sizeof(saddr)) == 0) { 1.79 + DEBUG(5) debugf("connected to %s\n", inet_ntoa(saddr.sin_addr)); 1.80 + return addr; 1.81 + } else { 1.82 + int saved_errno = errno; 1.83 1.84 - close(*psockfd); 1.85 + close(*psockfd); 1.86 1.87 - logwrite(LOG_WARNING, "connection to %s failed: %s\n", 1.88 - inet_ntoa(saddr.sin_addr), strerror(errno)); 1.89 + logwrite(LOG_WARNING, "connection to %s failed: %s\n", inet_ntoa(saddr.sin_addr), strerror(errno)); 1.90 1.91 - errno = saved_errno; 1.92 + errno = saved_errno; 1.93 1.94 - if((saved_errno != ECONNREFUSED) && 1.95 - (saved_errno != ETIMEDOUT) && 1.96 - (saved_errno != ENETUNREACH) && 1.97 - (saved_errno != EHOSTUNREACH)) 1.98 - 1.99 + if ((saved_errno != ECONNREFUSED) 1.100 + && (saved_errno != ETIMEDOUT) 1.101 + && (saved_errno != ENETUNREACH) 1.102 + && (saved_errno != EHOSTUNREACH)) 1.103 + return NULL; 1.104 + } 1.105 + } 1.106 return NULL; 1.107 - } 1.108 - } 1.109 - return NULL; 1.110 } 1.111 1.112 /* Given a list of resolver functions, this function 1.113 @@ -89,63 +85,62 @@ 1.114 if attempt failed for one it should not be tried again. 1.115 */ 1.116 1.117 -mxip_addr *connect_resolvelist(int *psockfd, gchar *host, guint port, 1.118 - GList *res_func_list) 1.119 +mxip_addr* 1.120 +connect_resolvelist(int *psockfd, gchar * host, guint port, GList * res_func_list) 1.121 { 1.122 - GList *res_node; 1.123 - GList *addr_list; 1.124 + GList *res_node; 1.125 + GList *addr_list; 1.126 1.127 - DEBUG(5) debugf("connect_resolvelist entered\n"); 1.128 + DEBUG(5) debugf("connect_resolvelist entered\n"); 1.129 1.130 - h_errno = 0; 1.131 + h_errno = 0; 1.132 1.133 - if(isdigit(host[0])){ 1.134 - mxip_addr *addr; 1.135 - 1.136 - addr_list = resolve_ip(NULL, host); 1.137 - if(addr_list){ 1.138 - addr = connect_hostlist(psockfd, host, port, addr_list); 1.139 - g_list_free(addr_list); 1.140 - return addr; 1.141 - } 1.142 - /* previous versions complained, until someone tried to use a hostname 1.143 - out there that begins with a digit. eg. '3dwars.de'. */ 1.144 - } 1.145 + if (isdigit(host[0])) { 1.146 + mxip_addr *addr; 1.147 1.148 - if(res_func_list == NULL){ 1.149 - logwrite(LOG_ALERT, "res_funcs == NULL !!!\n"); 1.150 - exit(EXIT_FAILURE); 1.151 - } 1.152 + addr_list = resolve_ip(NULL, host); 1.153 + if (addr_list) { 1.154 + addr = connect_hostlist(psockfd, host, port, addr_list); 1.155 + g_list_free(addr_list); 1.156 + return addr; 1.157 + } 1.158 + /* previous versions complained, until someone tried to use a hostname 1.159 + out there that begins with a digit. eg. '3dwars.de'. */ 1.160 + } 1.161 1.162 - foreach(res_func_list, res_node){ 1.163 - resolve_func res_func; 1.164 - DEBUG(6) debugf("connect_resolvelist 1a\n"); 1.165 - res_func = (resolve_func)(res_node->data); 1.166 - 1.167 - if(res_func == NULL){ 1.168 - logwrite(LOG_ALERT, "res_func == NULL !!!\n"); 1.169 - exit(EXIT_FAILURE); 1.170 - } 1.171 - 1.172 - errno = 0; 1.173 - if((addr_list = res_func(NULL, host))){ 1.174 - 1.175 - mxip_addr *addr; 1.176 - if((addr = connect_hostlist(psockfd, host, port, addr_list))) 1.177 - return addr; 1.178 + if (res_func_list == NULL) { 1.179 + logwrite(LOG_ALERT, "res_funcs == NULL !!!\n"); 1.180 + exit(EXIT_FAILURE); 1.181 + } 1.182 1.183 - DEBUG(5){ 1.184 - debugf("connect_hostlist failed: %s\n", strerror(errno)); 1.185 - } 1.186 - 1.187 - g_list_free(addr_list); 1.188 - }else{ 1.189 - if(!g_list_next(res_node)){ 1.190 - logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno)); 1.191 - } 1.192 - } 1.193 - } 1.194 - return NULL; 1.195 + foreach(res_func_list, res_node) { 1.196 + resolve_func res_func; 1.197 + DEBUG(6) debugf("connect_resolvelist 1a\n"); 1.198 + res_func = (resolve_func) (res_node->data); 1.199 + 1.200 + if (res_func == NULL) { 1.201 + logwrite(LOG_ALERT, "res_func == NULL !!!\n"); 1.202 + exit(EXIT_FAILURE); 1.203 + } 1.204 + 1.205 + errno = 0; 1.206 + if ((addr_list = res_func(NULL, host))) { 1.207 + 1.208 + mxip_addr *addr; 1.209 + if ((addr = connect_hostlist(psockfd, host, port, addr_list))) 1.210 + return addr; 1.211 + 1.212 + DEBUG(5) { 1.213 + debugf("connect_hostlist failed: %s\n", strerror(errno)); 1.214 + } 1.215 + 1.216 + g_list_free(addr_list); 1.217 + } else { 1.218 + if (!g_list_next(res_node)) { 1.219 + logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno)); 1.220 + } 1.221 + } 1.222 + } 1.223 + return NULL; 1.224 1.225 } 1.226 -