Move 'iscidraddr' to src/iscidraddr.c

This commit is contained in:
0x1eef 2023-08-27 02:01:15 -03:00
parent fa580011e1
commit 4d25b52775
3 changed files with 46 additions and 29 deletions

View file

@ -1,16 +1,17 @@
SRCDIR = src
INCDIR = include
TESTDIR = test
TESTBIN = $(TESTDIR)/runner
SRCDIR = src
SRCFILES = $(SRCDIR)/isinetaddr.c $(SRCDIR)/iscidraddr.c
INCDIR = include
TESTDIR = test
TESTBIN = $(TESTDIR)/runner
CC = cc
CFLAGS = -fstack-protector-all -I$(INCDIR) -Wall -Wextra -pedantic
test:
@$(CC) $(CFLAGS) $(SRCDIR)/isinetaddr.c $(TESTDIR)/isinetaddr_test.c -o $(TESTDIR)/isinetaddr
@$(CC) $(CFLAGS) $(SRCFILES) $(TESTDIR)/isinetaddr_test.c -o $(TESTDIR)/isinetaddr
@echo -n test/isinetaddr: ''
@$(TESTDIR)/isinetaddr
@$(CC) $(CFLAGS) $(SRCDIR)/isinetaddr.c $(TESTDIR)/iscidraddr_test.c -o $(TESTDIR)/iscidraddr
@$(CC) $(CFLAGS) $(SRCFILES) $(TESTDIR)/iscidraddr_test.c -o $(TESTDIR)/iscidraddr
@echo -n test/iscidraddr: ''
@$(TESTDIR)/isinetaddr

39
src/iscidraddr.c Normal file
View file

@ -0,0 +1,39 @@
#include <isinetaddr.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
static int in_range(char buf[4], int min, int max);
int
iscidraddr(const char *str)
{
size_t offset = 0;
size_t len = (str == NULL ? 0 : strnlen(str, 20));
for(size_t i = 0; i < len; i++) {
if(str[i] == '/') {
offset = i;
}
}
if (offset == 0) {
return isinetaddr(str);
} else {
char addr[offset], cidr[3];
char *c = (char*)&str[offset + 1];
memcpy(addr, str, offset);
memcpy(cidr, c, 2);
addr[offset] = '\0';
return isinetaddr(addr) && in_range(cidr, 0, 32);
}
}
static int
in_range(char buf[4], int min, int max)
{
char *err;
long r;
errno = 0;
r = strtol(buf, &err, 10);
return *err == '\0' && errno == 0 && (r >= min && r <= max);
}

View file

@ -41,29 +41,6 @@ isinetaddr(const char *str)
}
}
int
iscidraddr(const char *str)
{
size_t offset = 0;
size_t len = (str == NULL ? 0 : strnlen(str, 20));
for(size_t i = 0; i < len; i++) {
if(str[i] == '/') {
offset = i;
}
}
if (offset == 0) {
return isinetaddr(str);
} else {
char addr[offset], cidr[3];
char *c = (char*)&str[offset + 1];
memcpy(addr, str, offset);
memcpy(cidr, c, 2);
addr[offset] = '\0';
return isinetaddr(addr) && in_range(cidr, 0, 32);
}
}
static int
in_range(char buf[4], int min, int max)
{