Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / mask_addr.c
blob872d63360d9ddb0586ab1b873352fc307b2306c2
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* mask_addr 3
6 /* SUMMARY
7 /* address bit banging
8 /* SYNOPSIS
9 /* #include <mask_addr.h>
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.
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 /*--*/
40 /* System library. */
42 #include <sys_defs.h>
43 #include <limits.h> /* CHAR_BIT */
45 /* Utility library. */
47 #include <msg.h>
48 #include <mask_addr.h>
50 /* mask_addr - mask off a variable-length address */
52 void mask_addr(unsigned char *addr_bytes,
53 unsigned addr_byte_count,
54 unsigned network_bits)
56 unsigned char *p;
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);
62 p = addr_bytes + network_bits / CHAR_BIT;
63 network_bits %= CHAR_BIT;
65 if (network_bits != 0)
66 *p++ &= ~0 << (CHAR_BIT - network_bits);
68 while (p < addr_bytes + addr_byte_count)
69 *p++ = 0;