summaryrefslogtreecommitdiffstats
path: root/src/ifplugstatus.c
blob: fed74d8f09887fc23cb15881f4a1c47cb17f1fd6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* $Id$ */

/*
 * This file is part of ifplugd.
 *
 * ifplugd is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * ifplugd is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with ifplugd; if not, write to the Free Software Foundation,
 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <getopt.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <net/if.h>

#include <libdaemon/dlog.h>

#include "interface.h"
#include "svn-revision.h"

int interface_auto_up = 0, interface_do_message = 0;

int verbose = 0;
char *interface = NULL;

int handle(char *iface) {
    int fd, r = 0;
    interface_status_t s;
    
    if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0)
        return -1;
    
    if (verbose > 0) {
        printf("%s:\n", iface);
        
	if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR)
            printf("    SIOCETHTOOL failed (%s)\n", strerror(errno));
        else
            printf("    SIOCETHTOOL: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");
        
        if ((s = interface_detect_beat_mii(fd, iface)) == IFSTATUS_ERR)
            printf("    SIOCGMIIPHY failed (%s)\n", strerror(errno));
        else
            printf("    SIOCGMIIPHY: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");

        if ((s = interface_detect_beat_wlan(fd, iface)) == IFSTATUS_ERR)
            printf("    Wireless failed.\n");
        else
            printf("    Wireless: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");

        if ((s = interface_detect_beat_iff(fd, iface)) == IFSTATUS_ERR)
            printf("    IFF_RUNNING failed.\n");
        else
            printf("    IFF_RUNNING: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");
        
        if ((s = interface_detect_beat_priv(fd, iface)) == IFSTATUS_ERR)
            printf("    SIOCDEVPRIVATE failed (%s)\n", strerror(errno));
        else
            printf("    SIOCDEVPRIVATE: %s\n", s == IFSTATUS_UP ? "link beat detected" : "unplugged");

    } else {
        
        if ((s = interface_detect_beat_ethtool(fd, iface)) == IFSTATUS_ERR)
            if ((s = interface_detect_beat_mii(fd, iface)) == IFSTATUS_ERR)
                if ((s = interface_detect_beat_wlan(fd, iface)) == IFSTATUS_ERR)
                    s = interface_detect_beat_iff(fd, iface);

        switch(s) {
            case IFSTATUS_UP:
                if (!verbose)
                    printf("%s: link beat detected\n", iface);
                r = 1;
                break;
                
            case IFSTATUS_DOWN:
                if (!verbose)
                    printf("%s: unplugged\n", iface);
                r = 2;
                break;

            default:
                if (!verbose)
                    printf("%s: not supported%s\n", iface, getuid() != 0 ? " (Retry as root?)" : "");
                
                r = -1;
                break;
        }
    }
    
            
    close(fd);
    return r;
}

void usage(char *p) {
    if (strrchr(p, '/'))
        p = strchr(p, '/')+1;

    printf("%s [options] [INTERFACE]\n"
           "   -a --auto            Enable interface automatically (%s)\n"
           "   -q --quiet           Quiet behaviour (%s)\n"
           "   -v --verbose         Enable verbosity (%s)\n"
           "   -h --help            Show this help\n"
           "   -V --version         Show version number\n",
           p, interface_auto_up ? "on" : "off", verbose < 0 ? "on" : "off", verbose > 0 ? "on" : "off");
}


void parse(int argc, char *argv[]) {
    static struct option long_options[] = {
        {"auto",        no_argument, 0, 'a'},
        {"quiet",       no_argument, 0, 'q'},
        {"verbose",     no_argument, 0, 'v'},
        {"help",        no_argument, 0, 'h'},
        {"version",     no_argument, 0, 'V'},
        {0, 0, 0, 0}
    };
    int option_index = 0;
    int help = 0;
    
    for (;;) {
        int c; 
       
        if ((c = getopt_long(argc, argv, "avhqV", long_options, &option_index)) < 0)
            break;

        switch (c) {
            case 'a' :
                interface_auto_up = !interface_auto_up;
                break;
            case 'v':
                verbose++; 
                break;
            case 'q':
                verbose--; 
                break;
            case 'h':
                help = 1;
                break;
            case 'V':
#ifdef SVN_REVISION
		 printf("ifplugstatus "VERSION" (SVN: "SVN_REVISION")\n");
#else
		 printf("ifplugstatus "VERSION"\n");
#endif
                exit(0);
            default:
                fprintf(stderr, "Unknown parameter.\n");
                exit(1);

        }
    }

    if (help) {
        usage(argv[0]);
        exit(0);
    }

    if (optind < argc)
        interface = argv[optind];
}


int main(int argc, char *argv[]) {
    parse(argc, argv);

    if (interface) {
        int r;

        if ((r = handle(interface)) < 0) {
            if (verbose == 0)
                fprintf(stderr, "Failure (%s)\n", strerror(errno));
            return 1;
        }

        return r+1;
        
    } else {
        FILE *f;
        char ln[256];

        if (!(f = fopen("/proc/net/dev", "r"))) {
            fprintf(stderr, "Failed to open /proc/net/dev: %s\n", strerror(errno));
            return 1;
        }

        fgets(ln, sizeof(ln), f);
        fgets(ln, sizeof(ln), f);

        while (fgets(ln, sizeof(ln), f)) {
            char *p, *e;

            p = ln+strspn(ln, " \t");
            if (!(e = strchr(p, ':'))) {
                fprintf(stderr, "Parse failure in /proc/net/dev.\n");
                fclose(f);
                return 1;
            }

            *e = 0;
            handle(p);
        }
        
        fclose(f);
    }
    
    return 0;
}