1 /*
2  * Copyright (c) 1996 by Internet Software Consortium.
3  *
4  * Permission to use, copy, modify, and distribute this software for any
5  * purpose with or without fee is hereby granted, provided that the above
6  * copyright notice and this permission notice appear in all copies.
7  *
8  * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
9  * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
10  * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
11  * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
12  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
13  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
14  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15  * SOFTWARE.
16  */
17 
18 /*
19  * Portions copyright (c) 1999, 2000
20  * Intel Corporation.
21  * All rights reserved.
22  *
23  * Redistribution and use in source and binary forms, with or without
24  * modification, are permitted provided that the following conditions
25  * are met:
26  *
27  * 1. Redistributions of source code must retain the above copyright
28  *    notice, this list of conditions and the following disclaimer.
29  *
30  * 2. Redistributions in binary form must reproduce the above copyright
31  *    notice, this list of conditions and the following disclaimer in the
32  *    documentation and/or other materials provided with the distribution.
33  *
34  * 3. All advertising materials mentioning features or use of this software
35  *    must display the following acknowledgement:
36  *
37  *    This product includes software developed by Intel Corporation and
38  *    its contributors.
39  *
40  * 4. Neither the name of Intel Corporation or its contributors may be
41  *    used to endorse or promote products derived from this software
42  *    without specific prior written permission.
43  *
44  * THIS SOFTWARE IS PROVIDED BY INTEL CORPORATION AND CONTRIBUTORS ``AS IS''
45  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47  * ARE DISCLAIMED.  IN NO EVENT SHALL INTEL CORPORATION OR CONTRIBUTORS BE
48  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
49  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
50  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
51  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
52  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
53  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
54  * THE POSSIBILITY OF SUCH DAMAGE.
55  *
56  */
57 
58 #if defined(LIBC_SCCS) && !defined(lint)
59 static const char orig_rcsid[] = "From Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp";
60 static const char rcsid[] = "$Id: inet_neta.c,v 1.1.1.1 2003/11/19 01:51:29 kyu3 Exp $";
61 #endif
62 
63 #include <sys/types.h>
64 #include <sys/socket.h>
65 #include <netinet/in.h>
66 #include <arpa/inet.h>
67 
68 #include <errno.h>
69 #include <stdio.h>
70 #include <string.h>
71 
72 #ifdef SPRINTF_CHAR
73 # define SPRINTF(x) strlen(sprintf/**/x)
74 #else
75 # define SPRINTF(x) ((size_t)sprintf x)
76 #endif
77 
78 /*
79  * char *
80  * inet_neta(src, dst, size)
81  *	format a u_long network number into presentation format.
82  * return:
83  *	pointer to dst, or NULL if an error occurred (check errno).
84  * note:
85  *	format of ``src'' is as for inet_network().
86  * author:
87  *	Paul Vixie (ISC), July 1996
88  */
89 char *
inet_neta(u_long src,char * dst,size_t size)90 inet_neta(
91 	u_long src,
92 	char *dst,
93 	size_t size
94 	)
95 {
96 	char *odst = dst;
97 	char *tp;
98 
99 	while (src & 0xffffffff) {
100 		u_char b = (u_char)((src & 0xff000000) >> 24);
101 
102 		src <<= 8;
103 		if (b) {
104 			if (size < sizeof "255.")
105 				goto emsgsize;
106 			tp = dst;
107 			dst += SPRINTF((dst, "%u", b));
108 			if (src != 0L) {
109 				*dst++ = '.';
110 				*dst = '\0';
111 			}
112 			size -= (size_t)(dst - tp);
113 		}
114 	}
115 	if (dst == odst) {
116 		if (size < sizeof "0.0.0.0")
117 			goto emsgsize;
118 		strcpy(dst, "0.0.0.0");
119 	}
120 	return (odst);
121 
122  emsgsize:
123 	errno = EMSGSIZE;
124 	return (NULL);
125 }
126