Q.C. OK !

This commit is contained in:
malloc 2019-01-18 10:57:14 -06:00
parent b627b59b03
commit 4932d3c6ae
6 changed files with 120 additions and 10 deletions

View file

@ -1,7 +1,7 @@
#include <stdio.h>
#include "util/ipaddr.h"
int main(int argc, char** argv) {
printf("allo");
ipaddr_t test = glv_ip_aton("192.168.1.150/24");
return 0;
}

View file

@ -44,7 +44,7 @@ typedef struct {
glv_tcp_t* glv_tcp_create_server();
glv_tcp_t* glv_tcp_create_client();
int glv_tcp_send(glv_tcp_t* sock, char* data, unsigned int length);
int glv_tcp_send(glv_tcp_t* sock, const char* data, unsigned int length);
int glv_tcp_recv(glv_tcp_t* sock, char* data, unsigned int length, int flags);
int glv_tcp_data_ready(glv_tcp_t* sock);

View file

@ -1,2 +1,72 @@
#include "ipaddr.h"
const ipaddr_t error_addr = {{0, 0, 0, 0, 0, 0, 0, 0}, -1};
ipaddr_t glv_ip_aton(const char* addr) {
ipaddr_t addr_out = {{0, 0, 0, 0, 0, 0, 0, 0}, 0};
char token[5] = {0, 0, 0, 0, 0};
const int length = strlen(addr);
int i, j = 0, k = 0, tmp;
if(strchr(addr, ':') != NULL && strchr(addr, '.') != NULL)
return error_addr;
if(strcnt(addr, '/') > 1)
return error_addr;
if(strchr(addr, '.') != NULL) {
if(strcnt(addr, '.') != 3)
return error_addr;
addr_out.addr[5] = 0xFFFF;
for(i = 0; i < length; ++i) {
if(addr[i] == ' ')
continue;
else if(addr[i] == '.' || addr[i] == '/' || i == length - 1) {
if(j == 0)
return error_addr;
token[j] = 0;
if((tmp = atoi(token)) > 255)
return error_addr;
if(k < 4) {
addr_out.addr[6 + (k / 2)] |=
(tmp << (k % 2 == 0 ? 8 : 0));
j = 0;
++k;
} else {
if(tmp > 32)
return error_addr;
addr_out.cidr = tmp;
}
if(addr[i] == '/' && k != 4)
return error_addr;
} else if(addr[i] >= '0' && addr[i] <= '9') {
if(j == 3)
return error_addr;
token[j++] = addr[i];
} else
return error_addr;
}
} else {
if(strcnt(addr, ':') > 7)
return error_addr;
}
return addr_out;
}
ipaddr_t glv_ip_raw(const uint16_t* addr, uint8_t cidr) {
ipaddr_t addr_out;
memcpy(addr_out.addr, addr, 16);
addr_out.cidr = cidr;
return addr_out;
}
int glv_ip_valid(const ipaddr_t* addr) {
return addr->cidr == -1;
}

View file

@ -1,20 +1,26 @@
#ifndef GLV_UTIL_IPADDR_H
#define GLV_UTIL_IPADDR_H
#include "util/string.h"
#include <string.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct {
uint8_t addr[16];
uint16_t addr[8];
uint8_t cidr;
} ipaddr_t;
ipaddr_t glv_ip_aton(char* addr, int length);
ipaddr_t glv_ip_raw(uint8_t* addr, uint8_t cidr);
ipaddr_t glv_ip_aton(const char* addr);
ipaddr_t glv_ip_raw(const uint16_t* addr, uint8_t cidr);
int glv_ip_check(const char* addr);
int glv_ip_compare(ipaddr_t* lhs, ipaddr_t* rhs);
int glv_ip_identical(ipaddr_t* lhs, ipaddr_t* rhs);
int glv_ip_isv4(ipaddr_t* addr);
int glv_ip_compare(const ipaddr_t* lhs, const ipaddr_t* rhs);
int glv_ip_identical(const ipaddr_t* lhs, const ipaddr_t* rhs);
int glv_ip_isv4(const ipaddr_t* addr);
int glv_ip_valid(const ipaddr_t* addr);
int glv_ip_ntoa(ipaddr_t* addr, char* out);
int glv_ip_ntoa(const ipaddr_t* addr, char* out);
int glv_ipv6_ntoa(const ipaddr_t* addr, char* out);
#endif

25
src/server/util/string.c Normal file
View file

@ -0,0 +1,25 @@
#include "string.h"
int hatoi(const char* str) {
int value = 0, i;
for(i = 0; i < strlen(str); ++i) {
value *= 16;
if(str[i] >= '0' && str[i] <= '9')
value += str[i] - '0';
else if(str[i] >= 'a' && str[i] <= 'f')
value += (str[i] - 'a') + 10;
else if(str[i] >= 'A' && str[i] <= 'F')
value += (str[i] - 'A') + 10;
}
return value;
}
int strcnt(const char* str, char c) {
int cnt = 0, i;
for(i = 0; i < strlen(str); ++i)
if(str[i] == c)
++cnt;
return cnt;
}

9
src/server/util/string.h Normal file
View file

@ -0,0 +1,9 @@
#ifndef GLV_UTIL_STRING_H
#define GLV_UTIL_STRING_H
#include <string.h>
int hatoi(const char* str);
int strcnt(const char* str, char c);
#endif