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 * $FreeBSD$ 27ec214349SKristof Provost */ 28ec214349SKristof Provost 29ec214349SKristof Provost #include <err.h> 30ec214349SKristof Provost #include <errno.h> 31ec214349SKristof Provost #include <net/if.h> 32ec214349SKristof Provost #include <sys/ioctl.h> 33ec214349SKristof Provost #include <stdio.h> 34ec214349SKristof Provost #include <stdlib.h> 35ec214349SKristof Provost #include <string.h> 36ec214349SKristof Provost #include <libifconfig.h> 37ec214349SKristof Provost 38ec214349SKristof Provost 399a2ff315SKristof Provost int 409a2ff315SKristof Provost main(int argc, char *argv[]) 41ec214349SKristof Provost { 429a2ff315SKristof Provost char *ifname; 43*b1d757bcSAlan Somers ifconfig_handle_t *lifh; 449a2ff315SKristof Provost 45ec214349SKristof Provost if (argc != 2) { 46ec214349SKristof Provost errx(EINVAL, "Invalid number of arguments." 47ec214349SKristof Provost " Only one argument is accepted, and it should be the name" 48ec214349SKristof Provost " of the interface to be destroyed."); 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 54ec214349SKristof Provost printf("Interface name: %s\n", ifname); 55ec214349SKristof Provost 56*b1d757bcSAlan Somers lifh = ifconfig_open(); 57*b1d757bcSAlan Somers if (lifh == NULL) { 58*b1d757bcSAlan Somers errx(ENOMEM, "Failed to open libifconfig handle."); 59*b1d757bcSAlan Somers return (-1); 60*b1d757bcSAlan Somers } 61*b1d757bcSAlan Somers 62ec214349SKristof Provost if (ifconfig_destroy_interface(lifh, ifname) == 0) { 63ec214349SKristof Provost printf("Successfully destroyed interface '%s'.", ifname); 64ec214349SKristof Provost ifconfig_close(lifh); 65ec214349SKristof Provost lifh = NULL; 66ec214349SKristof Provost free(ifname); 67ec214349SKristof Provost return (0); 689a2ff315SKristof Provost } 699a2ff315SKristof Provost 70ec214349SKristof Provost switch (ifconfig_err_errtype(lifh)) { 71ec214349SKristof Provost case SOCKET: 72ec214349SKristof Provost warnx("couldn't create socket. This shouldn't happen.\n"); 73ec214349SKristof Provost break; 74ec214349SKristof Provost case IOCTL: 75ec214349SKristof Provost if (ifconfig_err_ioctlreq(lifh) == SIOCIFDESTROY) { 76ec214349SKristof Provost warnx( 77ec214349SKristof Provost "Failed to destroy interface (SIOCIFDESTROY)\n"); 78ec214349SKristof Provost } 79ec214349SKristof Provost break; 80ec214349SKristof Provost default: 81ec214349SKristof Provost warnx( 82ec214349SKristof Provost "Should basically never end up here in this example.\n"); 83ec214349SKristof Provost break; 84ec214349SKristof Provost } 85ec214349SKristof Provost 86ec214349SKristof Provost ifconfig_close(lifh); 87ec214349SKristof Provost lifh = NULL; 88ec214349SKristof Provost free(ifname); 89ec214349SKristof Provost return (-1); 90ec214349SKristof Provost } 91