xref: /openbsd/regress/sys/netinet/recvmsg/recvmsg.c (revision d89ec533)
1 /*	$OpenBSD: recvmsg.c,v 1.2 2018/07/08 02:18:51 anton Exp $	*/
2 /*
3  * Copyright (c) 2018 Anton Lindqvist <anton@openbsd.org>
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 /*
19  * Regression test for double free of mbuf caused by rip{6,}_usrreq().
20  */
21 
22 #include <sys/socket.h>
23 
24 #include <assert.h>
25 #include <err.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 
32 static __dead void usage(void);
33 
34 int
35 main(int argc, char *argv[])
36 {
37 	struct msghdr msg;
38 	ssize_t n;
39 	int c, s;
40 	int domain = -1;
41 	int type = -1;
42 
43 	while ((c = getopt(argc, argv, "46dr")) != -1)
44 		switch (c) {
45 		case '4':
46 			domain = AF_INET;
47 			break;
48 		case '6':
49 			domain = AF_INET6;
50 			break;
51 		case 'd':
52 			type = SOCK_DGRAM;
53 			break;
54 		case 'r':
55 			type = SOCK_RAW;
56 			break;
57 		default:
58 			usage();
59 		}
60 	argc -= optind;
61 	argv += optind;
62 	if (argc > 0 || domain == -1 || type == -1)
63 		usage();
64 
65 	s = socket(domain, type, 0);
66 	if (s == -1)
67 		err(1, "socket");
68 	memset(&msg, 0, sizeof(msg));
69 	n = recvmsg(s, &msg, MSG_OOB);
70 	assert(n == -1);
71 	assert(errno == EOPNOTSUPP);
72 	close(s);
73 
74 	return 0;
75 }
76 
77 static __dead void
78 usage(void)
79 {
80 	fprintf(stderr, "usage: recvmsg [-46dr]\n");
81 	exit(1);
82 }
83