Path: blob/main/tools/test/stress2/testcases/udp/udp.c
39566 views
/*-1* Copyright (c) 2008 Peter Holm <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*25*/2627#include <sys/types.h>28#include <sys/socket.h>29#include <netinet/in.h>30#include <arpa/inet.h>31#include <err.h>32#include <errno.h>33#include <netdb.h>34#include <stdio.h>35#include <stdlib.h>36#include <string.h>37#include <unistd.h>3839#include "stress.h"4041static int bufsize;4243int44setup(int nb __unused)45{46bufsize = 2 << random_int(1, 12);47return (0);48}4950void51cleanup(void)52{53}5455int56test(void)57{58struct sockaddr_in sock_in;59struct hostent *host;60const char *hostname;61int f, i, n;62int *buf;6364bzero((char *)&sock_in, sizeof(sock_in));65sock_in.sin_family = AF_INET;66f = socket(AF_INET, SOCK_DGRAM, 0);67if (f < 0)68err(1, "socket");69if (bind(f, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0) {70warn("bind");71return (1);72}73if (getenv("BLASTHOST") == NULL)74hostname = "localhost";75else76hostname = getenv("BLASTHOST");77host = gethostbyname(hostname);78if (host) {79sock_in.sin_family = host->h_addrtype;80bcopy(host->h_addr, &sock_in.sin_addr, host->h_length);81} else {82sock_in.sin_family = AF_INET;83sock_in.sin_addr.s_addr = inet_addr(hostname);84if (sock_in.sin_addr.s_addr == INADDR_NONE) {85err(1, "host: %s", hostname);86}87}88sock_in.sin_port = htons(9);8990if (connect(f, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0)91err(1, "connect");9293if ((buf = calloc(1, bufsize)) == NULL)94err(1, "malloc(%d), %s:%d", bufsize, __FILE__, __LINE__);9596if (op->verbose > 1)97printf("udp %s:9 with %d bytes\n", hostname, bufsize);98for (i = 0; i < 128 && done_testing == 0; i++) {99n = write(f, buf, bufsize);100if (n == -1 && errno == ENOBUFS)101continue;102if (n == -1 && errno == ECONNREFUSED)103break;104if (n == -1)105err(1, "write(%d) #%d", bufsize, i);106if (n == 0) break;107}108free(buf);109close(f);110return (0);111}112113114