1*f14fb602SLionel Sambuc /* $NetBSD: inet_neta.c,v 1.3 2012/06/25 22:32:44 abs Exp $ */
22fe8fb19SBen Gras
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras * Copyright (c) 1996 by Internet Software Consortium.
52fe8fb19SBen Gras *
62fe8fb19SBen Gras * Permission to use, copy, modify, and distribute this software for any
72fe8fb19SBen Gras * purpose with or without fee is hereby granted, provided that the above
82fe8fb19SBen Gras * copyright notice and this permission notice appear in all copies.
92fe8fb19SBen Gras *
102fe8fb19SBen Gras * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
112fe8fb19SBen Gras * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
122fe8fb19SBen Gras * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
132fe8fb19SBen Gras * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
142fe8fb19SBen Gras * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
152fe8fb19SBen Gras * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
162fe8fb19SBen Gras * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
172fe8fb19SBen Gras * SOFTWARE.
182fe8fb19SBen Gras */
192fe8fb19SBen Gras
202fe8fb19SBen Gras #include <sys/cdefs.h>
212fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
222fe8fb19SBen Gras #if 0
232fe8fb19SBen Gras static const char rcsid[] = "Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp ";
242fe8fb19SBen Gras #else
25*f14fb602SLionel Sambuc __RCSID("$NetBSD: inet_neta.c,v 1.3 2012/06/25 22:32:44 abs Exp $");
262fe8fb19SBen Gras #endif
272fe8fb19SBen Gras #endif
282fe8fb19SBen Gras
292fe8fb19SBen Gras #include "namespace.h"
302fe8fb19SBen Gras #include <sys/types.h>
312fe8fb19SBen Gras #include <sys/socket.h>
322fe8fb19SBen Gras #include <netinet/in.h>
332fe8fb19SBen Gras #include <arpa/inet.h>
342fe8fb19SBen Gras
352fe8fb19SBen Gras #include <assert.h>
362fe8fb19SBen Gras #include <errno.h>
372fe8fb19SBen Gras #include <stdio.h>
382fe8fb19SBen Gras #include <string.h>
392fe8fb19SBen Gras
402fe8fb19SBen Gras #ifdef __weak_alias
__weak_alias(inet_neta,_inet_neta)412fe8fb19SBen Gras __weak_alias(inet_neta,_inet_neta)
422fe8fb19SBen Gras #endif
432fe8fb19SBen Gras
442fe8fb19SBen Gras /*
452fe8fb19SBen Gras * char *
462fe8fb19SBen Gras * inet_neta(src, dst, size)
472fe8fb19SBen Gras * format a u_long network number into presentation format.
482fe8fb19SBen Gras * return:
492fe8fb19SBen Gras * pointer to dst, or NULL if an error occurred (check errno).
502fe8fb19SBen Gras * note:
512fe8fb19SBen Gras * format of ``src'' is as for inet_network().
522fe8fb19SBen Gras * author:
532fe8fb19SBen Gras * Paul Vixie (ISC), July 1996
542fe8fb19SBen Gras */
552fe8fb19SBen Gras char *
56*f14fb602SLionel Sambuc inet_neta(u_long src, char *dst, size_t size)
572fe8fb19SBen Gras {
582fe8fb19SBen Gras char *odst = dst;
592fe8fb19SBen Gras char *ep;
602fe8fb19SBen Gras int advance;
612fe8fb19SBen Gras
622fe8fb19SBen Gras _DIAGASSERT(dst != NULL);
632fe8fb19SBen Gras
642fe8fb19SBen Gras if (src == 0x00000000) {
652fe8fb19SBen Gras if (size < sizeof "0.0.0.0")
662fe8fb19SBen Gras goto emsgsize;
672fe8fb19SBen Gras strlcpy(dst, "0.0.0.0", size);
682fe8fb19SBen Gras return dst;
692fe8fb19SBen Gras }
702fe8fb19SBen Gras ep = dst + size;
712fe8fb19SBen Gras if (ep <= dst)
722fe8fb19SBen Gras goto emsgsize;
73*f14fb602SLionel Sambuc while (src & 0xffffffffUL) {
74*f14fb602SLionel Sambuc u_char b = (u_char)((src & 0xff000000UL) >> 24);
752fe8fb19SBen Gras
762fe8fb19SBen Gras src <<= 8;
772fe8fb19SBen Gras if (b || src) {
782fe8fb19SBen Gras advance = snprintf(dst, (size_t)(ep - dst), "%u", b);
792fe8fb19SBen Gras if (advance <= 0 || advance >= ep - dst)
802fe8fb19SBen Gras goto emsgsize;
812fe8fb19SBen Gras dst += advance;
822fe8fb19SBen Gras if (src != 0L) {
832fe8fb19SBen Gras if (dst + 1 >= ep)
842fe8fb19SBen Gras goto emsgsize;
852fe8fb19SBen Gras *dst++ = '.';
862fe8fb19SBen Gras *dst = '\0';
872fe8fb19SBen Gras }
882fe8fb19SBen Gras }
892fe8fb19SBen Gras }
902fe8fb19SBen Gras return (odst);
912fe8fb19SBen Gras
922fe8fb19SBen Gras emsgsize:
932fe8fb19SBen Gras errno = EMSGSIZE;
942fe8fb19SBen Gras return (NULL);
952fe8fb19SBen Gras }
96