inet_neta.c revision 170244
11499SN/A/*
22224Sctornqvi * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
31499SN/A * Copyright (c) 1996,1999 by Internet Software Consortium.
41499SN/A *
51499SN/A * Permission to use, copy, modify, and distribute this software for any
61499SN/A * purpose with or without fee is hereby granted, provided that the above
71499SN/A * copyright notice and this permission notice appear in all copies.
81499SN/A *
91499SN/A * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
101499SN/A * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
111499SN/A * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
121499SN/A * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
131499SN/A * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
141499SN/A * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
151499SN/A * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
161499SN/A */
171499SN/A
181499SN/A#if defined(LIBC_SCCS) && !defined(lint)
191499SN/Astatic const char rcsid[] = "$Id: inet_neta.c,v 1.2.18.1 2005/04/27 05:00:53 sra Exp $";
201499SN/A#endif
211499SN/A#include <sys/cdefs.h>
221499SN/A__FBSDID("$FreeBSD: head/lib/libc/inet/inet_neta.c 170244 2007-06-03 17:20:27Z ume $");
231499SN/A
241499SN/A#include "port_before.h"
251499SN/A
261499SN/A#include <sys/types.h>
271499SN/A#include <sys/socket.h>
281499SN/A#include <netinet/in.h>
291499SN/A#include <arpa/inet.h>
301499SN/A
311499SN/A#include <errno.h>
321499SN/A#include <stdio.h>
331499SN/A#include <string.h>
341499SN/A
351499SN/A#include "port_after.h"
361499SN/A
371499SN/A#ifdef SPRINTF_CHAR
381499SN/A# define SPRINTF(x) strlen(sprintf/**/x)
391499SN/A#else
401499SN/A# define SPRINTF(x) ((size_t)sprintf x)
411499SN/A#endif
421499SN/A
431499SN/A/*%
441499SN/A * char *
451499SN/A * inet_neta(src, dst, size)
461499SN/A *	format an in_addr_t network number into presentation format.
471499SN/A * return:
481499SN/A *	pointer to dst, or NULL if an error occurred (check errno).
491499SN/A * note:
501499SN/A *	format of ``src'' is as for inet_network().
511499SN/A * author:
521499SN/A *	Paul Vixie (ISC), July 1996
531499SN/A */
541499SN/Achar *
551499SN/Ainet_neta(src, dst, size)
561499SN/A	in_addr_t src;
571499SN/A	char *dst;
581499SN/A	size_t size;
591499SN/A{
601499SN/A	char *odst = dst;
611499SN/A	char *tp;
621499SN/A
631499SN/A	while (src & 0xffffffff) {
641499SN/A		u_char b = (src & 0xff000000) >> 24;
651499SN/A
661499SN/A		src <<= 8;
671499SN/A		if (b) {
681499SN/A			if (size < sizeof "255.")
691499SN/A				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