1 /* $NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 #if defined(LIBC_SCCS) && !defined(lint)
33 __RCSID("$NetBSD: sockaddr_snprintf.c,v 1.9 2008/04/28 20:23:03 martin Exp $");
34 #endif /* LIBC_SCCS and not lint */
36 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <netinet/in.h>
49 sockaddr_snprintf(char * const sbuf
, const size_t len
, const char * const fmt
,
50 const struct sockaddr
* const sa
)
53 char abuf
[1024], nbuf
[1024], *addr
= NULL
, *w
= NULL
;
54 char Abuf
[1024], pbuf
[32], *name
= NULL
, *port
= NULL
;
55 char *ebuf
= &sbuf
[len
- 1], *buf
= sbuf
;
58 const struct sockaddr_in
*sin4
= NULL
;
60 const struct sockaddr_at
*sat
= NULL
;
61 const struct sockaddr_in6
*sin6
= NULL
;
62 const struct sockaddr_un
*sun
= NULL
;
63 const struct sockaddr_dl
*sdl
= NULL
;
67 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
68 while (/*CONSTCOND*/0)
69 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
70 while (/*CONSTCOND*/0)
71 #define ADDNA() do { if (na) ADDS("N/A"); } \
72 while (/*CONSTCOND*/0)
74 switch (sa
->sa_family
) {
79 sat
= ((const struct sockaddr_at
*)(const void *)sa
);
80 p
= ntohs(sat
->sat_port
);
81 (void)snprintf(addr
= abuf
, sizeof(abuf
), "%u.%u",
82 ntohs(sat
->sat_addr
.s_net
), sat
->sat_addr
.s_node
);
83 (void)snprintf(port
= pbuf
, sizeof(pbuf
), "%d", p
);
86 sun
= ((const struct sockaddr_un
*)(const void *)sa
);
87 (void)strlcpy(addr
= abuf
, sun
->sun_path
, SUN_LEN(sun
));
91 sin4
= ((const struct sockaddr_in
*)(const void *)sa
);
92 p
= ntohs(sin4
->sin_port
);
97 sin6
= ((const struct sockaddr_in6
*)(const void *)sa
);
98 p
= ntohs(sin6
->sin6_port
);
102 sdl
= ((const struct sockaddr_dl
*)(const void *)sa
);
103 (void)strlcpy(addr
= abuf
, link_ntoa(sdl
), sizeof(abuf
));
104 if ((w
= strchr(addr
, ':')) != 0) {
111 errno
= EAFNOSUPPORT
;
118 if (a
&& getnameinfo(sa
, (socklen_t
)len
, addr
= abuf
,
119 (unsigned int)sizeof(abuf
), NULL
, 0,
120 NI_NUMERICHOST
|NI_NUMERICSERV
) != 0)
123 for (ptr
= fmt
; *ptr
; ptr
++) {
138 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", p
);
144 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", sa
->sa_family
);
148 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", len
);
157 getnameinfo(sa
, (socklen_t
)len
,
159 (unsigned int)sizeof(nbuf
), NULL
, 0, 0);
169 getnameinfo(sa
, (socklen_t
)len
, NULL
, 0,
171 (unsigned int)sizeof(pbuf
), 0);
177 if (sdl
&& addr
!= abuf
) {
185 (void)snprintf(nbuf
, sizeof(nbuf
), "%d",
186 sin6
->sin6_flowinfo
);
195 (void)snprintf(nbuf
, sizeof(nbuf
), "%d",
196 sin6
->sin6_scope_id
);
205 const struct netrange
*n
=
206 &sat
->sat_range
.r_netrange
;
207 (void)snprintf(nbuf
, sizeof(nbuf
),
208 "%d:[%d,%d]", n
->nr_phase
, n
->nr_firstnet
,
233 sbuf
[len
- 1] = '\0';
234 return (int)(buf
- sbuf
);