You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
1.6 KiB

9 years ago
#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;
}
void
f2b_ipaddr_destroy(f2b_ipaddr_t *ipaddr) {
assert(ipaddr != NULL);
f2b_matches_destroy(&ipaddr->matches);
free(ipaddr);
}
9 years ago
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) {
9 years ago
assert(addr != NULL);
if (list == NULL)
return NULL;
for (f2b_ipaddr_t *a = list; a != NULL; a = a->next) {
9 years ago
if (strncmp(a->text, addr, sizeof(a->text)) == 0)
return a;
}
return NULL;
}
f2b_ipaddr_t *
f2b_addrlist_remove(f2b_ipaddr_t *list, const char *addr) {
f2b_ipaddr_t *a = NULL;
assert(addr != NULL);
if (list == NULL)
return NULL;
if (strncmp(list->text, addr, sizeof(list->text)) == 0) {
a = list->next;
f2b_ipaddr_destroy(list);
return a;
}
for (a = list; a->next != NULL; a = a->next) {
assert(0); /* TODO */
}
return list;
}