inet_neta.c revision 270838
1127915Skientzle/*
2191240Skientzle * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
3127915Skientzle * Copyright (c) 1996,1999 by Internet Software Consortium.
4228797Smm *
5228797Smm * Permission to use, copy, modify, and distribute this software for any
6127915Skientzle * purpose with or without fee is hereby granted, provided that the above
7248616Smm * copyright notice and this permission notice appear in all copies.
8228797Smm *
9228797Smm * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
10203559Skientzle * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11203559Skientzle * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
12248616Smm * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13203559Skientzle * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14203559Skientzle * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
15203559Skientzle * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16203559Skientzle */
17224153Smm
18228797Smm#if defined(LIBC_SCCS) && !defined(lint)
19224153Smmstatic const char rcsid[] = "$Id: inet_neta.c,v 1.3 2005/04/27 04:56:20 sra Exp $";
20238856Smm#endif
21224153Smm#include <sys/cdefs.h>
22224566Smm__FBSDID("$FreeBSD: stable/10/lib/libc/inet/inet_neta.c 270838 2014-08-30 10:16:25Z ume $");
23224566Smm
24191240Skientzle#include "port_before.h"
25204329Sru
26191240Skientzle#include <sys/types.h>
27224566Smm#include <sys/socket.h>
28224566Smm#include <netinet/in.h>
29224566Smm#include <arpa/inet.h>
30191240Skientzle
31224566Smm#include <errno.h>
32232153Smm#include <stdio.h>
33232153Smm#include <string.h>
34232153Smm
35232153Smm#include "port_after.h"
36175051Skientzle
37232153Smm#ifdef SPRINTF_CHAR
38228797Smm# define SPRINTF(x) strlen(sprintf/**/x)
39228797Smm#else
40137616Sru# define SPRINTF(x) ((size_t)sprintf x)
41128446Skientzle#endif
42184761Skientzle
43128446Skientzle/*%
44228797Smm * char *
45179322Skientzle * inet_neta(src, dst, size)
46228797Smm *	format an in_addr_t network number into presentation format.
47175051Skientzle * return:
48228797Smm *	pointer to dst, or NULL if an error occurred (check errno).
49228797Smm * note:
50228797Smm *	format of ``src'' is as for inet_network().
51127915Skientzle * author:
52 *	Paul Vixie (ISC), July 1996
53 */
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