STATIC const struct in6_addr * find_rdnss(ptrlist_t * options_p, int * how_many) { int count; int i; count = ptrlist_count(options_p); *how_many = 0; for (i = 0; i < count; i++) { const struct nd_opt_rdnss * opt; opt = (const struct nd_opt_rdnss *)ptrlist_element(options_p, i); if (opt->nd_opt_rdnss_type == ND_OPT_RDNSS) { int opt_len = opt->nd_opt_rdnss_len * ND_OPT_ALIGN; if (opt_len < ND_OPT_RDNSS_MIN_LENGTH) { /* invalid option */ break; } if (opt->nd_opt_rdnss_lifetime == 0) { /* skip it */ continue; } *how_many = (opt_len - ND_OPT_RDNSS_HEADER_LENGTH) / sizeof(struct in6_addr); return (opt->nd_opt_rdnss_addr); } } return (NULL); }
STATIC const uint8_t * find_source_link_address(ptrlist_t * options_p, int * ret_len) { int count; int i; count = ptrlist_count(options_p); *ret_len = 0; for (i = 0; i < count; i++) { const struct _nd_opt_linkaddr * opt; opt = (const struct _nd_opt_linkaddr *)ptrlist_element(options_p, i); if (opt->nd_opt_linkaddr_type == ND_OPT_SOURCE_LINKADDR) { int opt_len = opt->nd_opt_linkaddr_len * ND_OPT_ALIGN; if (opt_len < (_ND_OPT_LINKADDR_HDR_SIZE + ETHER_ADDR_LEN)) { /* invalid option */ break; } *ret_len = opt_len - _ND_OPT_LINKADDR_HDR_SIZE; return (opt->nd_opt_linkaddr_data); } } return (NULL); }
__private_extern__ int ptrlist_index(ptrlist_t * list, void * element) { int i; for (i = 0; i < ptrlist_count(list); i++) { if (ptrlist_element(list, i) == element) return (i); } return (-1); }
const char* /*UNITTEST*/ test_insert(void) { ptrlist_t l = ptrlist_init(); int w, x, y, z; int *p = &w, *q = &x, *r = &y, *s = &z; ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != s || ptrlist_get(l, 4) != NULL) return "got something else out than what was put in"; ptrlist_clear(l); ptrlist_insert(l, -1, p); ptrlist_insert(l, -1, q); ptrlist_insert(l, -1, r); ptrlist_insert(l, -1, s); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != s || ptrlist_get(l, 4) != NULL) return "got something else out than what was put in"; ptrlist_clear(l); ptrlist_insert(l, 5, r); //tail ptrlist_insert(l, -1, s); //tail ptrlist_insert(l, 0, p); //head ptrlist_insert(l, 1, q); //2nd if (ptrlist_count(l) != 4) return "list size != 4 after inserting four elements"; if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != s || ptrlist_get(l, 4) != NULL) return "got something else out than what was put in"; ptrlist_dispose(l); return NULL; }
const char* /*UNITTEST*/ test_basic(void) { ptrlist_t l = ptrlist_init(); if (ptrlist_count(l) != 0) return "newly allocated list not empty"; ptrlist_dispose(l); return NULL; }
STATIC void print_nd_options(ptrlist_t * options_p) { int count; int i; count = ptrlist_count(options_p); printf("There are %d options\n", count); for (i = 0; i < count; i++) { const struct nd_opt_hdr * opt; const char * opt_name; opt = (const struct nd_opt_hdr *)ptrlist_element(options_p, i); opt_name = S_nd_opt_name(opt->nd_opt_type); printf("%s (%d) length %d\n", opt_name, opt->nd_opt_type, opt->nd_opt_len * ND_OPT_ALIGN); } return; }
int dhcpol_count(dhcpol_t * list) { return (ptrlist_count((ptrlist_t *)list)); }
const char* /*UNITTEST*/ test_remove(void) { ptrlist_t l = ptrlist_init(); int w, x, y, z; int *p = &w, *q = &x, *r = &y, *s = &z; ptrlist_insert(l, 0, s); ptrlist_remove(l, 1); if (ptrlist_count(l) != 1) return "bogus remove actually removed something?"; ptrlist_remove(l, 0); if (ptrlist_count(l) != 0) return "failed to remove single element"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_remove(l, 2); if (ptrlist_count(l) != 2) return "bogus remove actually removed something?"; ptrlist_remove(l, 0); if (ptrlist_count(l) != 1) return "failed to remove head of 2-element list"; ptrlist_remove(l, 0); if (ptrlist_count(l) != 0) return "failed to remove single remaining element"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 0); if (ptrlist_get(l, 0) != q || ptrlist_get(l, 1) != r || ptrlist_get(l, 2) != s || ptrlist_get(l, 3) != NULL) return "remove of element 0 in 4-elem list failed"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 1); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != r || ptrlist_get(l, 2) != s || ptrlist_get(l, 3) != NULL) return "remove of element 1 in 4-elem list failed"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 2); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != s || ptrlist_get(l, 3) != NULL) return "remove of element 2 in 4-elem list failed"; ptrlist_clear(l); ptrlist_insert(l, 0, s); ptrlist_insert(l, 0, r); ptrlist_insert(l, 0, q); ptrlist_insert(l, 0, p); ptrlist_remove(l, 3); if (ptrlist_get(l, 0) != p || ptrlist_get(l, 1) != q || ptrlist_get(l, 2) != r || ptrlist_get(l, 3) != NULL) return "remove of element 3 in 4-elem list failed"; ptrlist_clear(l); ptrlist_dispose(l); return NULL; }