1 /* $NetBSD: bootparam.c,v 1.11 1997/06/26 19:11:32 drochner Exp $ */
4 * Copyright (c) 1995 Gordon W. Ross
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 * 4. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by Gordon W. Ross
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * $DragonFly: src/lib/libstand/bootparam.c,v 1.2 2005/12/11 02:27:26 swildner Exp $
39 #include <sys/param.h>
40 #include <sys/socket.h>
44 #include <netinet/in.h>
45 #include <netinet/in_systm.h>
55 #include "bootparam.h"
58 #define RPC_PRINTF(a) printf a
63 struct in_addr bp_server_addr
; /* net order */
64 n_short bp_server_port
; /* net order */
67 * RPC definitions for bootparamd
69 #define BOOTPARAM_PROG 100026
70 #define BOOTPARAM_VERS 1
71 #define BOOTPARAM_WHOAMI 1
72 #define BOOTPARAM_GETFILE 2
75 * Inet address in RPC messages
76 * (Note, really four ints, NOT chars. Blech.)
83 int xdr_inaddr_encode(char **p
, struct in_addr ia
);
84 int xdr_inaddr_decode(char **p
, struct in_addr
*ia
);
86 int xdr_string_encode(char **p
, char *str
, int len
);
87 int xdr_string_decode(char **p
, char *str
, int *len_p
);
91 * RPC: bootparam/whoami
92 * Given client IP address, get:
93 * client name (hostname)
94 * domain name (domainname)
97 * The hostname and domainname are set here for convenience.
99 * Note - bpsin is initialized to the broadcast address,
100 * and will be replaced with the bootparam server address
101 * after this call is complete. Have to use PMAP_PROC_CALL
102 * to make sure we get responses only from a servers that
103 * know about us (don't want to broadcast a getport call).
106 bp_whoami(int sockfd
)
108 /* RPC structures for PMAPPROC_CALLIT */
114 struct xdr_inaddr xina
;
120 /* encapsulated data here */
124 n_long h
[RPC_HEADER_WORDS
];
128 n_long h
[RPC_HEADER_WORDS
];
131 char *send_tail
, *recv_head
;
135 RPC_PRINTF(("bp_whoami: myip=%s\n", inet_ntoa(myip
)));
137 if (!(d
= socktodesc(sockfd
))) {
138 RPC_PRINTF(("bp_whoami: bad socket. %d\n", sockfd
));
145 * Build request args for PMAPPROC_CALLIT.
147 args
->prog
= htonl(BOOTPARAM_PROG
);
148 args
->vers
= htonl(BOOTPARAM_VERS
);
149 args
->proc
= htonl(BOOTPARAM_WHOAMI
);
150 args
->arglen
= htonl(sizeof(struct xdr_inaddr
));
151 send_tail
= (char*) &args
->xina
;
154 * append encapsulated data (client IP address)
156 if (xdr_inaddr_encode(&send_tail
, myip
))
159 /* RPC: portmap/callit */
160 d
->myport
= htons(--rpc_port
);
161 d
->destip
.s_addr
= INADDR_BROADCAST
; /* XXX: subnet bcast? */
162 /* rpc_call will set d->destport */
164 len
= rpc_call(d
, PMAPPROG
, PMAPVERS
, PMAPPROC_CALLIT
,
165 args
, send_tail
- (char*)args
,
166 repl
, sizeof(*repl
));
168 printf("bootparamd: 'whoami' call failed\n");
172 /* Save bootparam server address (from IP header). */
173 rpc_fromaddr(repl
, &bp_server_addr
, &bp_server_port
);
176 * Note that bp_server_port is now 111 due to the
177 * indirect call (using PMAPPROC_CALLIT), so get the
178 * actual port number from the reply data.
180 bp_server_port
= repl
->port
;
182 RPC_PRINTF(("bp_whoami: server at %s:%d\n",
183 inet_ntoa(bp_server_addr
), ntohs(bp_server_port
)));
185 /* We have just done a portmap call, so cache the portnum. */
186 rpc_pmap_putcache(bp_server_addr
,
189 (int)ntohs(bp_server_port
));
192 * Parse the encapsulated results from bootparam/whoami
194 x
= ntohl(repl
->encap_len
);
196 printf("bp_whoami: short reply, %d < %d\n", len
, x
);
199 recv_head
= (char*) repl
->capsule
;
202 hostnamelen
= MAXHOSTNAMELEN
-1;
203 if (xdr_string_decode(&recv_head
, hostname
, &hostnamelen
)) {
204 RPC_PRINTF(("bp_whoami: bad hostname\n"));
209 domainnamelen
= MAXHOSTNAMELEN
-1;
210 if (xdr_string_decode(&recv_head
, domainname
, &domainnamelen
)) {
211 RPC_PRINTF(("bp_whoami: bad domainname\n"));
215 /* gateway address */
216 if (xdr_inaddr_decode(&recv_head
, &gateip
)) {
217 RPC_PRINTF(("bp_whoami: bad gateway\n"));
227 * RPC: bootparam/getfile
228 * Given client name and file "key", get:
234 bp_getfile(int sockfd
, char *key
, struct in_addr
*serv_addr
, char *pathname
)
237 n_long h
[RPC_HEADER_WORDS
];
241 n_long h
[RPC_HEADER_WORDS
];
244 char serv_name
[FNAME_SIZE
];
245 char *send_tail
, *recv_head
;
248 int sn_len
, path_len
, rlen
;
250 if (!(d
= socktodesc(sockfd
))) {
251 RPC_PRINTF(("bp_getfile: bad socket. %d\n", sockfd
));
255 send_tail
= (char*) sdata
.d
;
256 recv_head
= (char*) rdata
.d
;
259 * Build request message.
262 /* client name (hostname) */
263 if (xdr_string_encode(&send_tail
, hostname
, hostnamelen
)) {
264 RPC_PRINTF(("bp_getfile: bad client\n"));
268 /* key name (root or swap) */
269 if (xdr_string_encode(&send_tail
, key
, strlen(key
))) {
270 RPC_PRINTF(("bp_getfile: bad key\n"));
274 /* RPC: bootparam/getfile */
275 d
->myport
= htons(--rpc_port
);
276 d
->destip
= bp_server_addr
;
277 /* rpc_call will set d->destport */
280 BOOTPARAM_PROG
, BOOTPARAM_VERS
, BOOTPARAM_GETFILE
,
281 sdata
.d
, send_tail
- (char*)sdata
.d
,
282 rdata
.d
, sizeof(rdata
.d
));
284 RPC_PRINTF(("bp_getfile: short reply\n"));
288 recv_head
= (char*) rdata
.d
;
291 * Parse result message.
295 sn_len
= FNAME_SIZE
-1;
296 if (xdr_string_decode(&recv_head
, serv_name
, &sn_len
)) {
297 RPC_PRINTF(("bp_getfile: bad server name\n"));
301 /* server IP address (mountd/NFS) */
302 if (xdr_inaddr_decode(&recv_head
, serv_addr
)) {
303 RPC_PRINTF(("bp_getfile: bad server addr\n"));
307 /* server pathname */
308 path_len
= MAXPATHLEN
-1;
309 if (xdr_string_decode(&recv_head
, pathname
, &path_len
)) {
310 RPC_PRINTF(("bp_getfile: bad server path\n"));
320 * eXternal Data Representation routines.
321 * (but with non-standard args...)
326 xdr_string_encode(char **pkt
, char *str
, int len
)
330 int padlen
= (len
+ 3) & ~3; /* padded length */
332 /* The data will be int aligned. */
333 lenp
= (u_int32_t
*) *pkt
;
334 *pkt
+= sizeof(*lenp
);
339 bcopy(str
, datap
, len
);
349 xdr_string_decode(char **pkt
, char *str
, int *len_p
)
353 int slen
; /* string length */
354 int plen
; /* padded length */
356 /* The data will be int aligned. */
357 lenp
= (u_int32_t
*) *pkt
;
358 *pkt
+= sizeof(*lenp
);
360 plen
= (slen
+ 3) & ~3;
366 bcopy(datap
, str
, slen
);
379 xdr_inaddr_encode(char **pkt
, struct in_addr ia
)
381 struct xdr_inaddr
*xi
;
385 n_long l
; /* network order */
389 /* The data will be int aligned. */
390 xi
= (struct xdr_inaddr
*) *pkt
;
392 xi
->atype
= htonl(1);
397 * Note: the htonl() calls below DO NOT
398 * imply that uia.l is in host order.
399 * In fact this needs it in net order.
401 *ip
++ = htonl((unsigned int)*cp
++);
402 *ip
++ = htonl((unsigned int)*cp
++);
403 *ip
++ = htonl((unsigned int)*cp
++);
404 *ip
++ = htonl((unsigned int)*cp
++);
414 xdr_inaddr_decode(char **pkt
, struct in_addr
*ia
)
416 struct xdr_inaddr
*xi
;
420 n_long l
; /* network order */
424 /* The data will be int aligned. */
425 xi
= (struct xdr_inaddr
*) *pkt
;
427 if (xi
->atype
!= htonl(1)) {
428 RPC_PRINTF(("xdr_inaddr_decode: bad addrtype=%d\n",
436 * Note: the ntohl() calls below DO NOT
437 * imply that uia.l is in host order.
438 * In fact this needs it in net order.
440 *cp
++ = ntohl(*ip
++);
441 *cp
++ = ntohl(*ip
++);
442 *cp
++ = ntohl(*ip
++);
443 *cp
++ = ntohl(*ip
++);