you can now compare ::b00b:d00d/112 and ::b00b:b00b

This commit is contained in:
malloc 2019-01-31 14:00:23 -06:00
parent b3143ffcf8
commit b6546d5318
3 changed files with 47 additions and 2 deletions

View file

@ -2,6 +2,8 @@
#include "util/ipaddr.h" #include "util/ipaddr.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
ipaddr_t test = glv_ip_aton("1234::/64"); ipaddr_t a = glv_ip_aton("1234:5678::b00b:d00d:def0/62");
ipaddr_t b = glv_ip_aton("1234:5678::b00b:b00b:def0");
int out = glv_ip_compare(&a, &b);
return 0; return 0;
} }

View file

@ -1,5 +1,8 @@
#include "ipaddr.h" #include "ipaddr.h"
#define MAX(X,Y) (((X)>(Y))?(X):(Y))
#define MIN(X,Y) (((X)<(Y))?(X):(Y))
const ipaddr_t error_addr = {{0, 0, 0, 0, 0, 0, 0, 0}, -1}; const ipaddr_t error_addr = {{0, 0, 0, 0, 0, 0, 0, 0}, -1};
ipaddr_t glv_ip_aton(const char* addr) { ipaddr_t glv_ip_aton(const char* addr) {
@ -137,6 +140,45 @@ ipaddr_t glv_ip_raw(const uint16_t* addr, uint8_t cidr) {
return addr_out; return addr_out;
} }
int glv_ip_check(const char* addr) {
ipaddr_t test = glv_ip_aton(addr);
return glv_ip_valid(&test);
}
int glv_ip_compare(const ipaddr_t* lhs, const ipaddr_t* rhs) {
int i = 0, lhs_cidr = lhs->cidr, rhs_cidr = rhs->cidr, mask;
for(i = 0; i < 8; ++i) {
mask = 0xFFFF
& ~((1 << (16 - MIN(16, lhs_cidr))) - 1)
& ~((1 << (16 - MIN(16, rhs_cidr))) - 1);
if(mask == 0)
break;
if((lhs->addr[i] & mask) != (rhs->addr[i] & mask))
return 0;
lhs_cidr = MAX(0, lhs_cidr - 16);
rhs_cidr = MAX(0, rhs_cidr - 16);
}
return 1;
}
int glv_ip_identical(const ipaddr_t* lhs, const ipaddr_t* rhs) {
int i;
for(i = 0; i < 8; ++i)
if(lhs->addr[i] != rhs->addr[i])
return 0;
return 1;
}
int glv_ip_isv4(const ipaddr_t* addr) {
return addr->addr[0] == 0 && addr->addr[1] == 0 &&
addr->addr[2] == 0 && addr->addr[3] == 0 &&
addr->addr[4] == 0 && addr->addr[5] == 0xFFFF;
}
int glv_ip_valid(const ipaddr_t* addr) { int glv_ip_valid(const ipaddr_t* addr) {
return addr->cidr == -1; return addr->cidr == -1;
} }

View file

@ -1,12 +1,13 @@
#ifndef GLV_UTIL_IPADDR_H #ifndef GLV_UTIL_IPADDR_H
#define GLV_UTIL_IPADDR_H #define GLV_UTIL_IPADDR_H
#include "util/string.h"
#include <string.h> #include <string.h>
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#include <ctype.h> #include <ctype.h>
#include "util/string.h"
typedef struct { typedef struct {
uint16_t addr[8]; uint16_t addr[8];
uint8_t cidr; uint8_t cidr;