/*1* Copyright (c) 2014 Spectra Logic Corporation2* 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* without modification.10* 2. Redistributions in binary form must reproduce at minimum a disclaimer11* substantially similar to the "NO WARRANTY" disclaimer below12* ("Disclaimer") and any redistribution must be conditioned upon13* including a substantially similar Disclaimer requirement for further14* binary redistribution.15*16* NO WARRANTY17* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS18* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT19* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR20* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT21* HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL22* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS23* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)24* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,25* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING26* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE27* POSSIBILITY OF SUCH DAMAGES.28*29* Authors: Alan Somers (Spectra Logic Corporation)30*/3132#include <arpa/inet.h>33#include <netinet/in.h>34#include <sys/types.h>35#include <sys/socket.h>3637#include <err.h>38#include <errno.h>39#include <fcntl.h>40#include <stdio.h>41#include <stdbool.h>42#include <stdlib.h>43#include <string.h>44#include <unistd.h>4546/*47* Sends a single UDP packet to the provided address, with SO_DONTROUTE set48* I couldn't find a way to do this with builtin utilities like nc(1)49*/50int51main(int argc, char **argv)52{53struct sockaddr_storage dst;54int s, t;55int opt;56int ret;57ssize_t len;58const char* sendbuf = "Hello, World!";59const size_t buflen = 80;60char recvbuf[buflen];61bool v6 = false;62const char *addr, *tapdev;63const uint16_t port = 46120;6465bzero(&dst, sizeof(dst));66if (argc < 3 || argc > 4) {67fprintf(stderr, "Usage: %s [-6] ip_address tapdev\n", argv[0]);68exit(2);69}7071if (strcmp("-6", argv[1]) == 0) {72v6 = true;73addr = argv[2];74tapdev = argv[3];75} else {76addr = argv[1];77tapdev = argv[2];78}7980t = open(tapdev, O_RDWR | O_NONBLOCK);81if (t < 0)82err(EXIT_FAILURE, "open");8384if (v6)85s = socket(PF_INET6, SOCK_DGRAM, 0);86else87s = socket(PF_INET, SOCK_DGRAM, 0);88if (s < 0)89err(EXIT_FAILURE, "socket");90opt = 1;9192ret = setsockopt(s, SOL_SOCKET, SO_DONTROUTE, &opt, sizeof(opt));93if (ret == -1)94err(EXIT_FAILURE, "setsockopt(SO_DONTROUTE)");9596if (v6) {97struct sockaddr_in6 *dst6 = ((struct sockaddr_in6*)&dst);9899dst.ss_len = sizeof(struct sockaddr_in6);100dst.ss_family = AF_INET6;101dst6->sin6_port = htons(port);102ret = inet_pton(AF_INET6, addr, &dst6->sin6_addr);103} else {104struct sockaddr_in *dst4 = ((struct sockaddr_in*)&dst);105106dst.ss_len = sizeof(struct sockaddr_in);107dst.ss_family = AF_INET;108dst4->sin_port = htons(port);109ret = inet_pton(AF_INET, addr, &dst4->sin_addr);110}111if (ret != 1)112err(EXIT_FAILURE, "inet_pton returned %d", ret);113114ret = sendto(s, sendbuf, strlen(sendbuf), 0, (struct sockaddr*)&dst,115dst.ss_len);116if (ret == -1)117err(EXIT_FAILURE, "sendto");118119/* Verify that the packet went to the desired tap device */120121len = read(t, recvbuf, buflen);122if (len == 0)123errx(EXIT_FAILURE, "read returned EOF");124else if (len < 0 && errno == EAGAIN)125errx(EXIT_FAILURE, "Did not receive any packets");126else if (len < 0)127err(EXIT_FAILURE, "read");128129/*130* If read returned anything at all, consider it a success. The packet131* should be an Ethernet frame containing an ARP request for132* ip_address. We won't bother to decode it133*/134return (0);135}136137138