static struct rr *a_parse(char *name, long ttl, int type, char *s) { struct rr_a *rr = getmem(sizeof(*rr)); if (extract_ipv4(&s, "IPv4 address", &rr->address) <= 0) return NULL; if (*s) { return bitch("garbage after valid A data"); } return store_record(type, name, ttl, rr); }
extern int str2sock(n2n_sock_t *out, const n2n_sock_str_t str_orig) { int retval; char *last_colon_pos = NULL; n2n_sock_str_t str; memcpy(str, str_orig, sizeof(n2n_sock_str_t)); last_colon_pos = strrchr(str, ':'); if (strchr(str, ':') == last_colon_pos) { if (last_colon_pos) //TODO { *last_colon_pos = '\0'; out->port = atoi(last_colon_pos + 1); out->port = htons(out->port); } out->family = AF_INET; retval = extract_ipv4(out, str); } else { char *from_pos = strchr(str, '['); if (from_pos) { char *to_pos = strchr(str, ']'); if (!to_pos) return -1;//TODO if (to_pos < last_colon_pos) //TODO { //*last_colon_pos = '\0'; out->port = atoi(last_colon_pos + 1); out->port = htons(out->port); } from_pos += 1; *to_pos = 0; } else from_pos = str; out->family = AF_INET6; retval = extract_ipv6(out, from_pos); } return retval; }
static struct rr *l32_parse(char *name, long ttl, int type, char *s) { struct rr_l32 *rr = getmem(sizeof(*rr)); struct in_addr ipv4_like; int preference; rr->preference = preference = extract_integer(&s, "L32 preference", NULL); if (preference < 0) return NULL; if (extract_ipv4(&s, "Locator32", &ipv4_like) <= 0) return NULL; rr->locator32 = ipv4_like.s_addr; if (*s) { return bitch("garbage after valid L32 data"); } return store_record(type, name, ttl, rr); }
bool test_extract_ipv4_from_ipv6( void ) { struct in6_addr addr6; struct in_addr extracted4; struct in_addr correct4; bool success = true; if (pool6_init(NULL, 0) != 0) return false; success &= str_to_addr6_verbose(IPV6_EXTRACT_ADDR, &addr6); success &= str_to_addr4_verbose(IPV4_EXTRACTED_ADDR, &correct4); success &= assert_true(extract_ipv4(&addr6, &extracted4), "Check that an IPv4 address can be extracted from an IPv6 address."); success &= assert_equals_ipv4(&extracted4, &correct4, "Assert that the extraction of the IPv4 address was correct."); pool6_destroy(); return success; }
static inline int ping_v4(const char * hostname) { int sockfd; struct sockaddr_in address; icmp4_packet packet; int success = get_ipv4(hostname, IPPROTO_ICMP, &address); printf("ping "); print_host_v4(&address); printf("\n"); succeed_or_die(success, 0, create_raw_socket(AF_INET, SOCK_RAW, IPPROTO_ICMP, &sockfd)); succeed_or_die(success, 0, icmp4_packet_init(&packet, extract_ipv4(&address))); succeed_or_die(success, 0, icmp4_packet_set_length(&packet, sizeof packet)); int sent = 0; int i = 0; int gotten = 0; struct timeval wait_time = { 1, 0 }; long double min = 0.; long double max = 0.; long double sum = .0; struct timeval ping_start = { 0, 0 }; struct timeval ping_end = { 0, 0 }; gettimeofday(&ping_start, NULL); while (success == 0 && fin_des_temps == 1) { struct timeval start = { 0, 0 }; struct timeval end = { 0, 0 }; succeed_or_die(success, 0, icmp4_packet_set_echo_seq(&packet, i)); gettimeofday(&start, NULL); if (sendto(sockfd, &packet, sizeof packet, 0, (struct sockaddr *) &address, sizeof address) == sizeof packet) { sent++; icmp4_packet received; memset(&received, 0, sizeof received); int before = gotten; if (receive_icmp_v4(sockfd, &address, &wait_time, &received) == 0) { if (received.icmp_header.type == ICMP_ECHOREPLY && received.icmp_header.un.echo.sequence == i && received.icmp_header.un.echo.id == packet.icmp_header.un.echo.id ) { gotten++; gettimeofday(&end, NULL); struct timeval diff = diff_timeval(start, end); long double rtt = extract_time(diff); update_statistics(&min, &max, &sum, rtt, gotten, before); print_received(&received, &address, rtt); if (rtt > sum / gotten * 2 || rtt < sum / gotten / 2) success = -1; } } if ((float) gotten / sent < 0.7) success = -1; } i++; sleep(1); } gettimeofday(&ping_end, NULL); struct timeval total = diff_timeval(ping_start, ping_end); print_ping_statistics(sent, gotten, min, max, sum, total, &address); return success; }
static struct rr *ipseckey_parse(char *name, long ttl, int type, char *s) { struct rr_ipseckey *rr = getmem(sizeof(*rr)); int i; rr->precedence = i = extract_integer(&s, "precedence", NULL); if (i < 0) return NULL; if (i >= 256) return bitch("precedence range is not valid"); rr->gateway_type = i = extract_integer(&s, "gateway type", NULL); if (i < 0) return NULL; if (i > 3) return bitch("gateway type is not valid"); rr->algorithm = i = extract_integer(&s, "algorithm", NULL); if (i < 0) return NULL; if (i > 2) return bitch("algorithm is not valid"); switch (rr->gateway_type) { case 0: rr->gateway.gateway_none = extract_name(&s, "gateway/.", KEEP_CAPITALIZATION); if (!rr->gateway.gateway_none) return NULL; if (strcmp(rr->gateway.gateway_none, ".") != 0) return bitch("gateway must be \".\" for gateway type 0"); break; case 1: if (extract_ipv4(&s, "gateway/IPv4", &rr->gateway.gateway_ipv4) <= 0) return NULL; break; case 2: if (extract_ipv6(&s, "gateway/IPv6", &rr->gateway.gateway_ipv6) <= 0) return NULL; break; case 3: rr->gateway.gateway_name = extract_name(&s, "gateway/name", KEEP_CAPITALIZATION); if (!rr->gateway.gateway_name) return NULL; break; default: croakx(7, "assertion failed: gateway type %d not within range", rr->gateway_type); } /* My reading of http://tools.ietf.org/html/rfc4025 is fuzzy on: * * - whether it is possible to have algorithm 0 and non-empty key; * - whether it is possible to have empty key and algorithm != 0. * * Here I assume "not possible" for both. */ switch (rr->algorithm) { case 0: break; case 1: /* DSA key */ rr->public_key = extract_base64_binary_data(&s, "public key"); if (rr->public_key.length < 0) return NULL; break; case 2: /* RSA key */ rr->public_key = extract_base64_binary_data(&s, "public key"); if (rr->public_key.length < 0) return NULL; break; default: croakx(7, "assertion failed: algorithm %d not within range", rr->algorithm); } if (*s) { return bitch("garbage after valid IPSECKEY data"); } return store_record(type, name, ttl, rr); }