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