"Fossies" - the Fresh Open Source Software Archive

Member "sysvinit-2.99/src/ifdown.c" (21 Feb 2021, 3547 Bytes) of package /linux/misc/sysvinit-2.99.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ifdown.c" see the Fossies "Dox" file reference documentation and the latest Fossies "Diffs" side-by-side code changes report: 2.98_vs_2.99.

    1 /*
    2  * ifdown.c Find all network interfaces on the system and
    3  *      shut them down.
    4  *
    5  * Copyright (C) 1998 Miquel van Smoorenburg.
    6  *
    7  * This program is free software; you can redistribute it and/or modify
    8  * it under the terms of the GNU General Public License as published by 
    9  * the Free Software Foundation; either version 2 of the License, or 
   10  * (at your option) any later version.
   11  * 
   12  * This program is distributed in the hope that it will be useful, 
   13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
   14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   15  * GNU General Public License for more details.
   16  * 
   17  * You should have received a copy of the GNU General Public License
   18  * along with this program; if not, write to the Free Software
   19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
   20  *
   21  */
   22 char *v_ifdown = "@(#)ifdown.c  1.11  02-Jun-1998  miquels@cistron.nl";
   23 
   24 #include <stdio.h>
   25 #include <stdlib.h>
   26 #include <unistd.h>
   27 #include <time.h>
   28 #include <string.h>
   29 #include <errno.h>
   30 
   31 #include <sys/ioctl.h>
   32 #include <sys/socket.h>
   33 #include <sys/time.h>
   34 
   35 #include <net/if.h>
   36 #include <netinet/in.h>
   37 
   38 #define MAX_IFS 64
   39 
   40 /* XXX: Ideally this would get detected at configure time... */
   41 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
   42     defined(__NetBSD__) || defined(__OpenBSD__)
   43 #define HAVE_SOCKADDR_SA_LEN 1
   44 #endif
   45 
   46 #ifndef _SIZEOF_ADDR_IFREQ
   47 #ifdef HAVE_SOCKADDR_SA_LEN
   48 #define _SIZEOF_ADDR_IFREQ(ifr) \
   49     ((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
   50      (sizeof((ifr).ifr_name) + (ifr).ifr_addr.sa_len) : \
   51       sizeof(struct ifreq))
   52 #else
   53 #define _SIZEOF_ADDR_IFREQ(ifr) sizeof(struct ifreq)
   54 #endif
   55 #endif
   56 
   57 /*
   58  *  First, we find all shaper devices and down them. Then we
   59  *  down all real interfaces. This is because the comment in the
   60  *  shaper driver says "if you down the shaper device before the
   61  *  attached interface your computer will follow".
   62  */
   63 int ifdown(void)
   64 {
   65     char ifr_buf[sizeof(struct ifreq) * MAX_IFS];
   66     char *ifr_end;
   67     struct ifconf ifc;
   68     int fd;
   69     int shaper;
   70 
   71     if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
   72         fprintf(stderr, "ifdown: ");
   73         perror("socket");
   74         return -1;
   75     }
   76     ifc.ifc_len = sizeof(ifr_buf);
   77     ifc.ifc_buf = ifr_buf;
   78 
   79     if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
   80         fprintf(stderr, "ifdown: ");
   81         perror("SIOCGIFCONF");
   82         close(fd);
   83         return -1;
   84     }
   85     ifr_end = ifr_buf + ifc.ifc_len;
   86 
   87     for (shaper = 1; shaper >= 0; shaper--) {
   88         char *ifr_next = ifr_buf;
   89 
   90         while (ifr_next < ifr_end) {
   91             struct ifreq *ifr;
   92             int flags;
   93 
   94             ifr = (struct ifreq *)ifr_next;
   95             ifr_next += _SIZEOF_ADDR_IFREQ(*ifr);
   96 
   97             if ((strncmp(ifr->ifr_name, "shaper", 6) == 0)
   98                 != shaper) continue;
   99 
  100             if (strncmp(ifr->ifr_name, "lo", 2) == 0)
  101                 continue;
  102             if (strchr(ifr->ifr_name, ':') != NULL)
  103                 continue;
  104 
  105             /* Read interface flags */
  106             if (ioctl(fd, SIOCGIFFLAGS, ifr) < 0) {
  107                 fprintf(stderr, "ifdown: shutdown ");
  108                 perror(ifr->ifr_name);
  109                 continue;
  110             }
  111             /*
  112              * Expected in <net/if.h> according to
  113              * "UNIX Network Programming".
  114              */
  115 #ifdef ifr_flagshigh
  116             flags = (ifr->ifr_flags & 0xffff) |
  117                     (ifr->ifr_flagshigh << 16);
  118 #else
  119             flags = ifr->ifr_flags;
  120 #endif
  121             if (flags & IFF_UP) {
  122                 flags &= ~(IFF_UP);
  123 #ifdef ifr_flagshigh
  124                 ifr->ifr_flags = flags & 0xffff;
  125                 ifr->ifr_flagshigh = flags >> 16;
  126 #else
  127                 ifr->ifr_flags = flags;
  128 #endif
  129                 if (ioctl(fd, SIOCSIFFLAGS, ifr) < 0) {
  130                     fprintf(stderr, "ifdown: shutdown ");
  131                     perror(ifr->ifr_name);
  132                 }
  133             }
  134         }
  135     }
  136     close(fd);
  137 
  138     return 0;
  139 }