1 /* $OpenBSD: inet_neta.c,v 1.7 2005/08/06 20:30:03 espie Exp $ */
2
3 /*
4 * Copyright (c) 1996 by Internet Software Consortium.
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
11 * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
12 * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
13 * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
14 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
16 * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
17 * SOFTWARE.
18 */
19
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <netinet/in.h>
23 #include <arpa/inet.h>
24
25 #include <errno.h>
26 #include <stdio.h>
27 #include <string.h>
28
29 __RCSID("$MirOS: src/lib/libc/net/inet_neta.c,v 1.2 2009/11/09 21:30:51 tg Exp $");
30
31 /*
32 * char *
33 * inet_neta(src, dst, size)
34 * format an in_addr_t network number into presentation format.
35 * return:
36 * pointer to dst, or NULL if an error occurred (check errno).
37 * note:
38 * format of ``src'' is as for inet_network().
39 * author:
40 * Paul Vixie (ISC), July 1996
41 */
42 char *
inet_neta(in_addr_t src,char * dst,size_t size)43 inet_neta(in_addr_t src, char *dst, size_t size)
44 {
45 char *odst = dst;
46 char *ep;
47 int advance;
48
49 if (src == 0x00000000) {
50 if (size < sizeof "0.0.0.0")
51 goto emsgsize;
52 strlcpy(dst, "0.0.0.0", size);
53 return dst;
54 }
55 ep = dst + size;
56 if (ep <= dst)
57 goto emsgsize;
58 while (src & 0xffffffff) {
59 u_char b = (src & 0xff000000) >> 24;
60
61 src <<= 8;
62 if (b || src) {
63 if ((size_t)(ep - dst) < sizeof("255."))
64 goto emsgsize;
65 advance = snprintf(dst, ep - dst, "%u", b);
66 if (advance <= 0 || advance >= ep - dst)
67 goto emsgsize;
68 dst += advance;
69 if (src != 0L) {
70 if (dst + 1 >= ep)
71 goto emsgsize;
72 *dst++ = '.';
73 *dst = '\0';
74 }
75 }
76 }
77 return (odst);
78
79 emsgsize:
80 errno = EMSGSIZE;
81 return (NULL);
82 }
83