inet_neta.c revision 1.1 1 1.1 christos /* $NetBSD: inet_neta.c,v 1.1 2004/05/20 23:13:02 christos Exp $ */
2 1.1 christos
3 1.1 christos /*
4 1.1 christos * Copyright (c) 1996 by Internet Software Consortium.
5 1.1 christos *
6 1.1 christos * Permission to use, copy, modify, and distribute this software for any
7 1.1 christos * purpose with or without fee is hereby granted, provided that the above
8 1.1 christos * copyright notice and this permission notice appear in all copies.
9 1.1 christos *
10 1.1 christos * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 1.1 christos * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 1.1 christos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 1.1 christos * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 1.1 christos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 1.1 christos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 1.1 christos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 1.1 christos * SOFTWARE.
18 1.1 christos */
19 1.1 christos
20 1.1 christos #include <sys/cdefs.h>
21 1.1 christos #if defined(LIBC_SCCS) && !defined(lint)
22 1.1 christos #if 0
23 1.1 christos static const char rcsid[] = "Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp ";
24 1.1 christos #else
25 1.1 christos __RCSID("$NetBSD: inet_neta.c,v 1.1 2004/05/20 23:13:02 christos Exp $");
26 1.1 christos #endif
27 1.1 christos #endif
28 1.1 christos
29 1.1 christos #include "namespace.h"
30 1.1 christos #include <sys/types.h>
31 1.1 christos #include <sys/socket.h>
32 1.1 christos #include <netinet/in.h>
33 1.1 christos #include <arpa/inet.h>
34 1.1 christos
35 1.1 christos #include <assert.h>
36 1.1 christos #include <errno.h>
37 1.1 christos #include <stdio.h>
38 1.1 christos #include <string.h>
39 1.1 christos
40 1.1 christos #ifdef __weak_alias
41 1.1 christos __weak_alias(inet_neta,_inet_neta)
42 1.1 christos #endif
43 1.1 christos
44 1.1 christos /*
45 1.1 christos * char *
46 1.1 christos * inet_neta(src, dst, size)
47 1.1 christos * format a u_long network number into presentation format.
48 1.1 christos * return:
49 1.1 christos * pointer to dst, or NULL if an error occurred (check errno).
50 1.1 christos * note:
51 1.1 christos * format of ``src'' is as for inet_network().
52 1.1 christos * author:
53 1.1 christos * Paul Vixie (ISC), July 1996
54 1.1 christos */
55 1.1 christos char *
56 1.1 christos inet_neta(src, dst, size)
57 1.1 christos u_long src;
58 1.1 christos char *dst;
59 1.1 christos size_t size;
60 1.1 christos {
61 1.1 christos char *odst = dst;
62 1.1 christos char *ep;
63 1.1 christos int advance;
64 1.1 christos
65 1.1 christos _DIAGASSERT(dst != NULL);
66 1.1 christos
67 1.1 christos if (src == 0x00000000) {
68 1.1 christos if (size < sizeof "0.0.0.0")
69 1.1 christos goto emsgsize;
70 1.1 christos strlcpy(dst, "0.0.0.0", size);
71 1.1 christos return dst;
72 1.1 christos }
73 1.1 christos ep = dst + size;
74 1.1 christos if (ep <= dst)
75 1.1 christos goto emsgsize;
76 1.1 christos while (src & 0xffffffff) {
77 1.1 christos u_char b = (u_char)((src & 0xff000000) >> 24);
78 1.1 christos
79 1.1 christos src <<= 8;
80 1.1 christos if (b || src) {
81 1.1 christos advance = snprintf(dst, (size_t)(ep - dst), "%u", b);
82 1.1 christos if (advance <= 0 || advance >= ep - dst)
83 1.1 christos goto emsgsize;
84 1.1 christos dst += advance;
85 1.1 christos if (src != 0L) {
86 1.1 christos if (dst + 1 >= ep)
87 1.1 christos goto emsgsize;
88 1.1 christos *dst++ = '.';
89 1.1 christos *dst = '\0';
90 1.1 christos }
91 1.1 christos }
92 1.1 christos }
93 1.1 christos return (odst);
94 1.1 christos
95 1.1 christos emsgsize:
96 1.1 christos errno = EMSGSIZE;
97 1.1 christos return (NULL);
98 1.1 christos }
99