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>
42 #include <netatalk/at.h>
43 #include <net/if_dl.h>
53 sockaddr_snprintf(char * const sbuf
, const size_t len
, const char * const fmt
,
54 const struct sockaddr
* const sa
)
57 char abuf
[1024], nbuf
[1024], *addr
= NULL
, *w
= NULL
;
58 char Abuf
[1024], pbuf
[32], *name
= NULL
, *port
= NULL
;
59 char *ebuf
= &sbuf
[len
- 1], *buf
= sbuf
;
62 const struct sockaddr_in
*sin4
= NULL
;
64 const struct sockaddr_at
*sat
= NULL
;
65 const struct sockaddr_in6
*sin6
= NULL
;
66 const struct sockaddr_un
*sun
= NULL
;
67 const struct sockaddr_dl
*sdl
= NULL
;
71 #define ADDC(c) do { if (buf < ebuf) *buf++ = c; else buf++; } \
72 while (/*CONSTCOND*/0)
73 #define ADDS(p) do { for (s = p; *s; s++) ADDC(*s); } \
74 while (/*CONSTCOND*/0)
75 #define ADDNA() do { if (na) ADDS("N/A"); } \
76 while (/*CONSTCOND*/0)
78 switch (sa
->sa_family
) {
83 sat
= ((const struct sockaddr_at
*)(const void *)sa
);
84 p
= ntohs(sat
->sat_port
);
85 (void)snprintf(addr
= abuf
, sizeof(abuf
), "%u.%u",
86 ntohs(sat
->sat_addr
.s_net
), sat
->sat_addr
.s_node
);
87 (void)snprintf(port
= pbuf
, sizeof(pbuf
), "%d", p
);
90 sun
= ((const struct sockaddr_un
*)(const void *)sa
);
91 (void)strlcpy(addr
= abuf
, sun
->sun_path
, SUN_LEN(sun
));
95 sin4
= ((const struct sockaddr_in
*)(const void *)sa
);
96 p
= ntohs(sin4
->sin_port
);
101 sin6
= ((const struct sockaddr_in6
*)(const void *)sa
);
102 p
= ntohs(sin6
->sin6_port
);
103 a
= &sin6
->sin6_addr
;
106 sdl
= ((const struct sockaddr_dl
*)(const void *)sa
);
107 (void)strlcpy(addr
= abuf
, link_ntoa(sdl
), sizeof(abuf
));
108 if ((w
= strchr(addr
, ':')) != 0) {
115 errno
= EAFNOSUPPORT
;
123 if (a
&& getnameinfo(sa
, (socklen_t
)sa
->sa_len
, addr
= abuf
,
125 if (a
&& getnameinfo(sa
, (socklen_t
)len
, addr
= abuf
,
127 (unsigned int)sizeof(abuf
), NULL
, 0,
128 NI_NUMERICHOST
|NI_NUMERICSERV
) != 0)
131 for (ptr
= fmt
; *ptr
; ptr
++) {
146 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", p
);
152 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", sa
->sa_family
);
157 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", sa
->sa_len
);
159 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", len
);
170 getnameinfo(sa
, (socklen_t
)sa
->sa_len
,
172 getnameinfo(sa
, (socklen_t
)len
,
175 (unsigned int)sizeof(nbuf
), NULL
, 0, 0);
186 getnameinfo(sa
, (socklen_t
)sa
->sa_len
, NULL
, 0,
188 getnameinfo(sa
, (socklen_t
)len
, NULL
, 0,
191 (unsigned int)sizeof(pbuf
), 0);
197 if (sdl
&& addr
!= abuf
) {
205 (void)snprintf(nbuf
, sizeof(nbuf
), "%d",
206 sin6
->sin6_flowinfo
);
215 (void)snprintf(nbuf
, sizeof(nbuf
), "%d",
216 sin6
->sin6_scope_id
);
225 const struct netrange
*n
=
226 &sat
->sat_range
.r_netrange
;
227 (void)snprintf(nbuf
, sizeof(nbuf
),
228 "%d:[%d,%d]", n
->nr_phase
, n
->nr_firstnet
,
253 sbuf
[len
- 1] = '\0';
254 return (int)(buf
- sbuf
);