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