1 /*
2  * Copyright (c) 2017 Alexander Bluhm <bluhm@openbsd.org>
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15  */
16 
17 #include <sys/socket.h>
18 
19 #include <netinet/in.h>
20 #include <netinet/ip.h>
21 #include <netinet/ip6.h>
22 #include <netinet/tcp.h>
23 #include <netinet/udp.h>
24 #include <netinet/icmp6.h>
25 
26 #include <err.h>
27 #include <errno.h>
28 #include <string.h>
29 #include <unistd.h>
30 
31 int
main(void)32 main(void)
33 {
34 	char promises[1024];
35 	size_t i, l;
36 	int s, r;
37 	int optval;
38 	socklen_t optlen;
39 
40 	if (strcmp(PROMISES, "0") != 0) {
41 		l = strlcpy(promises, "stdio ", sizeof(promises));
42 		for (i = 0;
43 		    PROMISES[i] != '\0'&& l < sizeof(promises);
44 		    i++, l++) {
45 			promises[l] = PROMISES[i] == '+' ?
46 			    ' ' : PROMISES[i];
47 		}
48 		if (l >= sizeof(promises))
49 			l = sizeof(promises) - 1;
50 		promises[l] = '\0';
51 		warnx("pledge(%s)", promises);
52 		if (pledge(promises, NULL) == -1)
53 			err(1, "pledge");
54 	}
55 
56 	if ((s = socket(DOMAIN, TYPE, PROTOCOL)) == -1)
57 		err(1, "socket");
58 	optlen = sizeof(int);
59 	if (strcmp(CALL, "set") == 0) {
60 		optval = OPTVAL;
61 		r = setsockopt(s, LEVEL, OPTNAME, &optval, optlen);
62 	} else if (strcmp(CALL, "get") == 0) {
63 		optval = 0;
64 		r = getsockopt(s, LEVEL, OPTNAME, &optval, &optlen);
65 	} else {
66 		errx(1, "call: %s", CALL);
67 	}
68 	if (r == 0) {
69 		if (ERRNO != 0)
70 			errx(1, "success");
71 	} else if (r == -1) {
72 		if (errno != ERRNO)
73 			err(1, "error");
74 	} else {
75 		errx(1, "return: %d", r);
76 	}
77 	if (optval != OPTVAL)
78 		errx(1, "optval: %d", optval);
79 
80 	return (0);
81 }
82