xref: /freebsd/share/examples/libifconfig/ifcreate.c (revision 9a2ff3154b811a8210a7eca5531e233c69ed5924)
1ec214349SKristof Provost /*
2ec214349SKristof Provost  * Copyright (c) 2016, Marie Helene Kvello-Aune
3ec214349SKristof Provost  * All rights reserved.
4ec214349SKristof Provost  *
5ec214349SKristof Provost  * Redistribution and use in source and binary forms, with or without modification,
6ec214349SKristof Provost  * are permitted provided that the following conditions are met:
7ec214349SKristof Provost  *
8ec214349SKristof Provost  * 1. Redistributions of source code must retain the above copyright notice,
9ec214349SKristof Provost  * thislist of conditions and the following disclaimer.
10ec214349SKristof Provost  *
11ec214349SKristof Provost  * 2. Redistributions in binary form must reproduce the above copyright notice,
12ec214349SKristof Provost  * this list of conditions and the following disclaimer in the documentation and/or
13ec214349SKristof Provost  * other materials provided with the distribution.
14ec214349SKristof Provost  *
15ec214349SKristof Provost  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16ec214349SKristof Provost  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17ec214349SKristof Provost  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18ec214349SKristof Provost  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19ec214349SKristof Provost  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20ec214349SKristof Provost  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21ec214349SKristof Provost  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22ec214349SKristof Provost  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23ec214349SKristof Provost  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24ec214349SKristof Provost  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25ec214349SKristof Provost  *
26ec214349SKristof Provost  * $FreeBSD$
27ec214349SKristof Provost  */
28ec214349SKristof Provost 
29ec214349SKristof Provost #include <err.h>
30ec214349SKristof Provost #include <errno.h>
31ec214349SKristof Provost #include <net/if.h>
32ec214349SKristof Provost #include <sys/ioctl.h>
33ec214349SKristof Provost #include <stdio.h>
34ec214349SKristof Provost #include <stdlib.h>
35ec214349SKristof Provost #include <string.h>
36ec214349SKristof Provost #include <libifconfig.h>
37ec214349SKristof Provost 
38ec214349SKristof Provost 
39*9a2ff315SKristof Provost int
40*9a2ff315SKristof Provost main(int argc, char *argv[])
41ec214349SKristof Provost {
42*9a2ff315SKristof Provost 	char *ifname, *ifactualname;
43*9a2ff315SKristof Provost 
44ec214349SKristof Provost 	if (argc != 2) {
45ec214349SKristof Provost 		errx(EINVAL, "Invalid number of arguments."
46ec214349SKristof Provost 		    " Only one argument is accepted, and it should be the name"
47ec214349SKristof Provost 		    " of the interface to be created.");
48ec214349SKristof Provost 	}
49ec214349SKristof Provost 
50ec214349SKristof Provost 	/* We have a static number of arguments. Therefore we can do it simple. */
51ec214349SKristof Provost 	ifname = strdup(argv[1]);
52ec214349SKristof Provost 
53ec214349SKristof Provost 	printf("Requested interface name: %s\n", ifname);
54ec214349SKristof Provost 
55ec214349SKristof Provost 	ifconfig_handle_t *lifh = ifconfig_open();
56ec214349SKristof Provost 	if (ifconfig_create_interface(lifh, ifname, &ifactualname) == 0) {
57ec214349SKristof Provost 		printf("Successfully created interface '%s'\n", ifactualname);
58ec214349SKristof Provost 		ifconfig_close(lifh);
59ec214349SKristof Provost 		lifh = NULL;
60ec214349SKristof Provost 		free(ifname);
61ec214349SKristof Provost 		free(ifactualname);
62ec214349SKristof Provost 		return (0);
63*9a2ff315SKristof Provost 	}
64*9a2ff315SKristof Provost 
65ec214349SKristof Provost 	switch (ifconfig_err_errtype(lifh)) {
66ec214349SKristof Provost 	case SOCKET:
67ec214349SKristof Provost 		warnx("couldn't create socket. This shouldn't happen.\n");
68ec214349SKristof Provost 		break;
69ec214349SKristof Provost 	case IOCTL:
70ec214349SKristof Provost 		if (ifconfig_err_ioctlreq(lifh) == SIOCIFCREATE2) {
71ec214349SKristof Provost 			warnx(
72ec214349SKristof Provost 				"Failed to create interface (SIOCIFCREATE2)\n");
73ec214349SKristof Provost 		}
74ec214349SKristof Provost 		break;
75ec214349SKristof Provost 	default:
76ec214349SKristof Provost 		warnx(
77ec214349SKristof Provost 			"This is a thorough example accommodating for temporary"
78ec214349SKristof Provost 			" 'not implemented yet' errors. That's likely what happened"
79ec214349SKristof Provost 			" now. If not, your guess is as good as mine. ;)"
80ec214349SKristof Provost 			" Error code: %d\n", ifconfig_err_errno(
81ec214349SKristof Provost 				lifh));
82ec214349SKristof Provost 		break;
83ec214349SKristof Provost 	}
84ec214349SKristof Provost 
85ec214349SKristof Provost 	ifconfig_close(lifh);
86ec214349SKristof Provost 	lifh = NULL;
87ec214349SKristof Provost 	free(ifname);
88ec214349SKristof Provost 	free(ifactualname);
89ec214349SKristof Provost 	return (-1);
90ec214349SKristof Provost }
91