Changeset View
Changeset View
Standalone View
Standalone View
tests/sys/netinet/libalias/util.c
#include <atf-c.h> | |||||
#include <stdio.h> | #include <stdio.h> | ||||
#include <stdlib.h> | #include <stdlib.h> | ||||
#include <netinet/in.h> | #include <netinet/in.h> | ||||
#include "util.h" | #include "util.h" | ||||
#define REQUIRE(x) do { \ | |||||
if (!(x)) { \ | |||||
fprintf(stderr, "Failed in %s %s:%d.\n",\ | |||||
__FUNCTION__, __FILE__, __LINE__); \ | |||||
exit(-1); \ | |||||
} \ | |||||
} while(0) | |||||
int | int | ||||
randcmp(const void *a, const void *b) | randcmp(const void *a, const void *b) | ||||
{ | { | ||||
int res, r = rand(); | int res, r = rand(); | ||||
(void)a; | (void)a; | ||||
(void)b; | (void)b; | ||||
res = (r/4 < RAND_MAX/9) ? 1 | res = (r/4 < RAND_MAX/9) ? 1 | ||||
Show All 20 Lines | if ((i & 0xf) != 0x0) | ||||
printf("\n"); | printf("\n"); | ||||
} | } | ||||
struct ip * | struct ip * | ||||
ip_packet(struct in_addr src, struct in_addr dst, u_char protocol, size_t len) | ip_packet(struct in_addr src, struct in_addr dst, u_char protocol, size_t len) | ||||
{ | { | ||||
struct ip * p; | struct ip * p; | ||||
ATF_REQUIRE(len >= 64 && len <= IP_MAXPACKET); | REQUIRE(len >= 64 && len <= IP_MAXPACKET); | ||||
p = calloc(1, len); | p = calloc(1, len); | ||||
ATF_REQUIRE(p != NULL); | REQUIRE(p != NULL); | ||||
p->ip_v = IPVERSION; | p->ip_v = IPVERSION; | ||||
p->ip_hl = sizeof(*p)/4; | p->ip_hl = sizeof(*p)/4; | ||||
p->ip_len = htons(len); | p->ip_len = htons(len); | ||||
p->ip_ttl = IPDEFTTL; | p->ip_ttl = IPDEFTTL; | ||||
p->ip_src = src; | p->ip_src = src; | ||||
p->ip_dst = dst; | p->ip_dst = dst; | ||||
p->ip_p = protocol; | p->ip_p = protocol; | ||||
ATF_REQUIRE(p->ip_hl == 5); | REQUIRE(p->ip_hl == 5); | ||||
return (p); | return (p); | ||||
} | } | ||||
struct udphdr * | struct udphdr * | ||||
set_udp(struct ip *p, u_short sport, u_short dport) { | set_udp(struct ip *p, u_short sport, u_short dport) { | ||||
uint32_t *up = (void *)p; | uint32_t *up = (void *)p; | ||||
struct udphdr *u = (void *)&(up[p->ip_hl]); | struct udphdr *u = (void *)&(up[p->ip_hl]); | ||||
int payload = ntohs(p->ip_len) - 4*p->ip_hl; | int payload = ntohs(p->ip_len) - 4*p->ip_hl; | ||||
ATF_REQUIRE(payload >= (int)sizeof(*u)); | REQUIRE(payload >= (int)sizeof(*u)); | ||||
p->ip_p = IPPROTO_UDP; | p->ip_p = IPPROTO_UDP; | ||||
u->uh_sport = htons(sport); | u->uh_sport = htons(sport); | ||||
u->uh_dport = htons(dport); | u->uh_dport = htons(dport); | ||||
u->uh_ulen = htons(payload); | u->uh_ulen = htons(payload); | ||||
return (u); | return (u); | ||||
} | } |