Path: blob/master/thirdparty/miniupnpc/src/addr_is_reserved.c
9904 views
/* $Id: addr_is_reserved.c,v 1.7 2025/01/12 15:47:17 nanard Exp $ */1/* vim: tabstop=4 shiftwidth=4 noexpandtab2* Project : miniupnp3* Web : http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/4* Author : Thomas BERNARD5* copyright (c) 2005-2025 Thomas Bernard6* This software is subjet to the conditions detailed in the7* provided LICENSE file. */8#ifdef _WIN329/* Win32 Specific includes and defines */10#define WIN32_LEAN_AND_MEAN11#include <winsock2.h>12#include <ws2tcpip.h>13#if !defined(_MSC_VER)14#include <stdint.h>15#else /* !defined(_MSC_VER) */16typedef unsigned long uint32_t;17#endif /* !defined(_MSC_VER) */18#if !defined(_WIN32_WINNT_VISTA)19#define _WIN32_WINNT_VISTA 0x060020#endif21#else /* _WIN32 */22#include <sys/types.h>23#include <sys/socket.h>24#include <netinet/in.h>25#include <arpa/inet.h>26#endif /* _WIN32 */27#ifdef DEBUG28#include <stdio.h>29#endif3031/* List of IP address blocks which are private / reserved and therefore not suitable for public external IP addresses */32#define IP(a, b, c, d) (((a) << 24) + ((b) << 16) + ((c) << 8) + (d))33#define MSK(m) (32-(m))34static const struct { uint32_t address; uint32_t rmask; } reserved[] = {35{ IP( 0, 0, 0, 0), MSK( 8) }, /* RFC1122 "This host on this network" */36{ IP( 10, 0, 0, 0), MSK( 8) }, /* RFC1918 Private-Use */37{ IP(100, 64, 0, 0), MSK(10) }, /* RFC6598 Shared Address Space */38{ IP(127, 0, 0, 0), MSK( 8) }, /* RFC1122 Loopback */39{ IP(169, 254, 0, 0), MSK(16) }, /* RFC3927 Link-Local */40{ IP(172, 16, 0, 0), MSK(12) }, /* RFC1918 Private-Use */41{ IP(192, 0, 0, 0), MSK(24) }, /* RFC6890 IETF Protocol Assignments */42{ IP(192, 0, 2, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-1) */43{ IP(192, 31, 196, 0), MSK(24) }, /* RFC7535 AS112-v4 */44{ IP(192, 52, 193, 0), MSK(24) }, /* RFC7450 AMT */45{ IP(192, 88, 99, 0), MSK(24) }, /* RFC7526 6to4 Relay Anycast */46{ IP(192, 168, 0, 0), MSK(16) }, /* RFC1918 Private-Use */47{ IP(192, 175, 48, 0), MSK(24) }, /* RFC7534 Direct Delegation AS112 Service */48{ IP(198, 18, 0, 0), MSK(15) }, /* RFC2544 Benchmarking */49{ IP(198, 51, 100, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-2) */50{ IP(203, 0, 113, 0), MSK(24) }, /* RFC5737 Documentation (TEST-NET-3) */51{ IP(224, 0, 0, 0), MSK( 4) }, /* RFC1112 Multicast */52{ IP(240, 0, 0, 0), MSK( 4) }, /* RFC1112 Reserved for Future Use + RFC919 Limited Broadcast */53};54#undef IP55#undef MSK5657/**58* @return 1 or 059*/60int addr_is_reserved(const char * addr_str)61{62uint32_t addr_n, address;63size_t i;6465#if defined(_WIN32) && (_WIN32_WINNT < _WIN32_WINNT_VISTA)66addr_n = inet_addr(addr_str);67if (addr_n == INADDR_NONE)68return 1;69#else70/* was : addr_n = inet_addr(addr_str); */71if (inet_pton(AF_INET, addr_str, &addr_n) <= 0) {72/* error */73return 1;74}75#endif7677address = ntohl(addr_n);7879for (i = 0; i < sizeof(reserved)/sizeof(reserved[0]); ++i) {80if ((address >> reserved[i].rmask) == (reserved[i].address >> reserved[i].rmask)) {81#ifdef DEBUG82printf("IP address %s is reserved\n", addr_str);83#endif84return 1;85}86}8788return 0;89}909192