Browse Source

+ src/ipaddr.c

master
Alex 'AdUser' Z 8 years ago
parent
commit
cc2a5ab960
  1. 2
      src/CMakeLists.txt
  2. 54
      src/ipaddr.c
  3. 8
      src/ipaddr.h

2
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})

54
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;
}

8
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_ */

Loading…
Cancel
Save