1 /*
2  * Copyright (c) 1996,1999 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 #include <sys/types.h>
19 #include <sys/socket.h>
20 #include <netinet/in.h>
21 #include <arpa/inet.h>
22 
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 
27 #ifdef SPRINTF_CHAR
28 # define SPRINTF(x) strlen(sprintf/**/x)
29 #else
30 # define SPRINTF(x) ((size_t)sprintf x)
31 #endif
32 
33 /*
34  * char *
35  * inet_neta(src, dst, size)
36  *	format a u_long network number into presentation format.
37  * return:
38  *	pointer to dst, or NULL if an error occurred (check errno).
39  * note:
40  *	format of ``src'' is as for inet_network().
41  * author:
42  *	Paul Vixie (ISC), July 1996
43  */
44 char *
inet_neta(uint32_t src,char * dst,size_t size)45 inet_neta (uint32_t src, char *dst, size_t size)
46 {
47 	char *odst = dst;
48 	char *tp;
49 
50 	while (src & 0xffffffff) {
51 		u_char b = (src & 0xff000000) >> 24;
52 
53 		src <<= 8;
54 		if (b) {
55 			if (size < sizeof "255.")
56 				goto emsgsize;
57 			tp = dst;
58 			dst += SPRINTF((dst, "%u", b));
59 			if (src != 0L) {
60 				*dst++ = '.';
61 				*dst = '\0';
62 			}
63 			size -= (size_t)(dst - tp);
64 		}
65 	}
66 	if (dst == odst) {
67 		if (size < sizeof "0.0.0.0")
68 			goto emsgsize;
69 		strcpy(dst, "0.0.0.0");
70 	}
71 	return (odst);
72 
73  emsgsize:
74 	__set_errno (EMSGSIZE);
75 	return (NULL);
76 }
77