Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/thirdparty/miniupnpc/src/addr_is_reserved.c
9904 views
1
/* $Id: addr_is_reserved.c,v 1.7 2025/01/12 15:47:17 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-2025 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
#define WIN32_LEAN_AND_MEAN
12
#include <winsock2.h>
13
#include <ws2tcpip.h>
14
#if !defined(_MSC_VER)
15
#include <stdint.h>
16
#else /* !defined(_MSC_VER) */
17
typedef unsigned long uint32_t;
18
#endif /* !defined(_MSC_VER) */
19
#if !defined(_WIN32_WINNT_VISTA)
20
#define _WIN32_WINNT_VISTA 0x0600
21
#endif
22
#else /* _WIN32 */
23
#include <sys/types.h>
24
#include <sys/socket.h>
25
#include <netinet/in.h>
26
#include <arpa/inet.h>
27
#endif /* _WIN32 */
28
#ifdef DEBUG
29
#include <stdio.h>
30
#endif
31
32
/* List of IP address blocks which are private / reserved and therefore not suitable for public external IP addresses */
33
#define IP(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))
34
#define MSK(m) (32-(m))
35
static const struct { uint32_t address; uint32_t rmask; } reserved[] = {
36
{ IP( 0, 0, 0, 0), MSK( 8) }, /* RFC1122 "This host on this network" */
37
{ IP( 10, 0, 0, 0), MSK( 8) }, /* RFC1918 Private-Use */
38
{ IP(100, 64, 0, 0), MSK(10) }, /* RFC6598 Shared Address Space */
39
{ IP(127, 0, 0, 0), MSK( 8) }, /* RFC1122 Loopback */
40
{ IP(169, 254, 0, 0), MSK(16) }, /* RFC3927 Link-Local */
41
{ IP(172, 16, 0, 0), MSK(12) }, /* RFC1918 Private-Use */
42
{ IP(192, 0, 0, 0), MSK(24) }, /* RFC6890 IETF Protocol Assignments */
43
{ IP(192, 0, 2, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-1) */
44
{ IP(192, 31, 196, 0), MSK(24) }, /* RFC7535 AS112-v4 */
45
{ IP(192, 52, 193, 0), MSK(24) }, /* RFC7450 AMT */
46
{ IP(192, 88, 99, 0), MSK(24) }, /* RFC7526 6to4 Relay Anycast */
47
{ IP(192, 168, 0, 0), MSK(16) }, /* RFC1918 Private-Use */
48
{ IP(192, 175, 48, 0), MSK(24) }, /* RFC7534 Direct Delegation AS112 Service */
49
{ IP(198, 18, 0, 0), MSK(15) }, /* RFC2544 Benchmarking */
50
{ IP(198, 51, 100, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-2) */
51
{ IP(203, 0, 113, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-3) */
52
{ IP(224, 0, 0, 0), MSK( 4) }, /* RFC1112 Multicast */
53
{ IP(240, 0, 0, 0), MSK( 4) }, /* RFC1112 Reserved for Future Use + RFC919 Limited Broadcast */
54
};
55
#undef IP
56
#undef MSK
57
58
/**
59
* @return 1 or 0
60
*/
61
int addr_is_reserved(const char * addr_str)
62
{
63
uint32_t addr_n, address;
64
size_t i;
65
66
#if defined(_WIN32) && (_WIN32_WINNT < _WIN32_WINNT_VISTA)
67
addr_n = inet_addr(addr_str);
68
if (addr_n == INADDR_NONE)
69
return 1;
70
#else
71
/* was : addr_n = inet_addr(addr_str); */
72
if (inet_pton(AF_INET, addr_str, &addr_n) <= 0) {
73
/* error */
74
return 1;
75
}
76
#endif
77
78
address = ntohl(addr_n);
79
80
for (i = 0; i < sizeof(reserved)/sizeof(reserved[0]); ++i) {
81
if ((address >> reserved[i].rmask) == (reserved[i].address >> reserved[i].rmask)) {
82
#ifdef DEBUG
83
printf("IP address %s is reserved\n", addr_str);
84
#endif
85
return 1;
86
}
87
}
88
89
return 0;
90
}
91
92