From 39e569a6492a7f086d3549589b0f0745952decfa Mon Sep 17 00:00:00 2001 From: yuqing Date: Thu, 19 Jan 2017 11:11:39 +0800 Subject: [PATCH] ini_file_reader: LOCAL_IP support CIDR addresses --- HISTORY | 3 ++ src/ini_file_reader.c | 93 ++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 95 insertions(+), 1 deletion(-) diff --git a/HISTORY b/HISTORY index cf8da03..1f87b35 100644 --- a/HISTORY +++ b/HISTORY @@ -1,4 +1,7 @@ +Version 1.34 2017-01-19 + * ini_file_reader: LOCAL_IP support CIDR addresses + Version 1.33 2017-01-04 * add function hash_get_prime_capacity * refine getFileContent log info diff --git a/src/ini_file_reader.c b/src/ini_file_reader.c index 68d336c..b922301 100644 --- a/src/ini_file_reader.c +++ b/src/ini_file_reader.c @@ -846,12 +846,103 @@ static bool iniMatchValue(const char *target, char **values, const int count) return false; } + +static bool iniMatchCIDR(const char *target, const char *ip_addr, + const char *pSlash) +{ + char *pReservedEnd; + char ip_part[IP_ADDRESS_SIZE]; + int ip_len; + int network_bits; + struct in_addr addr; + uint32_t network_hip; + uint32_t target_hip; + uint32_t network_mask; + + ip_len = pSlash - ip_addr; + if (ip_len == 0 || ip_len >= IP_ADDRESS_SIZE) + { + logWarning("file: "__FILE__", line: %d, " + "invalid ip address: %s", __LINE__, ip_addr); + return false; + } + memcpy(ip_part, ip_addr, ip_len); + *(ip_part + ip_len) = '\0'; + + pReservedEnd = NULL; + network_bits = strtol(pSlash + 1, &pReservedEnd, 10); + if (!(pReservedEnd == NULL || *pReservedEnd == '\0')) + { + logError("file: "__FILE__", line: %d, " \ + "ip address: %s, invalid network bits: %s", + __LINE__, ip_addr, pSlash + 1); + return false; + } + + if (network_bits < 8 || network_bits > 30) + { + logError("file: "__FILE__", line: %d, " \ + "ip address: %s, invalid network bits: %d, " \ + "it should >= 8 and <= 30", \ + __LINE__, ip_addr, network_bits); + return false; + } + + if (inet_pton(AF_INET, ip_part, &addr) != 1) + { + logError("file: "__FILE__", line: %d, " \ + "ip address: %s, invalid ip part: %s", \ + __LINE__, ip_addr, ip_part); + return false; + } + network_hip = ntohl(addr.s_addr); + + if (inet_pton(AF_INET, target, &addr) != 1) + { + logError("file: "__FILE__", line: %d, " + "invalid ip: %s", __LINE__, ip_addr, target); + return false; + } + target_hip = ntohl(addr.s_addr); + + network_mask = ((1 << network_bits) - 1) << (32 - network_bits); + return (target_hip & network_mask) == (network_hip & network_mask); +} + +static bool iniMatchIP(const char *target, char **values, const int count) +{ + int i; + char *pSlash; + + for (i=0; i