summaryrefslogtreecommitdiff
path: root/appl/cmd/ip/ppp/pppdial.b
blob: ec689dc1e56a99c74e99499fb81f26133ba5a872 (plain)
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
implement PPPdial;

#
#	Module:		ispservice
#	Purpose:		Simple PPP Dial-on-Demand
#	Author:		Eric Van Hensbergen (ericvh@lucent.com)
#
# Copyright © 1998-1999 Lucent Technologies Inc.  All rights reserved.
# Revisions copyright © 2000-2003 Vita Nuova Holdings Limited.  All rights reserved.
#

include "sys.m";
	sys: Sys;
include "draw.m";
	draw: Draw;

include "cfgfile.m";
	cfg:	CfgFile;
	ConfigFile: import cfg;

include "lock.m";
include "modem.m";
include "script.m";
include "pppclient.m";
	ppp: PPPClient;
include "pppgui.m";

PPPdial: module
{
	init:	fn(nil: ref Draw->Context): string;
	connect:	fn(): string;
};

context:		ref Draw->Context;
modeminfo:		ref Modem->ModemInfo;
pppinfo:		ref PPPClient->PPPInfo;
scriptinfo:		ref Script->ScriptInfo;
isp_number:		string;						# should be part of pppinfo
lastCdir:		ref Sys->Dir;	# state of file when last read

DEFAULT_ISP_DB_PATH:	con "/services/ppp/isp.cfg";	# contains pppinfo & scriptinfo
DEFAULT_MODEM_DB_PATH:	con	"/services/ppp/modem.cfg";			# contains modeminfo
MODEM_DB_PATH:	con	"/usr/inferno/config/modem.cfg";			# contains modeminfo
ISP_DB_PATH:	con "/usr/inferno/config/isp.cfg";		# contains pppinfo & scriptinfo
ISP_RETRIES:	con 5;

getcfgstring(c: ref ConfigFile, key: string) :string
{
	l := c.getcfg(key);
	if (l == nil)
		return nil;
	for(ret := ""; l != nil; l = tl l)
		ret+= " " + hd l;
	
	return ret[1:];		# trim the first space
}

configinit()
{
	mi:	Modem->ModemInfo;
	pppi: PPPClient->PPPInfo;
	info: list of string;

	cfg = load CfgFile CfgFile->PATH;
	if (cfg == nil)
		raise "fail: load CfgFile";

	# Modem Configuration
	
	cfg->verify(DEFAULT_MODEM_DB_PATH, MODEM_DB_PATH);
	modemcfg := cfg->init(MODEM_DB_PATH);
	if (modemcfg == nil)
		raise "fail: read: "+MODEM_DB_PATH;
	modeminfo = ref mi;

	modeminfo.path = getcfgstring(modemcfg, "PATH");
	modeminfo.init = getcfgstring(modemcfg, "INIT");
	modeminfo.country = getcfgstring(modemcfg, "COUNTRY");
	modeminfo.other = getcfgstring(modemcfg, "OTHER");
	modeminfo.errorcorrection = getcfgstring(modemcfg,"CORRECT");
	modeminfo.compression = getcfgstring(modemcfg,"COMPRESS");
	modeminfo.flowctl = getcfgstring(modemcfg,"FLOWCTL");
	modeminfo.rateadjust = getcfgstring(modemcfg,"RATEADJ");
	modeminfo.mnponly = getcfgstring(modemcfg,"MNPONLY");
	modeminfo.dialtype = getcfgstring(modemcfg,"DIALING");
	if(modeminfo.dialtype!="ATDP")
		modeminfo.dialtype="ATDT";

	cfg->verify(DEFAULT_ISP_DB_PATH, ISP_DB_PATH);
	(ok, stat) := sys->stat(ISP_DB_PATH);
	if(ok >= 0)
		lastCdir = ref stat;
	sys->print("cfg->init(%s)\n", ISP_DB_PATH);

	# ISP Configuration
	pppcfg := cfg->init(ISP_DB_PATH);
	if (pppcfg == nil)
		raise "fail: Couldn't load ISP configuration file: "+ISP_DB_PATH;
	pppinfo = ref pppi;
	isp_number = getcfgstring(pppcfg, "NUMBER");
	pppinfo.ipaddr = getcfgstring(pppcfg,"IPADDR");
	pppinfo.ipmask = getcfgstring(pppcfg,"IPMASK");
	pppinfo.peeraddr = getcfgstring(pppcfg,"PEERADDR");
	pppinfo.maxmtu = getcfgstring(pppcfg,"MAXMTU");
	pppinfo.username = getcfgstring(pppcfg,"USERNAME");
	pppinfo.password = getcfgstring(pppcfg,"PASSWORD");

	info = pppcfg.getcfg("SCRIPT");
	if (info != nil) {
		scriptinfo = ref Script->ScriptInfo;
		scriptinfo.path = hd info;
		scriptinfo.username = pppinfo.username;
		scriptinfo.password = pppinfo.password;
	} else
		scriptinfo = nil;

	info = pppcfg.getcfg("TIMEOUT");
	if (info != nil)
		scriptinfo.timeout = int (hd info);

	cfg = nil;	# might as well unload it
}

#
# Parts of the following two functions could be generalized
#

isipaddr(a: string): int
{
	i, c, ac, np: int = 0;
 
	for(i = 0; i < len a; i++) {
		c = a[i];
		if(c >= '0' && c <= '9') {
			np = 10*np + c - '0';
			continue;
		}
		if (c == '.' && np) {
			ac++;
	 		if (np > 255)
				return 0;
			np = 0;
			continue;
		}
		return 0;
	}
	return np && np < 256 && ac == 3;
}

# check if there is an existing PPP connection
connected(): int
{
	ifd := sys->open("/net/ipifc", Sys->OREAD);
	if(ifd == nil)
		return 0;

	buf := array[1024] of byte;

	for(;;) {
		(n, d) := sys->dirread(ifd);
		if (n <= 0)
			return 0;
		for(i := 0; i < n; i++)
			if(d[i].name[0] <= '9') {
				sfd := sys->open("/net/ipifc/"+d[i].name+"/status", Sys->OREAD);
				if (sfd == nil)
					continue;
				ns := sys->read(sfd, buf, len buf);
				if (ns <= 0)
					continue;
				(nflds, flds) := sys->tokenize(string buf[0:ns], " \t\r\n");
				if(nflds < 4)
					continue;
				if (isipaddr(hd tl tl flds))
					return 1;
			}
	}
}

#
# called once when loaded
#
init(ctxt: ref Draw->Context): string
{
	sys = load Sys Sys->PATH;
	{
		ppp = load PPPClient PPPClient->PATH;
		if (ppp == nil)
			raise "fail: Couldn't load ppp module";

		# Contruct Config Tables During Init - may want to change later
		#	for multiple configs (Software Download Server versus ISP)
		configinit();	
		context = ctxt;
	}exception e {
	"fail:*" =>
		return e;
	}
	return nil;
}

dialup_cancelled := 0;
connecting := 0;

#
# called each time a translation is needed, to check that we're on line(!)
# eventually this will be replaced by a packet interface that does dial-on-demand
#
connect(): string
{
	{
		dialup_cancelled = 0;
		(ok, stat) := sys->stat(ISP_DB_PATH);
		if (ok < 0 || lastCdir == nil || !samefile(*lastCdir, stat))
			configinit();
		errc := chan of string;
		while(!connected()){
			if(!connecting) {
				connecting = 1;
				sync := chan of int;
				spawn pppconnect(errc, sync);
				<- sync;
				return <-errc;
			}else{
				sys->sleep(2500);
				if (dialup_cancelled)
					return "fail: dialup cancelled";	
			}
		}
	}exception e{
	"fail:*" =>
		return e;
	"*" =>
		sys->print("pppdial: caught exception: %s\n", e);
		return "fail: internal error: "+e;
	}
	return nil;	
}

pppconnect(errc: chan of string, sync: chan of int)
{
	connecting = 1;
	sys->pctl(Sys->NEWPGRP, nil);
	sync <-= 0;
	resp_chan: chan of int;
	logger := chan of int;
	pppgui := load PPPGUI PPPGUI->PATH;
	for (count :=0; count < ISP_RETRIES; count++) {
		resp_chan = pppgui->init(context, logger, ppp, nil);
		spawn ppp->connect(modeminfo, isp_number, scriptinfo, pppinfo, logger);
		x := <-resp_chan;
		if (x > 0) {
			if (x == 1) {
				# alt needed in case calling process has been killed
				alt {
					errc <-= nil => ;
					* => ;
				}
			} else	{		# user cancelled dial-in
				dialup_cancelled = 1;
				alt {
					errc <-= "fail: dialup cancelled" => ;
					* => ;
				}
			}
			connecting = 0;
			return;
		}
		# else connect failed, go around loop to try again
	}
	alt {
		errc <-= "fail: dialup failed" => ;
		* => ;
	}
	connecting = 0;
}

samefile(d1, d2: Sys->Dir): int
{
	return d1.dev==d2.dev && d1.dtype==d2.dtype &&
			d1.qid.path==d2.qid.path && d1.qid.vers==d2.qid.vers &&
			d1.mtime==d2.mtime;
}