From ab121f379a3cff458c90e6f480ba4bb68c8733dd Mon Sep 17 00:00:00 2001 From: Javier Martinez Canillas Date: Sat, 27 Nov 2010 07:49:17 +0100 Subject: Linux Device Drivers 3 examples --- misc-progs/netifdebug.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 misc-progs/netifdebug.c (limited to 'misc-progs/netifdebug.c') diff --git a/misc-progs/netifdebug.c b/misc-progs/netifdebug.c new file mode 100644 index 0000000..6f0ce75 --- /dev/null +++ b/misc-progs/netifdebug.c @@ -0,0 +1,84 @@ +/* + * netifdebug.c -- change the IFF_DEBUG flag of an interface + * + * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet + * Copyright (C) 2001 O'Reilly & Associates + * + * The source code in this file can be freely used, adapted, + * and redistributed in source or binary form, so long as an + * acknowledgment appears in derived source files. The citation + * should list that the code comes from the book "Linux Device + * Drivers" by Alessandro Rubini and Jonathan Corbet, published + * by O'Reilly & Associates. No warranty is attached; + * we cannot take responsibility for errors or fitness for use. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +int main(int argc, char **argv) +{ + int action = -1, sock; + struct ifreq req; + char *actname; + + if (argc < 2) { + fprintf(stderr,"%s: usage is \"%s []\"\n", + argv[0],argv[0]); + exit(1); + } + if (argc==2) + actname="tell"; + else + actname=argv[2]; + + /* a silly raw socket just for ioctl()ling it */ + sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW); + if (sock < 0) { + fprintf(stderr, "%s: socket(): %s\n", argv[0],strerror(errno)); + exit(1); + } + + /* retrieve flags */ + strcpy(req.ifr_name, argv[1]); + if ( ioctl(sock, SIOCGIFFLAGS, &req) < 0) { + fprintf(stderr, " %s: ioctl(SIOCGIFFLAGS): %s\n", + argv[0],strerror(errno)); + exit(1); + } + + if (!strcmp(actname,"on") + || !strcmp(actname,"+") + || !strcmp(actname,"1")) + action = IFF_DEBUG; + + if (!strcmp(actname,"off") + || !strcmp(actname,"-") + || !strcmp(actname,"0")) + action = 0; + + if (!strcmp(actname,"tell") + || actname[0]=='t') { + printf("%s: debug is %s\n", argv[1], + req.ifr_flags & IFF_DEBUG ? "on" : "off"); + exit(0); + } + + req.ifr_flags &= ~IFF_DEBUG; + req.ifr_flags |= action; + + if ( ioctl(sock, SIOCSIFFLAGS, &req) < 0) { + fprintf(stderr, " %s: ioctl(SIOCSIFFLAGS): %s\n", + argv[0],strerror(errno)); + exit(1); + } + exit(0); +} -- cgit v1.2.1-18-gbd029