xref: /openbsd/regress/usr.sbin/ospfd/opentap.c (revision da1b64c4)
1*da1b64c4Sbluhm /*	$OpenBSD: opentap.c,v 1.1 2016/09/28 12:40:35 bluhm Exp $ */
2*da1b64c4Sbluhm 
3*da1b64c4Sbluhm /*
4*da1b64c4Sbluhm  * Copyright (c) 2014 Alexander Bluhm <bluhm@openbsd.org>
5*da1b64c4Sbluhm  *
6*da1b64c4Sbluhm  * Permission to use, copy, modify, and distribute this software for any
7*da1b64c4Sbluhm  * purpose with or without fee is hereby granted, provided that the above
8*da1b64c4Sbluhm  * copyright notice and this permission notice appear in all copies.
9*da1b64c4Sbluhm  *
10*da1b64c4Sbluhm  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*da1b64c4Sbluhm  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*da1b64c4Sbluhm  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*da1b64c4Sbluhm  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*da1b64c4Sbluhm  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*da1b64c4Sbluhm  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*da1b64c4Sbluhm  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*da1b64c4Sbluhm  */
18*da1b64c4Sbluhm 
19*da1b64c4Sbluhm #include <ctype.h>
20*da1b64c4Sbluhm #include <err.h>
21*da1b64c4Sbluhm #include <fcntl.h>
22*da1b64c4Sbluhm #include <limits.h>
23*da1b64c4Sbluhm #include <stdio.h>
24*da1b64c4Sbluhm #include <stdlib.h>
25*da1b64c4Sbluhm #include <string.h>
26*da1b64c4Sbluhm 
27*da1b64c4Sbluhm #include <sys/socket.h>
28*da1b64c4Sbluhm 
29*da1b64c4Sbluhm void usage(void);
30*da1b64c4Sbluhm 
31*da1b64c4Sbluhm void
usage(void)32*da1b64c4Sbluhm usage(void)
33*da1b64c4Sbluhm {
34*da1b64c4Sbluhm 	fprintf(stderr, "usage: sudo %s fd# tap#\n", getprogname());
35*da1b64c4Sbluhm 	fprintf(stderr, "  fd#   number of file descriptor for fd passing\n");
36*da1b64c4Sbluhm 	fprintf(stderr, "  tap#  number of tap device to open\n");
37*da1b64c4Sbluhm 	exit(2);
38*da1b64c4Sbluhm }
39*da1b64c4Sbluhm 
40*da1b64c4Sbluhm int
main(int argc,char * argv[])41*da1b64c4Sbluhm main(int argc, char *argv[])
42*da1b64c4Sbluhm {
43*da1b64c4Sbluhm 	int		 fd, tap;
44*da1b64c4Sbluhm 	char		 dev[FILENAME_MAX];
45*da1b64c4Sbluhm 	const char	*errstr;
46*da1b64c4Sbluhm 	struct msghdr	 msg;
47*da1b64c4Sbluhm 	struct cmsghdr	*cmsg;
48*da1b64c4Sbluhm 	union {
49*da1b64c4Sbluhm 		struct cmsghdr	 hdr;
50*da1b64c4Sbluhm 		unsigned char	 buf[CMSG_SPACE(sizeof(int))];
51*da1b64c4Sbluhm 	} cmsgbuf;
52*da1b64c4Sbluhm 
53*da1b64c4Sbluhm 	if (argc != 3)
54*da1b64c4Sbluhm 		usage();
55*da1b64c4Sbluhm 
56*da1b64c4Sbluhm 	fd = strtonum(argv[1], 0, INT_MAX, &errstr);
57*da1b64c4Sbluhm 	if (errstr)
58*da1b64c4Sbluhm 		errx(2, "file descriptor number %s: %s", errstr, argv[1]);
59*da1b64c4Sbluhm 	tap = strtonum(argv[2], 0, INT_MAX, &errstr);
60*da1b64c4Sbluhm 	if (errstr)
61*da1b64c4Sbluhm 		errx(2, "tap device number %s: %s", errstr, argv[2]);
62*da1b64c4Sbluhm 	snprintf(dev, FILENAME_MAX, "/dev/tap%d", tap);
63*da1b64c4Sbluhm 
64*da1b64c4Sbluhm 	if ((tap = open(dev, O_RDWR)) == -1)
65*da1b64c4Sbluhm 		err(1, "open %s", dev);
66*da1b64c4Sbluhm 
67*da1b64c4Sbluhm 	memset(&msg, 0, sizeof(msg));
68*da1b64c4Sbluhm 	msg.msg_control = &cmsgbuf.buf;
69*da1b64c4Sbluhm 	msg.msg_controllen = sizeof(cmsgbuf.buf);
70*da1b64c4Sbluhm 
71*da1b64c4Sbluhm 	cmsg = CMSG_FIRSTHDR(&msg);
72*da1b64c4Sbluhm 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
73*da1b64c4Sbluhm 	cmsg->cmsg_level = SOL_SOCKET;
74*da1b64c4Sbluhm 	cmsg->cmsg_type = SCM_RIGHTS;
75*da1b64c4Sbluhm 	*(int *)CMSG_DATA(cmsg) = tap;
76*da1b64c4Sbluhm 
77*da1b64c4Sbluhm 	if (sendmsg(fd, &msg, 0) == -1)
78*da1b64c4Sbluhm 		err(1, "sendmsg %d", fd);
79*da1b64c4Sbluhm 
80*da1b64c4Sbluhm 	return 0;
81*da1b64c4Sbluhm }
82