1a5e10afkp/*
20d688beasomers * Copyright (c) 2016-2017, Marie Helene Kvello-Aune
3a5e10afkp * All rights reserved.
4a5e10afkp *
5a5e10afkp * Redistribution and use in source and binary forms, with or without modification,
6a5e10afkp * are permitted provided that the following conditions are met:
7a5e10afkp *
8a5e10afkp * 1. Redistributions of source code must retain the above copyright notice,
9a5e10afkp * thislist of conditions and the following disclaimer.
10a5e10afkp *
11a5e10afkp * 2. Redistributions in binary form must reproduce the above copyright notice,
12a5e10afkp * this list of conditions and the following disclaimer in the documentation and/or
13a5e10afkp * other materials provided with the distribution.
14a5e10afkp *
15a5e10afkp * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16a5e10afkp * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17a5e10afkp * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18a5e10afkp * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19a5e10afkp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20a5e10afkp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21a5e10afkp * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22a5e10afkp * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23a5e10afkp * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24a5e10afkp * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a5e10afkp *
26a5e10afkp * $FreeBSD$
27a5e10afkp */
28a5e10afkp
29a5e10afkp#include <err.h>
30a5e10afkp#include <errno.h>
31a5e10afkp#include <net/if.h>
32a5e10afkp#include <sys/ioctl.h>
33a5e10afkp#include <stdio.h>
34a5e10afkp#include <stdlib.h>
35a5e10afkp#include <string.h>
36e5074fakp#include <libifconfig.h>
37a5e10afkp
38a5e10afkp
390725653kpint
400725653kpmain(int argc, char *argv[])
41a5e10afkp{
420725653kp	char *ifname, *ifactualname;
430d688beasomers	ifconfig_handle_t *lifh;
440725653kp
45a5e10afkp	if (argc != 2) {
46a5e10afkp		errx(EINVAL, "Invalid number of arguments."
47a5e10afkp		    " Only one argument is accepted, and it should be the name"
48a5e10afkp		    " of the interface to be created.");
49a5e10afkp	}
50a5e10afkp
51a5e10afkp	/* We have a static number of arguments. Therefore we can do it simple. */
52a5e10afkp	ifname = strdup(argv[1]);
53a5e10afkp
54a5e10afkp	printf("Requested interface name: %s\n", ifname);
55a5e10afkp
560d688beasomers	lifh = ifconfig_open();
570d688beasomers	if (lifh == NULL) {
580d688beasomers		errx(ENOMEM, "Failed to open libifconfig handle.");
590d688beasomers		return (-1);
600d688beasomers	}
610d688beasomers
62e5074fakp	if (ifconfig_create_interface(lifh, ifname, &ifactualname) == 0) {
63a5e10afkp		printf("Successfully created interface '%s'\n", ifactualname);
64e5074fakp		ifconfig_close(lifh);
65a5e10afkp		lifh = NULL;
66a5e10afkp		free(ifname);
67a5e10afkp		free(ifactualname);
68a5e10afkp		return (0);
690725653kp	}
700725653kp
710725653kp	switch (ifconfig_err_errtype(lifh)) {
720725653kp	case SOCKET:
730725653kp		warnx("couldn't create socket. This shouldn't happen.\n");
740725653kp		break;
750725653kp	case IOCTL:
760725653kp		if (ifconfig_err_ioctlreq(lifh) == SIOCIFCREATE2) {
77a5e10afkp			warnx(
780725653kp				"Failed to create interface (SIOCIFCREATE2)\n");
79a5e10afkp		}
800725653kp		break;
810725653kp	default:
820725653kp		warnx(
830725653kp			"This is a thorough example accommodating for temporary"
840725653kp			" 'not implemented yet' errors. That's likely what happened"
850725653kp			" now. If not, your guess is as good as mine. ;)"
860725653kp			" Error code: %d\n", ifconfig_err_errno(
870725653kp				lifh));
880725653kp		break;
89a5e10afkp	}
900725653kp
910725653kp	ifconfig_close(lifh);
920725653kp	lifh = NULL;
930725653kp	free(ifname);
940725653kp	free(ifactualname);
950725653kp	return (-1);
96a5e10afkp}
97