1 /* $OpenBSD: nonxt-sendrecv.c,v 1.2 2018/05/21 01:19:21 bluhm Exp $ */ 2 /* 3 * Copyright (c) Alexander Bluhm <bluhm@genua.de> 4 * 5 * Permission to use, copy, modify, and distribute this software for any 6 * purpose with or without fee is hereby granted, provided that the above 7 * copyright notice and this permission notice appear in all copies. 8 * 9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 16 */ 17 18 #include <sys/types.h> 19 #include <sys/socket.h> 20 21 #include <netdb.h> 22 23 #include <err.h> 24 #include <errno.h> 25 #include <stdio.h> 26 #include <stdlib.h> 27 #include <string.h> 28 #include <unistd.h> 29 30 void __dead usage(void); 31 32 void 33 usage(void) 34 { 35 fprintf(stderr, "usage: nonxt-sendrecv [localaddr] remoteaddr\n" 36 "Send empty protocol 59 packet and wait for answer.\n"); 37 exit(1); 38 } 39 40 int 41 main(int argc, char *argv[]) 42 { 43 struct addrinfo hints, *res, *res0; 44 struct timeval to; 45 const char *cause = NULL, *local, *remote; 46 int error; 47 int save_errno; 48 int s; 49 char buf[1024]; 50 51 switch (argc) { 52 case 2: 53 local = NULL; 54 remote = argv[1]; 55 break; 56 case 3: 57 local = argv[1]; 58 remote = argv[2]; 59 break; 60 default: 61 usage(); 62 } 63 64 if (pledge("stdio inet dns", NULL) == -1) 65 err(1, "pledge"); 66 67 /* Create socket and connect it to remote address. */ 68 memset(&hints, 0, sizeof(hints)); 69 hints.ai_family = AF_UNSPEC; 70 hints.ai_socktype = SOCK_RAW; 71 hints.ai_protocol = IPPROTO_NONE; 72 error = getaddrinfo(remote, NULL, &hints, &res0); 73 if (error) 74 errx(1, "getaddrinfo remote: %s", gai_strerror(error)); 75 for (res = res0; res; res = res->ai_next) { 76 s = socket(res->ai_family, res->ai_socktype, res->ai_protocol); 77 if (s == -1) { 78 cause = "socket"; 79 continue; 80 } 81 if (connect(s, res->ai_addr, res->ai_addrlen) == -1) { 82 cause = "connect"; 83 save_errno = errno; 84 close(s); 85 errno = save_errno; 86 continue; 87 } 88 break; 89 } 90 if (res == NULL) 91 err(1, "%s", cause); 92 93 /* Optionally bind the socket to local address. */ 94 if (local != NULL) { 95 memset(&hints, 0, sizeof(hints)); 96 hints.ai_family = res->ai_family; 97 hints.ai_socktype = SOCK_RAW; 98 hints.ai_protocol = IPPROTO_NONE; 99 hints.ai_flags = AI_PASSIVE; 100 freeaddrinfo(res0); 101 error = getaddrinfo(local, NULL, &hints, &res0); 102 if (error) 103 errx(1, "getaddrinfo local: %s", gai_strerror(error)); 104 for (res = res0; res; res = res->ai_next) { 105 if (bind(s, res->ai_addr, res->ai_addrlen) == -1) 106 continue; 107 break; 108 } 109 if (res == NULL) 110 err(1, "bind"); 111 } 112 freeaddrinfo(res0); 113 114 if (pledge("stdio inet", NULL) == -1) 115 err(1, "pledge"); 116 117 /* Send a protocol 59 packet. */ 118 if (send(s, buf, 0, 0) == -1) 119 err(1, "send"); 120 /* Wait for up to 3 seconds to receive a reply packet. */ 121 to.tv_sec = 3; 122 to.tv_usec = 0; 123 if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)) == -1) 124 err(1, "setsockopt"); 125 if (recv(s, buf, sizeof(buf), 0) == -1) 126 err(1, "recv"); 127 128 return 0; 129 } 130