r2546 - in trunk/varnish-cache: bin/varnishd include

des at projects.linpro.no des at projects.linpro.no
Mon Mar 3 18:03:05 CET 2008


Author: des
Date: 2008-03-03 18:03:05 +0100 (Mon, 03 Mar 2008)
New Revision: 2546

Modified:
   trunk/varnish-cache/bin/varnishd/cache_vrt.c
   trunk/varnish-cache/include/vrt.h
Log:
Clean up the int -> str and ip -> str conversion code, and add double -> str.


Modified: trunk/varnish-cache/bin/varnishd/cache_vrt.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_vrt.c	2008-03-03 10:53:31 UTC (rev 2545)
+++ trunk/varnish-cache/bin/varnishd/cache_vrt.c	2008-03-03 17:03:05 UTC (rev 2546)
@@ -37,6 +37,7 @@
 #include <sys/socket.h>
 
 #include <netinet/in.h>
+#include <arpa/inet.h>
 
 #include <stdio.h>
 #include <string.h>
@@ -556,42 +557,53 @@
 char *
 VRT_IP_string(const struct sess *sp, const struct sockaddr *sa)
 {
-	char h[64], p[8], *q;
-	socklen_t len = 0;
+	char *p;
+	const void *addr;
+	int len;
 
-	/* XXX can't rely on sockaddr.sa_len */
 	switch (sa->sa_family) {
 	case AF_INET:
-		len = sizeof(struct sockaddr_in);
+		len = INET_ADDRSTRLEN;
+		addr = &((const struct sockaddr_in *)sa)->sin_addr;
 		break;
 	case AF_INET6:
-		len = sizeof(struct sockaddr_in6);
+		len = INET_ADDRSTRLEN;
+		addr = &((const struct sockaddr_in6 *)sa)->sin6_addr;
 		break;
 	default:
 		INCOMPL();
 	}
 	XXXAN(len);
-	TCP_name(sa, len, h, sizeof h, p, sizeof p);
-	q = WS_Alloc(sp->http->ws, strlen(h) + strlen(p) + 2);
-	AN(q);
-	strcpy(q, h);
-	strcat(q, ":");
-	strcat(q, p);
-	return (q);
+	AN(p = WS_Alloc(sp->http->ws, len));
+	AN(inet_ntop(sa->sa_family, addr, p, len));
+	return (p);
 }
 
 char *
 VRT_int_string(const struct sess *sp, int num)
 {
 	char *p;
-	int size = 12;
-	
-	p = WS_Alloc(sp->http->ws, size);
-	AN(p);
+	int size;
+
+	size = snprintf(NULL, 0, "%d", num) + 1;
+	AN(p = WS_Alloc(sp->http->ws, size));
 	assert(snprintf(p, size, "%d", num) < size);
 	return (p);
 }
 
+char *
+VRT_double_string(const struct sess *sp, double num)
+{
+	char *p;
+	int size;
+
+	size = snprintf(NULL, 0, "%.3f", num) + 1;
+	AN(p = WS_Alloc(sp->http->ws, size));
+	assert((p = malloc(size)) != 0);
+	assert(snprintf(p, size, "%.3f", num) < size);
+	return (p);
+}
+
 /*--------------------------------------------------------------------*/
 
 void

Modified: trunk/varnish-cache/include/vrt.h
===================================================================
--- trunk/varnish-cache/include/vrt.h	2008-03-03 10:53:31 UTC (rev 2545)
+++ trunk/varnish-cache/include/vrt.h	2008-03-03 17:03:05 UTC (rev 2546)
@@ -135,6 +135,7 @@
 
 char *VRT_IP_string(const struct sess *sp, const struct sockaddr *sa);
 char *VRT_int_string(const struct sess *sp, int);
+char *VRT_double_string(const struct sess *sp, double);
 
 #define VRT_done(sp, hand)			\
 	do {					\




More information about the varnish-commit mailing list