r5374 - in trunk/varnish-cache: bin/varnishd include lib/libvarnish

phk at varnish-cache.org phk at varnish-cache.org
Fri Oct 1 10:07:59 CEST 2010


Author: phk
Date: 2010-10-01 10:07:58 +0200 (Fri, 01 Oct 2010)
New Revision: 5374

Modified:
   trunk/varnish-cache/bin/varnishd/cache_vrt.c
   trunk/varnish-cache/include/libvarnish.h
   trunk/varnish-cache/lib/libvarnish/tcp.c
Log:
Add a TCP_Port() function and avoid the detour over ascii representation.



Modified: trunk/varnish-cache/bin/varnishd/cache_vrt.c
===================================================================
--- trunk/varnish-cache/bin/varnishd/cache_vrt.c	2010-10-01 07:50:44 UTC (rev 5373)
+++ trunk/varnish-cache/bin/varnishd/cache_vrt.c	2010-10-01 08:07:58 UTC (rev 5374)
@@ -573,15 +573,10 @@
 int
 VRT_r_beresp_backend_port(const struct sess *sp)
 {
-	char abuf[TCP_ADDRBUFSIZE];
-	char pbuf[TCP_PORTBUFSIZE];
 
 	CHECK_OBJ_NOTNULL(sp, SESS_MAGIC);
 	CHECK_OBJ_NOTNULL(sp->vbc, VBC_MAGIC);
-	TCP_name(sp->vbc->addr, sp->vbc->addrlen,
-	    abuf, sizeof abuf, pbuf, sizeof pbuf);
-
-	return (atoi(pbuf));
+	return (TCP_port(sp->vbc->addr));
 }
 
 /*--------------------------------------------------------------------*/
@@ -856,7 +851,7 @@
 	(void)sp;
 
 	if (vrt_hostname[0] == '\0')
-		AZ(gethostname(vrt_hostname, 255));
+		AZ(gethostname(vrt_hostname, sizeof(vrt_hostname)));
 
 	return (vrt_hostname);
 }
@@ -868,15 +863,10 @@
 int
 VRT_r_server_port(struct sess *sp)
 {
-	char abuf[TCP_ADDRBUFSIZE];
-	char pbuf[TCP_PORTBUFSIZE];
 
 	if (sp->mysockaddr->sa_family == AF_UNSPEC)
 		AZ(getsockname(sp->fd, sp->mysockaddr, &sp->mysockaddrlen));
-	TCP_name(sp->mysockaddr, sp->mysockaddrlen,
-	    abuf, sizeof abuf, pbuf, sizeof pbuf);
-
-	return (atoi(pbuf));
+	return (TCP_port(sp->mysockaddr));
 }
 
 /*--------------------------------------------------------------------

Modified: trunk/varnish-cache/include/libvarnish.h
===================================================================
--- trunk/varnish-cache/include/libvarnish.h	2010-10-01 07:50:44 UTC (rev 5373)
+++ trunk/varnish-cache/include/libvarnish.h	2010-10-01 08:07:58 UTC (rev 5374)
@@ -76,6 +76,7 @@
 int TCP_nonblocking(int sock);
 int TCP_linger(int sock, int linger);
 #ifdef SOL_SOCKET
+int TCP_port(const struct sockaddr *addr);
 void TCP_name(const struct sockaddr *addr, unsigned l, char *abuf,
     unsigned alen, char *pbuf, unsigned plen);
 int TCP_connect(int s, const struct sockaddr *name, socklen_t namelen,

Modified: trunk/varnish-cache/lib/libvarnish/tcp.c
===================================================================
--- trunk/varnish-cache/lib/libvarnish/tcp.c	2010-10-01 07:50:44 UTC (rev 5373)
+++ trunk/varnish-cache/lib/libvarnish/tcp.c	2010-10-01 08:07:58 UTC (rev 5374)
@@ -63,6 +63,24 @@
 
 /*--------------------------------------------------------------------*/
 
+int
+TCP_port(const struct sockaddr *addr)
+{
+
+	if (addr->sa_family == AF_INET) {
+		const struct sockaddr_in *ain = (const void *)addr;
+		return ntohs((ain->sin_port));
+	}
+	if (addr->sa_family == AF_INET6) {
+		const struct sockaddr_in6 *ain = (const void *)addr;
+		return ntohs((ain->sin6_port));
+	}
+	return (-1);
+}
+
+
+/*--------------------------------------------------------------------*/
+
 void
 TCP_name(const struct sockaddr *addr, unsigned l, char *abuf, unsigned alen,
     char *pbuf, unsigned plen)




More information about the varnish-commit mailing list