| 1 | /* $Id: addr_is_reserved.c,v 1.4 2021/03/02 23:40:32 nanard Exp $ */ |
| 2 | /* vim: tabstop=4 shiftwidth=4 noexpandtab |
| 3 | * Project : miniupnp |
| 4 | * Web : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/ |
| 5 | * Author : Thomas BERNARD |
| 6 | * copyright (c) 2005-2021 Thomas Bernard |
| 7 | * This software is subjet to the conditions detailed in the |
| 8 | * provided LICENSE file. */ |
| 9 | #ifdef _WIN32 |
| 10 | /* Win32 Specific includes and defines */ |
| 11 | #include <winsock2.h> |
| 12 | #include <ws2tcpip.h> |
| 13 | #if !defined(_MSC_VER) |
| 14 | #include <stdint.h> |
| 15 | #else /* !defined(_MSC_VER) */ |
| 16 | typedef unsigned long uint32_t; |
| 17 | #endif /* !defined(_MSC_VER) */ |
| 18 | #else /* _WIN32 */ |
| 19 | #include <sys/types.h> |
| 20 | #include <sys/socket.h> |
| 21 | #include <netinet/in.h> |
| 22 | #include <arpa/inet.h> |
| 23 | #endif /* _WIN32 */ |
| 24 | |
| 25 | /* List of IP address blocks which are private / reserved and therefore not suitable for public external IP addresses */ |
| 26 | #define IP(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d)) |
| 27 | #define MSK(m) (32-(m)) |
| 28 | static const struct { uint32_t address; uint32_t rmask; } reserved[] = { |
| 29 | { IP( 0, 0, 0, 0), MSK( 8) }, /* RFC1122 "This host on this network" */ |
| 30 | { IP( 10, 0, 0, 0), MSK( 8) }, /* RFC1918 Private-Use */ |
| 31 | { IP(100, 64, 0, 0), MSK(10) }, /* RFC6598 Shared Address Space */ |
| 32 | { IP(127, 0, 0, 0), MSK( 8) }, /* RFC1122 Loopback */ |
| 33 | { IP(169, 254, 0, 0), MSK(16) }, /* RFC3927 Link-Local */ |
| 34 | { IP(172, 16, 0, 0), MSK(12) }, /* RFC1918 Private-Use */ |
| 35 | { IP(192, 0, 0, 0), MSK(24) }, /* RFC6890 IETF Protocol Assignments */ |
| 36 | { IP(192, 0, 2, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-1) */ |
| 37 | { IP(192, 31, 196, 0), MSK(24) }, /* RFC7535 AS112-v4 */ |
| 38 | { IP(192, 52, 193, 0), MSK(24) }, /* RFC7450 AMT */ |
| 39 | { IP(192, 88, 99, 0), MSK(24) }, /* RFC7526 6to4 Relay Anycast */ |
| 40 | { IP(192, 168, 0, 0), MSK(16) }, /* RFC1918 Private-Use */ |
| 41 | { IP(192, 175, 48, 0), MSK(24) }, /* RFC7534 Direct Delegation AS112 Service */ |
| 42 | { IP(198, 18, 0, 0), MSK(15) }, /* RFC2544 Benchmarking */ |
| 43 | { IP(198, 51, 100, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-2) */ |
| 44 | { IP(203, 0, 113, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-3) */ |
| 45 | { IP(224, 0, 0, 0), MSK( 4) }, /* RFC1112 Multicast */ |
| 46 | { IP(240, 0, 0, 0), MSK( 4) }, /* RFC1112 Reserved for Future Use + RFC919 Limited Broadcast */ |
| 47 | }; |
| 48 | #undef IP |
| 49 | #undef MSK |
| 50 | |
| 51 | /** |
| 52 | * @return 1 or 0 |
| 53 | */ |
| 54 | int addr_is_reserved(const char * addr_str) |
| 55 | { |
| 56 | uint32_t addr_n, address; |
| 57 | size_t i; |
| 58 | |
| 59 | #if defined(_WIN32) && _WIN32_WINNT < 0x0600 // _WIN32_WINNT_VISTA |
| 60 | addr_n = inet_addr(addr_str); |
| 61 | if (addr_n == INADDR_NONE) |
| 62 | return 1; |
| 63 | #else |
| 64 | /* was : addr_n = inet_addr(addr_str); */ |
| 65 | if (inet_pton(AF_INET, addr_str, &addr_n) <= 0) { |
| 66 | /* error */ |
| 67 | return 1; |
| 68 | } |
| 69 | #endif |
| 70 | |
| 71 | address = ntohl(addr_n); |
| 72 | |
| 73 | for (i = 0; i < sizeof(reserved)/sizeof(reserved[0]); ++i) { |
| 74 | if ((address >> reserved[i].rmask) == (reserved[i].address >> reserved[i].rmask)) |
| 75 | return 1; |
| 76 | } |
| 77 | |
| 78 | return 0; |
| 79 | } |
| 80 | |