Expand PMF_FN_* macros.
[netbsd-mini2440.git] / regress / lib / libutil / sockaddr_snprintf / sockaddr_snprintf.c
blob49951dd7385a5c139259d78679f481655b218d96
1 /* $NetBSD: sockaddr_snprintf.c,v 1.3 2005/01/13 00:44:25 dyoung Exp $ */
3 /*-
4 * Copyright (c) 2004 The NetBSD Foundation, Inc.
5 * All rights reserved.
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
11 * are met:
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.
31 #include <stdio.h>
32 #include <string.h>
33 #include <util.h>
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 */
42 static void
43 in(void)
45 char buf[1024];
46 int i;
47 struct sockaddr_in sin4;
48 const char *res;
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);
57 if (i != 17)
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);
63 #ifdef INET6
64 static void
65 in6(void)
67 char buf[1024];
68 int i;
69 struct sockaddr_in6 sin6;
70 const char *res;
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);
79 if (i != 16)
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);
84 #endif /* INET6 */
86 static void
87 un(void)
89 char buf[1024];
90 int i;
91 struct sockaddr_un sun;
92 const char *res;
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);
100 if (i != 15)
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);
106 static void
107 at(void)
109 char buf[1024];
110 int i;
111 struct sockaddr_at sat;
112 const char *res;
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);
121 if (i != 11)
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);
127 static void
128 dl(void)
130 char buf[1024];
131 int i;
132 struct sockaddr_dl sdl;
133 const char *res;
135 (void)memset(&sdl, 0, sizeof(sdl));
136 sdl.sdl_len = sizeof(sdl);
137 sdl.sdl_family = AF_LINK;
138 sdl.sdl_index = 0;
139 sdl.sdl_type = 0;
140 sdl.sdl_nlen = 0;
141 sdl.sdl_alen = 6;
142 sdl.sdl_slen = 0;
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);
146 if (i != 17)
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[])
155 in();
156 #ifdef INET6
157 in6();
158 #endif
159 un();
160 at();
161 dl();
162 return 0;