From owner-freebsd-bugs@FreeBSD.ORG Tue Sep 25 23:20:02 2007 Return-Path: Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 7B56D16A49E for ; Tue, 25 Sep 2007 23:20:02 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [IPv6:2001:4f8:fff6::28]) by mx1.freebsd.org (Postfix) with ESMTP id 1511D13C474 for ; Tue, 25 Sep 2007 23:20:01 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.14.1/8.14.1) with ESMTP id l8PNK1R5001767 for ; Tue, 25 Sep 2007 23:20:01 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.14.1/8.14.1/Submit) id l8PNK1rd001751; Tue, 25 Sep 2007 23:20:01 GMT (envelope-from gnats) Resent-Date: Tue, 25 Sep 2007 23:20:01 GMT Resent-Message-Id: <200709252320.l8PNK1rd001751@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Jeremie Le Hen Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 9B34516A419 for ; Tue, 25 Sep 2007 23:13:20 +0000 (UTC) (envelope-from tataz@tataz.chchile.org) Received: from smtp5-g19.free.fr (smtp5-g19.free.fr [212.27.42.35]) by mx1.freebsd.org (Postfix) with ESMTP id 1EC2913C455 for ; Tue, 25 Sep 2007 23:13:19 +0000 (UTC) (envelope-from tataz@tataz.chchile.org) Received: from smtp5-g19.free.fr (localhost.localdomain [127.0.0.1]) by smtp5-g19.free.fr (Postfix) with ESMTP id 76C5287BAE for ; Wed, 26 Sep 2007 01:13:18 +0200 (CEST) Received: from tatooine.tataz.chchile.org (tataz.chchile.org [82.233.239.98]) by smtp5-g19.free.fr (Postfix) with ESMTP id 4A177878B9 for ; Wed, 26 Sep 2007 01:13:18 +0200 (CEST) Received: from obiwan.tataz.chchile.org (unknown [192.168.1.25]) by tatooine.tataz.chchile.org (Postfix) with ESMTP id C277C9B497; Tue, 25 Sep 2007 23:11:36 +0000 (UTC) Received: by obiwan.tataz.chchile.org (Postfix, from userid 1000) id B6515405B; Wed, 26 Sep 2007 01:11:36 +0200 (CEST) Message-Id: <20070925231136.B6515405B@obiwan.tataz.chchile.org> Date: Wed, 26 Sep 2007 01:11:36 +0200 (CEST) From: Jeremie Le Hen To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Jeremie Le Hen Subject: misc/116643: [patch] fstat: add INET/INET6 socket details as in NetBSD and OpenBSD X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Jeremie Le Hen List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 25 Sep 2007 23:20:02 -0000 >Number: 116643 >Category: misc >Synopsis: [patch] fstat: add INET/INET6 socket details as in NetBSD and OpenBSD >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Tue Sep 25 23:20:01 GMT 2007 >Closed-Date: >Last-Modified: >Originator: Jeremie Le Hen >Release: FreeBSD 7.0-CURRENT i386 >Organization: >Environment: FreeBSD 7.0-CURRENT >Description: This patch adds socket details to AF_INET/AF_INET6 output of fstat. Currently, we have an output like this: root sshd 721 3* internet stream tcp c3f08e10 tataz ssh 1271 3* internet stream tcp c4611870 With this patch, we have: root sshd 721 3* internet stream tcp c3f08e10 *:22 tataz ssh 1271 3* internet stream tcp c4611870 192.168.1.3:50995 <-> 192.168.1.2:22 >How-To-Repeat: >Fix: The patch below implements this behaviour. It has been ported from NetBSD and OpenBSD. Note that due to lack of inet6 connectivity, I couldn't test the AF_INET6 case. --- fstat_inetsock_detail.diff begins here --- Index: fstat.c =================================================================== RCS file: /mnt/octobre/space/freebsd-cvs/src/usr.bin/fstat/fstat.c,v retrieving revision 1.64 diff -u -p -r1.64 fstat.c --- fstat.c 9 Mar 2007 16:21:40 -0000 1.64 +++ fstat.c 25 Sep 2007 23:04:31 -0000 @@ -86,6 +86,8 @@ __FBSDID("$FreeBSD: src/usr.bin/fstat/fs #include #include +#include + #include #include #include @@ -151,6 +153,7 @@ int nfs_filestat(struct vnode *vp, stru int devfs_filestat(struct vnode *vp, struct filestat *fsp); char *getmnton(struct mount *m); void pipetrans(struct pipe *pi, int i, int flag); +const char *inet6_addrstr(struct in6_addr *); void socktrans(struct socket *sock, int i); void getinetproto(int number); int getfname(const char *filename); @@ -757,6 +760,31 @@ bad: printf("* error\n"); } +const char * +inet6_addrstr(struct in6_addr *p) +{ + struct sockaddr_in6 sin6; + static char hbuf[NI_MAXHOST]; + const int niflags = NI_NUMERICHOST; + + memset(&sin6, 0, sizeof(sin6)); + sin6.sin6_family = AF_INET6; + sin6.sin6_len = sizeof(struct sockaddr_in6); + sin6.sin6_addr = *p; + if (IN6_IS_ADDR_LINKLOCAL(p) && + *(u_int16_t *)&sin6.sin6_addr.s6_addr[2] != 0) { + sin6.sin6_scope_id = + ntohs(*(u_int16_t *)&sin6.sin6_addr.s6_addr[2]); + sin6.sin6_addr.s6_addr[2] = sin6.sin6_addr.s6_addr[3] = 0; + } + + if (getnameinfo((struct sockaddr *)&sin6, sin6.sin6_len, + hbuf, sizeof(hbuf), NULL, 0, niflags)) + return "invalid"; + + return hbuf; +} + void socktrans(struct socket *sock, int i) { @@ -776,6 +804,7 @@ socktrans(struct socket *sock, int i) struct unpcb unpcb; int len; char dname[32]; + char xaddrbuf[NI_MAXHOST + 2]; PREFIX(i); @@ -826,19 +855,68 @@ socktrans(struct socket *sock, int i) */ switch(dom.dom_family) { case AF_INET: + getinetproto(proto.pr_protocol); + if (proto.pr_protocol == IPPROTO_TCP || + proto.pr_protocol == IPPROTO_UDP) { + if (so.so_pcb == NULL) + break; + if (kvm_read(kd, (u_long)so.so_pcb, + (char *)&inpcb, sizeof(struct inpcb)) + != sizeof(struct inpcb)) { + dprintf(stderr, + "can't read inpcb at %p\n", + (void *)so.so_pcb); + goto bad; + } + if (proto.pr_protocol == IPPROTO_TCP) + printf(" %lx", (u_long)inpcb.inp_ppcb); + else + printf(" %lx", (u_long)so.so_pcb); + printf(" %s:%hu", + inpcb.inp_laddr.s_addr == INADDR_ANY ? "*" : + inet_ntoa(inpcb.inp_laddr), + ntohs(inpcb.inp_lport)); + if (inpcb.inp_fport) { + printf(" <-> %s:%hu", + inpcb.inp_faddr.s_addr == INADDR_ANY ? + "*" : inet_ntoa(inpcb.inp_faddr), + ntohs(inpcb.inp_fport)); + } + } + else if (so.so_pcb) + printf(" %lx", (u_long)so.so_pcb); + break; case AF_INET6: getinetproto(proto.pr_protocol); - if (proto.pr_protocol == IPPROTO_TCP ) { - if (so.so_pcb) { - if (kvm_read(kd, (u_long)so.so_pcb, - (char *)&inpcb, sizeof(struct inpcb)) - != sizeof(struct inpcb)) { - dprintf(stderr, - "can't read inpcb at %p\n", - (void *)so.so_pcb); - goto bad; - } + if (proto.pr_protocol == IPPROTO_TCP || + proto.pr_protocol == IPPROTO_UDP) { + if (so.so_pcb == NULL) + break; + if (kvm_read(kd, (u_long)so.so_pcb, + (char *)&inpcb, sizeof(struct inpcb)) + != sizeof(struct inpcb)) { + dprintf(stderr, + "can't read inpcb at %p\n", + (void *)so.so_pcb); + goto bad; + } + if (proto.pr_protocol == IPPROTO_TCP) printf(" %lx", (u_long)inpcb.inp_ppcb); + else + printf(" %lx", (u_long)so.so_pcb); + snprintf(xaddrbuf, sizeof(xaddrbuf), "[%s]", + inet6_addrstr(&inpcb.in6p_laddr)); + printf(" %s:%hu", + IN6_IS_ADDR_UNSPECIFIED(&inpcb.in6p_laddr) ? + "*" : xaddrbuf, + ntohs(inpcb.in6p_lport)); + if (inpcb.in6p_fport) { + snprintf(xaddrbuf, sizeof(xaddrbuf), + "[%s]", inet6_addrstr(&inpcb.in6p_faddr)); + printf(" <-> %s:%hu", + IN6_IS_ADDR_UNSPECIFIED(&inpcb.in6p_faddr)? + "*" : xaddrbuf, + ntohs(inpcb.in6p_fport)); } } else if (so.so_pcb) --- fstat_inetsock_detail.diff ends here --- >Release-Note: >Audit-Trail: >Unformatted: