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