-
Notifications
You must be signed in to change notification settings - Fork 2
/
net.c
169 lines (124 loc) · 4.48 KB
/
net.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
#include "net.h"
#include "runcommand.h"
#include "settings.h"
//different versions of DHCPCD have different pidfile locations
static void DHCPCDKill(TNetDev *Interface)
{
char *Tempstr=NULL;
Tempstr=MCopyStr(Tempstr, Settings.PidsDir, "/dhcpcd/", Interface->Name, ".pid", NULL);
PidPathKill(Tempstr);
Tempstr=MCopyStr(Tempstr, "dhcpcd-", Interface->Name, NULL);
PidFileKill(Tempstr);
Tempstr=MCopyStr(Tempstr, "dhclient-", Interface->Name, NULL);
PidFileKill(Tempstr);
usleep(10000);
Destroy(Tempstr);
}
void NetSetupInterface(TNetDev *Interface, const char *Address, const char *Netmask, const char *Gateway, const char *DNSServer)
{
char *Tempstr=NULL, *Output=NULL;
if ( StrValid(Address) && (strcmp(Address, "dhcp")==0) )
{
//Kill off any existing DHCPCD
DHCPCDKill(Interface);
if (DisplayStatus) DisplayStatus(Interface, "~Y~nLaunching dhcpcd");
if (CommandFound("dhcpcd")) Tempstr=MCopyStr(Tempstr, "dhcpcd ", Interface->Name, " -h ", OSSysInfoString(OSINFO_HOSTNAME), NULL);
else Tempstr=MCopyStr(Tempstr, "dhclient ", Interface->Name, " -pf ", Settings.PidsDir, "dhclient-", Interface->Name, ".pid", NULL);
Output=RunCommand(Output, Tempstr, RUNCMD_ROOT | RUNCMD_NOSHELL);
}
else
{
Tempstr=MCopyStr(Tempstr, "ifconfig ", Interface->Name, NULL);
if (StrValid(Address))
{
Tempstr=MCatStr(Tempstr, " ", Address, NULL);
if (DisplayStatus) DisplayStatus(Interface, "~Y~nConfiguring network");
}
if (StrValid(Netmask)) Tempstr=MCatStr(Tempstr, " netmask ", Netmask, NULL);
Tempstr=CatStr(Tempstr, " up");
Tempstr=CatStr(Tempstr, " &> /dev/null");
Output=RunCommand(Output, Tempstr, RUNCMD_ROOT);
if (StrValid(Gateway))
{
Tempstr=MCopyStr(Tempstr, "route add default gw ", Gateway, " &> /dev/null", NULL);
Output=RunCommand(Output, Tempstr, RUNCMD_ROOT);
}
if (StrValid(DNSServer))
{
Tempstr=MCopyStr(Tempstr, "echo \"nameserver ", DNSServer, "\" > /etc/resolv.conf", NULL);
Output=RunCommand(Output, Tempstr, RUNCMD_ROOT);
}
}
Destroy(Tempstr);
Destroy(Output);
}
void NetDown(TNetDev *Dev)
{
char *Tempstr=NULL, *Output=NULL;
Tempstr=MCopyStr(Tempstr,"wpa_supplicant-",Dev->Name,NULL);
PidFileKill(Tempstr);
DHCPCDKill(Dev);
Tempstr=MCopyStr(Tempstr, "ifconfig ", Dev->Name, " down", NULL);
Output=RunCommand(Output, Tempstr, RUNCMD_ROOT);
Destroy(Tempstr);
Destroy(Output);
}
const char *IfConfigToks[]= {"inet", "Bcast", "Mask", "HWaddr", "RX bytes", "TX bytes", NULL};
typedef enum {IFCT_INET_ADDR, IFCT_BCAST, IFCT_NETMASK, IFCT_HWADDR, IFCT_RX, OFCT_TX} EIFCONFIGS;
static void NetGetStatusFromIfConfigParseLine(TNet *Net, const char *Line)
{
char *Tempstr=NULL;
const char *ptr;
ptr=Line;
while (ptr)
{
ptr=GetToken(ptr,"\\S|:|=",&Tempstr,GETTOKEN_MULTI_SEPARATORS|GETTOKEN_QUOTES);
if (! ptr) break;
while (isspace(*ptr)) ptr++;
StripTrailingWhitespace(Tempstr);
StripLeadingWhitespace(Tempstr);
switch (MatchTokenFromList(Tempstr, IfConfigToks, 0))
{
case IFCT_INET_ADDR:
//grab and throw away the 'addr:' part of 'inet addr:'
ptr=GetToken(ptr, ":", &Net->Address, 0);
ptr=GetToken(ptr, "\\S", &Net->Address, 0);
break;
case IFCT_BCAST:
//ptr=GetToken(ptr, "\\S", &Net->Broadcast, 0);
break;
case IFCT_NETMASK:
ptr=GetToken(ptr, "\\S", &Net->Netmask, 0);
break;
case IFCT_HWADDR:
ptr=GetToken(ptr, "\\S", &Net->MacAddress, 0);
break;
}
}
Destroy(Tempstr);
}
static void NetGetStatusFromIfconfig(TNetDev *Dev, TNet *Net)
{
STREAM *S;
char *Output=NULL, *Line=NULL, *Tempstr=NULL;
const char *ptr;
ListNode *Curr;
int val, val2;
Net->Address=CopyStr(Net->Address,"");
Tempstr=MCopyStr(Tempstr, "ifconfig ", Dev->Name, NULL);
Output=RunCommand(Output, Tempstr, 0);
ptr=GetToken(Output, "\n", &Line, 0);
while (ptr)
{
StripTrailingWhitespace(Line);
NetGetStatusFromIfConfigParseLine(Net, Line);
ptr=GetToken(ptr, "\n", &Line, 0);
}
Destroy(Line);
Destroy(Output);
Destroy(Tempstr);
}
void NetGetStatus(TNetDev *Dev, TNet *Net)
{
NetGetStatusFromIfconfig(Dev, Net);
}