forked from huawei/openGauss-server
Update iprange.cpp
This commit is contained in:
parent
e807068a8f
commit
5d826c856b
|
|
@ -22,6 +22,11 @@
|
||||||
*
|
*
|
||||||
* ---------------------------------------------------------------------------------------
|
* ---------------------------------------------------------------------------------------
|
||||||
*/
|
*/
|
||||||
|
/*IP address, called Internet protocol address, is a way to address a host on the Internet.
|
||||||
|
It is a unified address format provided by IP protocol. Common IP addresses can be divided
|
||||||
|
into IPv4 and IPv6. It assigns a logical address to every network and every host on the Internet
|
||||||
|
to shield the difference of physical addresses.*/
|
||||||
|
|
||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
@ -134,6 +139,11 @@ void IPRange::net_ipv6_to_host_order(IPV6 *ip, const struct sockaddr_in6 *sa) co
|
||||||
ip->ip_32.d = ntohl(tmp_ip.ip_32.a);
|
ip->ip_32.d = ntohl(tmp_ip.ip_32.a);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Function name: net_ Ipv4_ To_ Host_ Order
|
||||||
|
Formal parameters: (IPV6 * ip, const construct in_addr * addr)
|
||||||
|
Return value: None
|
||||||
|
Convert IPv4 addresses to host addresses*/
|
||||||
|
|
||||||
void IPRange::net_ipv4_to_host_order(IPV6 *ip, const struct in_addr *addr) const
|
void IPRange::net_ipv4_to_host_order(IPV6 *ip, const struct in_addr *addr) const
|
||||||
{
|
{
|
||||||
ip->ip_32.a = ntohl(addr->s_addr);
|
ip->ip_32.a = ntohl(addr->s_addr);
|
||||||
|
|
@ -141,6 +151,10 @@ void IPRange::net_ipv4_to_host_order(IPV6 *ip, const struct in_addr *addr) const
|
||||||
ip->ip_32.c = ip->ip_32.d = 0;
|
ip->ip_32.c = ip->ip_32.d = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Function name: str_ To_ IP
|
||||||
|
Formal parameters: (const char * ip_str, IPV6 * ip)
|
||||||
|
Return value: bool
|
||||||
|
Convert string to IP address*/
|
||||||
bool IPRange::str_to_ip(const char* ip_str, IPV6 *ip)
|
bool IPRange::str_to_ip(const char* ip_str, IPV6 *ip)
|
||||||
{
|
{
|
||||||
struct in_addr addr;
|
struct in_addr addr;
|
||||||
|
|
@ -160,7 +174,10 @@ bool IPRange::str_to_ip(const char* ip_str, IPV6 *ip)
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
/*Function name: mask_ Range
|
||||||
|
Formal parameter: (Range * range, unsigned short cidr)
|
||||||
|
Return value: bool
|
||||||
|
Calculate Mask*/
|
||||||
bool IPRange::mask_range(Range *range, unsigned short cidr)
|
bool IPRange::mask_range(Range *range, unsigned short cidr)
|
||||||
{
|
{
|
||||||
if (IPRANGE_IS_IPV4(range->from)) { /* ipv4 */
|
if (IPRANGE_IS_IPV4(range->from)) { /* ipv4 */
|
||||||
|
|
@ -211,6 +228,10 @@ bool IPRange::mask_range(Range *range, unsigned short cidr)
|
||||||
* parse the ip with mask into range sturst , format is as below:
|
* parse the ip with mask into range sturst , format is as below:
|
||||||
* x.x.x.x|x, ptr is the postion of "|"
|
* x.x.x.x|x, ptr is the postion of "|"
|
||||||
*/
|
*/
|
||||||
|
/*Function name: parse_ Mask
|
||||||
|
Formal parameters: (const char * range, size_t range_len, const char * ptr, Range * new_range)
|
||||||
|
Return value: bool
|
||||||
|
Resolve mask.*/
|
||||||
bool IPRange::parse_mask(const char* range, size_t range_len, const char *ptr, Range *new_range)
|
bool IPRange::parse_mask(const char* range, size_t range_len, const char *ptr, Range *new_range)
|
||||||
{
|
{
|
||||||
if (range_len > 100) {
|
if (range_len > 100) {
|
||||||
|
|
@ -250,6 +271,10 @@ bool IPRange::parse_mask(const char* range, size_t range_len, const char *ptr, R
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Function name: parse_ Single
|
||||||
|
Formal parameters: (const char * range, size_t range_len, Range * new_range)
|
||||||
|
Return value: bool
|
||||||
|
Resolve a single IP.*/
|
||||||
bool IPRange::parse_single(const char* range, size_t range_len, Range *new_range)
|
bool IPRange::parse_single(const char* range, size_t range_len, Range *new_range)
|
||||||
{
|
{
|
||||||
if (range_len > 100) {
|
if (range_len > 100) {
|
||||||
|
|
@ -479,14 +504,20 @@ bool IPRange::add_range(Range *new_range)
|
||||||
m_ranges.swap(new_ranges);
|
m_ranges.swap(new_ranges);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
/*Function name: is_ Range_ Valid
|
||||||
|
Formal parameter: (const std:: string range)
|
||||||
|
Return value: bool
|
||||||
|
Determine if the IP type is empty*/
|
||||||
bool IPRange::is_range_valid(const std::string range)
|
bool IPRange::is_range_valid(const std::string range)
|
||||||
{
|
{
|
||||||
IPRange tmp;
|
IPRange tmp;
|
||||||
Range new_range;
|
Range new_range;
|
||||||
return tmp.parse_range(range.c_str(), range.size(), &new_range);
|
return tmp.parse_range(range.c_str(), range.size(), &new_range);
|
||||||
}
|
}
|
||||||
|
/*Function name: add_ Range
|
||||||
|
Formal parameter: (Range * new_range)
|
||||||
|
Return value: void
|
||||||
|
Increase range IP*/
|
||||||
bool IPRange::add_range(const char* range, size_t range_len)
|
bool IPRange::add_range(const char* range, size_t range_len)
|
||||||
{
|
{
|
||||||
Range new_range;
|
Range new_range;
|
||||||
|
|
@ -496,7 +527,10 @@ bool IPRange::add_range(const char* range, size_t range_len)
|
||||||
}
|
}
|
||||||
return add_range(&new_range);
|
return add_range(&new_range);
|
||||||
}
|
}
|
||||||
|
/*Function name: remove_ Range
|
||||||
|
Formal parameters: (const char * range, size_t range_len)
|
||||||
|
Return value: bool
|
||||||
|
Delete Scope IP*/
|
||||||
bool IPRange::remove_range(const char *range, size_t range_len)
|
bool IPRange::remove_range(const char *range, size_t range_len)
|
||||||
{
|
{
|
||||||
Ranges_t new_ranges;
|
Ranges_t new_ranges;
|
||||||
|
|
@ -530,6 +564,10 @@ std::string IPRange::ip_to_str(const IPV6 *ip) const
|
||||||
return std::string(ip_str);
|
return std::string(ip_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*Function name: binary_ Search
|
||||||
|
Formal parameter: (const IPV6 ip)
|
||||||
|
Return value: bool
|
||||||
|
Binary search IP*/
|
||||||
bool IPRange::binary_search(const IPV6 ip) const
|
bool IPRange::binary_search(const IPV6 ip) const
|
||||||
{
|
{
|
||||||
/* do a binary search */
|
/* do a binary search */
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue