1 : /*
2 : * v4global.c - Check whether an IPv4 address is global
3 : * $Id: v4global.c 1726 2006-08-27 08:13:18Z remi $
4 : */
5 :
6 : /***********************************************************************
7 : * Copyright © 2004-2005 Rémi Denis-Courmont. *
8 : * This program is free software; you can redistribute and/or modify *
9 : * it under the terms of the GNU General Public License as published *
10 : * by the Free Software Foundation; version 2 of the license. *
11 : * *
12 : * This program is distributed in the hope that it will be useful, *
13 : * but WITHOUT ANY WARRANTY; without even the implied warranty of *
14 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
15 : * See the GNU General Public License for more details. *
16 : * *
17 : * You should have received a copy of the GNU General Public License *
18 : * along with this program; if not, you can get it from: *
19 : * http://www.gnu.org/copyleft/gpl.html *
20 : ***********************************************************************/
21 :
22 : #ifdef HAVE_CONFIG_H
23 : # include <config.h>
24 : #endif
25 :
26 : #include <inttypes.h>
27 : #include <sys/types.h>
28 : #include <netinet/in.h> // ntohl()
29 :
30 : #include "v4global.h"
31 :
32 : /*
33 : * Checks that ip is a global unicast IPv4 address
34 : * (Values shoud maybe not be hardcoded that way).
35 : */
36 : int
37 : is_ipv4_global_unicast (uint32_t ip)
38 0 : {
39 : /*
40 : * NOTE (FIXME)
41 : *
42 : * Additionnaly, the specification forbids broadcast addresses of
43 : * subnets the node is attached to. It would be quite complex to check
44 : * that. We don't set the SO_BROADCAST socket option, which should be
45 : * sufficient to avoid security issues.
46 : */
47 :
48 0 : if ((ip & htonl (0x80000000)) == 0)
49 : {
50 : // Forbidden classes A:
51 : // 0.0.0.0/8, 10.0.0.0/8 and 127.0.0.0/8
52 0 : ip &= htonl (0xff000000);
53 0 : return ( ip != htonl (0x0a000000)) &&
54 : ( ip != htonl (0x7f000000)) &&
55 : ( ip != htonl (0x00000000));
56 : }
57 :
58 0 : if ((ip & htonl (0x40000000)) == 0)
59 : {
60 : // Forbidden classes B:
61 : // 169.254.0.0/16, 172.16.0.0/12
62 0 : return ((ip & htonl (0xffff0000)) != htonl (0xa9fe0000)) &&
63 : ((ip & htonl (0xfff00000)) != htonl (0xac100000));
64 : }
65 :
66 0 : if ((ip & htonl (0x20000000)) == 0)
67 : {
68 : // Forbidden classes C:
69 : // 192.168.0.0/16, 192.88.99.0/24
70 0 : return ((ip & htonl (0xffff0000)) != htonl (0xc0a80000)) &&
71 : ((ip & htonl (0xffffff00)) != htonl (0xc0586200));
72 : }
73 :
74 0 : if ((ip & htonl (0x10000000)) == 0)
75 : // Whole class D space (multicast) is forbidden:
76 0 : return 0;
77 :
78 0 : return ip != htonl (0xffffffff);
79 : }
80 :
|