masqmail

diff src/connect.c @ 401:885e3d886199

Various minor refactoring.
author markus schnalke <meillo@marmaro.de>
date Tue, 21 Feb 2012 16:11:00 +0100
parents 6500db550a03
children
line diff
     1.1 --- a/src/connect.c	Tue Feb 21 15:44:55 2012 +0100
     1.2 +++ b/src/connect.c	Tue Feb 21 16:11:00 2012 +0100
     1.3 @@ -39,39 +39,42 @@
     1.4  {
     1.5  	GList *addr_node;
     1.6  	struct sockaddr_in saddr;
     1.7 +	int saved_errno;
     1.8  
     1.9  	DEBUG(5) debugf("connect_hostlist entered\n");
    1.10  
    1.11 -	for (addr_node = g_list_first(addr_list); addr_node; addr_node = g_list_next(addr_node)) {
    1.12 +	for (addr_node = g_list_first(addr_list); addr_node;
    1.13 +			addr_node = g_list_next(addr_node)) {
    1.14  		mxip_addr *addr = (mxip_addr *) (addr_node->data);
    1.15 -
    1.16  		*psockfd = socket(PF_INET, SOCK_STREAM, 0);
    1.17  
    1.18  		memset(&saddr, 0, sizeof(saddr));
    1.19 -
    1.20  		saddr.sin_family = AF_INET;
    1.21  		saddr.sin_port = htons(port);
    1.22 -
    1.23  		/* clumsy, but makes compiler happy: */
    1.24  		saddr.sin_addr = *(struct in_addr *) (&(addr->ip));
    1.25 -		DEBUG(5) debugf("  trying ip %s port %d\n", inet_ntoa(saddr.sin_addr), port);
    1.26 -		if (connect(*psockfd, (struct sockaddr *) (&saddr), sizeof(saddr)) == 0) {
    1.27 -			DEBUG(5) debugf("  connected to %s\n", inet_ntoa(saddr.sin_addr));
    1.28 +
    1.29 +		DEBUG(5) debugf("  trying ip %s port %d\n",
    1.30 +				inet_ntoa(saddr.sin_addr), port);
    1.31 +
    1.32 +		if (connect(*psockfd, (struct sockaddr *) &saddr,
    1.33 +				sizeof(saddr))==0) {
    1.34 +			DEBUG(5) debugf("  connected to %s\n",
    1.35 +					inet_ntoa(saddr.sin_addr));
    1.36  			return addr;
    1.37 -		} else {
    1.38 -			int saved_errno = errno;
    1.39 +		}
    1.40  
    1.41 -			close(*psockfd);
    1.42 +		saved_errno = errno;
    1.43 +		close(*psockfd);
    1.44 +		logwrite(LOG_WARNING, "connection to %s failed: %s\n",
    1.45 +				inet_ntoa(saddr.sin_addr), strerror(errno));
    1.46 +		errno = saved_errno;
    1.47  
    1.48 -			logwrite(LOG_WARNING, "connection to %s failed: %s\n", inet_ntoa(saddr.sin_addr), strerror(errno));
    1.49 -
    1.50 -			errno = saved_errno;
    1.51 -
    1.52 -			if ((saved_errno != ECONNREFUSED)
    1.53 -			    && (saved_errno != ETIMEDOUT)
    1.54 -			    && (saved_errno != ENETUNREACH)
    1.55 -			    && (saved_errno != EHOSTUNREACH))
    1.56 -				return NULL;
    1.57 +		if ((saved_errno != ECONNREFUSED) &&
    1.58 +				(saved_errno != ETIMEDOUT) &&
    1.59 +				(saved_errno != ENETUNREACH) &&
    1.60 +				(saved_errno != EHOSTUNREACH)) {
    1.61 +			return NULL;
    1.62  		}
    1.63  	}
    1.64  	return NULL;
    1.65 @@ -87,7 +90,8 @@
    1.66  **  if attempt failed for one it should not be tried again.
    1.67  */
    1.68  mxip_addr*
    1.69 -connect_resolvelist(int *psockfd, gchar *host, guint port, GList *res_func_list)
    1.70 +connect_resolvelist(int *psockfd, gchar *host, guint port,
    1.71 +		GList *res_func_list)
    1.72  {
    1.73  	GList *res_node;
    1.74  	GList *addr_list;
    1.75 @@ -95,12 +99,12 @@
    1.76  	DEBUG(5) debugf("connect_resolvelist entered\n");
    1.77  
    1.78  	h_errno = 0;
    1.79 -
    1.80  	if (isdigit(*host)) {
    1.81  		mxip_addr *addr;
    1.82  
    1.83  		if ((addr_list = resolve_ip(host))) {
    1.84 -			addr = connect_hostlist(psockfd, host, port, addr_list);
    1.85 +			addr = connect_hostlist(psockfd, host, port,
    1.86 +					addr_list);
    1.87  			g_list_free(addr_list);
    1.88  			return addr;
    1.89  		}
    1.90 @@ -110,18 +114,18 @@
    1.91  		*/
    1.92  	}
    1.93  
    1.94 -	if (res_func_list == NULL) {
    1.95 -		logwrite(LOG_ALERT, "res_funcs == NULL !!!\n");
    1.96 +	if (!res_func_list) {
    1.97 +		logwrite(LOG_ALERT, "res_funcs not set!\n");
    1.98  		exit(1);
    1.99  	}
   1.100  
   1.101  	foreach(res_func_list, res_node) {
   1.102  		resolve_func res_func;
   1.103  		DEBUG(6) debugf("  foreach() body\n");
   1.104 -		res_func = (resolve_func) (res_node->data);
   1.105  
   1.106 -		if (res_func == NULL) {
   1.107 -			logwrite(LOG_ALERT, "res_func == NULL !!!\n");
   1.108 +		res_func = (resolve_func) res_node->data;
   1.109 +		if (!res_func) {
   1.110 +			logwrite(LOG_ALERT, "Empty res_func!\n");
   1.111  			exit(1);
   1.112  		}
   1.113  
   1.114 @@ -129,16 +133,16 @@
   1.115  		if ((addr_list = res_func(NULL, host))) {
   1.116  
   1.117  			mxip_addr *addr;
   1.118 -			if ((addr = connect_hostlist(psockfd, host, port, addr_list)))
   1.119 +			if ((addr = connect_hostlist(psockfd, host, port,
   1.120 +					addr_list))) {
   1.121  				return addr;
   1.122 -
   1.123 -			DEBUG(5) {
   1.124 -				debugf("connect_hostlist failed: %s\n", strerror(errno));
   1.125  			}
   1.126 -
   1.127 +			DEBUG(5) debugf("connect_hostlist failed: %s\n",
   1.128 +					strerror(errno));
   1.129  			g_list_free(addr_list);
   1.130  		} else if (!g_list_next(res_node)) {
   1.131 -			logwrite(LOG_ALERT, "could not resolve %s: %s\n", host, hstrerror(h_errno));
   1.132 +			logwrite(LOG_ALERT, "could not resolve %s: %s\n",
   1.133 +					host, hstrerror(h_errno));
   1.134  		}
   1.135  	}
   1.136  	return NULL;