summaryrefslogtreecommitdiffstats
path: root/regress/sys/netinet/ipsec/nonxt-sendrecv.c
blob: 2b92fdc4fb5bb4e51d9d273812c1977e73a9ae03 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*	$OpenBSD: nonxt-sendrecv.c,v 1.2 2018/05/21 01:19:21 bluhm Exp $	*/
/*
 * Copyright (c) Alexander Bluhm <bluhm@genua.de>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <sys/types.h>
#include <sys/socket.h>

#include <netdb.h>

#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void __dead usage(void);

void
usage(void)
{
	fprintf(stderr, "usage: nonxt-sendrecv [localaddr] remoteaddr\n"
	    "Send empty protocol 59 packet and wait for answer.\n");
	exit(1);
}

int
main(int argc, char *argv[])
{
	struct addrinfo hints, *res, *res0;
	struct timeval to;
	const char *cause = NULL, *local, *remote;
	int error;
	int save_errno;
	int s;
	char buf[1024];

	switch (argc) {
	case 2:
		local = NULL;
		remote = argv[1];
		break;
	case 3:
		local = argv[1];
		remote = argv[2];
		break;
	default:
		usage();
	}

	if (pledge("stdio inet dns", NULL) == -1)
		err(1, "pledge");

	/* Create socket and connect it to remote address. */
	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;
	hints.ai_socktype = SOCK_RAW;
	hints.ai_protocol = IPPROTO_NONE;
	error = getaddrinfo(remote, NULL, &hints, &res0);
	if (error)
		errx(1, "getaddrinfo remote: %s", gai_strerror(error));
	for (res = res0; res; res = res->ai_next) {
		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
		if (s == -1) {
			cause = "socket";
			continue;
		}
		if (connect(s, res->ai_addr, res->ai_addrlen) == -1) {
			cause = "connect";
			save_errno = errno;
			close(s);
			errno = save_errno;
			continue;
		}
		break;
	}
	if (res == NULL)
		err(1, "%s", cause);

	/* Optionally bind the socket to local address. */
	if (local != NULL) {
		memset(&hints, 0, sizeof(hints));
		hints.ai_family = res->ai_family;
		hints.ai_socktype = SOCK_RAW;
		hints.ai_protocol = IPPROTO_NONE;
		hints.ai_flags = AI_PASSIVE;
		freeaddrinfo(res0);
		error = getaddrinfo(local, NULL, &hints, &res0);
		if (error)
			errx(1, "getaddrinfo local: %s", gai_strerror(error));
		for (res = res0; res; res = res->ai_next) {
			if (bind(s, res->ai_addr, res->ai_addrlen) == -1)
				continue;
			break;
		}
		if (res == NULL)
			err(1, "bind");
	}
	freeaddrinfo(res0);

	if (pledge("stdio inet", NULL) == -1)
		err(1, "pledge");

	/* Send a protocol 59 packet. */
	if (send(s, buf, 0, 0) == -1)
		err(1, "send");
	/* Wait for up to 3 seconds to receive a reply packet. */
	to.tv_sec = 3;
	to.tv_usec = 0;
	if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &to, sizeof(to)) == -1)
		err(1, "setsockopt");
	if (recv(s, buf, sizeof(buf), 0) == -1)
		err(1, "recv");

	return 0;
}