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
290d688beasomers#include <arpa/inet.h>
300d688beasomers#include <net/if.h>
310d688beasomers
32a5e10afkp#include <err.h>
33a5e10afkp#include <errno.h>
34a5e10afkp#include <stdio.h>
35a5e10afkp#include <stdlib.h>
36a5e10afkp#include <string.h>
37e5074fakp#include <libifconfig.h>
38a5e10afkp
39a5e10afkp
400725653kpint
410725653kpmain(int argc, char *argv[])
42a5e10afkp{
430725653kp	char *ifname, *ifdescr, *curdescr;
440d688beasomers	ifconfig_handle_t *lifh;
450725653kp
46a5e10afkp	if (argc != 3) {
47a5e10afkp		errx(EINVAL, "Invalid number of arguments."
48a5e10afkp		    " First argument should be interface name, second argument"
49a5e10afkp		    " should be the description to set.");
50a5e10afkp	}
51a5e10afkp
52a5e10afkp	/* We have a static number of arguments. Therefore we can do it simple. */
53a5e10afkp	ifname = strdup(argv[1]);
54a5e10afkp	ifdescr = strdup(argv[2]);
55a5e10afkp	curdescr = NULL;
56a5e10afkp
57a5e10afkp	printf("Interface name: %s\n", ifname);
58a5e10afkp
590d688beasomers	lifh = ifconfig_open();
600d688beasomers	if (lifh == NULL) {
610d688beasomers		errx(ENOMEM, "Failed to open libifconfig handle.");
620d688beasomers		return (-1);
630d688beasomers	}
640d688beasomers
65e5074fakp	if (ifconfig_get_description(lifh, ifname, &curdescr) == 0) {
66a5e10afkp		printf("Old description: %s\n", curdescr);
67a5e10afkp	}
68a5e10afkp
69a5e10afkp	printf("New description: %s\n\n", ifdescr);
70a5e10afkp
71e5074fakp	if (ifconfig_set_description(lifh, ifname, ifdescr) == 0) {
72a5e10afkp		printf("New description successfully set.\n");
73a5e10afkp	} else {
74e5074fakp		switch (ifconfig_err_errtype(lifh)) {
75a5e10afkp		case SOCKET:
76e5074fakp			err(ifconfig_err_errno(lifh), "Socket error");
77a5e10afkp			break;
78a5e10afkp		case IOCTL:
79e5074fakp			err(ifconfig_err_errno(
80a5e10afkp				    lifh), "IOCTL(%lu) error",
81e5074fakp			    ifconfig_err_ioctlreq(lifh));
82a5e10afkp			break;
830d688beasomers		default:
84e5074fakp			err(ifconfig_err_errno(lifh), "Other error");
85a5e10afkp			break;
86a5e10afkp		}
87a5e10afkp	}
88a5e10afkp
89a5e10afkp	free(ifname);
90a5e10afkp	free(ifdescr);
91a5e10afkp	free(curdescr);
92a5e10afkp	ifname = NULL;
93a5e10afkp	ifdescr = NULL;
94a5e10afkp	curdescr = NULL;
95a5e10afkp
96e5074fakp	ifconfig_close(lifh);
97a5e10afkp	return (0);
98a5e10afkp}
99