1169689Skan/*
2169689Skan * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3169689Skan * Copyright (c) 1996,1999 by Internet Software Consortium.
4169689Skan *
5169689Skan * Permission to use, copy, modify, and distribute this software for any
6169689Skan * purpose with or without fee is hereby granted, provided that the above
7169689Skan * copyright notice and this permission notice appear in all copies.
8169689Skan *
9169689Skan * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10169689Skan * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11169689Skan * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12169689Skan * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13169689Skan * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14169689Skan * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15169689Skan * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16169689Skan */
17169689Skan
18169689Skan#if defined(LIBC_SCCS) && !defined(lint)
19169689Skanstatic const char rcsid[] = "$Id: inet_neta.c,v 1.3 2005/04/27 04:56:20 sra Exp $";
20169689Skan#endif
21169689Skan#include <sys/cdefs.h>
22169689Skan__FBSDID("$FreeBSD$");
23169689Skan
24169689Skan#include "port_before.h"
25169689Skan
26169689Skan#include <sys/types.h>
27169689Skan#include <sys/socket.h>
28169689Skan#include <netinet/in.h>
29169689Skan#include <arpa/inet.h>
30169689Skan
31169689Skan#include <errno.h>
32169689Skan#include <stdio.h>
33169689Skan#include <string.h>
34169689Skan
35169689Skan#include "port_after.h"
36169689Skan
37169689Skan#ifdef SPRINTF_CHAR
38169689Skan# define SPRINTF(x) strlen(sprintf/**/x)
39169689Skan#else
40169689Skan# define SPRINTF(x) ((size_t)sprintf x)
41169689Skan#endif
42169689Skan
43169689Skan/*%
44169689Skan * char *
45169689Skan * inet_neta(src, dst, size)
46169689Skan *	format an in_addr_t network number into presentation format.
47169689Skan * return:
48169689Skan *	pointer to dst, or NULL if an error occurred (check errno).
49169689Skan * note:
50169689Skan *	format of ``src'' is as for inet_network().
51169689Skan * author:
52169689Skan *	Paul Vixie (ISC), July 1996
53169689Skan */
54char *
55inet_neta(src, dst, size)
56	in_addr_t src;
57	char *dst;
58	size_t size;
59{
60	char *odst = dst;
61	char *tp;
62
63	while (src & 0xffffffff) {
64		u_char b = (src & 0xff000000) >> 24;
65
66		src <<= 8;
67		if (b) {
68			if (size < sizeof "255.")
69				goto emsgsize;
70			tp = dst;
71			dst += SPRINTF((dst, "%u", b));
72			if (src != 0L) {
73				*dst++ = '.';
74				*dst = '\0';
75			}
76			size -= (size_t)(dst - tp);
77		}
78	}
79	if (dst == odst) {
80		if (size < sizeof "0.0.0.0")
81			goto emsgsize;
82		strcpy(dst, "0.0.0.0");
83	}
84	return (odst);
85
86 emsgsize:
87	errno = EMSGSIZE;
88	return (NULL);
89}
90
91/*
92 * Weak aliases for applications that use certain private entry points,
93 * and fail to include <arpa/inet.h>.
94 */
95#undef inet_neta
96__weak_reference(__inet_neta, inet_neta);
97
98/*! \file */
99