1 /* $NetBSD: sockaddr_snprintf.c,v 1.3 2005/01/13 00:44:25 dyoung Exp $ */
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
7 * This code was contributed to The NetBSD Foundation by Christos Zoulas.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/socket.h> /* AF_ */
37 #include <netinet/in.h> /* sin/sin6 */
38 #include <sys/un.h> /* sun */
39 #include <netatalk/at.h> /* sat */
40 #include <net/if_dl.h> /* sdl */
47 struct sockaddr_in sin4
;
50 (void)memset(&sin4
, 0, sizeof(sin4
));
51 sin4
.sin_len
= sizeof(sin4
);
52 sin4
.sin_family
= AF_INET
;
53 sin4
.sin_port
= ntohs(80);
54 sin4
.sin_addr
.s_addr
= ntohl(INADDR_LOOPBACK
);
55 i
= sockaddr_snprintf(buf
, sizeof(buf
), "%f %l %p %a",
56 (struct sockaddr
*)&sin4
);
58 errx(1, "bad length for sin4 %d != %d", i
, 17);
59 if (strcmp(res
= "2 16 80 127.0.0.1", buf
) != 0)
60 errx(1, "res='%s' != '%s'", buf
, res
);
69 struct sockaddr_in6 sin6
;
72 (void)memset(&sin6
, 0, sizeof(sin6
));
73 sin6
.sin6_len
= sizeof(sin6
);
74 sin6
.sin6_family
= AF_INET6
;
75 sin6
.sin6_port
= ntohs(80);
76 sin6
.sin6_addr
= in6addr_nodelocal_allnodes
;
77 i
= sockaddr_snprintf(buf
, sizeof(buf
), "%f %l %p %a",
78 (struct sockaddr
*)&sin6
);
80 errx(1, "bad length for sin6 %d != %d", i
, 16);
81 if (strcmp(res
= "24 28 80 ff01::1", buf
) != 0)
82 errx(1, "res='%s' != '%s'", buf
, res
);
91 struct sockaddr_un sun
;
94 (void)memset(&sun
, 0, sizeof(sun
));
95 sun
.sun_len
= sizeof(sun
);
96 sun
.sun_family
= AF_UNIX
;
97 (void)strncpy(sun
.sun_path
, "/tmp/sock", sizeof(sun
.sun_path
));
98 i
= sockaddr_snprintf(buf
, sizeof(buf
), "%f %l %a",
99 (struct sockaddr
*)&sun
);
101 errx(1, "bad length for sun %d != %d", i
, 15);
102 if (strcmp(res
= "1 106 /tmp/sock", buf
) != 0)
103 errx(1, "res='%s' != '%s'", buf
, res
);
111 struct sockaddr_at sat
;
114 (void)memset(&sat
, 0, sizeof(sat
));
115 sat
.sat_len
= sizeof(sat
);
116 sat
.sat_family
= AF_APPLETALK
;
117 sat
.sat_addr
.s_net
= ntohs(101);
118 sat
.sat_addr
.s_node
= 3;
119 i
= sockaddr_snprintf(buf
, sizeof(buf
), "%f %l %a",
120 (struct sockaddr
*)&sat
);
122 errx(1, "bad length for sat %d != %d", i
, 11);
123 if (strcmp(res
= "16 16 101.3", buf
) != 0)
124 errx(1, "res='%s' != '%s'", buf
, res
);
132 struct sockaddr_dl sdl
;
135 (void)memset(&sdl
, 0, sizeof(sdl
));
136 sdl
.sdl_len
= sizeof(sdl
);
137 sdl
.sdl_family
= AF_LINK
;
143 (void)memcpy(sdl
.sdl_data
, "\01\02\03\04\05\06", 6);
144 i
= sockaddr_snprintf(buf
, sizeof(buf
), "%f %l %a",
145 (struct sockaddr
*)&sdl
);
147 errx(1, "bad length for sdl %d != %d", i
, 17);
148 if (strcmp(res
= "18 20 1.2.3.4.5.6", buf
) != 0)
149 errx(1, "res='%s' != '%s'", buf
, res
);
153 main(int argc
, char *argv
[])