1/*	$NetBSD: inet_neta.c,v 1.3 2012/06/25 22:32:44 abs 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#include <sys/cdefs.h>
21#if defined(LIBC_SCCS) && !defined(lint)
22#if 0
23static const char rcsid[] = "Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp ";
24#else
25__RCSID("$NetBSD: inet_neta.c,v 1.3 2012/06/25 22:32:44 abs Exp $");
26#endif
27#endif
28
29#include "port_before.h"
30
31#include <sys/types.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35
36#include <assert.h>
37#include <errno.h>
38#include <stdio.h>
39#include <string.h>
40
41#include "port_after.h"
42
43/*
44 * char *
45 * inet_neta(src, dst, size)
46 *	format a u_long network number into presentation format.
47 * return:
48 *	pointer to dst, or NULL if an error occurred (check errno).
49 * note:
50 *	format of ``src'' is as for inet_network().
51 * author:
52 *	Paul Vixie (ISC), July 1996
53 */
54char *
55inet_neta(u_long src, char *dst, size_t size)
56{
57	char *odst = dst;
58	char *ep;
59	int advance;
60
61	assert(dst != NULL);
62
63	if (src == 0x00000000) {
64		if (size < sizeof "0.0.0.0")
65			goto emsgsize;
66		strlcpy(dst, "0.0.0.0", size);
67		return dst;
68	}
69	ep = dst + size;
70	if (ep <= dst)
71		goto emsgsize;
72	while (src & 0xffffffffUL) {
73		u_char b = (u_char)((src & 0xff000000UL) >> 24);
74
75		src <<= 8;
76		if (b || src) {
77			advance = snprintf(dst, (size_t)(ep - dst), "%u", b);
78			if (advance <= 0 || advance >= ep - dst)
79				goto emsgsize;
80			dst += advance;
81			if (src != 0L) {
82				if (dst + 1 >= ep)
83					goto emsgsize;
84				*dst++ = '.';
85				*dst = '\0';
86			}
87		}
88	}
89	return (odst);
90
91 emsgsize:
92	errno = EMSGSIZE;
93	return (NULL);
94}
95
96#undef inet_neta
97#pragma weak inet_neta = __inet_neta
98