1ec214349SKristof Provost /*
2*b1d757bcSAlan Somers * Copyright (c) 2016-2017, 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
27ec214349SKristof Provost #include <err.h>
28ec214349SKristof Provost #include <errno.h>
29ec214349SKristof Provost #include <net/if.h>
30ec214349SKristof Provost #include <sys/ioctl.h>
31ec214349SKristof Provost #include <stdio.h>
32ec214349SKristof Provost #include <stdlib.h>
33ec214349SKristof Provost #include <string.h>
34ec214349SKristof Provost #include <libifconfig.h>
35ec214349SKristof Provost
36ec214349SKristof Provost
379a2ff315SKristof Provost int
main(int argc,char * argv[])389a2ff315SKristof Provost main(int argc, char *argv[])
39ec214349SKristof Provost {
409a2ff315SKristof Provost char *ifname, *ifactualname;
41*b1d757bcSAlan Somers ifconfig_handle_t *lifh;
429a2ff315SKristof Provost
43ec214349SKristof Provost if (argc != 2) {
44ec214349SKristof Provost errx(EINVAL, "Invalid number of arguments."
45ec214349SKristof Provost " Only one argument is accepted, and it should be the name"
46ec214349SKristof Provost " of the interface to be created.");
47ec214349SKristof Provost }
48ec214349SKristof Provost
49ec214349SKristof Provost /* We have a static number of arguments. Therefore we can do it simple. */
50ec214349SKristof Provost ifname = strdup(argv[1]);
51ec214349SKristof Provost
52ec214349SKristof Provost printf("Requested interface name: %s\n", ifname);
53ec214349SKristof Provost
54*b1d757bcSAlan Somers lifh = ifconfig_open();
55*b1d757bcSAlan Somers if (lifh == NULL) {
56*b1d757bcSAlan Somers errx(ENOMEM, "Failed to open libifconfig handle.");
57*b1d757bcSAlan Somers return (-1);
58*b1d757bcSAlan Somers }
59*b1d757bcSAlan Somers
60ec214349SKristof Provost if (ifconfig_create_interface(lifh, ifname, &ifactualname) == 0) {
61ec214349SKristof Provost printf("Successfully created interface '%s'\n", ifactualname);
62ec214349SKristof Provost ifconfig_close(lifh);
63ec214349SKristof Provost lifh = NULL;
64ec214349SKristof Provost free(ifname);
65ec214349SKristof Provost free(ifactualname);
66ec214349SKristof Provost return (0);
679a2ff315SKristof Provost }
689a2ff315SKristof Provost
69ec214349SKristof Provost switch (ifconfig_err_errtype(lifh)) {
70ec214349SKristof Provost case SOCKET:
71ec214349SKristof Provost warnx("couldn't create socket. This shouldn't happen.\n");
72ec214349SKristof Provost break;
73ec214349SKristof Provost case IOCTL:
74ec214349SKristof Provost if (ifconfig_err_ioctlreq(lifh) == SIOCIFCREATE2) {
75ec214349SKristof Provost warnx(
76ec214349SKristof Provost "Failed to create interface (SIOCIFCREATE2)\n");
77ec214349SKristof Provost }
78ec214349SKristof Provost break;
79ec214349SKristof Provost default:
80ec214349SKristof Provost warnx(
81ec214349SKristof Provost "This is a thorough example accommodating for temporary"
82ec214349SKristof Provost " 'not implemented yet' errors. That's likely what happened"
83ec214349SKristof Provost " now. If not, your guess is as good as mine. ;)"
84ec214349SKristof Provost " Error code: %d\n", ifconfig_err_errno(
85ec214349SKristof Provost lifh));
86ec214349SKristof Provost break;
87ec214349SKristof Provost }
88ec214349SKristof Provost
89ec214349SKristof Provost ifconfig_close(lifh);
90ec214349SKristof Provost lifh = NULL;
91ec214349SKristof Provost free(ifname);
92ec214349SKristof Provost free(ifactualname);
93ec214349SKristof Provost return (-1);
94ec214349SKristof Provost }
95