xref: /freebsd/share/examples/libifconfig/setdescription.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
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 
27*b1d757bcSAlan Somers #include <arpa/inet.h>
28*b1d757bcSAlan Somers #include <net/if.h>
29*b1d757bcSAlan Somers 
30ec214349SKristof Provost #include <err.h>
31ec214349SKristof Provost #include <errno.h>
32ec214349SKristof Provost #include <stdio.h>
33ec214349SKristof Provost #include <stdlib.h>
34ec214349SKristof Provost #include <string.h>
35ec214349SKristof Provost #include <libifconfig.h>
36ec214349SKristof Provost 
37ec214349SKristof Provost 
389a2ff315SKristof Provost int
main(int argc,char * argv[])399a2ff315SKristof Provost main(int argc, char *argv[])
40ec214349SKristof Provost {
419a2ff315SKristof Provost 	char *ifname, *ifdescr, *curdescr;
42*b1d757bcSAlan Somers 	ifconfig_handle_t *lifh;
439a2ff315SKristof Provost 
44ec214349SKristof Provost 	if (argc != 3) {
45ec214349SKristof Provost 		errx(EINVAL, "Invalid number of arguments."
46ec214349SKristof Provost 		    " First argument should be interface name, second argument"
47ec214349SKristof Provost 		    " should be the description to set.");
48ec214349SKristof Provost 	}
49ec214349SKristof Provost 
50ec214349SKristof Provost 	/* We have a static number of arguments. Therefore we can do it simple. */
51ec214349SKristof Provost 	ifname = strdup(argv[1]);
52ec214349SKristof Provost 	ifdescr = strdup(argv[2]);
53ec214349SKristof Provost 	curdescr = NULL;
54ec214349SKristof Provost 
55ec214349SKristof Provost 	printf("Interface name: %s\n", ifname);
56ec214349SKristof Provost 
57*b1d757bcSAlan Somers 	lifh = ifconfig_open();
58*b1d757bcSAlan Somers 	if (lifh == NULL) {
59*b1d757bcSAlan Somers 		errx(ENOMEM, "Failed to open libifconfig handle.");
60*b1d757bcSAlan Somers 		return (-1);
61*b1d757bcSAlan Somers 	}
62*b1d757bcSAlan Somers 
63ec214349SKristof Provost 	if (ifconfig_get_description(lifh, ifname, &curdescr) == 0) {
64ec214349SKristof Provost 		printf("Old description: %s\n", curdescr);
65ec214349SKristof Provost 	}
66ec214349SKristof Provost 
67ec214349SKristof Provost 	printf("New description: %s\n\n", ifdescr);
68ec214349SKristof Provost 
69ec214349SKristof Provost 	if (ifconfig_set_description(lifh, ifname, ifdescr) == 0) {
70ec214349SKristof Provost 		printf("New description successfully set.\n");
71ec214349SKristof Provost 	} else {
72ec214349SKristof Provost 		switch (ifconfig_err_errtype(lifh)) {
73ec214349SKristof Provost 		case SOCKET:
74ec214349SKristof Provost 			err(ifconfig_err_errno(lifh), "Socket error");
75ec214349SKristof Provost 			break;
76ec214349SKristof Provost 		case IOCTL:
77ec214349SKristof Provost 			err(ifconfig_err_errno(
78ec214349SKristof Provost 				    lifh), "IOCTL(%lu) error",
79ec214349SKristof Provost 			    ifconfig_err_ioctlreq(lifh));
80ec214349SKristof Provost 			break;
81*b1d757bcSAlan Somers 		default:
82ec214349SKristof Provost 			err(ifconfig_err_errno(lifh), "Other error");
83ec214349SKristof Provost 			break;
84ec214349SKristof Provost 		}
85ec214349SKristof Provost 	}
86ec214349SKristof Provost 
87ec214349SKristof Provost 	free(ifname);
88ec214349SKristof Provost 	free(ifdescr);
89ec214349SKristof Provost 	free(curdescr);
90ec214349SKristof Provost 	ifname = NULL;
91ec214349SKristof Provost 	ifdescr = NULL;
92ec214349SKristof Provost 	curdescr = NULL;
93ec214349SKristof Provost 
94ec214349SKristof Provost 	ifconfig_close(lifh);
95ec214349SKristof Provost 	return (0);
96ec214349SKristof Provost }
97