setdescription.c revision e5074fa18e1026973d644cd128dcf5a9e4b415fa
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 <stdio.h>
36#include <stdlib.h>
37#include <string.h>
38#include <libifconfig.h>
39
40
41int main(int argc, char *argv[])
42{
43	if (argc != 3) {
44		errx(EINVAL, "Invalid number of arguments."
45		    " First argument should be interface name, second argument"
46		    " should be the description to set.");
47	}
48
49	char *ifname, *ifdescr, *curdescr;
50	/* We have a static number of arguments. Therefore we can do it simple. */
51	ifname = strdup(argv[1]);
52	ifdescr = strdup(argv[2]);
53	curdescr = NULL;
54
55	printf("Interface name: %s\n", ifname);
56
57	ifconfig_handle_t *lifh = ifconfig_open();
58	if (ifconfig_get_description(lifh, ifname, &curdescr) == 0) {
59		printf("Old description: %s\n", curdescr);
60	}
61
62	printf("New description: %s\n\n", ifdescr);
63
64	if (ifconfig_set_description(lifh, ifname, ifdescr) == 0) {
65		printf("New description successfully set.\n");
66	} else {
67		switch (ifconfig_err_errtype(lifh)) {
68		case SOCKET:
69			err(ifconfig_err_errno(lifh), "Socket error");
70			break;
71		case IOCTL:
72			err(ifconfig_err_errno(
73				    lifh), "IOCTL(%lu) error",
74			    ifconfig_err_ioctlreq(lifh));
75			break;
76		case OTHER:
77			err(ifconfig_err_errno(lifh), "Other error");
78			break;
79		}
80	}
81
82	free(ifname);
83	free(ifdescr);
84	free(curdescr);
85	ifname = NULL;
86	ifdescr = NULL;
87	curdescr = NULL;
88
89	ifconfig_close(lifh);
90	return (0);
91}
92