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