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 *
16ec214349SKristof Provost * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17ec214349SKristof Provost * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18ec214349SKristof Provost * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19ec214349SKristof Provost * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
20ec214349SKristof Provost * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21ec214349SKristof Provost * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22ec214349SKristof Provost * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23ec214349SKristof Provost * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24ec214349SKristof Provost * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25ec214349SKristof Provost * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26ec214349SKristof Provost */
27ec214349SKristof Provost
28ec214349SKristof Provost #include <err.h>
29ec214349SKristof Provost #include <errno.h>
30ec214349SKristof Provost #include <net/if.h>
31ec214349SKristof Provost #include <sys/ioctl.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, *ptr;
429a2ff315SKristof Provost int mtu;
43*b1d757bcSAlan Somers ifconfig_handle_t *lifh;
449a2ff315SKristof Provost
45ec214349SKristof Provost if (argc != 3) {
46ec214349SKristof Provost errx(EINVAL, "Invalid number of arguments."
47ec214349SKristof Provost " First argument should be interface name, second argument"
48ec214349SKristof Provost " should be the MTU to set.");
49ec214349SKristof Provost }
50ec214349SKristof Provost
51ec214349SKristof Provost /* We have a static number of arguments. Therefore we can do it simple. */
52ec214349SKristof Provost ifname = strdup(argv[1]);
53ec214349SKristof Provost mtu = (int)strtol(argv[2], &ptr, 10);
54ec214349SKristof Provost
55ec214349SKristof Provost printf("Interface name: %s\n", ifname);
56ec214349SKristof Provost printf("New MTU: %d", mtu);
57ec214349SKristof Provost
58*b1d757bcSAlan Somers lifh = ifconfig_open();
59*b1d757bcSAlan Somers if (lifh == NULL) {
60*b1d757bcSAlan Somers errx(ENOMEM, "Failed to open libifconfig handle.");
61*b1d757bcSAlan Somers return (-1);
62*b1d757bcSAlan Somers }
63*b1d757bcSAlan Somers
64ec214349SKristof Provost if (ifconfig_set_mtu(lifh, ifname, mtu) == 0) {
65ec214349SKristof Provost printf("Successfully changed MTU of %s to %d\n", ifname, mtu);
66ec214349SKristof Provost ifconfig_close(lifh);
67ec214349SKristof Provost lifh = NULL;
68ec214349SKristof Provost free(ifname);
69ec214349SKristof Provost return (0);
709a2ff315SKristof Provost }
719a2ff315SKristof Provost
72ec214349SKristof Provost switch (ifconfig_err_errtype(lifh)) {
73ec214349SKristof Provost case SOCKET:
74ec214349SKristof Provost warnx("couldn't create socket. This shouldn't happen.\n");
75ec214349SKristof Provost break;
76ec214349SKristof Provost case IOCTL:
77ec214349SKristof Provost if (ifconfig_err_ioctlreq(lifh) == SIOCSIFMTU) {
78ec214349SKristof Provost warnx("Failed to set MTU (SIOCSIFMTU)\n");
79ec214349SKristof Provost } else {
80ec214349SKristof Provost warnx(
81ec214349SKristof Provost "Failed to set MTU due to error in unexpected ioctl() call %lu. Error code: %i.\n",
82ec214349SKristof Provost ifconfig_err_ioctlreq(lifh),
83ec214349SKristof Provost ifconfig_err_errno(lifh));
84ec214349SKristof Provost }
85ec214349SKristof Provost break;
86ec214349SKristof Provost default:
87ec214349SKristof Provost warnx(
88ec214349SKristof Provost "Should basically never end up here in this example.\n");
89ec214349SKristof Provost break;
90ec214349SKristof Provost }
91ec214349SKristof Provost
92ec214349SKristof Provost ifconfig_close(lifh);
93ec214349SKristof Provost lifh = NULL;
94ec214349SKristof Provost free(ifname);
95ec214349SKristof Provost return (-1);
96ec214349SKristof Provost }
97