1 /* $NetBSD: mask_addr.c,v 1.1.1.1 2009/06/23 10:09:00 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* mask_addr 3 6 /* SUMMARY 7 /* address bit banging 8 /* SYNOPSIS 9 /* #include <mask_addr.h> 10 /* 11 /* void mask_addr(addr_bytes, addr_byte_count, network_bits) 12 /* unsigned char *addr_bytes; 13 /* unsigned addr_byte_count; 14 /* unsigned network_bits; 15 /* DESCRIPTION 16 /* mask_addr() clears all the host bits in the specified 17 /* address. The code can handle addresses of any length, 18 /* and bytes of any width. 19 /* 20 /* Arguments: 21 /* .IP addr_bytes 22 /* The network address in network byte order. 23 /* .IP addr_byte_count 24 /* The network address length in bytes. 25 /* .IP network_bits 26 /* The number of initial bits that will not be cleared. 27 /* DIAGNOSTICS 28 /* Fatal errors: the number of network bits exceeds the address size. 29 /* LICENSE 30 /* .ad 31 /* .fi 32 /* The Secure Mailer license must be distributed with this software. 33 /* AUTHOR(S) 34 /* Wietse Venema 35 /* IBM T.J. Watson Research 36 /* P.O. Box 704 37 /* Yorktown Heights, NY 10598, USA 38 /*--*/ 39 40 /* System library. */ 41 42 #include <sys_defs.h> 43 #include <limits.h> /* CHAR_BIT */ 44 45 /* Utility library. */ 46 47 #include <msg.h> 48 #include <mask_addr.h> 49 50 /* mask_addr - mask off a variable-length address */ 51 52 void mask_addr(unsigned char *addr_bytes, 53 unsigned addr_byte_count, 54 unsigned network_bits) 55 { 56 unsigned char *p; 57 58 if (network_bits > addr_byte_count * CHAR_BIT) 59 msg_panic("mask_addr: address byte count %d too small for bit count %d", 60 addr_byte_count, network_bits); 61 62 p = addr_bytes + network_bits / CHAR_BIT; 63 network_bits %= CHAR_BIT; 64 65 if (network_bits != 0) 66 *p++ &= ~0 << (CHAR_BIT - network_bits); 67 68 while (p < addr_bytes + addr_byte_count) 69 *p++ = 0; 70 } 71