Port ifdown.c to FreeBSD. Patch from Guillem Jover and Debian.

This commit is contained in:
Petter Reinholdtsen 2014-02-07 17:55:37 +00:00
parent 58b3b75728
commit 303f3465f6
2 changed files with 52 additions and 21 deletions

View File

@ -81,6 +81,7 @@ sysvinit (2.89dsf) UNRELEASED; urgency=low
from Johannes Truschnigg and Debian. from Johannes Truschnigg and Debian.
* Fix typo in fstab-decode(8) font escape. Patch from Bjarni Ingi * Fix typo in fstab-decode(8) font escape. Patch from Bjarni Ingi
Gislason and Debian. Gislason and Debian.
* Port ifdown.c to FreeBSD. Patch from Guillem Jover and Debian.
-- Petter Reinholdtsen <pere@hungry.com> Sun Apr 11 11:28:55 CEST 2010 -- Petter Reinholdtsen <pere@hungry.com> Sun Apr 11 11:28:55 CEST 2010

View File

@ -37,6 +37,23 @@ char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl";
#define MAX_IFS 64 #define MAX_IFS 64
/* XXX: Ideally this would get detected at configure time... */
#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || \
defined(__NetBSD__) || defined(__OpenBSD__)
#define HAVE_SOCKADDR_SA_LEN 1
#endif
#ifndef _SIZEOF_ADDR_IFREQ
#ifdef HAVE_SOCKADDR_SA_LEN
#define _SIZEOF_ADDR_IFREQ(ifr) \
((ifr).ifr_addr.sa_len > sizeof(struct sockaddr) ? \
(sizeof((ifr).ifr_name) + (ifr).ifr_addr.sa_len) : \
sizeof(struct ifreq))
#else
#define _SIZEOF_ADDR_IFREQ(ifr) sizeof(struct ifreq)
#endif
#endif
/* /*
* First, we find all shaper devices and down them. Then we * First, we find all shaper devices and down them. Then we
* down all real interfaces. This is because the comment in the * down all real interfaces. This is because the comment in the
@ -45,10 +62,10 @@ char *v_ifdown = "@(#)ifdown.c 1.11 02-Jun-1998 miquels@cistron.nl";
*/ */
int ifdown(void) int ifdown(void)
{ {
struct ifreq ifr[MAX_IFS]; char ifr_buf[sizeof(struct ifreq) * MAX_IFS];
char *ifr_end;
struct ifconf ifc; struct ifconf ifc;
int i, fd; int fd;
int numif;
int shaper; int shaper;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
@ -56,8 +73,8 @@ int ifdown(void)
perror("socket"); perror("socket");
return -1; return -1;
} }
ifc.ifc_len = sizeof(ifr); ifc.ifc_len = sizeof(ifr_buf);
ifc.ifc_req = ifr; ifc.ifc_buf = ifr_buf;
if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) { if (ioctl(fd, SIOCGIFCONF, &ifc) < 0) {
fprintf(stderr, "ifdown: "); fprintf(stderr, "ifdown: ");
@ -65,42 +82,55 @@ int ifdown(void)
close(fd); close(fd);
return -1; return -1;
} }
numif = ifc.ifc_len / sizeof(struct ifreq); ifr_end = ifr_buf + ifc.ifc_len;
for (shaper = 1; shaper >= 0; shaper--) { for (shaper = 1; shaper >= 0; shaper--) {
for (i = 0; i < numif; i++) { char *ifr_next = ifr_buf;
if ((strncmp(ifr[i].ifr_name, "shaper", 6) == 0) while (ifr_next < ifr_end) {
struct ifreq *ifr;
int flags;
ifr = (struct ifreq *)ifr_next;
ifr_next += _SIZEOF_ADDR_IFREQ(*ifr);
if ((strncmp(ifr->ifr_name, "shaper", 6) == 0)
!= shaper) continue; != shaper) continue;
if (strcmp(ifr[i].ifr_name, "lo") == 0) if (strncmp(ifr->ifr_name, "lo", 2) == 0)
continue; continue;
if (strchr(ifr[i].ifr_name, ':') != NULL) if (strchr(ifr->ifr_name, ':') != NULL)
continue; continue;
/* Read interface flags */ /* Read interface flags */
if (ioctl(fd, SIOCGIFFLAGS, &ifr[i]) < 0) { if (ioctl(fd, SIOCGIFFLAGS, ifr) < 0) {
fprintf(stderr, "ifdown: shutdown "); fprintf(stderr, "ifdown: shutdown ");
perror(ifr[i].ifr_name); perror(ifr->ifr_name);
continue; continue;
} }
/* /*
* Expected in <net/if.h> according to * Expected in <net/if.h> according to
* "UNIX Network Programming". * "UNIX Network Programming".
*/ */
#ifdef ifr_flags #ifdef ifr_flagshigh
# define IRFFLAGS ifr_flags flags = (ifr->ifr_flags & 0xffff) |
#else /* Present on kFreeBSD */ (ifr->ifr_flagshigh << 16);
# define IRFFLAGS ifr_flagshigh #else
flags = ifr->ifr_flags;
#endif #endif
if (ifr[i].IRFFLAGS & IFF_UP) { if (flags & IFF_UP) {
ifr[i].IRFFLAGS &= ~(IFF_UP); flags &= ~(IFF_UP);
if (ioctl(fd, SIOCSIFFLAGS, &ifr[i]) < 0) { #ifdef ifr_flagshigh
ifr->ifr_flags = flags & 0xffff;
ifr->ifr_flagshigh = flags >> 16;
#else
ifr->ifr_flags = flags;
#endif
if (ioctl(fd, SIOCSIFFLAGS, ifr) < 0) {
fprintf(stderr, "ifdown: shutdown "); fprintf(stderr, "ifdown: shutdown ");
perror(ifr[i].ifr_name); perror(ifr->ifr_name);
} }
} }
#undef IRFFLAGS
} }
} }
close(fd); close(fd);