From 3a5305fafe7e444529c3140aadd2b4d669b41911 Mon Sep 17 00:00:00 2001 From: des Date: Mon, 3 Mar 2008 17:03:05 +0000 Subject: [PATCH] Clean up the int -> str and ip -> str conversion code, and add double -> str. git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@2546 d4fa192b-c00b-0410-8231-f00ffab90ce4 --- varnish-cache/bin/varnishd/cache_vrt.c | 44 ++++++++++++++++---------- varnish-cache/include/vrt.h | 1 + 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/varnish-cache/bin/varnishd/cache_vrt.c b/varnish-cache/bin/varnishd/cache_vrt.c index 64c1a1a0..b9e90fad 100644 --- a/varnish-cache/bin/varnishd/cache_vrt.c +++ b/varnish-cache/bin/varnishd/cache_vrt.c @@ -37,6 +37,7 @@ #include #include +#include #include #include @@ -556,42 +557,53 @@ VRT_r_backend_health(const struct sess *sp) 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 diff --git a/varnish-cache/include/vrt.h b/varnish-cache/include/vrt.h index ea7a0f2b..7048f8dc 100644 --- a/varnish-cache/include/vrt.h +++ b/varnish-cache/include/vrt.h @@ -135,6 +135,7 @@ void VRT_fini_dir(struct cli *, struct director *); 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 { \ -- 2.39.5