]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - share/examples/libifconfig/setdescription.c
Upgrade Unbound to 1.8.0. More to follow.
[FreeBSD/FreeBSD.git] / share / examples / libifconfig / setdescription.c
1 /*
2  * Copyright (c) 2016-2017, 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  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
17  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * $FreeBSD$
27  */
28
29 #include <arpa/inet.h>
30 #include <net/if.h>
31
32 #include <err.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <libifconfig.h>
38
39
40 int
41 main(int argc, char *argv[])
42 {
43         char *ifname, *ifdescr, *curdescr;
44         ifconfig_handle_t *lifh;
45
46         if (argc != 3) {
47                 errx(EINVAL, "Invalid number of arguments."
48                     " First argument should be interface name, second argument"
49                     " should be the description to set.");
50         }
51
52         /* We have a static number of arguments. Therefore we can do it simple. */
53         ifname = strdup(argv[1]);
54         ifdescr = strdup(argv[2]);
55         curdescr = NULL;
56
57         printf("Interface name: %s\n", ifname);
58
59         lifh = ifconfig_open();
60         if (lifh == NULL) {
61                 errx(ENOMEM, "Failed to open libifconfig handle.");
62                 return (-1);
63         }
64
65         if (ifconfig_get_description(lifh, ifname, &curdescr) == 0) {
66                 printf("Old description: %s\n", curdescr);
67         }
68
69         printf("New description: %s\n\n", ifdescr);
70
71         if (ifconfig_set_description(lifh, ifname, ifdescr) == 0) {
72                 printf("New description successfully set.\n");
73         } else {
74                 switch (ifconfig_err_errtype(lifh)) {
75                 case SOCKET:
76                         err(ifconfig_err_errno(lifh), "Socket error");
77                         break;
78                 case IOCTL:
79                         err(ifconfig_err_errno(
80                                     lifh), "IOCTL(%lu) error",
81                             ifconfig_err_ioctlreq(lifh));
82                         break;
83                 default:
84                         err(ifconfig_err_errno(lifh), "Other error");
85                         break;
86                 }
87         }
88
89         free(ifname);
90         free(ifdescr);
91         free(curdescr);
92         ifname = NULL;
93         ifdescr = NULL;
94         curdescr = NULL;
95
96         ifconfig_close(lifh);
97         return (0);
98 }