Path: blob/main/tools/regression/netinet/udpzerobyte/udpzerobyte.c
39498 views
/*-1* Copyright (c) 2008 Robert N. M. Watson2* 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*/2526#include <sys/param.h>27#include <sys/socket.h>28#include <sys/un.h>2930#include <netinet/in.h>3132#include <arpa/inet.h>3334#include <err.h>35#include <errno.h>36#include <fcntl.h>37#include <stdio.h>38#include <string.h>39#include <unistd.h>4041/*42* The UDP code allows transmitting zero-byte datagrams, but are they43* received?44*/4546#define THEPORT 9543 /* Arbitrary. */4748static void49usage(void)50{5152errx(-1, "no arguments allowed\n");53}5455static void56test(int domain, const char *domainstr, struct sockaddr *sa, socklen_t salen)57{58int sock_send, sock_receive;59ssize_t size;6061sock_send = socket(domain, SOCK_DGRAM, 0);62if (sock_send < 0)63err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);6465sock_receive = socket(domain, SOCK_DGRAM, 0);66if (sock_receive < 0)67err(-1, "socket(%s, SOCK_DGRAM, 0)", domainstr);6869if (bind(sock_receive, sa, salen) < 0)70err(-1, "Protocol %s bind(sock_receive)", domainstr);71if (fcntl(sock_receive, F_SETFL, O_NONBLOCK, 1) < 0)72err(-1, "Protocll %s fcntl(sock_receive, FL_SETFL, "73"O_NONBLOCK)", domainstr);7475if (connect(sock_send, sa, salen) < 0)76err(-1, "Protocol %s connect(sock_send)", domainstr);7778size = recv(sock_receive, NULL, 0, 0);79if (size > 0)80errx(-1, "Protocol %s recv(sock_receive, NULL, 0) before: %zd",81domainstr, size);82else if (size < 0)83err(-1, "Protocol %s recv(sock_receive, NULL, 0) before",84domainstr);8586size = send(sock_send, NULL, 0, 0);87if (size < 0)88err(-1, "Protocol %s send(sock_send, NULL, 0)", domainstr);8990(void)sleep(1);91size = recv(sock_receive, NULL, 0, 0);92if (size < 0)93err(-1, "Protocol %s recv(sock_receive, NULL, 0) test",94domainstr);9596size = recv(sock_receive, NULL, 0, 0);97if (size > 0)98errx(-1, "Protocol %s recv(sock_receive, NULL, 0) after: %zd",99domainstr, size);100else if (size < 0)101err(-1, "Protocol %s recv(sock_receive, NULL, 0) after",102domainstr);103}104105int106main(int argc, __unused char *argv[])107{108struct sockaddr_un sun;109struct sockaddr_in6 sin6;110struct sockaddr_in sin;111struct in6_addr loopback6addr = IN6ADDR_LOOPBACK_INIT;112113if (argc != 1)114usage();115116bzero(&sin, sizeof(sin));117sin.sin_len = sizeof(sin);118sin.sin_family = AF_INET;119sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK);120sin.sin_port = htons(THEPORT);121122test(PF_INET, "PF_INET", (struct sockaddr *)&sin, sizeof(sin));123124bzero(&sin6, sizeof(sin6));125sin6.sin6_len = sizeof(sin6);126sin6.sin6_family = AF_INET6;127sin6.sin6_addr = loopback6addr;128sin6.sin6_port = htons(THEPORT);129130test(PF_INET6, "PF_INET6", (struct sockaddr *)&sin6, sizeof(sin6));131132bzero(&sun, sizeof(sun));133sun.sun_len = sizeof(sun);134sun.sun_family = AF_LOCAL;135strlcpy(sun.sun_path, "/tmp/udpzerosize-socket", sizeof(sun.sun_path));136if (unlink(sun.sun_path) < 0 && errno != ENOENT)137err(-1, "unlink: %s", sun.sun_path);138139test(PF_LOCAL, "PF_LOCAL", (struct sockaddr *)&sun, sizeof(sun));140141return (0);142}143144145