-
Notifications
You must be signed in to change notification settings - Fork 1
/
drivers.c
1426 lines (1230 loc) · 33.6 KB
/
drivers.c
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* ifstat - InterFace STATistics
* Copyright (c) 2001, Gaël Roualland <[email protected]>
*
* This program 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.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: drivers.c,v 1.45 2003/11/22 01:27:51 gael Exp $
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#if STDC_HEADERS
#include <string.h>
#else
# ifndef HAVE_STRCHR
# define strchr index
# define strrchr rindex
# endif
char *strchr (), *strrchr ();
# ifndef HAVE_MEMCPY
# define memcpy(d, s, n) bcopy ((s), (d), (n))
# define memmove(d, s, n) bcopy ((s), (d), (n))
# endif
#endif
#ifdef HAVE_CTYPE_H
#include <ctype.h>
#endif
#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif
#ifdef HAVE_SYS_SOCKIO_H
#include <sys/sockio.h>
#endif
#ifdef HAVE_SYS_IOCTL_H
#include <sys/ioctl.h>
#endif
#ifdef HAVE_NET_SOIOCTL_H
#include <net/soioctl.h>
#endif
#ifdef HAVE_SYS_MBUF_H
#include <sys/mbuf.h>
#endif
#ifdef HAVE_NET_ROUTE_H
#include <net/route.h>
#endif
#ifdef HAVE_NET_IF_H
#include <net/if.h>
#endif
#ifdef HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#ifdef HAVE_NET_IF_MIB_H
#include <net/if_mib.h>
#endif
#ifdef HAVE_NET_IF_VAR_H
#include <net/if_var.h>
#endif
#ifdef HAVE_NET_IF_TYPES_H
#include <net/if_types.h>
#endif
#ifdef HAVE_NET_IF_DL_H
#include <net/if_dl.h>
#endif
#ifdef HAVE_SYS_DLPI_H
#include <sys/dlpi.h>
#endif
#ifdef HAVE_SYS_DLPI_EXT_H
#include <sys/dlpi_ext.h>
#endif
#ifdef HAVE_STROPTS_H
#include <stropts.h>
#endif
#ifdef HAVE_SYS_MIB_H
#include <sys/mib.h>
#endif
#ifdef HAVE_KSTAT_H
#include <kstat.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_KVM_H
#include <kvm.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
#ifdef HAVE_NLIST_H
#include <nlist.h>
#endif
#ifdef HAVE_PATHS_H
#include <paths.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include "ifstat.h"
#ifdef USE_SNMP
#include "snmp.h"
#endif
#ifndef IFNAMSIZ
#define IFNAMSIZ 16
#endif
#ifdef USE_WIN32
#include <windows.h>
#include <iphlpapi.h>
#ifndef IFF_UP
#define IFF_UP 1
#endif
#ifndef IFF_LOOPBACK
#define IFF_LOOPBACK 2
#endif
#endif
static void examine_interface(struct ifstat_list *ifs, char *name,
int ifflags, int iftype) {
(void)iftype;
#ifdef IFF_LOOPBACK
if ((ifflags & IFF_LOOPBACK) && !(ifs->flags & IFSTAT_LOOPBACK))
return;
#endif
#ifdef IFF_UP
if (!(ifflags & IFF_UP) && !(ifs->flags & IFSTAT_DOWN))
return;
#endif
#ifdef IFT_PFLOG
/* assume PFLOG interfaces are loopbacks (on OpenBSD) */
if (iftype == IFT_PFLOG && !(ifs->flags & IFSTAT_LOOPBACK))
return;
#endif
ifstat_add_interface(ifs, name, 0);
}
#ifdef USE_IOCTL
#ifdef USE_IFNAMEINDEX
static int ioctl_map_ifs(int sd,
int (*mapf)(int sd, struct ifreq *ifr, void *data),
void *mdata) {
struct if_nameindex *iflist, *cur;
struct ifreq ifr;
if ((iflist = if_nameindex()) == NULL) {
ifstat_perror("if_nameindex");
return 0;
}
for(cur = iflist; cur->if_index != 0 && cur->if_name != NULL; cur++) {
memcpy(ifr.ifr_name, cur->if_name, sizeof(ifr.ifr_name));
ifr.ifr_name[sizeof(ifr.ifr_name) - 1] = '\0';
if (!mapf(sd, &ifr, mdata))
return 0;
}
if_freenameindex(iflist);
return 1;
}
#else
static int ioctl_map_ifs(int sd,
int (*mapf)(int sd, struct ifreq *ifr, void *data),
void *mdata) {
struct ifconf ifc;
struct ifreq *ifr;
int len, n, res = 0;
char *buf;
#ifdef SIOCGIFNUM
if (ioctl(sd, SIOCGIFNUM, &n) < 0) {
ifstat_perror("ioctl(SIOCGIFNUM):");
goto end;
}
n += 2;
#else
n = 256; /* bad bad bad... */
#endif
len = n * sizeof(struct ifreq);
if ((buf = malloc(len)) == NULL) {
ifstat_perror("malloc");
return 0;
}
ifc.ifc_buf = buf;
ifc.ifc_len = len;
if (ioctl(sd, SIOCGIFCONF, (char *)&ifc) < 0) {
ifstat_perror("ioctl(SIOCGIFCONF):");
goto end;
}
n = 0;
while (n < ifc.ifc_len) {
ifr = (struct ifreq *) (buf + n);
#ifdef HAVE_SOCKADDR_SA_LEN
n += sizeof(ifr->ifr_name) + ifr->ifr_addr.sa_len;
#else
n += sizeof(struct ifreq);
#endif
if (!mapf(sd, ifr, mdata))
goto end;
}
res = 1;
end:
free(buf);
return res;
}
#endif
static int ioctl_map_scan(int sd, struct ifreq *ifr, void *data) {
if (ioctl(sd, SIOCGIFFLAGS, (char *)ifr) != 0)
return 1;
examine_interface((struct ifstat_list *) data, ifr->ifr_name,
ifr->ifr_flags, 0);
return 1;
}
static int ioctl_scan_interfaces(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
int sd;
(void)driver;
if ((sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
ifstat_perror("socket");
return 0;
}
ioctl_map_ifs(sd, &ioctl_map_scan, (void *) ifs);
close(sd);
return 1;
}
#endif
#ifdef USE_KSTAT
static int get_kstat_long(kstat_t *ksp, char *name, unsigned long long *value) {
kstat_named_t *data;
if ((data = kstat_data_lookup(ksp, name)) == NULL)
return 0;
switch (data->data_type) {
#ifdef KSTAT_DATA_INT32
/* solaris 2.6 and over */
case KSTAT_DATA_INT32:
*value = data->value.i32;
break;
case KSTAT_DATA_INT64:
*value = data->value.i64;
break;
case KSTAT_DATA_UINT32:
*value = data->value.ui32;
break;
case KSTAT_DATA_UINT64:
*value = data->value.ui64;
break;
#else
case KSTAT_DATA_LONG LONGLONG:
*value = data->value.ll;
break;
case KSTAT_DATA_ULONGLONG:
*value = data->value.ull;
break;
case KSTAT_DATA_LONG:
*value = data->value.l;
break;
case KSTAT_DATA_ULONG:
*value = data->value.ul;
break;
#endif
default:
return 0;
}
return 1;
}
static int kstat_open_driver(struct ifstat_driver *driver,
char *options) {
kstat_ctl_t *kc;
if ((kc = kstat_open()) == NULL) {
ifstat_perror("kstat_open");
return 0;
}
driver->data = (void *) kc;
return 1;
}
static int kstat_get_stats(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
unsigned long long bytesin, bytesout;
struct ifstat_data *cur;
kstat_ctl_t *kc = driver->data;
kstat_t *ksp;
for (cur = ifs->first; cur != NULL; cur = cur->next) {
if (cur->flags & IFSTAT_TOTAL)
continue;
if ((ksp = kstat_lookup(kc, NULL, -1, cur->name)) == NULL ||
ksp->ks_type != KSTAT_TYPE_NAMED)
continue;
if (kstat_read(kc, ksp, 0) >= 0 &&
get_kstat_long(ksp, "obytes", &bytesout) &&
get_kstat_long(ksp, "rbytes", &bytesin))
ifstat_set_interface_stats(cur, bytesin, bytesout);
}
return 1;
}
static void kstat_close_driver(struct ifstat_driver *driver) {
kstat_close(((kstat_ctl_t *) driver->data));
}
#endif
#ifdef USE_KVM
#ifndef HAVE_KVM
/* use internal emulation using open/read/nlist */
#ifndef _POSIX2_LINE_MAX
#define _POSIX2_LINE_MAX 2048
#endif
#ifndef _PATH_KMEM
#define _PATH_KMEM "/dev/kmem"
#endif
#ifndef _PATH_UNIX
#define _PATH_UNIX "/vmunix"
#endif
typedef struct _kvm_t {
int fd;
char *errbuf;
char *execfile;
} kvm_t;
static void _kvm_error(char *errbuf, char *message) {
strncpy(errbuf, message ? message : strerror(errno),
_POSIX2_LINE_MAX - 1);
errbuf[_POSIX2_LINE_MAX - 1] = '\0';
}
static kvm_t *kvm_openfiles(const char *execfile, const char *corefile,
const char *swapfile, int flags, char *errbuf) {
kvm_t *kd;
if (swapfile != NULL) {
_kvm_error(errbuf, "swap file option not supported");
return NULL;
}
if ((kd = malloc(sizeof(kvm_t))) == NULL) {
_kvm_error(errbuf, NULL);
return NULL;
}
if ((kd->fd = open(corefile ? corefile : _PATH_KMEM, flags)) < 0) {
_kvm_error(errbuf, NULL);
free(kd);
return NULL;
}
kd->execfile = execfile ? strdup(execfile) : NULL;
kd->errbuf = errbuf;
return kd;
}
static int kvm_nlist (kvm_t *kd, struct nlist *nl) {
int count;
#ifdef HAVE_KNLIST
if (kd->execfile == NULL) {
#ifdef HAVE_KNLIST_ARGS3
for(count = 0; nl[count].n_name != NULL; count++);
count = knlist(nl, count, sizeof(struct nlist));
#else
count = knlist(nl);
#endif
if (count < 0)
_kvm_error(kd->errbuf, "error looking up symbol in live kernel");
return count;
}
#endif
if ((count = nlist(kd->execfile ? kd->execfile : _PATH_UNIX, nl)) < 0)
_kvm_error(kd->errbuf, "error looking up symbol in kernel file");
return count;
}
#ifdef HAVE_READX
#define KOFFSET(x) ((x) & 0x7FFFFFFF)
#define KREAD(fd, buf, size, addr) readx((fd), (buf), (size), ((off_t) (addr)) < 0 ? 1 : 0)
#else
#define KOFFSET(x) (x)
#define KREAD(fd, buf, size, addr) read((fd), (buf), (size))
#endif
static ssize_t kvm_read(kvm_t *kd, unsigned long addr, void *buf, size_t nbytes) {
ssize_t len;
if (lseek(kd->fd, KOFFSET(addr), SEEK_SET) == -1) {
_kvm_error(kd->errbuf, NULL);
return -1;
}
if ((len = KREAD(kd->fd, buf, nbytes, addr)) < 0) {
_kvm_error(kd->errbuf, NULL);
return -1;
}
return len;
}
static int kvm_close(kvm_t *kd) {
close(kd->fd);
free(kd->execfile);
free(kd);
return 0;
}
#endif
struct kvm_driver_data {
kvm_t *kvmfd;
unsigned long ifnetaddr;
char errbuf[_POSIX2_LINE_MAX + 1];
};
static int kvm_open_driver(struct ifstat_driver *driver,
char *options) {
struct kvm_driver_data *data;
struct nlist kvm_syms[2];
unsigned long ifnetaddr;
char *files[3] = { NULL /* execfile */,
NULL /* corefile */,
NULL /* swapfile */ };
int i;
if ((data = malloc(sizeof(struct kvm_driver_data))) == NULL) {
ifstat_perror("malloc");
return 0;
}
data->errbuf[0] = '\0';
/* cut options : [execfile][,[corefile][,[swapfile]]] */
i = 0;
while (options != NULL && i < 3) {
char *v = strchr(options, ',');
if (v != NULL)
*v++ = '\0';
if (*options != '\0')
files[i] = options;
i++;
options = v;
}
if ((data->kvmfd = kvm_openfiles(files[0], files[1], files[2],
O_RDONLY, data->errbuf)) == NULL) {
ifstat_error("kvm_openfiles: %s", data->errbuf);
return 0;
}
memset(&kvm_syms, 0, sizeof(kvm_syms));
kvm_syms[0].n_name = "_ifnet";
if (kvm_nlist(data->kvmfd, kvm_syms) < 0 ||
kvm_syms[0].n_value == 0) {
kvm_syms[0].n_name = "ifnet";
if (kvm_nlist(data->kvmfd, kvm_syms) < 0) {
ifstat_error("kvm_nlist: %s", data->errbuf);
return 0;
}
}
if (kvm_syms[0].n_value == 0) {
ifstat_error("kvm: no _ifnet or ifnet symbol found");
return 0;
}
if (kvm_read(data->kvmfd, (unsigned long) kvm_syms[0].n_value,
&ifnetaddr, sizeof(ifnetaddr)) < 0) {
ifstat_error("kvm_read(ifnetaddr): %s", data->errbuf);
return 0;
}
if (ifnetaddr == 0) {
ifstat_error("kvm: ifnetaddr has null address.");
return 0;
}
data->ifnetaddr = ifnetaddr;
driver->data = (void *) data;
return 1;
}
#ifdef HAVE_IFNET_IF_NEXT
#define IFNET_NEXT(ifnet) (ifnet).if_next
#else
#ifdef HAVE_IFNET_IF_LINK
#define if_list if_link
#endif
#ifndef TAILQ_NEXT
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
#endif
#define IFNET_NEXT(ifnet) TAILQ_NEXT((&ifnet), if_list)
#endif
static int kvm_map_ifs(struct ifstat_driver *driver,
int (*mapf)(char *name, struct ifnet *ifnet, void *data),
void *mdata) {
struct kvm_driver_data *data = driver->data;
unsigned long ifaddr;
struct ifnet ifnet;
#ifndef HAVE_IFNET_IF_XNAME
char ifname[IFNAMSIZ + 1];
#endif
char interface[IFNAMSIZ + 10];
for (ifaddr = data->ifnetaddr; ifaddr != 0;
ifaddr = (unsigned long) IFNET_NEXT(ifnet)) {
if (kvm_read(data->kvmfd, ifaddr, &ifnet, sizeof(ifnet)) < 0) {
ifstat_error("kvm_read: %s", data->errbuf);
return 0;
}
#ifdef HAVE_IFNET_IF_XNAME
memcpy(interface, ifnet.if_xname, sizeof(interface));
interface[sizeof(interface) - 1] = '\0';
#else
if (kvm_read(data->kvmfd, (unsigned long) ifnet.if_name, &ifname, sizeof(ifname)) < 0) {
ifstat_error("kvm_read: %s", data->errbuf);
return 0;
}
ifname[sizeof(ifname) - 1] = '\0';
sprintf(interface, "%s%d", ifname, ifnet.if_unit);
#endif
if (!mapf(interface, &ifnet, mdata))
return 0;
}
return 1;
}
static int kvm_map_stats(char *name, struct ifnet *ifnet, void *data) {
struct ifstat_data *cur;
if ((cur = ifstat_get_interface((struct ifstat_list *) data, name)) == NULL)
return 1;
ifstat_set_interface_stats(cur, ifnet->if_ibytes, ifnet->if_obytes);
return 1;
}
static int kvm_get_stats(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
return kvm_map_ifs(driver, &kvm_map_stats, (void *) ifs);
}
static int kvm_map_scan(char *name, struct ifnet *ifnet, void *data) {
examine_interface((struct ifstat_list *) data, name,
ifnet->if_flags, ifnet->if_type);
return 1;
}
static int kvm_scan_interfaces(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
return kvm_map_ifs(driver, &kvm_map_scan, (void *) ifs);
}
static void kvm_close_driver(struct ifstat_driver *driver) {
kvm_close(((struct kvm_driver_data *) driver->data)->kvmfd);
}
#endif
#ifdef USE_IFMIB
static int get_ifcount() {
int ifcount[] = {
CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT
};
int count, size;
size = sizeof(count);
if (sysctl(ifcount, sizeof(ifcount) / sizeof(int), &count, &size, NULL, 0) < 0) {
ifstat_perror("sysctl(net.link.generic.ifmib.ifcount)");
return -1;
}
return count;
}
static int get_ifdata(int index, struct ifmibdata * ifmd) {
int ifinfo[] = {
CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, index, IFDATA_GENERAL
};
int size = sizeof(*ifmd);
if (sysctl(ifinfo, sizeof(ifinfo) / sizeof(int), ifmd, &size, NULL, 0) < 0)
return 0;
return 1;
}
static int ifmib_scan_interfaces(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
int count, i;
struct ifmibdata ifmd;
if ((count = get_ifcount()) <= 0)
return 0;
for (i = 1; i <= count; i++) {
if (!get_ifdata(i, &ifmd))
continue;
examine_interface(ifs, ifmd.ifmd_name, ifmd.ifmd_flags,
ifmd.ifmd_data.ifi_type);
}
return 1;
}
static int ifmib_get_stats(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
int count, i;
struct ifmibdata ifmd;
struct ifstat_data *cur;
if (ifs->flags & IFSTAT_HASINDEX) { /* poll by index */
for (cur = ifs->first; cur != NULL; cur = cur->next) {
i = ifstat_get_interface_index(cur);
if (i < 0 || !get_ifdata(i, &ifmd))
continue;
if (strcmp(ifstat_get_interface_name(cur), ifmd.ifmd_name))
continue;
ifstat_set_interface_stats(cur,
ifmd.ifmd_data.ifi_ibytes,
ifmd.ifmd_data.ifi_obytes);
ifstat_set_interface_index(cur, i);
}
return 1;
}
if ((count = get_ifcount()) <= 0)
return 0;
for (i = 1; i <= count; i++) {
if (!get_ifdata(i, &ifmd))
continue;
if ((cur = ifstat_get_interface(ifs, ifmd.ifmd_name)) == NULL)
continue;
ifstat_set_interface_stats(cur,
ifmd.ifmd_data.ifi_ibytes,
ifmd.ifmd_data.ifi_obytes);
ifstat_set_interface_index(cur, i);
}
return 1;
}
#endif
#ifdef USE_IFDATA
struct ifdata_driver_data {
int sd;
#ifdef HAVE_IFREQ_IFR_DATA
struct if_data ifd;
#else
struct ifdatareq ifd;
#endif
};
static int ifdata_open_driver(struct ifstat_driver *driver,
char *options) {
struct ifdata_driver_data *data;
if ((data = malloc(sizeof(struct ifdata_driver_data))) == NULL) {
ifstat_perror("malloc");
return 0;
}
if ((data->sd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
ifstat_perror("socket");
free(data);
return 0;
}
driver->data = (void *) data;
return 1;
}
static struct if_data *ifdata_get_data(struct ifdata_driver_data *data,
char *name) {
#ifdef HAVE_IFREQ_IFR_DATA
struct ifreq ifr;
strncpy(ifr.ifr_name, name, sizeof(ifr.ifr_name));
ifr.ifr_data = (caddr_t) &(data->ifd);
if (ioctl(data->sd, SIOCGIFDATA, (char *) &ifr) < 0)
return NULL;
return &(data->ifd);
#else
strncpy(data->ifd.ifd_name, name, sizeof(data->ifd.ifd_name));
if (ioctl(data->sd, SIOCGIFDATA, (char *) &(data->ifd)) < 0)
return NULL;
return &(data->ifd.ifd_ifd);
#endif
}
struct ifdata_scan_data {
struct ifstat_list *ifs;
struct ifdata_driver_data *data;
};
static int ifdata_map_scan(int sd, struct ifreq *ifr, void *pdata) {
struct ifdata_scan_data *sdata = pdata;
struct if_data *ifd;
if (ioctl(sd, SIOCGIFFLAGS, (char *)ifr) != 0)
return 1;
if ((ifd = ifdata_get_data(sdata->data, ifr->ifr_name)) == NULL)
return 1;
examine_interface(sdata->ifs, ifr->ifr_name,
ifr->ifr_flags, ifd->ifi_type);
return 1;
}
static int ifdata_scan_interfaces(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
struct ifdata_driver_data *data = driver->data;
struct ifdata_scan_data sdata = { ifs, data };
return ioctl_map_ifs(data->sd, &ifdata_map_scan, &sdata);
}
static int ifdata_get_stats(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
struct ifdata_driver_data *data = driver->data;
struct if_data *ifd;
struct ifstat_data *cur;
for (cur = ifs->first; cur != NULL; cur = cur->next) {
if (cur->flags & IFSTAT_TOTAL)
continue;
if ((ifd = ifdata_get_data(data, cur->name)) != NULL)
ifstat_set_interface_stats(cur, ifd->ifi_ibytes, ifd->ifi_obytes);
}
return 1;
}
static void ifdata_close_driver(struct ifstat_driver *driver) {
struct ifdata_driver_data *data = driver->data;
if (data->sd >= 0)
close(data->sd);
free(data);
}
#endif
#ifdef USE_PROC
struct proc_driver_data {
char *file;
int checked;
};
static int proc_open_driver(struct ifstat_driver *driver,
char *options) {
struct proc_driver_data *data;
if ((data = malloc(sizeof(struct proc_driver_data))) == NULL) {
ifstat_perror("malloc");
return 0;
}
data->file = (options != NULL) ? strdup(options) : NULL;
data->checked = 0;
driver->data = (void *) data;
return 1;
}
static void proc_close_driver(struct ifstat_driver *driver) {
struct proc_driver_data *data = driver->data;
if (data->file != NULL)
free(data->file);
free(data);
}
static int proc_get_stats(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
char buf[1024];
FILE *f;
char *iface, *stats;
unsigned long long bytesin, bytesout;
struct ifstat_data *cur;
struct proc_driver_data *data = driver->data;
char *file;
if (data->file != NULL)
file = data->file;
else
file = PROC_FILE;
if ((f = fopen(file, "r")) == NULL) {
ifstat_error("can't open %s: %s", file, strerror(errno));
return 0;
}
/* check first lines */
if (fgets(buf, sizeof(buf), f) == NULL)
goto badproc;
if (!data->checked && strncmp(buf, "Inter-|", 7))
goto badproc;
if (fgets(buf, sizeof(buf), f) == NULL)
goto badproc;
if (!data->checked) {
if (strncmp(buf, " face |by", 9))
goto badproc;
data->checked = 1;
}
while (fgets(buf, sizeof(buf), f) != NULL) {
if ((stats = strchr(buf, ':')) == NULL)
continue;
*stats++ = '\0';
iface = buf;
while (*iface == ' ')
iface++;
if (*iface == '\0')
continue;
if (sscanf(stats, "%llu %*u %*u %*u %*u %*u %*u %*u %llu %*u", &bytesin, &bytesout) != 2)
continue;
if ((cur = ifstat_get_interface(ifs, iface)) != NULL)
ifstat_set_interface_stats(cur, bytesin, bytesout);
}
fclose(f);
return 1;
badproc:
fclose(f);
ifstat_error("%s: unsupported format.", file);
return 0;
}
#endif
#ifdef USE_ROUTE
struct route_driver_data {
char *buf;
size_t size;
};
#define DEFAULT_SIZE 16384
static int route_open_driver(struct ifstat_driver *driver,
char *options) {
struct route_driver_data *data;
if ((data = malloc(sizeof(struct route_driver_data))) == NULL) {
ifstat_perror("malloc");
return 0;
}
if ((data->buf = malloc(DEFAULT_SIZE)) < 0) {
ifstat_perror("malloc");
free(data);
return 0;
}
data->size = DEFAULT_SIZE;
driver->data = (void *) data;
return 1;
}
static int route_map_ifs(struct ifstat_driver *driver,
int (*mapf)(char *name, struct if_msghdr *ifmsg, void *data),
void *mdata) {
struct route_driver_data *data = driver->data;
int iflist[] = {
CTL_NET, PF_ROUTE, 0, AF_LINK, NET_RT_IFLIST, 0
};
struct if_msghdr *ifm;
struct sockaddr_dl *dl;
char *ptr;
char ifname[IFNAMSIZ + 1];
size_t len;
if (data->size != 0) { /* try with current buf size */
len = data->size;
if (sysctl(iflist, sizeof(iflist) / sizeof(int),
data->buf, &len, NULL, 0) < 0) {
if (errno != ENOMEM) {
ifstat_perror("sysctl");
return 0;
}
/* buffer too small */
free (data->buf);
data->size = 0;
}
}
if (data->size == 0) {
/* ask for size */
if (sysctl(iflist, sizeof(iflist) / sizeof(int),
NULL, &len, NULL, 0) < 0) {
ifstat_perror("sysctl");
return 0;
}
if ((data->buf = malloc(len)) < 0) {
ifstat_perror("malloc");
return 0;
}
if (sysctl(iflist, sizeof(iflist) / sizeof(int),
data->buf, &len, NULL, 0) < 0) {
ifstat_perror("sysctl");
return 0;
}
}
/* browse interfaes */
for (ptr = data->buf; ptr < data->buf + len; ptr += ifm->ifm_msglen) {
ifm = (struct if_msghdr *) ptr;
if (ifm->ifm_type != RTM_IFINFO)
continue;
if (ifm->ifm_msglen <= sizeof(struct if_msghdr)) /* no address */
continue;
dl = (struct sockaddr_dl *) (ptr + sizeof(struct if_msghdr));
if (dl->sdl_family != AF_LINK)
continue;
if (dl->sdl_nlen > (sizeof(ifname) - 1))
dl->sdl_nlen = sizeof(ifname) - 1;
memcpy(ifname, dl->sdl_data, dl->sdl_nlen);
ifname[dl->sdl_nlen] = '\0';
if (!mapf(ifname, ifm, mdata))
return 0;
}
return 1;
}
static int route_map_stats(char *name, struct if_msghdr *ifmsg, void *data) {
struct ifstat_data *cur;
if ((cur = ifstat_get_interface((struct ifstat_list *) data, name)) == NULL)
return 1;
ifstat_set_interface_stats(cur, ifmsg->ifm_data.ifi_ibytes,
ifmsg->ifm_data.ifi_obytes);
return 1;
}
static int route_get_stats(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
return route_map_ifs(driver, &route_map_stats, ifs);
}
static int route_map_scan(char *name, struct if_msghdr *ifmsg, void *data) {
examine_interface((struct ifstat_list *) data, name,
ifmsg->ifm_flags, ifmsg->ifm_data.ifi_type);
return 1;
}
static int route_scan_interfaces(struct ifstat_driver *driver,
struct ifstat_list *ifs) {
return route_map_ifs(driver, &route_map_scan, (void *) ifs);
}
static void route_close_driver(struct ifstat_driver *driver) {
struct route_driver_data *data = driver->data;
if (data->buf != NULL)
free(data->buf);
free(data);
}
#endif
#ifdef USE_DLPI
#define DLPI_DEFBUF_LEN 1024
#define DLPI_NO_PPA -1
#define DLPI_DEVICE "/dev/dlpi"
struct dlpi_driver_data {
int fd;
unsigned int *buf;
int maxlen;
int ppa;
};