From owner-freebsd-net@FreeBSD.ORG Tue Nov 20 14:48:30 2007 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1208516A417 for ; Tue, 20 Nov 2007 14:48:30 +0000 (UTC) (envelope-from jinmei@isl.rdc.toshiba.co.jp) Received: from shuttle.wide.toshiba.co.jp (shuttle.wide.toshiba.co.jp [IPv6:2001:200:1b1::35]) by mx1.freebsd.org (Postfix) with ESMTP id C5EC013C4D3 for ; Tue, 20 Nov 2007 14:48:29 +0000 (UTC) (envelope-from jinmei@isl.rdc.toshiba.co.jp) Received: from mymb.local (g046141.ppp.asahi-net.or.jp [211.132.46.141]) by shuttle.wide.toshiba.co.jp (Postfix) with ESMTP id CC08373024 for ; Tue, 20 Nov 2007 23:48:28 +0900 (JST) Date: Tue, 20 Nov 2007 23:48:28 +0900 Message-ID: From: JINMEI Tatuya / =?ISO-2022-JP?B?GyRCP0BMQEMjOkgbKEI=?= To: freebsd-net@freebsd.org In-Reply-To: References: User-Agent: Wanderlust/2.14.0 (Africa) Emacs/22.0 Mule/5.0 (SAKAKI) MIME-Version: 1.0 (generated by SEMI 1.14.6 - "Maruoka") Content-Type: text/plain; charset=US-ASCII Subject: Re: a format error in pf_print_host() X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 20 Nov 2007 14:48:30 -0000 At Tue, 20 Nov 2007 23:17:43 +0900, JINMEI Tatuya wrote: > formats "1:2:3:4:5:6:7:8" as ":2:3:4:5:6:7:8". This can be confirmed > by the sample code attached to this message by > - saving the file as e.g. "foo.c" > - cc -o foo foo.c > - ./foo 1:2:3:4:5:6:7:8 > > I've also attached a proposed patch to this problem. The diff was > made against 6-STABLE, but it's probably applicable to other versions. Hmm...apparently attachments were stripped. I'm directly copying the file contents below: =================== sample program =================== #include #include #include #include #include #include #include struct pf_addr { union { struct in_addr v4; struct in6_addr v6; u_int8_t addr8[16]; u_int16_t addr16[8]; u_int32_t addr32[4]; } pfa; /* 128-bit address */ #define v4 pfa.v4 #define v6 pfa.v6 #define addr8 pfa.addr8 #define addr16 pfa.addr16 #define addr32 pfa.addr32 }; static void pf_print_host(struct pf_addr *addr, u_int16_t p, sa_family_t af) { switch (af) { case AF_INET: { u_int32_t a = ntohl(addr->addr32[0]); printf("%u.%u.%u.%u", (a>>24)&255, (a>>16)&255, (a>>8)&255, a&255); if (p) { p = ntohs(p); printf(":%u", p); } break; } case AF_INET6: { u_int16_t b; u_int8_t i, curstart = 255, curend = 0, maxstart = 0, maxend = 0; for (i = 0; i < 8; i++) { if (!addr->addr16[i]) { if (curstart == 255) curstart = i; else curend = i; } else { if (curstart) { if ((curend - curstart) > (maxend - maxstart)) { maxstart = curstart; maxend = curend; curstart = 255; } } } } for (i = 0; i < 8; i++) { if (i >= maxstart && i <= maxend) { if (maxend != 7) { if (i == maxstart) printf(":"); } else { if (i == maxend) printf(":"); } } else { b = ntohs(addr->addr16[i]); printf("%x", b); if (i < 7) printf(":"); } } if (p) { p = ntohs(p); printf("[%u]", p); } break; } } } main(int argc, char *argv[]) { struct pf_addr addr; if (argc < 2) { fprintf(stderr, "specify an address\n"); exit(1); } if (inet_pton(AF_INET6, argv[1], &addr) != 1) { fprintf(stderr, "inet_pton failed for %s\n", argv[1]); exit(1); } pf_print_host(&addr, 0, AF_INET6); putchar('\n'); exit(0); } =================== sample program =================== =================== patch for pf.c =================== Index: pf.c =================================================================== RCS file: /home/ncvs/src/sys/contrib/pf/net/pf.c,v retrieving revision 1.34.2.6 diff -u -r1.34.2.6 pf.c --- pf.c 23 Aug 2007 09:38:14 -0000 1.34.2.6 +++ pf.c 20 Nov 2007 14:06:34 -0000 @@ -1211,7 +1211,7 @@ case AF_INET6: { u_int16_t b; u_int8_t i, curstart = 255, curend = 0, - maxstart = 0, maxend = 0; + maxstart = 255, maxend = 255; for (i = 0; i < 8; i++) { if (!addr->addr16[i]) { if (curstart == 255) =================== patch for pf.c ===================