r5475 - trunk/varnish-cache/bin/varnishd

phk at varnish-cache.org phk at varnish-cache.org
Wed Oct 27 13:54:02 CEST 2010


Author: phk
Date: 2010-10-27 13:54:02 +0200 (Wed, 27 Oct 2010)
New Revision: 5475

Modified:
   trunk/varnish-cache/bin/varnishd/cache_dir_dns.c
Log:
Stylistic and some minor simplifications to shut up Flexelint.



Modified: trunk/varnish-cache/bin/varnishd/cache_dir_dns.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_dir_dns.c	2010-10-27 11:25:55 UTC (rev 5474)
+++ trunk/varnish-cache/bin/varnishd/cache_dir_dns.c	2010-10-27 11:54:02 UTC (rev 5475)
@@ -81,54 +81,48 @@
 	double			ttl;
 };
 
-
-
 /* Compare an IPv4 backend to a IPv4 addr/len */
 static int
 vdi_dns_comp_addrinfo4(const struct backend *bp,
-		       const struct sockaddr_in *addr,
+		       const struct sockaddr_storage *addr,
 		       const socklen_t len)
 {
 	uint32_t u, p;
-	struct sockaddr_in *bps = (struct sockaddr_in *) bp->ipv4;
+	const struct sockaddr_in *bps = (const void *)bp->ipv4;
+	const struct sockaddr_in *bpd = (const void *)addr;
 
 	if (bp->ipv4len != len || len <= 0)
-		return 0;
+		return (0);
 
-	u = addr->sin_addr.s_addr;
+	u = bpd->sin_addr.s_addr;
 	p = bps->sin_addr.s_addr;
 
-	return u == p;
+	return (u == p);
 }
 
 /* Compare an IPv6 backend to a IPv6 addr/len */
 static int
 vdi_dns_comp_addrinfo6(const struct backend *bp,
-		       struct sockaddr_in6 *addr,
+		       const struct sockaddr_storage *addr,
 		       const socklen_t len)
 {
-	uint8_t *u, *p;
-	int i;
-	struct sockaddr_in6 *bps = (struct sockaddr_in6 *) bp->ipv6;
+	const uint8_t *u, *p;
+	const struct sockaddr_in6 *bps = (const void *)bp->ipv6;
+	const struct sockaddr_in6 *bpd = (const void *)addr;
 
 	if (bp->ipv6len != len || len <= 0)
-		return 0;
+		return (0);
 
-	u = addr->sin6_addr.s6_addr;
+	u = bpd->sin6_addr.s6_addr;
 	p = bps->sin6_addr.s6_addr;
 
-	for (i=0; i < 16; i++) {
-		if (u[i] != p[i])
-			return 0;
-	}
-
-	return 1;
+	return (!memcmp(u, p, 16));
 }
 
 /* Check if a backends socket is the same as addr */
 static int
 vdi_dns_comp_addrinfo(const struct director *dir,
-		      struct sockaddr_storage *addr,
+		      const struct sockaddr_storage *addr,
 		      const socklen_t len)
 {
 	struct backend *bp;
@@ -136,13 +130,11 @@
 	bp = vdi_get_backend_if_simple(dir);
 	AN(bp);
 	if (addr->ss_family == PF_INET && bp->ipv4) {
-		return (vdi_dns_comp_addrinfo4(bp, (struct sockaddr_in *)
-			addr, len));
+		return (vdi_dns_comp_addrinfo4(bp, addr, len));
 	} else if (addr->ss_family == PF_INET6 && bp->ipv6) {
-		return (vdi_dns_comp_addrinfo6(bp, (struct sockaddr_in6 *)
-			addr, len));
+		return (vdi_dns_comp_addrinfo6(bp, addr, len));
 	}
-	return 0;
+	return (0);
 }
 
 /* Pick a host from an existing hostgroup.
@@ -167,11 +159,11 @@
 			current = i + initial;
 		if (VDI_Healthy_sp(sp, group->hosts[current])) {
 			group->next_host = current+1;
-			return group->hosts[current];
+			return (group->hosts[current]);
 		}
 	}
 
-	return NULL;
+	return (NULL);
 }
 
 /* Remove an item from the dns cache.
@@ -196,7 +188,7 @@
 static inline int
 vdi_dns_groupmatch(const struct vdi_dns_hostgroup *group, const char *hostname)
 {
-	return !strcmp(group->hostname, hostname);
+	return (!strcmp(group->hostname, hostname));
 }
 
 /* Search the cache for 'hostname' and put a backend-pointer as necessary,
@@ -221,17 +213,17 @@
 		if (hostgr->ttl <= sp->t_req) {
 			if (rwlock)
 				vdi_dns_pop_cache(vs, hostgr);
-			return 0;
+			return (0);
 		}
 		if (vdi_dns_groupmatch(hostgr, hostname)) {
 			ret = (vdi_dns_pick_host(sp, hostgr));
 			*backend = ret;
 			if (*backend != NULL)
 				CHECK_OBJ_NOTNULL(*backend, DIRECTOR_MAGIC);
-			return 1;
+			return (1);
 		}
 	}
-	return 0;
+	return (0);
 }
 
 /* Add a newly cached item to the dns cache list.
@@ -266,6 +258,7 @@
 	int error, i, host = 0;
 	struct addrinfo *res0, *res, hint;
 	struct vdi_dns_hostgroup *new;
+
 	/* Due to possible race while upgrading the lock, we have to
 	 * recheck if the result is already looked up. The overhead for
 	 * this is insignificant unless dns isn't cached properly (all
@@ -273,7 +266,7 @@
 	 */
 
 	if (vdi_dns_cache_has(sp, vs, hostname, backend, 1))
-		return 1;
+		return (1);
 
 	memset(&hint, 0, sizeof hint);
 	hint.ai_family = PF_UNSPEC;
@@ -281,16 +274,15 @@
 
 	ALLOC_OBJ(new, VDI_DNSDIR_MAGIC);
 	XXXAN(new);
-	new->hostname = calloc(sizeof(char), strlen(hostname)+1);
-	XXXAN(new->hostname);
-	strcpy(new->hostname, hostname);
 
+	REPLACE(new->hostname, hostname);
+
 	error = getaddrinfo(hostname, "80", &hint, &res0);
 	VSC_main->dir_dns_lookups++;
 	if (error) {
 		vdi_dns_cache_list_add(sp, vs, new);
 		VSC_main->dir_dns_failed++;
-		return 0;
+		return (0);
 	}
 
 	for (res = res0; res; res = res->ai_next) {
@@ -314,7 +306,7 @@
 	new->nhosts = host;
 	vdi_dns_cache_list_add(sp, vs, new);
 	*backend = vdi_dns_pick_host(sp, new);
-	return 1;
+	return (1);
 }
 
 /* Walk through the cached lookups looking for the relevant host, add one
@@ -329,10 +321,16 @@
 {
 	struct director *backend = NULL;
 	int ret;
+
 	AZ(pthread_rwlock_rdlock(&vs->rwlock));
 	ret = vdi_dns_cache_has(sp, vs, hostname, &backend, 0);
 	AZ(pthread_rwlock_unlock(&vs->rwlock));
 	if (!ret) {
+		/*
+		 * XXX: Isn't there a race here where another thread
+		 * XXX: could grab the lock and add it before we do ?
+		 * XXX: Should 'ret' be checked for that ?
+		 */
 		AZ(pthread_rwlock_wrlock(&vs->rwlock));
 		ret = vdi_dns_cache_add(sp, vs, hostname, &backend);
 		AZ(pthread_rwlock_unlock(&vs->rwlock));
@@ -342,7 +340,7 @@
 	/* Bank backend == cached a failure, so to speak */
 	if (backend != NULL)
 		CHECK_OBJ_NOTNULL(backend, DIRECTOR_MAGIC);
-	return backend;
+	return (backend);
 }
 
 /* Parses the Host:-header and heads out to find a backend.
@@ -352,9 +350,8 @@
 {
 	struct director *ret;
 	struct http *hp;
-	char *p;
+	char *p, *q;
 	char hostname[NI_MAXHOST];
-	int i;
 
 	/* bereq is only present after recv et. al, otherwise use req (ie:
 	 * use req for health checks in vcl_recv and such).
@@ -369,22 +366,16 @@
 	if (http_GetHdr(hp, H_Host, &p) == 0)
 		return (NULL);
 
-	/* We need a working copy since it's going to be modified */
-	strncpy(hostname, p, sizeof(hostname));
+	q = strchr(p, ':');
+	if (q == NULL)
+		q = strchr(p, '\0');
+	AN(q);
 
-	/* remove port-portion of the Host-header, if present. */
-	for (i = 0; i < strlen(hostname); i++) {
-		if (hostname[i] == ':') {
-			hostname[i] = '\0';
-			break;
-		}
-	}
+	bprintf(hostname, "%.*s%s", (int)(q - p), p,
+	    vs->suffix ? vs->suffix : "");
 
-	if (vs->suffix)
-		strncat(hostname, vs->suffix, sizeof(hostname) - strlen(hostname));
-
 	ret = vdi_dns_walk_cache(sp, vs, hostname);
-	return ret;
+	return (ret);
 }
 
 static struct vbc *
@@ -412,10 +403,10 @@
 	/* XXX: Fooling -Werror for a bit until it's actually implemented.
 	 */
 	if (now || dir || target)
-		return 1;
+		return (1);
 	else
-		return 1;
-	return 1;
+		return (1);
+	return (1);
 	/*
 	struct vdi_dns *vs;
 	struct director *dir;
@@ -428,8 +419,8 @@
 	dir = vdi_dns_find_backend(sp, vs);
 
 	if (dir)
-		return 1;
-	return 0;
+		return (1);
+	return (0);
 	*/
 }
 




More information about the varnish-commit mailing list