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