This source file includes following definitions.
- bp_whoami
- bp_getfile
- xdr_string_encode
- xdr_string_decode
- xdr_inaddr_encode
- xdr_inaddr_decode
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 #include <sys/param.h>
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42
43 #include <netinet/in.h>
44 #include <netinet/in_systm.h>
45
46 #include <nfs/rpcv2.h>
47
48 #include "stand.h"
49 #include "net.h"
50 #include "netif.h"
51 #include "rpc.h"
52 #include "bootparam.h"
53
54 #ifdef DEBUG_RPC
55 #define RPC_PRINTF(a) printf a
56 #else
57 #define RPC_PRINTF(a)
58 #endif
59
60 struct in_addr bp_server_addr;
61 n_short bp_server_port;
62
63
64
65
66 #define BOOTPARAM_PROG 100026
67 #define BOOTPARAM_VERS 1
68 #define BOOTPARAM_WHOAMI 1
69 #define BOOTPARAM_GETFILE 2
70
71
72
73
74
75 struct xdr_inaddr {
76 u_int32_t atype;
77 int32_t addr[4];
78 };
79
80 int xdr_inaddr_encode(char **p, struct in_addr ia);
81 int xdr_inaddr_decode(char **p, struct in_addr *ia);
82
83 int xdr_string_encode(char **p, char *str, int len);
84 int xdr_string_decode(char **p, char *str, int *len_p);
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102 int
103 bp_whoami(int sockfd)
104 {
105
106 struct args {
107 u_int32_t prog;
108 u_int32_t vers;
109 u_int32_t proc;
110 u_int32_t arglen;
111 struct xdr_inaddr xina;
112 } *args;
113 struct repl {
114 u_int16_t _pad;
115 u_int16_t port;
116 u_int32_t encap_len;
117
118 n_long capsule[64];
119 } *repl;
120 struct {
121 n_long h[RPC_HEADER_WORDS];
122 struct args d;
123 } sdata;
124 struct {
125 n_long h[RPC_HEADER_WORDS];
126 struct repl d;
127 } rdata;
128 char *send_tail, *recv_head;
129 struct iodesc *d;
130 int len, x;
131
132 RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip)));
133
134 if (!(d = socktodesc(sockfd))) {
135 RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd));
136 return (-1);
137 }
138 args = &sdata.d;
139 repl = &rdata.d;
140
141
142
143
144 args->prog = htonl(BOOTPARAM_PROG);
145 args->vers = htonl(BOOTPARAM_VERS);
146 args->proc = htonl(BOOTPARAM_WHOAMI);
147 args->arglen = htonl(sizeof(struct xdr_inaddr));
148 send_tail = (char *)&args->xina;
149
150
151
152
153 if (xdr_inaddr_encode(&send_tail, myip))
154 return (-1);
155
156
157 d->myport = htons(--rpc_port);
158 d->destip.s_addr = INADDR_BROADCAST;
159
160
161 len = rpc_call(d, PMAPPROG, PMAPVERS, PMAPPROC_CALLIT,
162 args, send_tail - (char *)args,
163 repl, sizeof(*repl));
164 if (len < 8) {
165 printf("bootparamd: 'whoami' call failed\n");
166 return (-1);
167 }
168
169
170 rpc_fromaddr(repl, &bp_server_addr, &bp_server_port);
171
172
173
174
175
176
177 bp_server_port = repl->port;
178
179 RPC_PRINTF(("bp_whoami: server at %s:%d\n",
180 inet_ntoa(bp_server_addr), ntohs(bp_server_port)));
181
182
183 rpc_pmap_putcache(bp_server_addr, BOOTPARAM_PROG, BOOTPARAM_VERS,
184 (int)ntohs(bp_server_port));
185
186
187
188
189 x = ntohl(repl->encap_len);
190 if (len < x) {
191 printf("bp_whoami: short reply, %d < %d\n", len, x);
192 return (-1);
193 }
194 recv_head = (char *)repl->capsule;
195
196
197 hostnamelen = MAXHOSTNAMELEN-1;
198 if (xdr_string_decode(&recv_head, hostname, &hostnamelen)) {
199 RPC_PRINTF(("bp_whoami: bad hostname\n"));
200 return (-1);
201 }
202
203
204 domainnamelen = MAXHOSTNAMELEN-1;
205 if (xdr_string_decode(&recv_head, domainname, &domainnamelen)) {
206 RPC_PRINTF(("bp_whoami: bad domainname\n"));
207 return (-1);
208 }
209
210
211 if (xdr_inaddr_decode(&recv_head, &gateip)) {
212 RPC_PRINTF(("bp_whoami: bad gateway\n"));
213 return (-1);
214 }
215
216
217 return(0);
218 }
219
220
221
222
223
224
225
226
227
228 int
229 bp_getfile(int sockfd, char *key, struct in_addr *serv_addr, char *pathname)
230 {
231 struct {
232 n_long h[RPC_HEADER_WORDS];
233 n_long d[64];
234 } sdata;
235 struct {
236 n_long h[RPC_HEADER_WORDS];
237 n_long d[128];
238 } rdata;
239 char serv_name[FNAME_SIZE];
240 char *send_tail, *recv_head;
241
242 struct iodesc *d;
243 int sn_len, path_len, rlen;
244
245 if (!(d = socktodesc(sockfd))) {
246 RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd));
247 return (-1);
248 }
249
250 send_tail = (char *)sdata.d;
251 recv_head = (char *)rdata.d;
252
253
254
255
256
257
258 if (xdr_string_encode(&send_tail, hostname, hostnamelen)) {
259 RPC_PRINTF(("bp_getfile: bad client\n"));
260 return (-1);
261 }
262
263
264 if (xdr_string_encode(&send_tail, key, strlen(key))) {
265 RPC_PRINTF(("bp_getfile: bad key\n"));
266 return (-1);
267 }
268
269
270 d->myport = htons(--rpc_port);
271 d->destip = bp_server_addr;
272
273
274 rlen = rpc_call(d,
275 BOOTPARAM_PROG, BOOTPARAM_VERS, BOOTPARAM_GETFILE,
276 sdata.d, send_tail - (char *)sdata.d,
277 rdata.d, sizeof(rdata.d));
278 if (rlen < 4) {
279 RPC_PRINTF(("bp_getfile: short reply\n"));
280 errno = EBADRPC;
281 return (-1);
282 }
283 recv_head = (char *)rdata.d;
284
285
286
287
288
289
290 sn_len = FNAME_SIZE-1;
291 if (xdr_string_decode(&recv_head, serv_name, &sn_len)) {
292 RPC_PRINTF(("bp_getfile: bad server name\n"));
293 return (-1);
294 }
295
296
297 if (xdr_inaddr_decode(&recv_head, serv_addr)) {
298 RPC_PRINTF(("bp_getfile: bad server addr\n"));
299 return (-1);
300 }
301
302
303 path_len = MAXPATHLEN-1;
304 if (xdr_string_decode(&recv_head, pathname, &path_len)) {
305 RPC_PRINTF(("bp_getfile: bad server path\n"));
306 return (-1);
307 }
308
309
310 return(0);
311 }
312
313
314
315
316
317
318
319 int
320 xdr_string_encode(char **pkt, char *str, int len)
321 {
322 u_int32_t *lenp;
323 char *datap;
324 int padlen = (len + 3) & ~3;
325
326
327 lenp = (u_int32_t*) *pkt;
328 *pkt += sizeof(*lenp);
329 *lenp = htonl(len);
330
331 datap = *pkt;
332 *pkt += padlen;
333 bcopy(str, datap, len);
334
335 return (0);
336 }
337
338 int
339 xdr_string_decode(char **pkt, char *str, int *len_p)
340 {
341 u_int32_t *lenp;
342 char *datap;
343 int slen;
344 int plen;
345
346
347 lenp = (u_int32_t*) *pkt;
348 *pkt += sizeof(*lenp);
349 slen = ntohl(*lenp);
350 plen = (slen + 3) & ~3;
351
352 if (slen > *len_p)
353 slen = *len_p;
354 datap = *pkt;
355 *pkt += plen;
356 bcopy(datap, str, slen);
357
358 str[slen] = '\0';
359 *len_p = slen;
360
361 return (0);
362 }
363
364 int
365 xdr_inaddr_encode(char **pkt, struct in_addr ia)
366 {
367 struct xdr_inaddr *xi;
368 u_char *cp;
369 int32_t *ip;
370 union {
371 n_long l;
372 u_char c[4];
373 } uia;
374
375
376 xi = (struct xdr_inaddr *) *pkt;
377 *pkt += sizeof(*xi);
378 xi->atype = htonl(1);
379 uia.l = ia.s_addr;
380 cp = uia.c;
381 ip = xi->addr;
382
383
384
385
386
387 *ip++ = htonl((unsigned int)*cp++);
388 *ip++ = htonl((unsigned int)*cp++);
389 *ip++ = htonl((unsigned int)*cp++);
390 *ip++ = htonl((unsigned int)*cp++);
391
392 return (0);
393 }
394
395 int
396 xdr_inaddr_decode(char **pkt, struct in_addr *ia)
397 {
398 struct xdr_inaddr *xi;
399 u_char *cp;
400 int32_t *ip;
401 union {
402 n_long l;
403 u_char c[4];
404 } uia;
405
406
407 xi = (struct xdr_inaddr *) *pkt;
408 *pkt += sizeof(*xi);
409 if (xi->atype != htonl(1)) {
410 RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
411 ntohl(xi->atype)));
412 return(-1);
413 }
414
415 cp = uia.c;
416 ip = xi->addr;
417
418
419
420
421
422 *cp++ = ntohl(*ip++);
423 *cp++ = ntohl(*ip++);
424 *cp++ = ntohl(*ip++);
425 *cp++ = ntohl(*ip++);
426 ia->s_addr = uia.l;
427
428 return (0);
429 }