FreeBSD/Linux Kernel Cross Reference
sys/boot/bootip.c
1 #include <u.h>
2 #include <libc.h>
3 #include <ip.h>
4
5 #include "boot.h"
6
7 static uchar fsip[IPaddrlen];
8 uchar auip[IPaddrlen];
9 static char mpoint[32];
10
11 static int isvalidip(uchar*);
12 static void netndb(char*, uchar*);
13 static void netenv(char*, uchar*);
14
15
16 void
17 configip(int bargc, char **bargv, int needfs)
18 {
19 int argc, pid;
20 char **argv, *p;
21 Waitmsg *w;
22 char **arg;
23 char buf[32];
24
25 fmtinstall('I', eipfmt);
26 fmtinstall('M', eipfmt);
27 fmtinstall('E', eipfmt);
28
29 arg = malloc((bargc+1) * sizeof(char*));
30 if(arg == nil)
31 fatal("malloc");
32 memmove(arg, bargv, bargc * sizeof(char*));
33 arg[bargc] = 0;
34
35 argc = bargc;
36 argv = arg;
37 strcpy(mpoint, "/net");
38 ARGBEGIN {
39 case 'x':
40 p = ARGF();
41 if(p != nil)
42 snprint(mpoint, sizeof(mpoint), "/net%s", p);
43 break;
44 case 'g':
45 case 'b':
46 case 'h':
47 case 'm':
48 p = ARGF();
49 USED(p);
50 break;
51 } ARGEND;
52
53 /* bind in an ip interface */
54 bind("#I", mpoint, MAFTER);
55 bind("#l0", mpoint, MAFTER);
56 bind("#l1", mpoint, MAFTER);
57 bind("#l2", mpoint, MAFTER);
58 bind("#l3", mpoint, MAFTER);
59 werrstr("");
60
61 /* let ipconfig configure the ip interface */
62 switch(pid = fork()){
63 case -1:
64 fatal("configuring ip");
65 case 0:
66 exec("/boot/ipconfig", arg);
67 fatal("execing /ipconfig");
68 default:
69 break;
70 }
71
72 /* wait for ipconfig to finish */
73 for(;;){
74 w = wait();
75 if(w != nil && w->pid == pid){
76 if(w->msg[0] != 0)
77 fatal(w->msg);
78 free(w);
79 break;
80 } else if(w == nil)
81 fatal("configuring ip");
82 free(w);
83 }
84
85 if(!needfs)
86 return;
87
88 /* if we didn't get a file and auth server, query user */
89 netndb("fs", fsip);
90 if(!isvalidip(fsip))
91 netenv("fs", fsip);
92 while(!isvalidip(fsip)){
93 buf[0] = 0;
94 outin("filesystem IP address", buf, sizeof(buf));
95 if (parseip(fsip, buf) == -1)
96 fprint(2, "configip: can't parse fs ip %s\n", buf);
97 }
98
99 netndb("auth", auip);
100 if(!isvalidip(auip))
101 netenv("auth", auip);
102 while(!isvalidip(auip)){
103 buf[0] = 0;
104 outin("authentication server IP address", buf, sizeof(buf));
105 if (parseip(auip, buf) == -1)
106 fprint(2, "configip: can't parse auth ip %s\n", buf);
107 }
108 }
109
110 static void
111 setauthaddr(char *proto, int port)
112 {
113 char buf[128];
114
115 snprint(buf, sizeof buf, "%s!%I!%d", proto, auip, port);
116 authaddr = strdup(buf);
117 }
118
119 void
120 configtcp(Method*)
121 {
122 configip(bargc, bargv, 1);
123 setauthaddr("tcp", 567);
124 }
125
126 int
127 connecttcp(void)
128 {
129 int fd;
130 char buf[64];
131
132 snprint(buf, sizeof buf, "tcp!%I!564", fsip);
133 fd = dial(buf, 0, 0, 0);
134 if (fd < 0)
135 werrstr("dial %s: %r", buf);
136 return fd;
137 }
138
139 void
140 configil(Method*)
141 {
142 configip(bargc, bargv, 1);
143 setauthaddr("tcp", 567);
144 }
145
146 int
147 connectil(void)
148 {
149 char buf[64];
150
151 snprint(buf, sizeof buf, "il!%I!17008", fsip);
152 return dial(buf, 0, 0, 0);
153 }
154
155 static int
156 isvalidip(uchar *ip)
157 {
158 if(ipcmp(ip, IPnoaddr) == 0)
159 return 0;
160 if(ipcmp(ip, v4prefix) == 0)
161 return 0;
162 return 1;
163 }
164
165 static void
166 netenv(char *attr, uchar *ip)
167 {
168 int fd, n;
169 char buf[128];
170
171 ipmove(ip, IPnoaddr);
172 snprint(buf, sizeof(buf), "#e/%s", attr);
173 fd = open(buf, OREAD);
174 if(fd < 0)
175 return;
176
177 n = read(fd, buf, sizeof(buf)-1);
178 if(n <= 0)
179 return;
180 buf[n] = 0;
181 if (parseip(ip, buf) == -1)
182 fprint(2, "netenv: can't parse ip %s\n", buf);
183 }
184
185 static void
186 netndb(char *attr, uchar *ip)
187 {
188 int fd, n, c;
189 char buf[1024];
190 char *p;
191
192 ipmove(ip, IPnoaddr);
193 snprint(buf, sizeof(buf), "%s/ndb", mpoint);
194 fd = open(buf, OREAD);
195 if(fd < 0)
196 return;
197 n = read(fd, buf, sizeof(buf)-1);
198 close(fd);
199 if(n <= 0)
200 return;
201 buf[n] = 0;
202 n = strlen(attr);
203 for(p = buf; ; p++){
204 p = strstr(p, attr);
205 if(p == nil)
206 break;
207 c = *(p-1);
208 if(*(p + n) == '=' && (p == buf || c == '\n' || c == ' ' || c == '\t')){
209 p += n+1;
210 if (parseip(ip, p) == -1)
211 fprint(2, "netndb: can't parse ip %s\n", p);
212 return;
213 }
214 }
215 return;
216 }
Cache object: 6df971450ff1aff72d544e47cd1a2d90
|