-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnetdev.c
107 lines (84 loc) · 2.4 KB
/
netdev.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
#include "netdev.h"
#define SYSFS_NETS_GLOB "/sys/class/net/*"
TNetDev *NetDevCreate(const char *Name, int Flags)
{
TNetDev *Dev;
Dev=(TNetDev *) calloc(1, sizeof(TNetDev));
Dev->Name=CopyStr(Dev->Name, Name);
Dev->Flags=Flags;
return(Dev);
}
TNetDev *NetDevClone(TNetDev *Parent)
{
TNetDev *Dev;
Dev=NetDevCreate(Parent->Name, Parent->Flags);
Dev->Driver=CopyStr(Dev->Driver, Parent->Driver);
return(Dev);
}
void NetDevDestroy(void *p_Item)
{
TNetDev *Dev;
Dev=(TNetDev *) p_Item;
Destroy(Dev->Name);
Destroy(Dev->Driver);
free(Dev);
}
int NetDevLoadInterfaces(ListNode *Interfaces)
{
glob_t Glob;
TNetDev *Dev;
char *Tempstr=NULL, *Token=NULL;
const char *iname, *ptr;
int i, Flags=0;
STREAM *S;
glob(SYSFS_NETS_GLOB, 0, 0, &Glob);
for (i=0; i < Glob.gl_pathc; i++)
{
iname=GetBasename(Glob.gl_pathv[i]);
if (StrValid(iname))
{
Tempstr=MCopyStr(Tempstr, Glob.gl_pathv[i], "/wireless", NULL);
if (access(Tempstr, F_OK)==0) Flags |= DEV_WIFI;
Dev=NetDevCreate(iname, Flags);
Tempstr=MCopyStr(Tempstr, Glob.gl_pathv[i], "/device/uevent", NULL);
S=STREAMOpen(Tempstr, "r");
if (S)
{
Tempstr=STREAMReadLine(Tempstr, S);
while (Tempstr)
{
StripTrailingWhitespace(Tempstr);
ptr=GetToken(Tempstr, "=", &Token, 0);
if (strcmp(Token, "DRIVER")==0) Dev->Driver=CopyStr(Dev->Driver, ptr);
Tempstr=STREAMReadLine(Tempstr, S);
}
STREAMClose(S);
}
ListAddNamedItem(Interfaces, iname, Dev);
}
}
Destroy(Tempstr);
return(ListSize(Interfaces));
}
TNetDev *NetDevSelectInterface(ListNode *Interfaces, const char *Interface)
{
ListNode *Curr;
TNetDev *Dev;
if (StrValid(Interface)) Curr=ListFindNamedItem(Interfaces, Interface);
else
{
Curr=ListFindNamedItem(Interfaces, "wlan0");
if (! Curr)
{
Curr=ListGetNext(Interfaces);
while (Curr)
{
Dev=(TNetDev *) Curr->Item;
if (Dev->Flags & DEV_WIFI) break;
Curr=ListGetNext(Curr);
}
}
}
if (Curr) return((TNetDev *) Curr->Item);
return(NULL);
}