1*5bbd2a12Schristos /* $NetBSD: inet_neta.c,v 1.1.1.2 2012/09/09 16:07:50 christos Exp $ */
2b5677b36Schristos
3b5677b36Schristos /*
4b5677b36Schristos * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
5b5677b36Schristos * Copyright (c) 1996,1999 by Internet Software Consortium.
6b5677b36Schristos *
7b5677b36Schristos * Permission to use, copy, modify, and distribute this software for any
8b5677b36Schristos * purpose with or without fee is hereby granted, provided that the above
9b5677b36Schristos * copyright notice and this permission notice appear in all copies.
10b5677b36Schristos *
11b5677b36Schristos * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
12b5677b36Schristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13b5677b36Schristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
14b5677b36Schristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15b5677b36Schristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16b5677b36Schristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
17b5677b36Schristos * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18b5677b36Schristos */
19b5677b36Schristos
20b5677b36Schristos #if defined(LIBC_SCCS) && !defined(lint)
21b5677b36Schristos static const char rcsid[] = "Id: inet_neta.c,v 1.3 2005/04/27 04:56:20 sra Exp ";
22b5677b36Schristos #endif
23b5677b36Schristos
24b5677b36Schristos #include "port_before.h"
25b5677b36Schristos
26b5677b36Schristos #include <sys/types.h>
27b5677b36Schristos #include <sys/socket.h>
28b5677b36Schristos #include <netinet/in.h>
29b5677b36Schristos #include <arpa/inet.h>
30b5677b36Schristos
31b5677b36Schristos #include <errno.h>
32b5677b36Schristos #include <stdio.h>
33b5677b36Schristos #include <string.h>
34b5677b36Schristos
35b5677b36Schristos #include "port_after.h"
36b5677b36Schristos
37b5677b36Schristos #ifdef SPRINTF_CHAR
38b5677b36Schristos # define SPRINTF(x) strlen(sprintf/**/x)
39b5677b36Schristos #else
40b5677b36Schristos # define SPRINTF(x) ((size_t)sprintf x)
41b5677b36Schristos #endif
42b5677b36Schristos
43b5677b36Schristos /*%
44b5677b36Schristos * char *
45b5677b36Schristos * inet_neta(src, dst, size)
46b5677b36Schristos * format a u_long network number into presentation format.
47b5677b36Schristos * return:
48b5677b36Schristos * pointer to dst, or NULL if an error occurred (check errno).
49b5677b36Schristos * note:
50b5677b36Schristos * format of ``src'' is as for inet_network().
51b5677b36Schristos * author:
52b5677b36Schristos * Paul Vixie (ISC), July 1996
53b5677b36Schristos */
54b5677b36Schristos char *
inet_neta(src,dst,size)55b5677b36Schristos inet_neta(src, dst, size)
56b5677b36Schristos u_long src;
57b5677b36Schristos char *dst;
58b5677b36Schristos size_t size;
59b5677b36Schristos {
60b5677b36Schristos char *odst = dst;
61b5677b36Schristos char *tp;
62b5677b36Schristos
63b5677b36Schristos while (src & 0xffffffff) {
64b5677b36Schristos u_char b = (src & 0xff000000) >> 24;
65b5677b36Schristos
66b5677b36Schristos src <<= 8;
67b5677b36Schristos if (b) {
68b5677b36Schristos if (size < sizeof "255.")
69b5677b36Schristos goto emsgsize;
70b5677b36Schristos tp = dst;
71b5677b36Schristos dst += SPRINTF((dst, "%u", b));
72b5677b36Schristos if (src != 0L) {
73b5677b36Schristos *dst++ = '.';
74b5677b36Schristos *dst = '\0';
75b5677b36Schristos }
76b5677b36Schristos size -= (size_t)(dst - tp);
77b5677b36Schristos }
78b5677b36Schristos }
79b5677b36Schristos if (dst == odst) {
80b5677b36Schristos if (size < sizeof "0.0.0.0")
81b5677b36Schristos goto emsgsize;
82b5677b36Schristos strcpy(dst, "0.0.0.0");
83b5677b36Schristos }
84b5677b36Schristos return (odst);
85b5677b36Schristos
86b5677b36Schristos emsgsize:
87b5677b36Schristos errno = EMSGSIZE;
88b5677b36Schristos return (NULL);
89b5677b36Schristos }
90b5677b36Schristos
91b5677b36Schristos /*! \file */
92