1 /* $NetBSD: sockaddr_snprintf.c,v 1.8 2007/07/24 08:45:45 dyoung 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.8 2007/07/24 08:45:45 dyoung Exp $");
34 #endif /* LIBC_SCCS and not lint */
36 #include <sys/types.h>
37 #include <sys/socket.h>
40 #include <netinet/in.h>
41 #include <netatalk/at.h>
42 #include <net/if_dl.h>
51 sockaddr_snprintf(char * const sbuf
, const size_t len
, const char * const fmt
,
52 const struct sockaddr
* const sa
)
55 char abuf
[1024], nbuf
[1024], *addr
= NULL
, *w
= NULL
;
56 char Abuf
[1024], pbuf
[32], *name
= NULL
, *port
= NULL
;
57 char *ebuf
= &sbuf
[len
- 1], *buf
= sbuf
;
60 const struct sockaddr_at
*sat
= NULL
;
61 const struct sockaddr_in
*sin4
= NULL
;
62 const struct sockaddr_in6
*sin6
= NULL
;
63 const struct sockaddr_un
*sun
= NULL
;
64 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
) {
78 sat
= ((const struct sockaddr_at
*)(const void *)sa
);
79 p
= ntohs(sat
->sat_port
);
80 (void)snprintf(addr
= abuf
, sizeof(abuf
), "%u.%u",
81 ntohs(sat
->sat_addr
.s_net
), sat
->sat_addr
.s_node
);
82 (void)snprintf(port
= pbuf
, sizeof(pbuf
), "%d", p
);
85 sun
= ((const struct sockaddr_un
*)(const void *)sa
);
86 (void)strlcpy(addr
= abuf
, sun
->sun_path
, SUN_LEN(sun
));
89 sin4
= ((const struct sockaddr_in
*)(const void *)sa
);
90 p
= ntohs(sin4
->sin_port
);
94 sin6
= ((const struct sockaddr_in6
*)(const void *)sa
);
95 p
= ntohs(sin6
->sin6_port
);
99 sdl
= ((const struct sockaddr_dl
*)(const void *)sa
);
100 (void)strlcpy(addr
= abuf
, link_ntoa(sdl
), sizeof(abuf
));
101 if ((w
= strchr(addr
, ':')) != 0) {
107 errno
= EAFNOSUPPORT
;
114 if (a
&& getnameinfo(sa
, (socklen_t
)sa
->sa_len
, addr
= abuf
,
115 (unsigned int)sizeof(abuf
), NULL
, 0,
116 NI_NUMERICHOST
|NI_NUMERICSERV
) != 0)
119 for (ptr
= fmt
; *ptr
; ptr
++) {
134 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", p
);
140 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", sa
->sa_family
);
144 (void)snprintf(nbuf
, sizeof(nbuf
), "%d", sa
->sa_len
);
153 getnameinfo(sa
, (socklen_t
)sa
->sa_len
,
155 (unsigned int)sizeof(nbuf
), NULL
, 0, 0);
165 getnameinfo(sa
, (socklen_t
)sa
->sa_len
, NULL
, 0,
167 (unsigned int)sizeof(pbuf
), 0);
172 if (sdl
&& addr
!= abuf
) {
180 (void)snprintf(nbuf
, sizeof(nbuf
), "%d",
181 sin6
->sin6_flowinfo
);
190 (void)snprintf(nbuf
, sizeof(nbuf
), "%d",
191 sin6
->sin6_scope_id
);
200 const struct netrange
*n
=
201 &sat
->sat_range
.r_netrange
;
202 (void)snprintf(nbuf
, sizeof(nbuf
),
203 "%d:[%d,%d]", n
->nr_phase
, n
->nr_firstnet
,
227 sbuf
[len
- 1] = '\0';
228 return (int)(buf
- sbuf
);