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