1 /*
2 * netifdebug.c -- change the IFF_DEBUG flag of an interface
3 *
4 * Copyright (C) 2001 Alessandro Rubini and Jonathan Corbet
5 * Copyright (C) 2001 O'Reilly & Associates
6 *
7 * The source code in this file can be freely used, adapted,
8 * and redistributed in source or binary form, so long as an
9 * acknowledgment appears in derived source files. The citation
10 * should list that the code comes from the book "Linux Device
11 * Drivers" by Alessandro Rubini and Jonathan Corbet, published
12 * by O'Reilly & Associates. No warranty is attached;
13 * we cannot take responsibility for errors or fitness for use.
14 */
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <unistd.h>
19 #include <string.h>
20 #include <errno.h>
21 #include <sys/types.h>
22 #include <sys/socket.h>
23 #include <sys/ioctl.h>
24 #include <net/if.h>
25 #include <netinet/in.h>
26
27 int main(int argc, char **argv)
28 {
29 int action = -1, sock;
30 struct ifreq req;
31 char *actname;
32
33 if (argc < 2) {
34 fprintf(stderr,"%s: usage is \"%s <ifname> [<on|off|tell>]\"\n",
35 argv[0],argv[0]);
36 exit(1);
37 }
38 if (argc==2)
39 actname="tell";
40 else
41 actname=argv[2];
42
43 /* a silly raw socket just for ioctl()ling it */
44 sock = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
45 if (sock < 0) {
46 fprintf(stderr, "%s: socket(): %s\n", argv[0],strerror(errno));
47 exit(1);
48 }
49
50 /* retrieve flags */
51 strcpy(req.ifr_name, argv[1]);
52 if ( ioctl(sock, SIOCGIFFLAGS, &req) < 0) {
53 fprintf(stderr, " %s: ioctl(SIOCGIFFLAGS): %s\n",
54 argv[0],strerror(errno));
55 exit(1);
56 }
57
58 if (!strcmp(actname,"on")
59 || !strcmp(actname,"+")
60 || !strcmp(actname,"1"))
61 action = IFF_DEBUG;
62
63 if (!strcmp(actname,"off")
64 || !strcmp(actname,"-")
65 || !strcmp(actname,""))
66 action = 0;
67
68 if (!strcmp(actname,"tell")
69 || actname[0]=='t') {
70 printf("%s: debug is %s\n", argv[1],
71 req.ifr_flags & IFF_DEBUG ? "on" : "off");
72 exit(0);
73 }
74
75 req.ifr_flags &= ~IFF_DEBUG;
76 req.ifr_flags |= action;
77
78 if ( ioctl(sock, SIOCSIFFLAGS, &req) < 0) {
79 fprintf(stderr, " %s: ioctl(SIOCSIFFLAGS): %s\n",
80 argv[0],strerror(errno));
81 exit(1);
82 }
83 exit(0);
84 }
85
|
This page was automatically generated by the
LXR engine.
|