1
/******************************************************************************
3
* src/if_media.h - part of slurm
5
* this file handles basic network information functions for all
8
*****************************************************************************
9
* $Id: if_media.c,v 1.3 2003/09/13 12:30:10 bwalle Exp $
10
*****************************************************************************/
12
#if defined (__FreeBSD__) || (__OpenBSD__) || (__NetBSD__) || (__MicroBSD__) || (__APPLE__)
14
#define MEDIA_H_SUPPORTED
17
/******************************************************************************
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
25
* tested/supported operating systems:
30
* - MicroBSD (99% OpenBSD)
33
*****************************************************************************/
35
#ifdef MEDIA_H_SUPPORTED
36
int get_if_speed (char *ifstring)
38
int speed=ERR_IFACE_NO_SPEED;
40
struct ifmediareq ifmr;
44
if ((s = socket(AF_INET, SOCK_DGRAM, 0)) == NULL)
46
fprintf(stderr, "cannot create socket!\n");
47
return ERR_IFACE_NO_SPEED;
50
memset(&ifmr, 0, sizeof(ifmr));
51
strncpy(ifmr.ifm_name, (char *)ifstring, sizeof(ifmr.ifm_name));
53
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
55
fprintf(stderr, "interface does not support SIOCGIFMEDIA ioctl()\n");
56
return ERR_IFACE_NO_SPEED;
59
if (ifmr.ifm_count == 0)
61
fprintf(stderr, "%s: no media types?\n", (char *)ifstring);
62
return ERR_IFACE_NO_SPEED;
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;
70
if (ioctl(s, SIOCGIFMEDIA, (caddr_t)&ifmr) < 0)
72
fprintf(stderr, "ioctl(SIOCGIFMEDIA) failed\n");
77
* define type and physical
85
type = ifmr.ifm_active & 0xf0;
86
physical = ifmr.ifm_active & 0x0f;
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);
132
#if defined(__OpenBSD__) || (__MicroBSD__) || (__NetBSD__) || (__APPLE__)
142
speed = ERR_IFACE_NO_SPEED;
144
} /* end switch physical */
146
/* FDDI interfaces */
147
/* fpa doesn't seem to support SIOCGIFMEDIA on FreeBSD
148
* so we won't get here but anyway ...
159
speed = ERR_IFACE_NO_SPEED;
162
/* IEEE 802.11 wireless interfaces */
166
case IFM_IEEE80211_FH1:
167
case IFM_IEEE80211_DS1:
170
case IFM_IEEE80211_FH2:
171
case IFM_IEEE80211_DS2:
174
case IFM_IEEE80211_DS5:
175
speed = (int) 5.5 * 1000;
177
case IFM_IEEE80211_DS11:
180
#if __FreeBSD_version >= 460102
181
case IFM_IEEE80211_DS22:
184
#if __FreeBSD_version > 500111
185
case IFM_IEEE80211_OFDM6:
188
case IFM_IEEE80211_OFDM9:
191
case IFM_IEEE80211_OFDM12:
194
case IFM_IEEE80211_OFDM18:
197
case IFM_IEEE80211_OFDM24:
200
case IFM_IEEE80211_OFDM36:
203
case IFM_IEEE80211_OFDM48:
206
case IFM_IEEE80211_OFDM54:
209
case IFM_IEEE80211_OFDM72:
213
/* these are the old common typos */
214
case IFM_IEEE80211_ODFM6:
217
case IFM_IEEE80211_ODFM9:
220
case IFM_IEEE80211_ODFM12:
223
case IFM_IEEE80211_ODFM18:
226
case IFM_IEEE80211_ODFM24:
229
case IFM_IEEE80211_ODFM36:
232
case IFM_IEEE80211_ODFM48:
235
case IFM_IEEE80211_ODFM54:
238
case IFM_IEEE80211_ODFM72:
244
speed = ERR_IFACE_NO_SPEED;
249
speed = ERR_IFACE_NO_SPEED;
250
} /* end switch type */
253
printf(" speed: %6d\n", speed);
258
int get_if_speed(char *ifstring)
260
int speed=ERR_IFACE_NO_SPEED, buffer, fd, val, ret = -1;
262
struct nmparms params;
263
mib_ifEntry * if_buf;
265
for (i=0; i <= data->ifdata.if_amount; i++)
267
if ((fd = open_mib("/dev/lan", O_RDWR, i, 0)) >= 0)
269
if ((if_buf = (mib_ifEntry *) malloc (sizeof(mib_ifEntry))) != 0)
271
params.objid = ID_ifEntry;
272
params.buffer = if_buf;
273
len = sizeof(mib_ifEntry);
275
if_buf->ifIndex = i+1;
276
if ((ret = get_mib_info(fd, ¶ms)) == 0)
278
if ( i+1 == data->ifdata.if_id)
279
if (if_buf->ifOper == 1)
280
speed = if_buf->ifSpeed/1000;
282
speed ERR_IFACE_DOWN;
291
#elif defined (__Solaris__)
292
/******************************************************************************
294
* Solaris interface speed detection
296
*****************************************************************************/
297
int get_if_speed(char *ifstring)
299
int speed=ERR_IFACE_NO_SPEED;
304
if ((kc = kstat_open()) == NULL)
305
return ERR_IFACE_NO_SPEED;
307
ksp = kstat_lookup(kc, NULL, -1, ifstring);
308
if (ksp && kstat_read(kc, ksp, NULL) >= 0)
310
knp = (kstat_named_t *)kstat_data_lookup(ksp, "ifspeed");
312
speed = (int) knp->value.ui64 / 1000;
319
int get_if_speed(char *ifstring)
321
ifstring++; /* ugly hack to prevent compiler warning on Linux */
322
return ERR_IFACE_NO_SPEED;