~ubuntu-branches/ubuntu/natty/iptraf/natty-proposed

« back to all changes in this revision

Viewing changes to src/promisc.c

  • Committer: Bazaar Package Importer
  • Author(s): Frederic Peters
  • Date: 2006-10-15 13:34:14 UTC
  • mfrom: (1.1.2 upstream) (3.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061015133414-77itbhydih1z3amr
* Sync with Ubuntu fixes (by Oliver Grawert and Michael Vogt)
  * added fix for /var/run detection (since it is a tmpfs by default on
    Ubuntu) [and fixed ubuntu fix]
  * added support for ath devices
  * fixed FTBFS by changing linux/if_tr.h to netinet/if_tr.h

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/***
2
2
 
3
 
promisc.c       - handles the promiscuous mode flag for the Ethernet/FDDI
4
 
                  interfaces
 
3
promisc.c       - handles the promiscuous mode flag for the Ethernet/FDDI/
 
4
              Token Ring interfaces
5
5
                  
6
6
Written by Gerard Paul Java
7
 
Copyright (c) Gerard Paul Java 1997, 1998
 
7
Copyright (c) Gerard Paul Java 1997, 2002
8
8
 
9
9
This module contains functions that manage the promiscuous states of
10
10
the interfaces.
32
32
#include <stdlib.h>
33
33
#include <string.h>
34
34
#include <netinet/in.h>
35
 
#include <net/if.h>
 
35
#include <linux/if.h>
36
36
#include <linux/if_ether.h>
37
37
#include "ifstats.h"
38
38
#include "ifaces.h"
62
62
    fd = open_procnetdev();
63
63
 
64
64
    do {
65
 
        get_next_iface(fd, buf);
66
 
 
67
 
        if (strcmp(buf, "") != 0) {
68
 
            ptmp = malloc(sizeof(struct promisc_states));
69
 
            strcpy(ptmp->params.ifname, buf);
70
 
 
71
 
            if (*list == NULL) {
72
 
                *list = ptmp;
73
 
            } else
74
 
                tail->next_entry = ptmp;
75
 
 
76
 
            tail = ptmp;
77
 
            ptmp->next_entry = NULL;
78
 
 
79
 
            /*
80
 
             * Retrieve and save interface flags
81
 
             */
82
 
 
83
 
            if ((strncmp(buf, "eth", 3) == 0) ||
84
 
            (strncmp(buf, "vlan", 4) == 0) ||
85
 
                (strncmp(buf, "fddi", 4) == 0) ||
86
 
                (strncmp(ptmp->params.ifname, "wvlan", 4) == 0) ||
87
 
                (strncmp(ptmp->params.ifname, "lec", 3) == 0) ||
88
 
                (accept_unsupported_interfaces)) {
89
 
                strcpy(ifr.ifr_name, buf);
90
 
 
91
 
                istat = ioctl(ifd, SIOCGIFFLAGS, &ifr);
92
 
 
93
 
                if (istat < 0) {
94
 
                    sprintf(err_msg,
95
 
                            "Unable to obtain interface parameters for %s",
96
 
                            buf);
97
 
                    write_error(err_msg, daemonized);
98
 
                    ptmp->params.state_valid = 0;
99
 
                } else {
100
 
                    ptmp->params.saved_state = ifr.ifr_flags;
101
 
                    ptmp->params.state_valid = 1;
102
 
                }
103
 
            }
104
 
        }
 
65
        get_next_iface(fd, buf);
 
66
 
 
67
        if (strcmp(buf, "") != 0) {
 
68
            ptmp = malloc(sizeof(struct promisc_states));
 
69
            strcpy(ptmp->params.ifname, buf);
 
70
 
 
71
            if (*list == NULL) {
 
72
                *list = ptmp;
 
73
            } else
 
74
                tail->next_entry = ptmp;
 
75
 
 
76
            tail = ptmp;
 
77
            ptmp->next_entry = NULL;
 
78
 
 
79
            /*
 
80
             * Retrieve and save interface flags
 
81
             */
 
82
 
 
83
            if ((strncmp(buf, "eth", 3) == 0) ||
 
84
                (strncmp(buf, "ath", 3) == 0) ||
 
85
                (strncmp(buf, "fddi", 4) == 0) ||
 
86
                (strncmp(buf, "tr", 2) == 0) ||
 
87
                (strncmp(ptmp->params.ifname, "wvlan", 4) == 0) ||
 
88
                (strncmp(ptmp->params.ifname, "lec", 3) == 0) ||
 
89
                (accept_unsupported_interfaces)) {
 
90
                strcpy(ifr.ifr_name, buf);
 
91
 
 
92
                istat = ioctl(ifd, SIOCGIFFLAGS, &ifr);
 
93
 
 
94
                if (istat < 0) {
 
95
                    sprintf(err_msg,
 
96
                            "Unable to obtain interface parameters for %s",
 
97
                            buf);
 
98
                    write_error(err_msg, daemonized);
 
99
                    ptmp->params.state_valid = 0;
 
100
                } else {
 
101
                    ptmp->params.saved_state = ifr.ifr_flags;
 
102
                    ptmp->params.state_valid = 1;
 
103
                }
 
104
            }
 
105
        }
105
106
    } while (strcmp(buf, "") != 0);
106
107
}
107
108
 
121
122
    fd = open(PROMISCLISTFILE, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
122
123
 
123
124
    if (fd < 0) {
124
 
        write_error("Unable to save interface flags", daemonized);
125
 
        return;
 
125
        write_error("Unable to save interface flags", daemonized);
 
126
        return;
126
127
    }
127
128
 
128
129
    while (ptmp != NULL) {
129
 
        write(fd, &(ptmp->params), sizeof(struct promisc_params));
130
 
        ptmp = ptmp->next_entry;
 
130
        write(fd, &(ptmp->params), sizeof(struct promisc_params));
 
131
        ptmp = ptmp->next_entry;
131
132
    }
132
133
 
133
134
    close(fd);
147
148
    fd = open(PROMISCLISTFILE, O_RDONLY);
148
149
 
149
150
    if (fd < 0) {
150
 
        write_error("Unable to retrieve saved interface flags",
151
 
                    daemonized);
152
 
        *list = NULL;
153
 
        return;
 
151
        write_error("Unable to retrieve saved interface flags",
 
152
                    daemonized);
 
153
        *list = NULL;
 
154
        return;
154
155
    }
155
156
 
156
157
    do {
157
 
        ptmp = malloc(sizeof(struct promisc_states));
158
 
        br = read(fd, &(ptmp->params), sizeof(struct promisc_params));
159
 
 
160
 
        if (br > 0) {
161
 
            if (tail != NULL)
162
 
                tail->next_entry = ptmp;
163
 
            else
164
 
                *list = ptmp;
165
 
 
166
 
            ptmp->next_entry = NULL;
167
 
            tail = ptmp;
168
 
        } else
169
 
            free(ptmp);
 
158
        ptmp = malloc(sizeof(struct promisc_states));
 
159
        br = read(fd, &(ptmp->params), sizeof(struct promisc_params));
 
160
 
 
161
        if (br > 0) {
 
162
            if (tail != NULL)
 
163
                tail->next_entry = ptmp;
 
164
            else
 
165
                *list = ptmp;
 
166
 
 
167
            ptmp->next_entry = NULL;
 
168
            tail = ptmp;
 
169
        } else
 
170
            free(ptmp);
170
171
    } while (br > 0);
171
172
 
172
173
    close(fd);
189
190
    fd = socket(PF_INET, SOCK_DGRAM, 0);
190
191
 
191
192
    if (fd < 0) {
192
 
        write_error("Unable to open socket for flag change", daemonized);
193
 
        return;
 
193
        write_error("Unable to open socket for flag change", daemonized);
 
194
        return;
194
195
    }
195
196
 
196
197
    while (ptmp != NULL) {
197
 
        if (((strncmp(ptmp->params.ifname, "eth", 3) == 0) ||
198
 
             (strncmp(ptmp->params.ifname, "vlan", 4) == 0) ||
199
 
             (strncmp(ptmp->params.ifname, "fddi", 4) == 0) ||
200
 
             (strncmp(ptmp->params.ifname, "wvlan", 4) == 0) ||
201
 
             (strncmp(ptmp->params.ifname, "lec", 3) == 0)) &&
202
 
             (ptmp->params.state_valid)) {
203
 
 
204
 
            strcpy(ifr.ifr_name, ptmp->params.ifname);
205
 
 
206
 
            if (mode)
207
 
                ifr.ifr_flags = ptmp->params.saved_state | IFF_PROMISC;
208
 
            else
209
 
                ifr.ifr_flags = ptmp->params.saved_state;
210
 
 
211
 
            istat = ioctl(fd, SIOCSIFFLAGS, &ifr);
212
 
 
213
 
            if (istat < 0) {
214
 
                sprintf(fullmsg, "Promisc change failed for %s",
215
 
                        ptmp->params.ifname);
216
 
                write_error(fullmsg, daemonized);
217
 
            }
218
 
        }
219
 
        ptmp = ptmp->next_entry;
 
198
        if (((strncmp(ptmp->params.ifname, "eth", 3) == 0) ||
 
199
             (strncmp(ptmp->params.ifname, "fddi", 4) == 0) ||
 
200
             (strncmp(ptmp->params.ifname, "tr", 2) == 0) ||
 
201
             (strncmp(ptmp->params.ifname, "wvlan", 4) == 0) ||
 
202
             (strncmp(ptmp->params.ifname, "lec", 3) == 0)) &&
 
203
            (ptmp->params.state_valid)) {
 
204
 
 
205
            strcpy(ifr.ifr_name, ptmp->params.ifname);
 
206
 
 
207
            if (mode)
 
208
                ifr.ifr_flags = ptmp->params.saved_state | IFF_PROMISC;
 
209
            else
 
210
                ifr.ifr_flags = ptmp->params.saved_state;
 
211
 
 
212
            istat = ioctl(fd, SIOCSIFFLAGS, &ifr);
 
213
 
 
214
            if (istat < 0) {
 
215
                sprintf(fullmsg, "Promisc change failed for %s",
 
216
                        ptmp->params.ifname);
 
217
                write_error(fullmsg, daemonized);
 
218
            }
 
219
        }
 
220
        ptmp = ptmp->next_entry;
220
221
    }
221
222
 
222
223
    close(fd);
228
229
    struct promisc_states *ctmp;
229
230
 
230
231
    if (ptmp != NULL)
231
 
        ctmp = ptmp->next_entry;
 
232
        ctmp = ptmp->next_entry;
232
233
 
233
234
    while (ptmp != NULL) {
234
 
        free(ptmp);
235
 
        ptmp = ctmp;
236
 
        if (ctmp != NULL)
237
 
            ctmp = ctmp->next_entry;
 
235
        free(ptmp);
 
236
        ptmp = ctmp;
 
237
        if (ctmp != NULL)
 
238
            ctmp = ctmp->next_entry;
238
239
    }
239
240
}