Handle DNS timeouts in old-style lookkup code.
[glibc.git] / sunrpc / xdr_mem.c
blobf9a0b1495d5b178cf835c3ac34a787c83d811e91
1 /*
2 * xdr_mem.h, XDR implementation using memory buffers.
4 * Copyright (C) 1984, Sun Microsystems, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
16 * * Neither the name of Sun Microsystems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
25 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
27 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * If you have some data to be interpreted as external data representation
34 * or to be converted to external data representation in a memory buffer,
35 * then this is the package for you.
38 #include <string.h>
39 #include <limits.h>
40 #include <rpc/rpc.h>
42 static bool_t xdrmem_getlong (XDR *, long *);
43 static bool_t xdrmem_putlong (XDR *, const long *);
44 static bool_t xdrmem_getbytes (XDR *, caddr_t, u_int);
45 static bool_t xdrmem_putbytes (XDR *, const char *, u_int);
46 static u_int xdrmem_getpos (const XDR *);
47 static bool_t xdrmem_setpos (XDR *, u_int);
48 static int32_t *xdrmem_inline (XDR *, u_int);
49 static void xdrmem_destroy (XDR *);
50 static bool_t xdrmem_getint32 (XDR *, int32_t *);
51 static bool_t xdrmem_putint32 (XDR *, const int32_t *);
53 static const struct xdr_ops xdrmem_ops =
55 xdrmem_getlong,
56 xdrmem_putlong,
57 xdrmem_getbytes,
58 xdrmem_putbytes,
59 xdrmem_getpos,
60 xdrmem_setpos,
61 xdrmem_inline,
62 xdrmem_destroy,
63 xdrmem_getint32,
64 xdrmem_putint32
68 * The procedure xdrmem_create initializes a stream descriptor for a
69 * memory buffer.
71 void
72 xdrmem_create (XDR *xdrs, const caddr_t addr, u_int size, enum xdr_op op)
74 xdrs->x_op = op;
75 /* We have to add the const since the `struct xdr_ops' in `struct XDR'
76 is not `const'. */
77 xdrs->x_ops = (struct xdr_ops *) &xdrmem_ops;
78 xdrs->x_private = xdrs->x_base = addr;
79 xdrs->x_handy = size;
81 INTDEF(xdrmem_create)
84 * Nothing needs to be done for the memory case. The argument is clearly
85 * const.
88 static void
89 xdrmem_destroy (XDR *xdrs)
94 * Gets the next word from the memory referenced by xdrs and places it
95 * in the long pointed to by lp. It then increments the private word to
96 * point at the next element. Neither object pointed to is const
98 static bool_t
99 xdrmem_getlong (XDR *xdrs, long *lp)
101 if (xdrs->x_handy < 4)
102 return FALSE;
103 xdrs->x_handy -= 4;
104 *lp = (int32_t) ntohl ((*((int32_t *) (xdrs->x_private))));
105 xdrs->x_private += 4;
106 return TRUE;
110 * Puts the long pointed to by lp in the memory referenced by xdrs. It
111 * then increments the private word to point at the next element. The
112 * long pointed at is const
114 static bool_t
115 xdrmem_putlong (XDR *xdrs, const long *lp)
117 if (xdrs->x_handy < 4)
118 return FALSE;
119 xdrs->x_handy -= 4;
120 *(int32_t *) xdrs->x_private = htonl (*lp);
121 xdrs->x_private += 4;
122 return TRUE;
126 * Gets an unaligned number of bytes from the xdrs structure and writes them
127 * to the address passed in addr. Be very careful when calling this routine
128 * as it could leave the xdrs pointing to an unaligned structure which is not
129 * a good idea. None of the things pointed to are const.
131 static bool_t
132 xdrmem_getbytes (XDR *xdrs, caddr_t addr, u_int len)
134 if (xdrs->x_handy < len)
135 return FALSE;
136 xdrs->x_handy -= len;
137 memcpy (addr, xdrs->x_private, len);
138 xdrs->x_private += len;
139 return TRUE;
143 * The complementary function to the above. The same warnings apply about
144 * unaligned data. The source address is const.
146 static bool_t
147 xdrmem_putbytes (XDR *xdrs, const char *addr, u_int len)
149 if (xdrs->x_handy < len)
150 return FALSE;
151 xdrs->x_handy -= len;
152 memcpy (xdrs->x_private, addr, len);
153 xdrs->x_private += len;
154 return TRUE;
158 * Not sure what this one does. But it clearly doesn't modify the contents
159 * of xdrs. **FIXME** does this not assume u_int == u_long?
161 static u_int
162 xdrmem_getpos (const XDR *xdrs)
164 return (u_long) xdrs->x_private - (u_long) xdrs->x_base;
168 * xdrs modified
170 static bool_t
171 xdrmem_setpos (xdrs, pos)
172 XDR *xdrs;
173 u_int pos;
175 caddr_t newaddr = xdrs->x_base + pos;
176 caddr_t lastaddr = xdrs->x_private + xdrs->x_handy;
177 size_t handy = lastaddr - newaddr;
179 if (newaddr > lastaddr
180 || newaddr < xdrs->x_base
181 || handy != (u_int) handy)
182 return FALSE;
184 xdrs->x_private = newaddr;
185 xdrs->x_handy = (u_int) handy;
186 return TRUE;
190 * xdrs modified
192 static int32_t *
193 xdrmem_inline (XDR *xdrs, u_int len)
195 int32_t *buf = 0;
197 if (xdrs->x_handy >= len)
199 xdrs->x_handy -= len;
200 buf = (int32_t *) xdrs->x_private;
201 xdrs->x_private += len;
203 return buf;
207 * Gets the next word from the memory referenced by xdrs and places it
208 * in the int pointed to by ip. It then increments the private word to
209 * point at the next element. Neither object pointed to is const
211 static bool_t
212 xdrmem_getint32 (XDR *xdrs, int32_t *ip)
214 if (xdrs->x_handy < 4)
215 return FALSE;
216 xdrs->x_handy -= 4;
217 *ip = ntohl ((*((int32_t *) (xdrs->x_private))));
218 xdrs->x_private += 4;
219 return TRUE;
223 * Puts the long pointed to by lp in the memory referenced by xdrs. It
224 * then increments the private word to point at the next element. The
225 * long pointed at is const
227 static bool_t
228 xdrmem_putint32 (XDR *xdrs, const int32_t *ip)
230 if (xdrs->x_handy < 4)
231 return FALSE;
232 xdrs->x_handy -= 4;
233 *(int32_t *) xdrs->x_private = htonl (*ip);
234 xdrs->x_private += 4;
235 return TRUE;