Subject: Unnecessary masking in inet_netof.c (#178) Index: lib/libc/inet/inet_netof.c 2.11BSD Description: There is an unnecessary mask operation being done in the inet_netof.c routine. The masks in /sys/netinet/in.h are no longer needed either. Repeat-By: By observation. Fix: All updates are available via anonymous FTP to ftp.iipo.gtegsc.com in the directory /pub/2.11BSD. The C compiler now supports the 'unsigned long' data type. There is no need now to mask the network numbers after shifting since the net numbers are unsigned and no sign extension occurs. Also, the include file /sys/netinet/ip_acct.h is obsolete (it was never used for anything) - it should be removed. Do the following: 1) save the patch below into a file ('/tmp/patchfile') 2) patch -p0 < /tmp/patchfile 3) rm -f /sys/netinet/ip_acct.h 4) cd /usr/src/lib/libc/inet 5) make 6) ar rv /lib/libc.a *.o 7) ranlib /lib/libc.a 8) cd profiled 9) ar rv /usr/lib/libc_p.a *.o 10) ranlib /usr/lib/libc_p.a 11) cd .. 12) make clean ==========cut here *** /usr/src/sys/netinet/in.h.old Mon Jan 10 21:33:14 1994 --- /usr/src/sys/netinet/in.h Sun Feb 20 18:15:04 1994 *************** *** 9,15 **** * software without specific prior written permission. This software * is provided ``as is'' without express or implied warranty. * ! * @(#)in.h 7.5.1 (2.11BSD GTE) 12/31/93 */ /* --- 9,15 ---- * software without specific prior written permission. This software * is provided ``as is'' without express or implied warranty. * ! * @(#)in.h 7.5.2 (2.11BSD GTE) 2/20/94 */ /* *************** *** 91,102 **** #endif #define IN_LOOPBACKNET 127 /* official! */ - - #ifdef pdp11 /* used in lib/libc/inet/inet_netof.c */ - #define IN_CLASSA_NET_SHIFTMASK 0x000000ffL - #define IN_CLASSB_NET_SHIFTMASK 0x0000ffffL - #define IN_CLASSC_NET_SHIFTMASK 0x00ffffffL - #endif /* * Socket address, internet style. --- 91,96 ---- *** /usr/src/lib/libc/inet/inet_netof.c.old Mon Dec 26 14:33:42 1988 --- /usr/src/lib/libc/inet/inet_netof.c Sat Feb 12 00:03:43 1994 *************** *** 1,15 **** /* * Copyright (c) 1983 Regents of the University of California. ! * All rights reserved. The Berkeley software License Agreement ! * specifies the terms and conditions for redistribution. */ #if defined(LIBC_SCCS) && !defined(lint) ! static char sccsid[] = "@(#)inet_netof.c 5.2 (Berkeley) 3/9/86"; ! #endif LIBC_SCCS and not lint ! #include #include /* * Return the network number from an internet --- 1,43 ---- /* * Copyright (c) 1983 Regents of the University of California. ! * All rights reserved. ! * ! * Redistribution and use in source and binary forms, with or without ! * modification, are permitted provided that the following conditions ! * are met: ! * 1. Redistributions of source code must retain the above copyright ! * notice, this list of conditions and the following disclaimer. ! * 2. Redistributions in binary form must reproduce the above copyright ! * notice, this list of conditions and the following disclaimer in the ! * documentation and/or other materials provided with the distribution. ! * 3. All advertising materials mentioning features or use of this software ! * must display the following acknowledgement: ! * This product includes software developed by the University of ! * California, Berkeley and its contributors. ! * 4. Neither the name of the University nor the names of its contributors ! * may be used to endorse or promote products derived from this software ! * without specific prior written permission. ! * ! * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ! * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE ! * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ! * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE ! * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL ! * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS ! * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ! * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT ! * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY ! * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF ! * SUCH DAMAGE. */ #if defined(LIBC_SCCS) && !defined(lint) ! static char sccsid[] = "@(#)inet_netof.c 5.7 (Berkeley) 2/24/91"; ! #endif /* LIBC_SCCS and not lint */ ! #include #include + #include /* * Return the network number from an internet *************** *** 19,43 **** inet_netof(in) struct in_addr in; { ! register u_long i = ntohl(in.s_addr); - #ifdef pdp11 - /* - * The PDP-11 doesn't have an unsigned long so we have to shift then - * mask. - */ if (IN_CLASSA(i)) - return (((i)>>IN_CLASSA_NSHIFT) & IN_CLASSA_NET_SHIFTMASK); - else if (IN_CLASSB(i)) - return (((i)>>IN_CLASSB_NSHIFT) & IN_CLASSB_NET_SHIFTMASK); - else - return (((i)>>IN_CLASSC_NSHIFT) & IN_CLASSC_NET_SHIFTMASK); - #else !pdp11 - if (IN_CLASSA(i)) return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); else if (IN_CLASSB(i)) return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); else return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); - #endif pdp11 } --- 47,58 ---- inet_netof(in) struct in_addr in; { ! u_long i = ntohl(in.s_addr); if (IN_CLASSA(i)) return (((i)&IN_CLASSA_NET) >> IN_CLASSA_NSHIFT); else if (IN_CLASSB(i)) return (((i)&IN_CLASSB_NET) >> IN_CLASSB_NSHIFT); else return (((i)&IN_CLASSC_NET) >> IN_CLASSC_NSHIFT); }