krpc_subr.c revision 1.8 1 1.8 gwr /* $NetBSD: krpc_subr.c,v 1.8 1995/04/24 21:55:05 gwr Exp $ */
2 1.3 cgd
3 1.1 glass /*
4 1.8 gwr * Copyright (c) 1995 Gordon Ross, Adam Glass
5 1.1 glass * Copyright (c) 1992 Regents of the University of California.
6 1.1 glass * All rights reserved.
7 1.1 glass *
8 1.1 glass * This software was developed by the Computer Systems Engineering group
9 1.1 glass * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
10 1.1 glass * contributed to Berkeley.
11 1.1 glass *
12 1.1 glass * Redistribution and use in source and binary forms, with or without
13 1.1 glass * modification, are permitted provided that the following conditions
14 1.1 glass * are met:
15 1.1 glass * 1. Redistributions of source code must retain the above copyright
16 1.1 glass * notice, this list of conditions and the following disclaimer.
17 1.1 glass * 2. Redistributions in binary form must reproduce the above copyright
18 1.1 glass * notice, this list of conditions and the following disclaimer in the
19 1.1 glass * documentation and/or other materials provided with the distribution.
20 1.1 glass * 3. All advertising materials mentioning features or use of this software
21 1.1 glass * must display the following acknowledgement:
22 1.1 glass * This product includes software developed by the University of
23 1.1 glass * California, Lawrence Berkeley Laboratory and its contributors.
24 1.1 glass * 4. Neither the name of the University nor the names of its contributors
25 1.1 glass * may be used to endorse or promote products derived from this software
26 1.1 glass * without specific prior written permission.
27 1.1 glass *
28 1.1 glass * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
29 1.1 glass * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
30 1.1 glass * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
31 1.1 glass * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
32 1.1 glass * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33 1.1 glass * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34 1.1 glass * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35 1.1 glass * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
36 1.1 glass * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
37 1.1 glass * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 1.1 glass * SUCH DAMAGE.
39 1.1 glass *
40 1.1 glass * partially based on:
41 1.1 glass * libnetboot/rpc.c
42 1.1 glass * @(#) Header: rpc.c,v 1.12 93/09/28 08:31:56 leres Exp (LBL)
43 1.1 glass */
44 1.1 glass
45 1.1 glass #include <sys/param.h>
46 1.8 gwr #include <sys/systm.h>
47 1.1 glass #include <sys/conf.h>
48 1.1 glass #include <sys/ioctl.h>
49 1.1 glass #include <sys/proc.h>
50 1.1 glass #include <sys/mount.h>
51 1.1 glass #include <sys/mbuf.h>
52 1.8 gwr #include <sys/reboot.h>
53 1.1 glass #include <sys/socket.h>
54 1.8 gwr #include <sys/socketvar.h>
55 1.1 glass
56 1.1 glass #include <net/if.h>
57 1.1 glass #include <netinet/in.h>
58 1.1 glass
59 1.1 glass #include <nfs/rpcv2.h>
60 1.7 gwr #include <nfs/krpc.h>
61 1.1 glass
62 1.1 glass /*
63 1.1 glass * Kernel support for Sun RPC
64 1.1 glass *
65 1.1 glass * Used currently for bootstrapping in nfs diskless configurations.
66 1.1 glass */
67 1.1 glass
68 1.1 glass /*
69 1.1 glass * Generic RPC headers
70 1.1 glass */
71 1.1 glass
72 1.1 glass struct auth_info {
73 1.8 gwr int32_t rp_atype; /* auth type */
74 1.8 gwr u_int32_t rp_alen; /* auth length */
75 1.1 glass };
76 1.1 glass
77 1.1 glass struct rpc_call {
78 1.8 gwr u_int32_t rp_xid; /* request transaction id */
79 1.8 gwr int32_t rp_direction; /* call direction (0) */
80 1.8 gwr u_int32_t rp_rpcvers; /* rpc version (2) */
81 1.8 gwr u_int32_t rp_prog; /* program */
82 1.8 gwr u_int32_t rp_vers; /* version */
83 1.8 gwr u_int32_t rp_proc; /* procedure */
84 1.1 glass struct auth_info rp_auth;
85 1.1 glass struct auth_info rp_verf;
86 1.1 glass };
87 1.1 glass
88 1.1 glass struct rpc_reply {
89 1.8 gwr u_int32_t rp_xid; /* request transaction id */
90 1.8 gwr int32_t rp_direction; /* call direction (1) */
91 1.8 gwr int32_t rp_astatus; /* accept status (0: accepted) */
92 1.1 glass union {
93 1.8 gwr u_int32_t rpu_errno;
94 1.1 glass struct {
95 1.1 glass struct auth_info rp_auth;
96 1.8 gwr u_int32_t rp_rstatus; /* reply status */
97 1.1 glass } rpu_ok;
98 1.1 glass } rp_u;
99 1.1 glass };
100 1.1 glass
101 1.1 glass #define MIN_REPLY_HDR 16 /* xid, dir, astat, errno */
102 1.1 glass
103 1.1 glass /*
104 1.2 gwr * What is the longest we will wait before re-sending a request?
105 1.2 gwr * Note this is also the frequency of "RPC timeout" messages.
106 1.2 gwr * The re-send loop count sup linearly to this maximum, so the
107 1.2 gwr * first complaint will happen after (1+2+3+4+5)=15 seconds.
108 1.2 gwr */
109 1.2 gwr #define MAX_RESEND_DELAY 5 /* seconds */
110 1.2 gwr
111 1.2 gwr /*
112 1.1 glass * Call portmap to lookup a port number for a particular rpc program
113 1.2 gwr * Returns non-zero error on failure.
114 1.1 glass */
115 1.1 glass int
116 1.7 gwr krpc_portmap(sin, prog, vers, portp)
117 1.7 gwr struct sockaddr_in *sin; /* server address */
118 1.8 gwr u_int prog, vers; /* host order */
119 1.8 gwr u_int16_t *portp; /* network order */
120 1.1 glass {
121 1.1 glass struct sdata {
122 1.8 gwr u_int32_t prog; /* call program */
123 1.8 gwr u_int32_t vers; /* call version */
124 1.8 gwr u_int32_t proto; /* call protocol */
125 1.8 gwr u_int32_t port; /* call port (unused) */
126 1.1 glass } *sdata;
127 1.1 glass struct rdata {
128 1.8 gwr u_int16_t pad;
129 1.8 gwr u_int16_t port;
130 1.1 glass } *rdata;
131 1.1 glass struct mbuf *m;
132 1.1 glass int error;
133 1.1 glass
134 1.1 glass /* The portmapper port is fixed. */
135 1.1 glass if (prog == PMAPPROG) {
136 1.1 glass *portp = htons(PMAPPORT);
137 1.1 glass return 0;
138 1.1 glass }
139 1.1 glass
140 1.8 gwr m = m_get(M_WAIT, MT_DATA);
141 1.1 glass if (m == NULL)
142 1.1 glass return ENOBUFS;
143 1.8 gwr sdata = mtod(m, struct sdata *);
144 1.1 glass m->m_len = sizeof(*sdata);
145 1.1 glass
146 1.1 glass /* Do the RPC to get it. */
147 1.1 glass sdata->prog = htonl(prog);
148 1.1 glass sdata->vers = htonl(vers);
149 1.1 glass sdata->proto = htonl(IPPROTO_UDP);
150 1.1 glass sdata->port = 0;
151 1.1 glass
152 1.7 gwr sin->sin_port = htons(PMAPPORT);
153 1.7 gwr error = krpc_call(sin, PMAPPROG, PMAPVERS,
154 1.7 gwr PMAPPROC_GETPORT, &m, NULL);
155 1.1 glass if (error)
156 1.1 glass return error;
157 1.1 glass
158 1.8 gwr if (m->m_len < sizeof(*rdata)) {
159 1.8 gwr m = m_pullup(m, sizeof(*rdata));
160 1.8 gwr if (m == NULL)
161 1.8 gwr return ENOBUFS;
162 1.8 gwr }
163 1.1 glass rdata = mtod(m, struct rdata *);
164 1.1 glass *portp = rdata->port;
165 1.1 glass
166 1.1 glass m_freem(m);
167 1.1 glass return 0;
168 1.1 glass }
169 1.1 glass
170 1.1 glass /*
171 1.1 glass * Do a remote procedure call (RPC) and wait for its reply.
172 1.7 gwr * If from_p is non-null, then we are doing broadcast, and
173 1.7 gwr * the address from whence the response came is saved there.
174 1.1 glass */
175 1.1 glass int
176 1.7 gwr krpc_call(sa, prog, vers, func, data, from_p)
177 1.7 gwr struct sockaddr_in *sa;
178 1.8 gwr u_int prog, vers, func;
179 1.1 glass struct mbuf **data; /* input/output */
180 1.7 gwr struct mbuf **from_p; /* output */
181 1.1 glass {
182 1.1 glass struct socket *so;
183 1.1 glass struct sockaddr_in *sin;
184 1.7 gwr struct mbuf *m, *nam, *mhead, *from;
185 1.1 glass struct rpc_call *call;
186 1.1 glass struct rpc_reply *reply;
187 1.1 glass struct uio auio;
188 1.1 glass int error, rcvflg, timo, secs, len;
189 1.8 gwr static u_int32_t xid = ~0xFF;
190 1.8 gwr u_int tport;
191 1.1 glass
192 1.1 glass /*
193 1.1 glass * Validate address family.
194 1.1 glass * Sorry, this is INET specific...
195 1.1 glass */
196 1.7 gwr if (sa->sin_family != AF_INET)
197 1.1 glass return (EAFNOSUPPORT);
198 1.1 glass
199 1.1 glass /* Free at end if not null. */
200 1.1 glass nam = mhead = NULL;
201 1.7 gwr from = NULL;
202 1.1 glass
203 1.1 glass /*
204 1.1 glass * Create socket and set its recieve timeout.
205 1.1 glass */
206 1.1 glass if ((error = socreate(AF_INET, &so, SOCK_DGRAM, 0)))
207 1.1 glass goto out;
208 1.1 glass
209 1.1 glass m = m_get(M_WAIT, MT_SOOPTS);
210 1.1 glass if (m == NULL) {
211 1.1 glass error = ENOBUFS;
212 1.1 glass goto out;
213 1.7 gwr } else {
214 1.7 gwr struct timeval *tv;
215 1.7 gwr tv = mtod(m, struct timeval *);
216 1.7 gwr m->m_len = sizeof(*tv);
217 1.7 gwr tv->tv_sec = 1;
218 1.7 gwr tv->tv_usec = 0;
219 1.7 gwr if ((error = sosetopt(so, SOL_SOCKET, SO_RCVTIMEO, m)))
220 1.7 gwr goto out;
221 1.7 gwr }
222 1.7 gwr
223 1.7 gwr /*
224 1.7 gwr * Enable broadcast if necessary.
225 1.7 gwr */
226 1.7 gwr if (from_p) {
227 1.7 gwr int *on;
228 1.7 gwr m = m_get(M_WAIT, MT_SOOPTS);
229 1.7 gwr if (m == NULL) {
230 1.7 gwr error = ENOBUFS;
231 1.7 gwr goto out;
232 1.7 gwr }
233 1.7 gwr on = mtod(m, int *);
234 1.7 gwr m->m_len = sizeof(*on);
235 1.7 gwr *on = 1;
236 1.7 gwr if ((error = sosetopt(so, SOL_SOCKET, SO_BROADCAST, m)))
237 1.7 gwr goto out;
238 1.1 glass }
239 1.1 glass
240 1.1 glass /*
241 1.5 gwr * Bind the local endpoint to a reserved port,
242 1.5 gwr * because some NFS servers refuse requests from
243 1.5 gwr * non-reserved (non-privileged) ports.
244 1.5 gwr */
245 1.5 gwr m = m_getclr(M_WAIT, MT_SONAME);
246 1.5 gwr sin = mtod(m, struct sockaddr_in *);
247 1.5 gwr sin->sin_len = m->m_len = sizeof(*sin);
248 1.5 gwr sin->sin_family = AF_INET;
249 1.5 gwr sin->sin_addr.s_addr = INADDR_ANY;
250 1.5 gwr tport = IPPORT_RESERVED;
251 1.5 gwr do {
252 1.5 gwr tport--;
253 1.5 gwr sin->sin_port = htons(tport);
254 1.5 gwr error = sobind(so, m);
255 1.5 gwr } while (error == EADDRINUSE &&
256 1.5 gwr tport > IPPORT_RESERVED / 2);
257 1.5 gwr m_freem(m);
258 1.5 gwr if (error) {
259 1.5 gwr printf("bind failed\n");
260 1.5 gwr goto out;
261 1.5 gwr }
262 1.5 gwr
263 1.5 gwr /*
264 1.1 glass * Setup socket address for the server.
265 1.1 glass */
266 1.1 glass nam = m_get(M_WAIT, MT_SONAME);
267 1.1 glass if (nam == NULL) {
268 1.1 glass error = ENOBUFS;
269 1.1 glass goto out;
270 1.1 glass }
271 1.1 glass sin = mtod(nam, struct sockaddr_in *);
272 1.8 gwr bcopy((caddr_t)sa, (caddr_t)sin,
273 1.8 gwr (nam->m_len = sa->sin_len));
274 1.1 glass
275 1.1 glass /*
276 1.2 gwr * Prepend RPC message header.
277 1.1 glass */
278 1.8 gwr mhead = m_gethdr(M_WAIT, MT_DATA);
279 1.8 gwr mhead->m_next = *data;
280 1.1 glass call = mtod(mhead, struct rpc_call *);
281 1.8 gwr mhead->m_len = sizeof(*call);
282 1.1 glass bzero((caddr_t)call, sizeof(*call));
283 1.7 gwr xid++;
284 1.7 gwr call->rp_xid = htonl(xid);
285 1.1 glass /* call->rp_direction = 0; */
286 1.1 glass call->rp_rpcvers = htonl(2);
287 1.1 glass call->rp_prog = htonl(prog);
288 1.1 glass call->rp_vers = htonl(vers);
289 1.1 glass call->rp_proc = htonl(func);
290 1.1 glass /* call->rp_auth = 0; */
291 1.1 glass /* call->rp_verf = 0; */
292 1.1 glass
293 1.1 glass /*
294 1.8 gwr * Setup packet header
295 1.8 gwr */
296 1.8 gwr len = 0;
297 1.8 gwr m = mhead;
298 1.8 gwr while (m) {
299 1.8 gwr len += m->m_len;
300 1.8 gwr m = m->m_next;
301 1.8 gwr }
302 1.8 gwr mhead->m_pkthdr.len = len;
303 1.8 gwr mhead->m_pkthdr.rcvif = NULL;
304 1.8 gwr
305 1.8 gwr /*
306 1.2 gwr * Send it, repeatedly, until a reply is received,
307 1.2 gwr * but delay each re-send by an increasing amount.
308 1.2 gwr * If the delay hits the maximum, start complaining.
309 1.1 glass */
310 1.1 glass timo = 0;
311 1.1 glass for (;;) {
312 1.1 glass /* Send RPC request (or re-send). */
313 1.1 glass m = m_copym(mhead, 0, M_COPYALL, M_WAIT);
314 1.2 gwr if (m == NULL) {
315 1.2 gwr error = ENOBUFS;
316 1.2 gwr goto out;
317 1.2 gwr }
318 1.1 glass error = sosend(so, nam, NULL, m, NULL, 0);
319 1.2 gwr if (error) {
320 1.2 gwr printf("krpc_call: sosend: %d\n", error);
321 1.1 glass goto out;
322 1.2 gwr }
323 1.1 glass m = NULL;
324 1.1 glass
325 1.2 gwr /* Determine new timeout. */
326 1.2 gwr if (timo < MAX_RESEND_DELAY)
327 1.1 glass timo++;
328 1.1 glass else
329 1.2 gwr printf("RPC timeout for server 0x%x\n",
330 1.1 glass ntohl(sin->sin_addr.s_addr));
331 1.1 glass
332 1.1 glass /*
333 1.1 glass * Wait for up to timo seconds for a reply.
334 1.1 glass * The socket receive timeout was set to 1 second.
335 1.1 glass */
336 1.1 glass secs = timo;
337 1.1 glass while (secs > 0) {
338 1.7 gwr if (from) {
339 1.7 gwr m_freem(from);
340 1.7 gwr from = NULL;
341 1.7 gwr }
342 1.7 gwr if (m) {
343 1.7 gwr m_freem(m);
344 1.7 gwr m = NULL;
345 1.7 gwr }
346 1.1 glass auio.uio_resid = len = 1<<16;
347 1.1 glass rcvflg = 0;
348 1.7 gwr error = soreceive(so, &from, &auio, &m, NULL, &rcvflg);
349 1.1 glass if (error == EWOULDBLOCK) {
350 1.1 glass secs--;
351 1.1 glass continue;
352 1.1 glass }
353 1.1 glass if (error)
354 1.1 glass goto out;
355 1.1 glass len -= auio.uio_resid;
356 1.1 glass
357 1.7 gwr /* Does the reply contain at least a header? */
358 1.7 gwr if (len < MIN_REPLY_HDR)
359 1.7 gwr continue;
360 1.7 gwr if (m->m_len < MIN_REPLY_HDR)
361 1.7 gwr continue;
362 1.7 gwr reply = mtod(m, struct rpc_reply *);
363 1.7 gwr
364 1.7 gwr /* Is it the right reply? */
365 1.7 gwr if (reply->rp_direction != htonl(RPC_REPLY))
366 1.7 gwr continue;
367 1.7 gwr
368 1.7 gwr if (reply->rp_xid != htonl(xid))
369 1.7 gwr continue;
370 1.7 gwr
371 1.7 gwr /* Was RPC accepted? (authorization OK) */
372 1.7 gwr if (reply->rp_astatus != 0) {
373 1.7 gwr error = ntohl(reply->rp_u.rpu_errno);
374 1.7 gwr printf("rpc denied, error=%d\n", error);
375 1.1 glass continue;
376 1.1 glass }
377 1.7 gwr
378 1.7 gwr /* Did the call succeed? */
379 1.7 gwr if ((error = ntohl(reply->rp_u.rpu_ok.rp_rstatus)) != 0) {
380 1.7 gwr printf("rpc status=%d\n", error);
381 1.7 gwr continue;
382 1.1 glass }
383 1.7 gwr
384 1.7 gwr goto gotreply; /* break two levels */
385 1.7 gwr
386 1.1 glass } /* while secs */
387 1.1 glass } /* forever send/receive */
388 1.4 pk
389 1.4 pk error = ETIMEDOUT;
390 1.4 pk goto out;
391 1.4 pk
392 1.1 glass gotreply:
393 1.1 glass
394 1.1 glass /*
395 1.8 gwr * Get RPC reply header into first mbuf,
396 1.8 gwr * get its length, then strip it off.
397 1.2 gwr */
398 1.8 gwr len = sizeof(*reply);
399 1.2 gwr if (m->m_len < len) {
400 1.2 gwr m = m_pullup(m, len);
401 1.2 gwr if (m == NULL) {
402 1.2 gwr error = ENOBUFS;
403 1.2 gwr goto out;
404 1.2 gwr }
405 1.2 gwr }
406 1.8 gwr reply = mtod(m, struct rpc_reply *);
407 1.1 glass if (reply->rp_u.rpu_ok.rp_auth.rp_atype != 0) {
408 1.1 glass len += ntohl(reply->rp_u.rpu_ok.rp_auth.rp_alen);
409 1.1 glass len = (len + 3) & ~3; /* XXX? */
410 1.1 glass }
411 1.1 glass m_adj(m, len);
412 1.2 gwr
413 1.1 glass /* result */
414 1.1 glass *data = m;
415 1.7 gwr if (from_p) {
416 1.7 gwr *from_p = from;
417 1.7 gwr from = NULL;
418 1.7 gwr }
419 1.1 glass
420 1.1 glass out:
421 1.1 glass if (nam) m_freem(nam);
422 1.1 glass if (mhead) m_freem(mhead);
423 1.7 gwr if (from) m_freem(from);
424 1.1 glass soclose(so);
425 1.1 glass return error;
426 1.8 gwr }
427 1.8 gwr
428 1.8 gwr /*
429 1.8 gwr * eXternal Data Representation routines.
430 1.8 gwr * (but with non-standard args...)
431 1.8 gwr */
432 1.8 gwr
433 1.8 gwr /*
434 1.8 gwr * String representation for RPC.
435 1.8 gwr */
436 1.8 gwr struct xdr_string {
437 1.8 gwr u_int32_t len; /* length without null or padding */
438 1.8 gwr char data[4]; /* data (longer, of course) */
439 1.8 gwr /* data is padded to a long-word boundary */
440 1.8 gwr };
441 1.8 gwr
442 1.8 gwr struct mbuf *
443 1.8 gwr xdr_string_encode(str, len)
444 1.8 gwr char *str;
445 1.8 gwr int len;
446 1.8 gwr {
447 1.8 gwr struct mbuf *m;
448 1.8 gwr struct xdr_string *xs;
449 1.8 gwr int dlen; /* padded string length */
450 1.8 gwr int mlen; /* message length */
451 1.8 gwr
452 1.8 gwr dlen = (len + 3) & ~3;
453 1.8 gwr mlen = dlen + 4;
454 1.8 gwr
455 1.8 gwr m = m_get(M_WAIT, MT_DATA);
456 1.8 gwr if (mlen > MLEN) {
457 1.8 gwr if (mlen > MCLBYTES)
458 1.8 gwr return(NULL);
459 1.8 gwr MCLGET(m, M_WAIT);
460 1.8 gwr if (m == NULL)
461 1.8 gwr return NULL;
462 1.8 gwr }
463 1.8 gwr xs = mtod(m, struct xdr_string *);
464 1.8 gwr m->m_len = mlen;
465 1.8 gwr xs->len = htonl(len);
466 1.8 gwr bcopy(str, xs->data, len);
467 1.8 gwr return (m);
468 1.8 gwr }
469 1.8 gwr
470 1.8 gwr struct mbuf *
471 1.8 gwr xdr_string_decode(m, str, len_p)
472 1.8 gwr struct mbuf *m;
473 1.8 gwr char *str;
474 1.8 gwr int *len_p; /* bufsize - 1 */
475 1.8 gwr {
476 1.8 gwr struct xdr_string *xs;
477 1.8 gwr int mlen; /* message length */
478 1.8 gwr int slen; /* string length */
479 1.8 gwr
480 1.8 gwr if (m->m_len < 4) {
481 1.8 gwr m = m_pullup(m, 4);
482 1.8 gwr if (m == NULL)
483 1.8 gwr return (NULL);
484 1.8 gwr }
485 1.8 gwr xs = mtod(m, struct xdr_string *);
486 1.8 gwr slen = ntohl(xs->len);
487 1.8 gwr mlen = 4 + ((slen + 3) & ~3);
488 1.8 gwr
489 1.8 gwr if (slen > *len_p)
490 1.8 gwr slen = *len_p;
491 1.8 gwr m_copydata(m, 4, slen, str);
492 1.8 gwr m_adj(m, mlen);
493 1.8 gwr
494 1.8 gwr str[slen] = '\0';
495 1.8 gwr *len_p = slen;
496 1.8 gwr
497 1.8 gwr return (m);
498 1.8 gwr }
499 1.8 gwr
500 1.8 gwr
501 1.8 gwr /*
502 1.8 gwr * Inet address in RPC messages
503 1.8 gwr * (Note, really four ints, NOT chars. Blech.)
504 1.8 gwr */
505 1.8 gwr struct xdr_inaddr {
506 1.8 gwr u_int32_t atype;
507 1.8 gwr int32_t addr[4];
508 1.8 gwr };
509 1.8 gwr
510 1.8 gwr struct mbuf *
511 1.8 gwr xdr_inaddr_encode(ia)
512 1.8 gwr struct in_addr *ia; /* already in network order */
513 1.8 gwr {
514 1.8 gwr struct mbuf *m;
515 1.8 gwr struct xdr_inaddr *xi;
516 1.8 gwr u_char *cp;
517 1.8 gwr int32_t *ip;
518 1.8 gwr
519 1.8 gwr m = m_get(M_WAIT, MT_DATA);
520 1.8 gwr xi = mtod(m, struct xdr_inaddr *);
521 1.8 gwr m->m_len = sizeof(*xi);
522 1.8 gwr xi->atype = htonl(1);
523 1.8 gwr ip = xi->addr;
524 1.8 gwr cp = (u_char *)&ia->s_addr;
525 1.8 gwr *ip++ = *cp++;
526 1.8 gwr *ip++ = *cp++;
527 1.8 gwr *ip++ = *cp++;
528 1.8 gwr *ip++ = *cp++;
529 1.8 gwr
530 1.8 gwr return (m);
531 1.8 gwr }
532 1.8 gwr
533 1.8 gwr struct mbuf *
534 1.8 gwr xdr_inaddr_decode(m, ia)
535 1.8 gwr struct mbuf *m;
536 1.8 gwr struct in_addr *ia; /* already in network order */
537 1.8 gwr {
538 1.8 gwr struct xdr_inaddr *xi;
539 1.8 gwr u_char *cp;
540 1.8 gwr int32_t *ip;
541 1.8 gwr
542 1.8 gwr if (m->m_len < sizeof(*xi)) {
543 1.8 gwr m = m_pullup(m, sizeof(*xi));
544 1.8 gwr if (m == NULL)
545 1.8 gwr return (NULL);
546 1.8 gwr }
547 1.8 gwr xi = mtod(m, struct xdr_inaddr *);
548 1.8 gwr if (xi->atype != htonl(1)) {
549 1.8 gwr ia->s_addr = INADDR_ANY;
550 1.8 gwr goto out;
551 1.8 gwr }
552 1.8 gwr ip = xi->addr;
553 1.8 gwr cp = (u_char *)&ia->s_addr;
554 1.8 gwr *cp++ = *ip++;
555 1.8 gwr *cp++ = *ip++;
556 1.8 gwr *cp++ = *ip++;
557 1.8 gwr *cp++ = *ip++;
558 1.8 gwr
559 1.8 gwr out:
560 1.8 gwr m_adj(m, sizeof(*xi));
561 1.8 gwr return (m);
562 1.1 glass }
563