ATF_TC_BODY(data_string_copy, tc) { struct data_string a, b; const char *str = "Lorem ipsum dolor sit amet orci aliquam."; /* * Create the string we want to copy. */ memset(&a, 0, sizeof(a)); a.len = strlen(str); if (!buffer_allocate(&a.buffer, a.len, MDL)) { atf_tc_fail("out of memory"); } a.data = a.buffer->data; memcpy(a.buffer->data, str, a.len); /* * Copy the string, and confirm it works. */ memset(&b, 0, sizeof(b)); data_string_copy(&b, &a, MDL); if (b.len != a.len) { atf_tc_fail("incorrect length"); } if (b.data != a.data) { atf_tc_fail("incorrect data"); } if (b.terminated != a.terminated) { atf_tc_fail("incorrect terminated"); } if (b.buffer != a.buffer) { atf_tc_fail("incorrect buffer"); } /* * Clean up. */ data_string_forget(&b, MDL); data_string_forget(&a, MDL); }
ATF_TC_BODY(data_string_copy_nobuf, tc) { struct data_string a, b; const char *str = "Lorem ipsum dolor sit amet cras amet."; /* * Create the string we want to copy. */ memset(&a, 0, sizeof(a)); a.len = strlen(str); a.data = (const unsigned char *)str; a.terminated = 1; /* * Copy the string, and confirm it works. */ memset(&b, 0, sizeof(b)); data_string_copy(&b, &a, MDL); if (b.len != a.len) { atf_tc_fail("incorrect length"); } if (b.data != a.data) { atf_tc_fail("incorrect data"); } if (b.terminated != a.terminated) { atf_tc_fail("incorrect terminated"); } if (b.buffer != a.buffer) { atf_tc_fail("incorrect buffer"); } /* * Clean up. */ data_string_forget(&b, MDL); data_string_forget(&a, MDL); }
/* * Process a by-address lease query. */ static int process_lq_by_address(struct lq6_state *lq) { struct packet *packet = lq->packet; struct option_cache *oc; struct ipv6_pool *pool = NULL; struct data_string data; struct in6_addr addr; struct iasubopt *iaaddr = NULL; struct option_state *opt_state = NULL; u_int32_t lifetime; unsigned opt_cursor; int ret_val = 0; /* * Get the IAADDR. */ oc = lookup_option(&dhcpv6_universe, lq->query_opts, D6O_IAADDR); if (oc == NULL) { if (!set_error(lq, STATUS_MalformedQuery, "No OPTION_IAADDR.")) { log_error("process_lq_by_address: unable " "to set MalformedQuery status code."); return 0; } return 1; } memset(&data, 0, sizeof(data)); if (!evaluate_option_cache(&data, packet, NULL, NULL, lq->query_opts, NULL, &global_scope, oc, MDL) || (data.len < IAADDR_OFFSET)) { log_error("process_lq_by_address: error evaluating IAADDR."); goto exit; } memcpy(&addr, data.data, sizeof(addr)); data_string_forget(&data, MDL); /* * Find the lease. * Note the RFC 5007 says to use the link-address to find the link * or the ia-aadr when it is :: but in any case the ia-addr has * to be on the link, so we ignore the link-address here. */ if (find_ipv6_pool(&pool, D6O_IA_NA, &addr) != ISC_R_SUCCESS) { if (!set_error(lq, STATUS_NotConfigured, "Address not in a pool.")) { log_error("process_lq_by_address: unable " "to set NotConfigured status code."); goto exit; } ret_val = 1; goto exit; } if (iasubopt_hash_lookup(&iaaddr, pool->leases, &addr, sizeof(addr), MDL) == 0) { ret_val = 1; goto exit; } if ((iaaddr == NULL) || (iaaddr->state != FTS_ACTIVE) || (iaaddr->ia == NULL) || (iaaddr->ia->iaid_duid.len <= 4)) { ret_val = 1; goto exit; } /* * Build the client-data option (with client-id, ia-addr and clt-time). */ if (!option_state_allocate(&opt_state, MDL)) { log_error("process_lq_by_address: " "no memory for option state."); goto exit; } data_string_copy(&data, &iaaddr->ia->iaid_duid, MDL); data.data += 4; data.len -= 4; if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL, (unsigned char *)data.data, data.len, D6O_CLIENTID, 0)) { log_error("process_lq_by_address: error saving client ID."); goto exit; } data_string_forget(&data, MDL); data.len = IAADDR_OFFSET; if (!buffer_allocate(&data.buffer, data.len, MDL)) { log_error("process_lq_by_address: no memory for ia-addr."); goto exit; } data.data = data.buffer->data; memcpy(data.buffer->data, &iaaddr->addr, 16); lifetime = iaaddr->prefer; putULong(data.buffer->data + 16, lifetime); lifetime = iaaddr->valid; putULong(data.buffer->data + 20, lifetime); if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL, (unsigned char *)data.data, data.len, D6O_IAADDR, 0)) { log_error("process_lq_by_address: error saving ia-addr."); goto exit; } data_string_forget(&data, MDL); lifetime = htonl(iaaddr->ia->cltt); if (!save_option_buffer(&dhcpv6_universe, opt_state, NULL, (unsigned char *)&lifetime, 4, D6O_CLT_TIME, 0)) { log_error("process_lq_by_address: error saving clt time."); goto exit; } /* * Store the client-data option. */ opt_cursor = lq->cursor; putUShort(lq->buf.data + lq->cursor, (unsigned)D6O_CLIENT_DATA); lq->cursor += 2; /* Skip option length. */ lq->cursor += 2; lq->cursor += store_options6((char *)lq->buf.data + lq->cursor, sizeof(lq->buf) - lq->cursor, opt_state, lq->packet, required_opt_CLIENT_DATA, NULL); /* Reset the length. */ putUShort(lq->buf.data + opt_cursor + 2, lq->cursor - (opt_cursor + 4)); /* Done. */ ret_val = 1; exit: if (data.data != NULL) data_string_forget(&data, MDL); if (pool != NULL) ipv6_pool_dereference(&pool, MDL); if (iaaddr != NULL) iasubopt_dereference(&iaaddr, MDL); if (opt_state != NULL) option_state_dereference(&opt_state, MDL); return ret_val; }