"Fossies" - the Fresh Open Source Software Archive 
Member "portfwd-0.29/src/net_portion.cc" (5 May 2002, 1522 Bytes) of package /linux/privat/old/portfwd-0.29.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
1 /*
2 net_portion.cc
3
4 $Id: net_portion.cc,v 1.2 2002/05/05 03:30:56 evertonm Exp $
5 */
6
7 #include <syslog.h>
8 #include "util.h"
9 #include "addr.h"
10 #include "net_portion.h"
11
12 net_portion::net_portion(struct ip_addr addr, short m_len)
13 {
14 address = addr;
15 mask_len = m_len;
16
17 /*
18 * Fix last significant byte of mask here so it is ready on compare time.
19 */
20 char last = mask_len >> 3; /* last = mask_len / 8 */
21 if (last < address.len)
22 address.addr[last] &= (char) (0xFF << (8 - (mask_len & 7)));
23 }
24
25 void net_portion::show() const
26 {
27 show_addr(&address);
28 syslog(LOG_INFO, "/%d", mask_len);
29 }
30
31 /*
32 * Verifica se um endereco IP pertence `a rede.
33 */
34 int net_portion::owns(const struct ip_addr *ip) const
35 {
36 char *net_a, *net_pastend;
37 char *ip_a;
38 char len, left;
39
40 /*
41 * Compara comprimento de endereco.
42 */
43 if (address.len != ip->len)
44 return 0;
45
46 /*
47 * Compara todos os bytes, exceto o ultimo.
48 */
49 len = mask_len >> 3; /* mask_len / 8 */
50 net_a = address.addr;
51 net_pastend = net_a + len;
52 ip_a = ip->addr;
53 for (; net_a < net_pastend; ++net_a, ++ip_a)
54 if (*net_a != *ip_a)
55 return 0;
56
57 /*
58 * left = mask_len % 8 = numero de uns `a esquerda
59 */
60 left = mask_len & 7;
61 if (!left)
62 return -1;
63
64 /*
65 * Compara ultimo byte.
66 *
67 * 8 - () => numero de zeros `a direta
68 * 0xFF << () => insere zeros `a direta
69 * *ip_a & () => desconsidera bits que sejam zero
70 */
71 return *net_a == (*ip_a & (char) (0xFF << (8 - left)));
72 }
73
74 /* Eof: net_portion.cc */