diff options
author | Claudio Jeker <claudio@cvs.openbsd.org> | 2016-09-04 17:05:25 +0000 |
---|---|---|
committer | Claudio Jeker <claudio@cvs.openbsd.org> | 2016-09-04 17:05:25 +0000 |
commit | bf44c72ec788344a048a3190adf5cb4c78008ca2 (patch) | |
tree | 795df51e5b33dee454ac8f38cd4ccc9feee13104 /sys/netinet | |
parent | efb163ab9f44659e77b903832efbc5747f53e611 (diff) |
Implement a sockaddr_ntop() function that works like inet_ntop() but prints
sockaddrs. Works for all sockaddrs so can be used to print sockaddrs nicely.
OK phessler@
Diffstat (limited to 'sys/netinet')
-rw-r--r-- | sys/netinet/in.h | 12 | ||||
-rw-r--r-- | sys/netinet/inet_ntop.c | 31 |
2 files changed, 36 insertions, 7 deletions
diff --git a/sys/netinet/in.h b/sys/netinet/in.h index a390574da3a..4b2b0f54f55 100644 --- a/sys/netinet/in.h +++ b/sys/netinet/in.h @@ -1,4 +1,4 @@ -/* $OpenBSD: in.h,v 1.118 2016/08/16 22:21:17 vgross Exp $ */ +/* $OpenBSD: in.h,v 1.119 2016/09/04 17:05:24 claudio Exp $ */ /* $NetBSD: in.h,v 1.20 1996/02/13 23:41:47 christos Exp $ */ /* @@ -798,6 +798,10 @@ extern struct niqueue ipintrq; /* ip packet input queue */ extern struct in_addr zeroin_addr; struct mbuf; +struct sockaddr; +struct sockaddr_in; +struct ifaddr; +struct in_ifaddr; int in_broadcast(struct in_addr, u_int); int in_canforward(struct in_addr); @@ -813,15 +817,11 @@ int inet_nat64(int, const void *, void *, const void *, u_int8_t); int inet_nat46(int, const void *, void *, const void *, u_int8_t); const char *inet_ntop(int, const void *, char *, socklen_t); +const char *sockaddr_ntop(struct sockaddr *, char *, size_t); #define in_hosteq(s,t) ((s).s_addr == (t).s_addr) #define in_nullhost(x) ((x).s_addr == INADDR_ANY) -struct sockaddr; -struct sockaddr_in; -struct ifaddr; -struct in_ifaddr; - /* * Convert between address family specific and general structs. * Inline functions check the source type and are stricter than diff --git a/sys/netinet/inet_ntop.c b/sys/netinet/inet_ntop.c index 6a1d3f99057..bc9c0f89ee8 100644 --- a/sys/netinet/inet_ntop.c +++ b/sys/netinet/inet_ntop.c @@ -1,4 +1,4 @@ -/* $OpenBSD: inet_ntop.c,v 1.3 2015/03/14 03:38:51 jsg Exp $ */ +/* $OpenBSD: inet_ntop.c,v 1.4 2016/09/04 17:05:24 claudio Exp $ */ /* Copyright (c) 1996 by Internet Software Consortium. * @@ -59,6 +59,35 @@ inet_ntop(int af, const void *src, char *dst, socklen_t size) /* NOTREACHED */ } +const char * +sockaddr_ntop(struct sockaddr *sa, char *dst, size_t size) +{ + u_int8_t l; + size_t n; + + if (sa->sa_len < 2) + return "bad sa"; + switch (sa->sa_family) { + case AF_INET: + return inet_ntop4((u_char *)&satosin(sa)->sin_addr, dst, size); +#ifdef INET6 + case AF_INET6: + return inet_ntop6((u_char *)&satosin6(sa)->sin6_addr, dst, size); +#endif + default: + n = snprintf(dst, size, "%d ", sa->sa_family); + for (l = 0; l < sa->sa_len - offsetof(struct sockaddr, sa_data); l++) { + int r = snprintf(dst + n, size - n, "%02x", sa->sa_data[l]); + if (r == -1) + return "bad sa"; + n += r; + if (n > size) + n = size; + } + return (dst); + } +} + /* const char * * inet_ntop4(src, dst, size) * format an IPv4 address, more or less like inet_ntoa() |