From cc2a5ab960432bc61df6151303d518872a35b227 Mon Sep 17 00:00:00 2001 From: Alex 'AdUser' Z Date: Sat, 20 Feb 2016 17:10:28 +1000 Subject: [PATCH] + src/ipaddr.c --- src/CMakeLists.txt | 2 +- src/ipaddr.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++ src/ipaddr.h | 8 +++++-- 3 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 src/ipaddr.c diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7c0fcde..c14c823 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,6 +1,6 @@ set(CMAKE_INCLUDE_CURRENT_DIR ON) -set(SOURCES "main.c" "logfile.c" "log.c" "matches.c") +set(SOURCES "main.c" "logfile.c" "log.c" "matches.c" "ipaddr.c") add_executable(f2b ${SOURCES}) diff --git a/src/ipaddr.c b/src/ipaddr.c new file mode 100644 index 0000000..733f37e --- /dev/null +++ b/src/ipaddr.c @@ -0,0 +1,54 @@ +#include "common.h" +#include "ipaddr.h" + +f2b_ipaddr_t * +f2b_ipaddr_create(const char *addr, size_t matches) { + f2b_ipaddr_t *a = NULL; + + assert(addr != NULL); + assert(matches != 0); + + if ((a = calloc(1, sizeof(f2b_ipaddr_t))) != NULL) { + strncpy(a->text, addr, sizeof(a->text)); + if (strchr(addr, ':') == NULL) { + a->type = AF_INET; + if (inet_pton(a->type, addr, &a->binary.v4) < 1) + goto cleanup; + } else { + a->type = AF_INET6; + if (inet_pton(a->type, addr, &a->binary.v6) < 1) + goto cleanup; + } + + if (f2b_matches_create(&a->matches, matches) == false) + goto cleanup; + } + return a; + + cleanup: + FREE(a); + return NULL; +} + +f2b_ipaddr_t * +f2b_addrlist_append(f2b_ipaddr_t *list, f2b_ipaddr_t *ipaddr) { + assert(ipaddr != NULL); + + ipaddr->next = list; + return ipaddr; +} + +f2b_ipaddr_t * +f2b_addrlist_lookup(f2b_ipaddr_t *list, const char *addr) { + assert(addr != NULL); + + if (list == NULL) + return NULL; + + for (f2b_ipaddr_t *a = list; a->next != NULL; a = a->next) { + if (strncmp(a->text, addr, sizeof(a->text)) == 0) + return a; + } + + return NULL; +} diff --git a/src/ipaddr.h b/src/ipaddr.h index dbc2c30..a71c04d 100644 --- a/src/ipaddr.h +++ b/src/ipaddr.h @@ -5,10 +5,10 @@ #include "matches.h" -typedef struct { +typedef struct f2b_ipaddr_t { struct f2b_ipaddr_t *next; + int type; char text[40]; /* 8 x "ffff" + 7 x ":" + '\0' */ - int addr_type; union { struct in_addr v4; struct in6_addr v6; @@ -16,4 +16,8 @@ typedef struct { f2b_matches_t matches; } f2b_ipaddr_t; +f2b_ipaddr_t * f2b_ipaddr_create(const char *addr, size_t max_matches); +f2b_ipaddr_t * f2b_addrlist_append(f2b_ipaddr_t *list, f2b_ipaddr_t *ipaddr); +f2b_ipaddr_t * f2b_addrlist_lookup(f2b_ipaddr_t *list, const char *addr); + #endif /* F2B_IPADDR_H_ */