Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys Add sockaddr_format to ease debugging code dealing with ...
details: https://anonhg.NetBSD.org/src/rev/eb9ee791d882
branches: trunk
changeset: 784536:eb9ee791d882
user: joerg <joerg%NetBSD.org@localhost>
date: Thu Jan 31 14:30:47 2013 +0000
description:
Add sockaddr_format to ease debugging code dealing with socket
addresses.
diffstat:
sys/kern/uipc_domain.c | 65 ++++++++++++++++++++++++++++++++++++++++++++++++-
sys/sys/socket.h | 3 +-
2 files changed, 65 insertions(+), 3 deletions(-)
diffs (110 lines):
diff -r c8a0cc683192 -r eb9ee791d882 sys/kern/uipc_domain.c
--- a/sys/kern/uipc_domain.c Thu Jan 31 12:41:41 2013 +0000
+++ b/sys/kern/uipc_domain.c Thu Jan 31 14:30:47 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: uipc_domain.c,v 1.87 2011/10/27 21:10:55 seanb Exp $ */
+/* $NetBSD: uipc_domain.c,v 1.88 2013/01/31 14:30:47 joerg Exp $ */
/*
* Copyright (c) 1982, 1986, 1993
@@ -32,7 +32,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.87 2011/10/27 21:10:55 seanb Exp $");
+__KERNEL_RCSID(0, "$NetBSD: uipc_domain.c,v 1.88 2013/01/31 14:30:47 joerg Exp $");
#include <sys/param.h>
#include <sys/socket.h>
@@ -52,6 +52,7 @@
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kauth.h>
+#include <netinet/in.h>
MALLOC_DECLARE(M_SOCKADDR);
@@ -331,6 +332,66 @@
free(sa, M_SOCKADDR);
}
+void
+sockaddr_format(const struct sockaddr *sa, char *buf, size_t len)
+{
+ const struct sockaddr_un *sun = (const struct sockaddr_un *)sa;
+ const struct sockaddr_in *sin = (const struct sockaddr_in *)sa;
+ const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sa;
+ const uint8_t *data;
+ size_t data_len;
+
+ if (sa == NULL) {
+ strlcpy(buf, "(null)", len);
+ return;
+ }
+
+ switch (sa->sa_family) {
+ default:
+ snprintf(buf, len, "(unknown socket family %d)",
+ (int)sa->sa_family);
+ return;
+ case AF_LOCAL:
+ strlcpy(buf, "unix:", len);
+ strlcat(buf, sun->sun_path, len);
+ return;
+ case AF_INET:
+ strlcpy(buf, "inet:", len);
+ if (len < 6)
+ return;
+ buf += 5;
+ len -= 5;
+ data = (const uint8_t *)&sin->sin_addr;
+ data_len = sizeof(sin->sin_addr);
+ break;
+ case AF_INET6:
+ strlcpy(buf, "inet6:", len);
+ if (len < 7)
+ return;
+ buf += 6;
+ len -= 6;
+ data = (const uint8_t *)&sin6->sin6_addr;
+ data_len = sizeof(sin6->sin6_addr);
+ break;
+ }
+ for (;;) {
+ if (--len == 0)
+ break;
+
+ uint8_t hi = *data >> 4;
+ uint8_t lo = *data & 15;
+ --data_len;
+ ++data;
+ *buf++ = hi + (hi >= 10 ? 'a' - 10 : '0');
+ if (--len == 0)
+ break;
+ *buf++ = lo + (lo >= 10 ? 'a' - 10 : '0');
+ if (data_len == 0)
+ break;
+ }
+ *buf = 0;
+}
+
/*
* sysctl helper to stuff PF_LOCAL pcbs into sysctl structures
*/
diff -r c8a0cc683192 -r eb9ee791d882 sys/sys/socket.h
--- a/sys/sys/socket.h Thu Jan 31 12:41:41 2013 +0000
+++ b/sys/sys/socket.h Thu Jan 31 14:30:47 2013 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: socket.h,v 1.107 2012/06/22 18:26:35 christos Exp $ */
+/* $NetBSD: socket.h,v 1.108 2013/01/31 14:30:47 joerg Exp $ */
/*
* Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
@@ -602,6 +602,7 @@
const void *sockaddr_anyaddr(const struct sockaddr *, socklen_t *);
int sockaddr_cmp(const struct sockaddr *, const struct sockaddr *);
struct sockaddr *sockaddr_dup(const struct sockaddr *, int);
+void sockaddr_format(const struct sockaddr *, char *, size_t);
void sockaddr_free(struct sockaddr *);
__END_DECLS
#endif /* _KERNEL */
Home |
Main Index |
Thread Index |
Old Index