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