#include "cidr_utils.h"
// 判断 IPv4 是否在 CIDR 网段中
int is_in_cidr(const char *ip_str, const char *cidr_str) {
struct in_addr ip_addr, network, mask;
if (inet_pton(AF_INET, ip_str, &ip_addr) <= 0) {
return 0; // 无效 IP
}
char cidr_copy[32];
strncpy(cidr_copy, cidr_str, sizeof(cidr_copy) - 1);
cidr_copy[sizeof(cidr_copy) - 1] = '\0';
char *slash = strchr(cidr_copy, '/');
if (!slash) return 0;
*slash = '\0';
int bits = atoi(slash + 1);
if (inet_pton(AF_INET, cidr_copy, &network) <= 0) {
return 0; // 无效网络
}
// 计算子网掩码
mask.s_addr = (bits == 0) ? 0 : htonl((0xFFFFFFFF << (32 - bits)) & 0xFFFFFFFF);
// 判断是否在子网中
return (ip_addr.s_addr & mask.s_addr) == (network.s_addr & mask.s_addr);
}
// 判断 IPv6 是否在 CIDR 网段中
int is_in_ipv6_cidr(const char *ip_str, const char *cidr_str) {
struct in6_addr ip_addr, network_addr;
unsigned int prefix_len;
// 解析 CIDR
char cidr_copy[128];
strncpy(cidr_copy, cidr_str, sizeof(cidr_copy) - 1);
cidr_copy[sizeof(cidr_copy) - 1] = '\0';
char *slash = strchr(cidr_copy, '/');
if (!slash) return 0;
*slash = '\0';
prefix_len = atoi(slash + 1);
// 解析网络地址
if (inet_pton(AF_INET6, cidr_copy, &network_addr) <= 0) {
return 0;
}
// 解析输入的 IP
if (inet_pton(AF_INET6, ip_str, &ip_addr) <= 0) {
return 0;
}
// 比较前缀
int full_bytes = prefix_len / 8;
int partial_bits = prefix_len % 8;
for (int i = 0; i < full_bytes; i++) {
if (ip_addr.s6_addr[i] != network_addr.s6_addr[i]) {
return 0;
}
}
if (partial_bits > 0) {
unsigned char mask = (0xFF << (8 - partial_bits)) & 0xFF;
if ((ip_addr.s6_addr[full_bytes] & mask) != (network_addr.s6_addr[full_bytes] & mask)) {
return 0;
}
}
return 1;
}
/* ANSI-C code produced by gperf version 3.1 */
/* Command-line: gperf -t -N is_ip_denied -K ip -L ANSI-C denylist.gperf */
/* Computed positions: -k'7,9' */
#include "denylist_hash.h"
#if !((' ' == 32) && ('!' == 33) && ('"' == 34) && ('#' == 35) \
&& ('%' == 37) && ('&' == 38) && ('\'' == 39) && ('(' == 40) \
&& (')' == 41) && ('*' == 42) && ('+' == 43) && (',' == 44) \
&& ('-' == 45) && ('.' == 46) && ('/' == 47) && ('0' == 48) \
&& ('1' == 49) && ('2' == 50) && ('3' == 51) && ('4' == 52) \
&& ('5' == 53) && ('6' == 54) && ('7' == 55) && ('8' == 56) \
&& ('9' == 57) && (':' == 58) && (';' == 59) && ('<' == 60) \
&& ('=' == 61) && ('>' == 62) && ('?' == 63) && ('A' == 65) \
&& ('B' == 66) && ('C' == 67) && ('D' == 68) && ('E' == 69) \
&& ('F' == 70) && ('G' == 71) && ('H' == 72) && ('I' == 73) \
&& ('J' == 74) && ('K' == 75) && ('L' == 76) && ('M' == 77) \
&& ('N' == 78) && ('O' == 79) && ('P' == 80) && ('Q' == 81) \
&& ('R' == 82) && ('S' == 83) && ('T' == 84) && ('U' == 85) \
&& ('V' == 86) && ('W' == 87) && ('X' == 88) && ('Y' == 89) \
&& ('Z' == 90) && ('[' == 91) && ('\\' == 92) && (']' == 93) \
&& ('^' == 94) && ('_' == 95) && ('a' == 97) && ('b' == 98) \
&& ('c' == 99) && ('d' == 100) && ('e' == 101) && ('f' == 102) \
&& ('g' == 103) && ('h' == 104) && ('i' == 105) && ('j' == 106) \
&& ('k' == 107) && ('l' == 108) && ('m' == 109) && ('n' == 110) \
&& ('o' == 111) && ('p' == 112) && ('q' == 113) && ('r' == 114) \
&& ('s' == 115) && ('t' == 116) && ('u' == 117) && ('v' == 118) \
&& ('w' == 119) && ('x' == 120) && ('y' == 121) && ('z' == 122) \
&& ('{' == 123) && ('|' == 124) && ('}' == 125) && ('~' == 126))
/* The character set is not based on ISO-646. */
#error "gperf generated tables don't work with this execution character set. Please report a bug to <bug-gperf@gnu.org>."
#endif
#line 3 "denylist.gperf"
struct denylist_entry {
const char* ip; // 存储要匹配的 IPv4/IPv6 地址
};
#define TOTAL_KEYWORDS 12
#define MIN_WORD_LENGTH 14
#define MAX_WORD_LENGTH 18
#define MIN_HASH_VALUE 18
#define MAX_HASH_VALUE 43
/* maximum key range = 26, duplicates = 0 */
#ifdef __GNUC__
__inline
#else
#ifdef __cplusplus
inline
#endif
#endif
static unsigned int
hash (register const char *str, register size_t len)
{
static unsigned char asso_values[] =
{
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 15, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 9, 44, 15, 5,
0, 0, 44, 0, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 10, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44, 44, 44, 44, 44,
44, 44, 44, 44, 44, 44
};
return len + asso_values[(unsigned char)str[8]] + asso_values[(unsigned char)str[6]];
}
struct denylist_entry *
is_ip_denied (register const char *str, register size_t len)
{
static struct denylist_entry wordlist[] =
{
{""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
{""}, {""}, {""}, {""}, {""}, {""}, {""}, {""}, {""},
#line 8 "denylist.gperf"
{"239.255.255.250 "},
#line 19 "denylist.gperf"
{"ff05::1:5 "},
{""}, {""},
#line 18 "denylist.gperf"
{"ff05::1:3 "},
#line 10 "denylist.gperf"
{"239.255.12.42 "},
{""}, {""}, {""},
#line 11 "denylist.gperf"
{"224.0.23.0 "},
#line 12 "denylist.gperf"
{"224.0.1.178 "},
{""}, {""},
#line 16 "denylist.gperf"
{"ff05::2 "},
{""},
#line 14 "denylist.gperf"
{"ff01::2 "},
{""}, {""}, {""},
#line 13 "denylist.gperf"
{"ff01::1 "},
#line 9 "denylist.gperf"
{"232.0.0.100 "},
{""}, {""}, {""},
#line 17 "denylist.gperf"
{"ff05::fb "},
#line 15 "denylist.gperf"
{"ff01::fb "}
};
if (len <= MAX_WORD_LENGTH && len >= MIN_WORD_LENGTH)
{
register unsigned int key = hash (str, len);
if (key <= MAX_HASH_VALUE)
{
register const char *s = wordlist[key].ip;
if (*str == *s && !strncmp (str + 1, s + 1, len - 1) && s[len] == '\0')
return &wordlist[key];
}
}
return 0;
}
#line 20 "denylist.gperf"
#include <stdio.h>
#include <string.h>
#include "denylist_hash.h"
#include "cidr_utils.h"
int is_ip_denied_extended(const char* ip) {
// 1. 固定 denylist 检查
if (is_ip_denied(ip, strlen(ip)) != NULL) {
return 1;
}
// 2. IPv4 CIDR 匹配
if (is_in_cidr(ip, "224.0.0.0/24")) {
return 1;
}
// 3. IPv6 CIDR 匹配
if (is_in_ipv6_cidr(ip, "ff02::/16")) {
return 1;
}
return 0;
}
int main() {
const char* test_ips[] = {
"224.0.0.1", // 固定 denylist
"224.0.0.123", // IPv4 CIDR
"224.0.1.1", // 允许
"ff02::1", // 固定 denylist
"ff02::a1", // IPv6 CIDR
"ff03::1", // 允许
"8.8.8.8", // 允许
"::1", // 允许
"ff02::2", // 固定 denylist
"ff02:0000:0000:0000::1" // IPv6 CIDR
};
for (int i = 0; i < 10; ++i) {
const char* ip = test_ips[i];
printf("IP: %s -> %s\n", ip,
is_ip_denied_extended(ip) ? "DENIED" : "ALLOWED");
}
return 0;
}
这是我的程序完整.c文件内容,帮我生成一下文件结构图
最新发布