summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--src/sock.h14
-rw-r--r--src/transparent-proxy.c8
2 files changed, 17 insertions, 5 deletions
diff --git a/src/sock.h b/src/sock.h
index 722371d..aee5bf5 100644
--- a/src/sock.h
+++ b/src/sock.h
@@ -31,6 +31,20 @@
#include "common.h"
#include "sblist.h"
+#define SOCKADDR_UNION_AF(PTR) (PTR)->v4.sin_family
+
+#define SOCKADDR_UNION_LENGTH(PTR) ( \
+ ( SOCKADDR_UNION_AF(PTR) == AF_INET ) ? sizeof((PTR)->v4) : ( \
+ ( SOCKADDR_UNION_AF(PTR) == AF_INET6 ) ? sizeof((PTR)->v6) : 0 ) )
+
+#define SOCKADDR_UNION_ADDRESS(PTR) ( \
+ ( SOCKADDR_UNION_AF(PTR) == AF_INET ) ? (void*) &(PTR)->v4.sin_addr : ( \
+ ( SOCKADDR_UNION_AF(PTR) == AF_INET6 ) ? (void*) &(PTR)->v6.sin6_addr : (void*) 0 ) )
+
+#define SOCKADDR_UNION_PORT(PTR) ( \
+ ( SOCKADDR_UNION_AF(PTR) == AF_INET ) ? (PTR)->v4.sin_port : ( \
+ ( SOCKADDR_UNION_AF(PTR) == AF_INET6 ) ? (PTR)->v6.sin6_port : 0 ) )
+
union sockaddr_union {
struct sockaddr_in v4;
struct sockaddr_in6 v6;
diff --git a/src/transparent-proxy.c b/src/transparent-proxy.c
index e3265f9..d090ae3 100644
--- a/src/transparent-proxy.c
+++ b/src/transparent-proxy.c
@@ -83,16 +83,14 @@ do_transparent_proxy (struct conn_s *connptr, orderedmap hashofheaders,
return 0;
}
- af = length == sizeof(dest_addr.v4) ? AF_INET : AF_INET6;
- if (af == AF_INET) dest_inaddr = &dest_addr.v4.sin_addr;
- else dest_inaddr = &dest_addr.v6.sin6_addr;
+ af = SOCKADDR_UNION_AF(&dest_addr);
+ dest_inaddr = SOCKADDR_UNION_ADDRESS(&dest_addr);
if (!inet_ntop(af, dest_inaddr, namebuf, sizeof namebuf))
goto addr_err;
request->host = safestrdup (namebuf);
- request->port = ntohs (af == AF_INET ? dest_addr.v4.sin_port
- : dest_addr.v6.sin6_port);
+ request->port = ntohs (SOCKADDR_UNION_PORT(&dest_addr));
request->path = (char *) safemalloc (ulen + 1);
strlcpy (request->path, *url, ulen + 1);