~ubuntu-branches/ubuntu/maverick/xfce4-netload-plugin/maverick

« back to all changes in this revision

Viewing changes to panel-plugin/wormulon/if_media.c

  • Committer: Bazaar Package Importer
  • Author(s): Andrew Pollock
  • Date: 2004-04-25 11:13:24 UTC
  • Revision ID: james.westby@ubuntu.com-20040425111324-ju7umz5xpe4ex9e4
Tags: upstream-0.2.2
ImportĀ upstreamĀ versionĀ 0.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/******************************************************************************
 
2
 *
 
3
 * src/if_media.h - part of slurm
 
4
 *
 
5
 * this file handles basic network information functions for all
 
6
 * operating systems.
 
7
 *
 
8
 *****************************************************************************
 
9
 * $Id: if_media.c,v 1.3 2003/09/13 12:30:10 bwalle Exp $
 
10
 *****************************************************************************/
 
11
 
 
12
#if defined (__FreeBSD__) || (__OpenBSD__) || (__NetBSD__) || (__MicroBSD__) || (__APPLE__)
 
13
 
 
14
#define MEDIA_H_SUPPORTED
 
15
#endif
 
16
 
 
17
/******************************************************************************
 
18
 *
 
19
 * get_if_speed()
 
20
 *
 
21
 * determine current interface speed, needs interface name as argument
 
22
 * return the interface speed as an integer. unit: kbit/s
 
23
 * in case of error return ERR_IFACE_NO_SPEED
 
24
 *
 
25
 * tested/supported operating systems:
 
26
 *
 
27
 *  - FreeBSD
 
28
 *  - OpenBSD
 
29
 *  - NetBSD
 
30
 *  - MicroBSD (99% OpenBSD)
 
31
 *  - Mac OS X
 
32
 *
 
33
 *****************************************************************************/
 
34
 
 
35
#ifdef MEDIA_H_SUPPORTED
 
36
int get_if_speed (char *ifstring)
 
37
{
 
38
    int speed=ERR_IFACE_NO_SPEED;
 
39
    int s; /* socket */
 
40
    struct ifmediareq ifmr;
 
41
    int *media_list;
 
42
    int type, physical;
 
43
 
 
44
    if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL)
 
45
    {
 
46
        fprintf(stderr, "cannot create socket!\n");
 
47
        return ERR_IFACE_NO_SPEED;
 
48
    }
 
49
 
 
50
    memset(&ifmr, 0, sizeof(ifmr));
 
51
    strncpy(ifmr.ifm_name, (char *)ifstring, sizeof(ifmr.ifm_name));
 
52
 
 
53
    if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
 
54
    {
 
55
        fprintf(stderr, "interface does not support SIOCGIFMEDIA ioctl()\n");
 
56
        return ERR_IFACE_NO_SPEED;
 
57
    }
 
58
 
 
59
    if (ifmr.ifm_count == 0)
 
60
    {
 
61
        fprintf(stderr, "%s: no media types?\n", (char *)ifstring);
 
62
        return ERR_IFACE_NO_SPEED;
 
63
    }
 
64
 
 
65
    media_list = (int *)malloc(ifmr.ifm_count * sizeof(int));
 
66
    if (media_list == NULL)
 
67
        fprintf(stderr, "malloc() error in if_media.c\n");
 
68
    ifmr.ifm_ulist = media_list;
 
69
 
 
70
    if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
 
71
    {
 
72
        fprintf(stderr, "ioctl(SIOCGIFMEDIA) failed\n");
 
73
        return -1;
 
74
    }
 
75
 
 
76
    /*
 
77
     * define type and physical
 
78
     *
 
79
     * bits:
 
80
     *      0-4 Media variant
 
81
     *      5-7 Media type
 
82
     *
 
83
     */
 
84
 
 
85
    type = ifmr.ifm_active & 0xf0;
 
86
    physical = ifmr.ifm_active & 0x0f;
 
87
 
 
88
#ifdef MEDIADEBUG
 
89
    printf("      all: %6d\n", ifmr.ifm_current);
 
90
    printf("   active: %6d\n", ifmr.ifm_active);
 
91
    printf("   status: %6d\n", ifmr.ifm_status);
 
92
    printf("     type: %6d\n", type);
 
93
    printf("     phys: %6d\n", physical);
 
94
    printf("if active: %6d\n", ifmr.ifm_active & IFM_ACTIVE);
 
95
#endif
 
96
 
 
97
    /* switch type */
 
98
    switch (type)
 
99
    {
 
100
        /* Ethernet */
 
101
        case IFM_ETHER:
 
102
            switch (physical)
 
103
            {
 
104
#ifdef __FreeBSD__
 
105
#if __FreeBSD__ <= 4
 
106
                case IFM_1000_FX:
 
107
                case IFM_1000_TX:
 
108
#endif
 
109
#endif
 
110
                case IFM_1000_SX:
 
111
                case IFM_1000_LX:
 
112
                case IFM_1000_CX:
 
113
#ifdef IFM_1000_T
 
114
                case IFM_1000_T:
 
115
#endif
 
116
                    speed = 1000 * 1000;
 
117
                    break;
 
118
                case IFM_100_TX:
 
119
                case IFM_100_FX:
 
120
                case IFM_100_T4:
 
121
                case IFM_100_VG:
 
122
                case IFM_100_T2:
 
123
                    speed = 100 * 1000;
 
124
                    break;
 
125
                case IFM_10_T:
 
126
                case IFM_10_2:
 
127
                case IFM_10_5:
 
128
                case IFM_10_FL:
 
129
                case IFM_10_STP:
 
130
                    speed = 10 * 1000;
 
131
                    break;
 
132
#if defined(__OpenBSD__) || (__MicroBSD__) || (__NetBSD__) || (__APPLE__)
 
133
        case IFM_HPNA_1:
 
134
#else
 
135
#if __FreeBSD__ <= 4
 
136
                case IFM_homePNA:
 
137
#endif
 
138
#endif
 
139
                    speed = 1 * 1000;
 
140
                    break;
 
141
                default:
 
142
                    speed = ERR_IFACE_NO_SPEED;
 
143
                    break;
 
144
            } /* end switch physical */
 
145
            break;
 
146
        /* FDDI interfaces */
 
147
        /* fpa doesn't seem to support SIOCGIFMEDIA on FreeBSD
 
148
         * so we won't get here but anyway ...
 
149
         */
 
150
        case IFM_FDDI:
 
151
            switch (physical)
 
152
            {
 
153
                case IFM_FDDI_SMF:
 
154
                case IFM_FDDI_MMF:
 
155
                case IFM_FDDI_UTP:
 
156
                    speed = 100 * 1000;
 
157
                    break;
 
158
                default:
 
159
                    speed = ERR_IFACE_NO_SPEED;
 
160
            }
 
161
            break;
 
162
        /* IEEE 802.11 wireless interfaces */
 
163
        case IFM_IEEE80211:
 
164
            switch (physical)
 
165
            {
 
166
                case IFM_IEEE80211_FH1:
 
167
                case IFM_IEEE80211_DS1:
 
168
                    speed = 1 * 1000;
 
169
                    break;
 
170
                case IFM_IEEE80211_FH2:
 
171
                case IFM_IEEE80211_DS2:
 
172
                    speed = 2 * 1000;
 
173
                    break;
 
174
                case IFM_IEEE80211_DS5:
 
175
                    speed = (int) 5.5 * 1000;
 
176
                    break;
 
177
                case IFM_IEEE80211_DS11:
 
178
                    speed = 11 * 1000;
 
179
                    break;
 
180
#if __FreeBSD_version >= 460102
 
181
                case IFM_IEEE80211_DS22:
 
182
                    speed = 22 * 1000;
 
183
                    break;
 
184
#if __FreeBSD_version >  500111
 
185
                case IFM_IEEE80211_OFDM6:
 
186
                    speed = 6 * 1000;
 
187
                    break;
 
188
                case IFM_IEEE80211_OFDM9:
 
189
                    speed = 9 * 1000;
 
190
                    break;
 
191
                case IFM_IEEE80211_OFDM12:
 
192
                    speed = 12 * 1000;
 
193
                    break;
 
194
                case IFM_IEEE80211_OFDM18:
 
195
                    speed = 18 * 1000;
 
196
                    break;
 
197
                case IFM_IEEE80211_OFDM24:
 
198
                    speed = 24 * 1000;
 
199
                    break;
 
200
                case IFM_IEEE80211_OFDM36:
 
201
                    speed = 36 * 1000;
 
202
                    break;
 
203
                case IFM_IEEE80211_OFDM48:
 
204
                    speed = 48 * 1000;
 
205
                    break;
 
206
                case IFM_IEEE80211_OFDM54:
 
207
                    speed = 54 * 1000;
 
208
                    break;
 
209
                case IFM_IEEE80211_OFDM72:
 
210
                    speed = 72 * 1000;
 
211
                    break;
 
212
#else
 
213
                                /* these are the old common typos */
 
214
                case IFM_IEEE80211_ODFM6:
 
215
                    speed = 6 * 1000;
 
216
                    break;
 
217
                case IFM_IEEE80211_ODFM9:
 
218
                    speed = 9 * 1000;
 
219
                    break;
 
220
                case IFM_IEEE80211_ODFM12:
 
221
                    speed = 12 * 1000;
 
222
                    break;
 
223
                case IFM_IEEE80211_ODFM18:
 
224
                    speed = 18 * 1000;
 
225
                    break;
 
226
                case IFM_IEEE80211_ODFM24:
 
227
                    speed = 24 * 1000;
 
228
                    break;
 
229
                case IFM_IEEE80211_ODFM36:
 
230
                    speed = 36 * 1000;
 
231
                    break;
 
232
                case IFM_IEEE80211_ODFM48:
 
233
                    speed = 48 * 1000;
 
234
                    break;
 
235
                case IFM_IEEE80211_ODFM54:
 
236
                    speed = 54 * 1000;
 
237
                    break;
 
238
                case IFM_IEEE80211_ODFM72:
 
239
                    speed = 72 * 1000;
 
240
                    break;
 
241
#endif
 
242
#endif
 
243
                default:
 
244
                    speed = ERR_IFACE_NO_SPEED;
 
245
                    break;
 
246
            }
 
247
            break;
 
248
        default:
 
249
            speed = ERR_IFACE_NO_SPEED;
 
250
    } /* end switch type */
 
251
 
 
252
#ifdef MEDIADEBUG
 
253
    printf("    speed: %6d\n", speed);
 
254
#endif
 
255
    return speed;
 
256
}
 
257
#elif __HPUX__
 
258
int get_if_speed(char *ifstring)
 
259
{
 
260
    int speed=ERR_IFACE_NO_SPEED, buffer, fd, val, ret = -1;
 
261
    unsigned int len, i;
 
262
    struct nmparms params;
 
263
    mib_ifEntry * if_buf;
 
264
 
 
265
    for (i=0; i <= data->ifdata.if_amount; i++)
 
266
    {
 
267
        if ((fd = open_mib("/dev/lan", O_RDWR, i, 0)) >= 0)
 
268
        {
 
269
            if ((if_buf = (mib_ifEntry *) malloc (sizeof(mib_ifEntry))) != 0)
 
270
            {
 
271
                params.objid  = ID_ifEntry;
 
272
                params.buffer = if_buf;
 
273
                len = sizeof(mib_ifEntry);
 
274
                params.len    = &len;
 
275
                if_buf->ifIndex = i+1;
 
276
                if ((ret = get_mib_info(fd, &params)) == 0)
 
277
                {
 
278
                    if ( i+1 == data->ifdata.if_id)
 
279
                        if (if_buf->ifOper == 1)
 
280
                            speed = if_buf->ifSpeed/1000;                         
 
281
                        else
 
282
                            speed ERR_IFACE_DOWN;
 
283
                }
 
284
            }
 
285
        }
 
286
        free(if_buf);
 
287
        close_mib(fd);
 
288
    }
 
289
    return speed;
 
290
}
 
291
#elif defined (__Solaris__)
 
292
/******************************************************************************
 
293
 *
 
294
 * Solaris interface speed detection
 
295
 *
 
296
 *****************************************************************************/
 
297
int get_if_speed(char *ifstring)
 
298
{
 
299
        int speed=ERR_IFACE_NO_SPEED;
 
300
        kstat_t *ksp;
 
301
        kstat_named_t *knp;
 
302
        kstat_ctl_t *kc;
 
303
 
 
304
        if ((kc = kstat_open()) == NULL)
 
305
                return ERR_IFACE_NO_SPEED;
 
306
 
 
307
        ksp = kstat_lookup(kc, NULL, -1, ifstring);
 
308
        if (ksp && kstat_read(kc, ksp, NULL) >= 0)
 
309
        {
 
310
                knp = (kstat_named_t *)kstat_data_lookup(ksp, "ifspeed");
 
311
                if (knp)
 
312
                        speed = (int) knp->value.ui64 / 1000;
 
313
        }
 
314
        kstat_close(kc);
 
315
 
 
316
        return speed;
 
317
}
 
318
#else
 
319
int get_if_speed(char *ifstring)
 
320
{
 
321
    ifstring++; /* ugly hack to prevent compiler warning on Linux */
 
322
    return ERR_IFACE_NO_SPEED;
 
323
}
 
324
#endif