ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/src/trunk/tests/sys/netinet/udp_dontroute.c
Revision: 11970
Committed: Sat Jul 28 20:41:20 2018 UTC (5 years, 9 months ago) by laffer1
Content type: text/plain
File size: 3557 byte(s)
Log Message:
add netinet tests

File Contents

# Content
1 /* $MidnightBSD$ */
2 /*
3 * Copyright (c) 2014 Spectra Logic Corporation
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
13 * substantially similar to the "NO WARRANTY" disclaimer below
14 * ("Disclaimer") and any redistribution must be conditioned upon
15 * including a substantially similar Disclaimer requirement for further
16 * binary redistribution.
17 *
18 * NO WARRANTY
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
28 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGES.
30 *
31 * Authors: Alan Somers (Spectra Logic Corporation)
32 *
33 * $FreeBSD: stable/10/tests/sys/netinet/udp_dontroute.c 267186 2014-06-06 20:35:40Z asomers $
34 */
35
36 #include <arpa/inet.h>
37 #include <netinet/in.h>
38 #include <sys/types.h>
39 #include <sys/socket.h>
40
41 #include <err.h>
42 #include <errno.h>
43 #include <fcntl.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48
49 /*
50 * Sends a single UDP packet to the provided address, with SO_DONTROUTE set
51 * I couldn't find a way to do this with builtin utilities like nc(1)
52 */
53 int
54 main(int argc, char **argv)
55 {
56 struct sockaddr_in dst;
57 int s, t;
58 int opt;
59 int ret;
60 ssize_t len;
61 const char* sendbuf = "Hello, World!";
62 const size_t buflen = 80;
63 char recvbuf[buflen];
64
65 if (argc != 3) {
66 fprintf(stderr, "Usage: %s ip_address tapdev\n", argv[0]);
67 exit(2);
68 }
69
70 t = open(argv[2], O_RDWR | O_NONBLOCK);
71 if (t < 0)
72 err(EXIT_FAILURE, "open");
73
74 s = socket(PF_INET, SOCK_DGRAM, 0);
75 if (s < 0)
76 err(EXIT_FAILURE, "socket");
77 opt = 1;
78
79 ret = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, &opt, sizeof(opt));
80 if (ret == -1)
81 err(EXIT_FAILURE, "setsockopt(SO_DONTROUTE)");
82
83 dst.sin_len = sizeof(dst);
84 dst.sin_family = AF_INET;
85 dst.sin_port = htons(46120);
86 dst.sin_addr.s_addr = inet_addr(argv[1]);
87 if (dst.sin_addr.s_addr == htonl(INADDR_NONE)) {
88 fprintf(stderr, "Invalid address: %s\n", argv[1]);
89 exit(2);
90 }
91 ret = sendto(s, sendbuf, strlen(sendbuf), 0, (struct sockaddr*)&dst,
92 dst.sin_len);
93 if (ret == -1)
94 err(EXIT_FAILURE, "sendto");
95
96 /* Verify that the packet went to the desired tap device */
97
98 len = read(t, recvbuf, buflen);
99 if (len == 0)
100 errx(EXIT_FAILURE, "read returned EOF");
101 else if (len < 0 && errno == EAGAIN)
102 errx(EXIT_FAILURE, "Did not receive any packets");
103 else if (len < 0)
104 err(EXIT_FAILURE, "read");
105
106 /*
107 * If read returned anything at all, consider it a success. The packet
108 * should be an Ethernet frame containing an ARP request for
109 * ip_address. We won't bother to decode it
110 */
111 return (0);
112 }

Properties

Name Value
svn:eol-style native
svn:keywords MidnightBSD=%H
svn:mime-type text/plain