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