1 /* $NetBSD: uipc_syscalls_43.c,v 1.1 2001/07/17 11:32:28 jdolecek Exp $ */
4 * This is regression test for COMPAT_43 code. Tested 4.3 syscalls are:
5 * - getsockname(2), getpeername(2)
6 * - recv(2), recvfrom(2), recvmsg(2)
7 * - send(2), sendmsg(2)
9 * This program uses inetd echo service. You need to configure
10 * inetd to provide echo for both tcp and udp in order to run
11 * this program successfully, and adjust 'echoserver' to IP address
12 * of the machine running the service.
14 * Public domain. Do whatever you please with this. Jaromir Dolecek
17 #include <sys/syscall.h>
20 #include <sys/types.h>
21 #include <netinet/in.h>
23 #include <sys/socket.h>
26 const char *unixd
= "unixdomain";
27 const char *echoserver
= "127.0.0.1";
28 const char *localhost
= "127.0.0.1";
29 const int echoport
= 7;
36 struct sockaddr_in sa
;
37 struct sockaddr_un sun
;
38 struct osockaddr
*osa
= (struct osockaddr
*) &sa
;
44 * TCP connection, test connect(2), bind(2), recv(2), send(2),
45 * getsockname(2), getpeername(2).
47 if ((s
= socket(PF_INET
, SOCK_STREAM
, 0)) < 0)
50 sa
.sin_addr
.s_addr
= inet_addr(echoserver
);
51 sa
.sin_port
= htons(echoport
);
52 osa
->sa_family
= AF_INET
;
53 if (connect(s
, (struct sockaddr
*)&sa
, sizeof(sa
)) < 0)
58 memset(&sa
, '\0', sizeof(sa
));
59 if (syscall(SYS_compat_43_ogetpeername
, s
, (struct sockaddr
*) &sa
, &sz
))
60 err(1, "getpeername");
62 printf("ogetpeername: sz %d:%d name %s port %d family %d\n",
64 inet_ntoa(sa
.sin_addr
),
70 memset(&sa
, '\0', sizeof(sa
));
71 if (syscall(SYS_compat_43_ogetsockname
, s
, (struct sockaddr
*) &sa
, &sz
))
72 err(1, "getsockname");
74 printf("osockname: sz %d:%d name %s port %d family %d\n",
76 inet_ntoa(sa
.sin_addr
),
81 if (syscall(SYS_compat_43_osend
, s
, "fobj", 4, 0) < 0)
85 memset(buf
, '\0', sizeof(buf
));
86 if (syscall(SYS_compat_43_orecv
, s
, buf
, sizeof(buf
), 0) < 0)
89 printf("orecv: %s\n", buf
);
91 shutdown(s
, SHUT_RDWR
);
94 /* UDP connection, test sendto()/recvfrom() */
96 if ((s
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
99 sa
.sin_addr
.s_addr
= INADDR_ANY
;
100 sa
.sin_port
= htons(65533);
101 osa
->sa_family
= AF_INET
;
102 if (bind(s
, (struct sockaddr
*) &sa
, sizeof(sa
)))
107 memset(&sa
, '\0', sizeof(sa
));
108 if (syscall(SYS_compat_43_ogetsockname
, s
, (struct sockaddr
*) &sa
, &sz
))
109 err(1, "getsockname");
111 printf("osockname2: sz %d:%d name %s port %d family %d\n",
113 inet_ntoa(sa
.sin_addr
),
117 sa
.sin_addr
.s_addr
= inet_addr(echoserver
);
118 sa
.sin_port
= htons(echoport
);
119 osa
->sa_family
= AF_INET
;
120 /* common sendto(2) - not versioned */
121 if (sendto(s
, "fob2", 4, 0, (struct sockaddr
*) &sa
, sizeof(sa
)) < 0)
125 memset(buf
, '\0', sizeof(buf
));
126 memset(&sa
, '\0', sizeof(sa
));
128 if (syscall(SYS_compat_43_orecvfrom
, s
, buf
, sizeof(buf
), 0, (struct osockaddr
*) &sa
, &sz
) < 0)
130 printf("orecvfrom: '%s' sz %d:%d name %s port %d family %d\n",
133 inet_ntoa(sa
.sin_addr
),
137 shutdown(s
, SHUT_RDWR
);
140 /* UDP connection, test sendmsg()/recvmsg() */
142 if ((s
= socket(PF_INET
, SOCK_DGRAM
, IPPROTO_UDP
)) < 0)
145 sa
.sin_addr
.s_addr
= INADDR_ANY
;
146 sa
.sin_port
= htons(65533);
147 osa
->sa_family
= AF_INET
;
148 if (bind(s
, (struct sockaddr
*) &sa
, sizeof(sa
)))
151 sa
.sin_addr
.s_addr
= inet_addr(echoserver
);
152 sa
.sin_port
= htons(echoport
);
153 osa
->sa_family
= AF_INET
;
154 memset(&msg
, '\0', sizeof(msg
));
155 msg
.msg_name
= (void *) &sa
;
156 msg
.msg_namelen
= sizeof(sa
);
157 iov
.iov_base
= "fob3";
162 if (syscall(SYS_compat_43_osendmsg
, s
, &msg
, 0) < 0)
166 memset(&sa
, '\0', sizeof(sa
));
168 iov
.iov_len
= sizeof(buf
);
169 if (syscall(SYS_compat_43_orecvmsg
, s
, &msg
, 0) < 0)
172 printf("orecvmsg: '%s' sz %d:%d name %s port %d family %d\n",
174 sizeof(sa
), msg
.msg_namelen
,
175 inet_ntoa(sa
.sin_addr
),
179 shutdown(s
, SHUT_RDWR
);
183 * Local (unix domain) socket, test sendmsg()/recvmsg() with
187 if ((s
= socket(PF_LOCAL
, SOCK_DGRAM
, 0)) < 0)
190 osa
= (struct osockaddr
*) &sun
;
191 strcpy(sun
.sun_path
, unixd
);
192 osa
->sa_family
= AF_LOCAL
;
193 if (bind(s
, (struct sockaddr
*) &sun
, SUN_LEN(&sun
)))
196 /* osendmsg, old style descriptor passing */
197 memset(&msg
, '\0', sizeof(msg
));
198 msg
.msg_name
= (void *) &sun
;
199 msg
.msg_namelen
= sizeof(sun
);
200 iov
.iov_base
= "fob4";
205 msg
.msg_accrights
= (caddr_t
) &descr
;
206 msg
.msg_accrightslen
= sizeof(int);
207 if (syscall(SYS_compat_43_osendmsg
, s
, &msg
, 0) < 0) {
212 memset(&sun
, '\0', sizeof(sa
));
214 iov
.iov_len
= sizeof(buf
);
218 if (syscall(SYS_compat_43_orecvmsg
, s
, &msg
, 0) < 0) {
223 printf("orecvmsg: '%s' sz %d:%d name '%s' family %d descr %d\n",
225 sizeof(sun
), msg
.msg_namelen
, sun
.sun_path
,
226 osa
->sa_family
, descr
);