1*9e66e6d7Sabs /* $NetBSD: inet_neta.c,v 1.3 2012/06/25 22:32:44 abs Exp $ */
23fa54233Schristos
33fa54233Schristos /*
43fa54233Schristos * Copyright (c) 1996 by Internet Software Consortium.
53fa54233Schristos *
63fa54233Schristos * Permission to use, copy, modify, and distribute this software for any
73fa54233Schristos * purpose with or without fee is hereby granted, provided that the above
83fa54233Schristos * copyright notice and this permission notice appear in all copies.
93fa54233Schristos *
103fa54233Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
113fa54233Schristos * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
123fa54233Schristos * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
133fa54233Schristos * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
143fa54233Schristos * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
153fa54233Schristos * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
163fa54233Schristos * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
173fa54233Schristos * SOFTWARE.
183fa54233Schristos */
193fa54233Schristos
203fa54233Schristos #include <sys/cdefs.h>
213fa54233Schristos #if defined(LIBC_SCCS) && !defined(lint)
223fa54233Schristos #if 0
233fa54233Schristos static const char rcsid[] = "Id: inet_neta.c,v 8.2 1996/08/08 06:54:44 vixie Exp ";
243fa54233Schristos #else
25*9e66e6d7Sabs __RCSID("$NetBSD: inet_neta.c,v 1.3 2012/06/25 22:32:44 abs Exp $");
263fa54233Schristos #endif
273fa54233Schristos #endif
283fa54233Schristos
293fa54233Schristos #include "namespace.h"
303fa54233Schristos #include <sys/types.h>
313fa54233Schristos #include <sys/socket.h>
323fa54233Schristos #include <netinet/in.h>
333fa54233Schristos #include <arpa/inet.h>
343fa54233Schristos
353fa54233Schristos #include <assert.h>
363fa54233Schristos #include <errno.h>
373fa54233Schristos #include <stdio.h>
383fa54233Schristos #include <string.h>
393fa54233Schristos
403fa54233Schristos #ifdef __weak_alias
__weak_alias(inet_neta,_inet_neta)413fa54233Schristos __weak_alias(inet_neta,_inet_neta)
423fa54233Schristos #endif
433fa54233Schristos
443fa54233Schristos /*
453fa54233Schristos * char *
463fa54233Schristos * inet_neta(src, dst, size)
473fa54233Schristos * format a u_long network number into presentation format.
483fa54233Schristos * return:
493fa54233Schristos * pointer to dst, or NULL if an error occurred (check errno).
503fa54233Schristos * note:
513fa54233Schristos * format of ``src'' is as for inet_network().
523fa54233Schristos * author:
533fa54233Schristos * Paul Vixie (ISC), July 1996
543fa54233Schristos */
553fa54233Schristos char *
56*9e66e6d7Sabs inet_neta(u_long src, char *dst, size_t size)
573fa54233Schristos {
583fa54233Schristos char *odst = dst;
593fa54233Schristos char *ep;
603fa54233Schristos int advance;
613fa54233Schristos
623fa54233Schristos _DIAGASSERT(dst != NULL);
633fa54233Schristos
643fa54233Schristos if (src == 0x00000000) {
653fa54233Schristos if (size < sizeof "0.0.0.0")
663fa54233Schristos goto emsgsize;
673fa54233Schristos strlcpy(dst, "0.0.0.0", size);
683fa54233Schristos return dst;
693fa54233Schristos }
703fa54233Schristos ep = dst + size;
713fa54233Schristos if (ep <= dst)
723fa54233Schristos goto emsgsize;
73c5e820caSchristos while (src & 0xffffffffUL) {
74c5e820caSchristos u_char b = (u_char)((src & 0xff000000UL) >> 24);
753fa54233Schristos
763fa54233Schristos src <<= 8;
773fa54233Schristos if (b || src) {
783fa54233Schristos advance = snprintf(dst, (size_t)(ep - dst), "%u", b);
793fa54233Schristos if (advance <= 0 || advance >= ep - dst)
803fa54233Schristos goto emsgsize;
813fa54233Schristos dst += advance;
823fa54233Schristos if (src != 0L) {
833fa54233Schristos if (dst + 1 >= ep)
843fa54233Schristos goto emsgsize;
853fa54233Schristos *dst++ = '.';
863fa54233Schristos *dst = '\0';
873fa54233Schristos }
883fa54233Schristos }
893fa54233Schristos }
903fa54233Schristos return (odst);
913fa54233Schristos
923fa54233Schristos emsgsize:
933fa54233Schristos errno = EMSGSIZE;
943fa54233Schristos return (NULL);
953fa54233Schristos }
96