summaryrefslogtreecommitdiff
path: root/appl/cmd/ip/dhcp.b
blob: 2957b5b41f1b74551e3be28f66ec95b5ef8ce80b (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
implement Dhcp;

#
# configure an interface using DHCP
#

include "sys.m";
	sys: Sys;

include "draw.m";

include "ip.m";
	ip: IP;
	IPv4off, IPaddrlen: import IP;
	IPaddr: import ip;
	get2, get4, put2, put4: import ip;

include "dhcp.m";
	dhcpclient: Dhcpclient;
	Bootconf, Lease: import dhcpclient;

include "arg.m";

Dhcp: module
{
	init:	fn(nil: ref Draw->Context, nil: list of string);
};

RetryTime: con 10*1000;	# msec

init(nil: ref Draw->Context, args: list of string)
{
	sys = load Sys Sys->PATH;
	ip = load IP IP->PATH;
	dhcpclient = load Dhcpclient Dhcpclient->PATH;

	sys->pctl(Sys->NEWFD|Sys->NEWPGRP, 0 :: 1 :: 2 :: nil);

	arg := load Arg Arg->PATH;
	arg->init(args);
	arg->setusage("dhcp [-bdmnpr] [-g ipgw] [-h hostname] [-x /net] ifcdir [ip [ipmask]]");
	trace := 0;
	pcfg := 0;
	bootp := 0;
	monitor := 0;
	retry := 0;
	noctl := 0;
	netdir := "/net";
	cfg := Bootconf.new();
	while((o := arg->opt()) != 0)
		case o {
		'b' =>	bootp = 1;
		'd' =>	trace++;
		'g' =>	cfg.ipgw = arg->earg();
		'h' =>	cfg.puts(Dhcpclient->Ohostname, arg->earg());
		'm' =>	monitor = 1;
		'n' =>	noctl = 1;
		'p' =>	pcfg = 1;
		'r' =>		retry = 1;
		'x' =>	netdir = arg->earg();
		* =>		arg->usage();
		}
	args = arg->argv();
	if(len args == 0)
		arg->usage();

	ifcdir := hd args;
	args = tl args;
	if(args != nil){
		cfg.ip = hd args;
		args = tl args;
		if(args != nil){
			cfg.ipmask = hd args;
			args = tl args;
			if(args != nil)
				arg->usage();
		}
	}
	arg = nil;

	ifcctl: ref Sys->FD;
	if(noctl == 0){
		ifcctl = sys->open(ifcdir+"/ctl", Sys->OWRITE);
		if(ifcctl == nil)
			err(sys->sprint("cannot open %s/ctl: %r", ifcdir));
	}
	etherdir := finddev(ifcdir);
	if(etherdir == nil)
		err(sys->sprint("cannot find network device in %s/status: %r", ifcdir));
	if(etherdir[0] != '/' && etherdir[0] != '#')
		etherdir = netdir+"/"+etherdir;

	ip->init();
	dhcpclient->init();
	dhcpclient->tracing(trace);
	e: string;
	lease: ref Lease;
	for(;;){
		if(bootp){
			(cfg, e) = dhcpclient->bootp(netdir, ifcctl, etherdir+"/addr", cfg);
			if(e == nil){
				if(cfg != nil)
					dhcpclient->applycfg(netdir, ifcctl, cfg);
				if(pcfg)
					printcfg(cfg);
				break;
			}
		}else{
			(cfg, lease, e) = dhcpclient->dhcp(netdir, ifcctl, etherdir+"/addr", cfg, nil);	# last is array of int options
			if(e == nil){
				if(pcfg)
					printcfg(cfg);
				if(cfg.lease > 0 && monitor)
					leasemon(lease.configs, pcfg);
				break;
			}
		}
		if(!retry)
			err("failed to configure network: "+e);
		sys->fprint(sys->fildes(2), "dhcp: failed to configure network: %s; retrying", e);
		sys->sleep(RetryTime);
	}
}

leasemon(configs: chan of (ref Bootconf, string), pcfg: int)
{
	for(;;){
		(cfg, e) := <-configs;
		if(e != nil)
			sys->fprint(sys->fildes(2), "dhcp: %s", e);
		if(pcfg)
			printcfg(cfg);
	}
}

printcfg(cfg: ref Bootconf)
{
	sys->print("ip=%s ipmask=%s ipgw=%s iplease=%d\n", cfg.ip, cfg.ipmask, cfg.ipgw, cfg.lease);
}

finddev(ifcdir: string): string
{
	fd := sys->open(ifcdir+"/status", Sys->OREAD);
	if(fd == nil)
		return nil;
	buf := array[1024] of byte;
	n := sys->read(fd, buf, len buf);
	if(n < 0)
		return nil;
	(nf, l) := sys->tokenize(string buf[0:n], " \n");
	if(nf < 2){
		sys->werrstr("unexpected format for status file");
		return nil;
	}
	return hd tl l;
}

err(s: string)
{
	sys->fprint(sys->fildes(2), "dhcp: %s\n", s);
	raise "fail:error";
}