Add snprintf and vsnprintf.
[dragonfly.git] / lib / libstand / net.c
1 /*      $NetBSD: net.c,v 1.20 1997/12/26 22:41:30 scottr Exp $  */
2
3 /*
4  * Copyright (c) 1992 Regents of the University of California.
5  * All rights reserved.
6  *
7  * This software was developed by the Computer Systems Engineering group
8  * at Lawrence Berkeley Laboratory under DARPA contract BG 91-66 and
9  * contributed to Berkeley.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *      This product includes software developed by the University of
22  *      California, Lawrence Berkeley Laboratory and its contributors.
23  * 4. Neither the name of the University nor the names of its contributors
24  *    may be used to endorse or promote products derived from this software
25  *    without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37  * SUCH DAMAGE.
38  *
39  * @(#) Header: net.c,v 1.9 93/08/06 19:32:15 leres Exp  (LBL)
40  * $FreeBSD: src/lib/libstand/net.c,v 1.1.1.1.6.1 2000/04/15 03:09:28 ps Exp $
41  * $DragonFly: src/lib/libstand/net.c,v 1.4 2004/10/25 19:38:45 drhodus Exp $
42  */
43
44 #include <sys/param.h>
45 #include <sys/socket.h>
46
47 #include <string.h>
48
49 #include <net/if.h>
50 #include <netinet/in.h>
51 #include <netinet/if_ether.h>
52 #include <netinet/in_systm.h>
53
54 #include <netinet/ip.h>
55 #include <netinet/ip_var.h>
56 #include <netinet/udp.h>
57 #include <netinet/udp_var.h>
58
59 #include "stand.h"
60 #include "net.h"
61
62 /*
63  * Send a packet and wait for a reply, with exponential backoff.
64  *
65  * The send routine must return the actual number of bytes written,
66  * or -1 on error.
67  *
68  * The receive routine can indicate success by returning the number of
69  * bytes read; it can return 0 to indicate EOF; it can return -1 with a
70  * non-zero errno to indicate failure; finally, it can return -1 with a
71  * zero errno to indicate it isn't done yet.
72  */
73 ssize_t
74 sendrecv(d, sproc, sbuf, ssize, rproc, rbuf, rsize)
75         struct iodesc *d;
76         ssize_t (*sproc)(struct iodesc *, void *, size_t);
77         void *sbuf;
78         size_t ssize;
79         ssize_t (*rproc)(struct iodesc *, void *, size_t, time_t);
80         void *rbuf;
81         size_t rsize;
82 {
83         ssize_t cc;
84         time_t t, tmo, tlast;
85         long tleft;
86
87 #ifdef NET_DEBUG
88         if (debug)
89                 printf("sendrecv: called\n");
90 #endif
91
92         tmo = MINTMO;
93         tlast = tleft = 0;
94         t = getsecs();
95         for (;;) {
96                 if (tleft <= 0) {
97                         if (tmo >= MAXTMO) {
98                                 errno = ETIMEDOUT;
99                                 return -1;
100                         }
101                         cc = (*sproc)(d, sbuf, ssize);
102                         if (cc != -1 && cc < ssize)
103                                 panic("sendrecv: short write! (%d < %d)",
104                                     cc, ssize);
105
106                         tleft = tmo;
107                         tmo <<= 1;
108                         if (tmo > MAXTMO)
109                                 tmo = MAXTMO;
110
111                         if (cc == -1) {
112                                 /* Error on transmit; wait before retrying */
113                                 while ((getsecs() - t) < tmo);
114                                 tleft = 0;
115                                 continue;
116                         }
117
118                         tlast = t;
119                 }
120
121                 /* Try to get a packet and process it. */
122                 cc = (*rproc)(d, rbuf, rsize, tleft);
123                 /* Return on data, EOF or real error. */
124                 if (cc != -1 || errno != 0)
125                         return (cc);
126
127                 /* Timed out or didn't get the packet we're waiting for */
128                 t = getsecs();
129                 tleft -= t - tlast;
130                 tlast = t;
131         }
132 }
133
134 /*
135  * Like inet_addr() in the C library, but we only accept base-10.
136  * Return values are in network order.
137  */
138 n_long
139 inet_addr(cp)
140         char *cp;
141 {
142         u_long val;
143         int n;
144         char c;
145         u_int parts[4];
146         u_int *pp = parts;
147
148         for (;;) {
149                 /*
150                  * Collect number up to ``.''.
151                  * Values are specified as for C:
152                  * 0x=hex, 0=octal, other=decimal.
153                  */
154                 val = 0;
155                 while ((c = *cp) != '\0') {
156                         if (c >= '0' && c <= '9') {
157                                 val = (val * 10) + (c - '0');
158                                 cp++;
159                                 continue;
160                         }
161                         break;
162                 }
163                 if (*cp == '.') {
164                         /*
165                          * Internet format:
166                          *      a.b.c.d
167                          *      a.b.c   (with c treated as 16-bits)
168                          *      a.b     (with b treated as 24 bits)
169                          */
170                         if (pp >= parts + 3 || val > 0xff)
171                                 goto bad;
172                         *pp++ = val, cp++;
173                 } else
174                         break;
175         }
176         /*
177          * Check for trailing characters.
178          */
179         if (*cp != '\0')
180                 goto bad;
181
182         /*
183          * Concoct the address according to
184          * the number of parts specified.
185          */
186         n = pp - parts + 1;
187         switch (n) {
188
189         case 1:                         /* a -- 32 bits */
190                 break;
191
192         case 2:                         /* a.b -- 8.24 bits */
193                 if (val > 0xffffff)
194                         goto bad;
195                 val |= parts[0] << 24;
196                 break;
197
198         case 3:                         /* a.b.c -- 8.8.16 bits */
199                 if (val > 0xffff)
200                         goto bad;
201                 val |= (parts[0] << 24) | (parts[1] << 16);
202                 break;
203
204         case 4:                         /* a.b.c.d -- 8.8.8.8 bits */
205                 if (val > 0xff)
206                         goto bad;
207                 val |= (parts[0] << 24) | (parts[1] << 16) | (parts[2] << 8);
208                 break;
209         }
210
211         return (htonl(val));
212  bad:
213         return (htonl(INADDR_NONE));
214 }
215
216 char *
217 inet_ntoa(ia)
218         struct in_addr ia;
219 {
220         return (intoa(ia.s_addr));
221 }
222
223 /* Similar to inet_ntoa() */
224 char *
225 intoa(addr)
226         n_long addr;
227 {
228         char *cp;
229         u_int byte;
230         int n;
231         static char buf[17];    /* strlen(".255.255.255.255") + 1 */
232
233         addr = ntohl(addr);
234         cp = &buf[sizeof buf];
235         *--cp = '\0';
236
237         n = 4;
238         do {
239                 byte = addr & 0xff;
240                 *--cp = byte % 10 + '0';
241                 byte /= 10;
242                 if (byte > 0) {
243                         *--cp = byte % 10 + '0';
244                         byte /= 10;
245                         if (byte > 0)
246                                 *--cp = byte + '0';
247                 }
248                 *--cp = '.';
249                 addr >>= 8;
250         } while (--n > 0);
251
252         return (cp+1);
253 }
254
255 static char *
256 number(s, n)
257         char *s;
258         int *n;
259 {
260         for (*n = 0; isdigit(*s); s++)
261                 *n = (*n * 10) + *s - '0';
262         return s;
263 }
264
265 n_long
266 ip_convertaddr(p)
267         char *p;
268 {
269 #define IP_ANYADDR      0
270         n_long addr = 0, n;
271
272         if (p == (char *)0 || *p == '\0')
273                 return IP_ANYADDR;
274         p = number(p, &n);
275         addr |= (n << 24) & 0xff000000;
276         if (*p == '\0' || *p++ != '.')
277                 return IP_ANYADDR;
278         p = number(p, &n);
279         addr |= (n << 16) & 0xff0000;
280         if (*p == '\0' || *p++ != '.')
281                 return IP_ANYADDR;
282         p = number(p, &n);
283         addr |= (n << 8) & 0xff00;
284         if (*p == '\0' || *p++ != '.')
285                 return IP_ANYADDR;
286         p = number(p, &n);
287         addr |= n & 0xff;
288         if (*p != '\0')
289                 return IP_ANYADDR;
290
291         return htonl(addr);
292 }