#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <err.h>
#include <errno.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "stress.h"
static int bufsize;
int
setup(int nb __unused)
{
bufsize = 2 << random_int(1, 12);
return (0);
}
void
cleanup(void)
{
}
int
test(void)
{
struct sockaddr_in sock_in;
struct hostent *host;
const char *hostname;
int f, i, n;
int *buf;
bzero((char *)&sock_in, sizeof(sock_in));
sock_in.sin_family = AF_INET;
f = socket(AF_INET, SOCK_DGRAM, 0);
if (f < 0)
err(1, "socket");
if (bind(f, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0) {
warn("bind");
return (1);
}
if (getenv("BLASTHOST") == NULL)
hostname = "localhost";
else
hostname = getenv("BLASTHOST");
host = gethostbyname(hostname);
if (host) {
sock_in.sin_family = host->h_addrtype;
bcopy(host->h_addr, &sock_in.sin_addr, host->h_length);
} else {
sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = inet_addr(hostname);
if (sock_in.sin_addr.s_addr == INADDR_NONE) {
err(1, "host: %s", hostname);
}
}
sock_in.sin_port = htons(9);
if (connect(f, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0)
err(1, "connect");
if ((buf = calloc(1, bufsize)) == NULL)
err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);
if (op->verbose > 1)
printf("udp %s:9 with %d bytes\n", hostname, bufsize);
for (i = 0; i < 128 && done_testing == 0; i++) {
n = write(f, buf, bufsize);
if (n == -1 && errno == ENOBUFS)
continue;
if (n == -1 && errno == ECONNREFUSED)
break;
if (n == -1)
err(1, "write(%d) #%d", bufsize, i);
if (n == 0) break;
}
free(buf);
close(f);
return (0);
}