[BACK]Return to uipc_syscalls_43.c CVS log [TXT][DIR] Up to [cvs.NetBSD.org] / src / regress / sys / compat

File: [cvs.NetBSD.org] / src / regress / sys / compat / uipc_syscalls_43.c (download)

Revision 1.2, Thu Feb 21 07:38:20 2002 UTC (20 years, 5 months ago) by itojun
Branch: MAIN
CVS Tags: yamt-pf42-baseX, yamt-pf42-base4, yamt-pf42-base3, yamt-pf42-base2, yamt-pf42-base, yamt-pf42, yamt-pagecache-tag8, yamt-pagecache-base9, yamt-pagecache-base8, yamt-pagecache-base7, yamt-pagecache-base6, yamt-pagecache-base5, yamt-pagecache-base4, yamt-pagecache-base3, yamt-pagecache-base2, yamt-pagecache-base, yamt-pagecache, wrstuden-revivesa-base-3, wrstuden-revivesa-base-2, wrstuden-revivesa-base-1, wrstuden-revivesa-base, wrstuden-revivesa, wrstuden-fixsa-newbase, wrstuden-fixsa-base-1, wrstuden-fixsa-base, wrstuden-fixsa, tls-maxphys-base, tls-maxphys, tls-earlyentropy-base, tls-earlyentropy, riastradh-xf86-video-intel-2-7-1-pre-2-21-15, riastradh-drm2-base3, riastradh-drm2-base2, riastradh-drm2-base1, riastradh-drm2-base, riastradh-drm2, prg-localcount2-base3, prg-localcount2-base2, prg-localcount2-base1, prg-localcount2-base, prg-localcount2, phil-wifi-base, phil-wifi-20200421, phil-wifi-20200411, phil-wifi-20200406, phil-wifi-20191119, phil-wifi-20190609, phil-wifi, pgoyette-localcount-base, pgoyette-localcount-20170426, pgoyette-localcount-20170320, pgoyette-localcount-20170107, pgoyette-localcount-20161104, pgoyette-localcount-20160806, pgoyette-localcount-20160726, pgoyette-localcount, pgoyette-compat-merge-20190127, pgoyette-compat-base, pgoyette-compat-20190127, pgoyette-compat-20190118, pgoyette-compat-1226, pgoyette-compat-1126, pgoyette-compat-1020, pgoyette-compat-0930, pgoyette-compat-0906, pgoyette-compat-0728, pgoyette-compat-0625, pgoyette-compat-0521, pgoyette-compat-0502, pgoyette-compat-0422, pgoyette-compat-0415, pgoyette-compat-0407, pgoyette-compat-0330, pgoyette-compat-0322, pgoyette-compat-0315, pgoyette-compat, perseant-stdc-iso10646-base, perseant-stdc-iso10646, netbsd-9-base, netbsd-9-3-RELEASE, netbsd-9-2-RELEASE, netbsd-9-1-RELEASE, netbsd-9-0-RELEASE, netbsd-9-0-RC2, netbsd-9-0-RC1, netbsd-9, netbsd-8-base, netbsd-8-2-RELEASE, netbsd-8-1-RELEASE, netbsd-8-1-RC1, netbsd-8-0-RELEASE, netbsd-8-0-RC2, netbsd-8-0-RC1, netbsd-8, netbsd-7-nhusb-base-20170116, netbsd-7-nhusb-base, netbsd-7-nhusb, netbsd-7-base, netbsd-7-2-RELEASE, netbsd-7-1-RELEASE, netbsd-7-1-RC2, netbsd-7-1-RC1, netbsd-7-1-2-RELEASE, netbsd-7-1-1-RELEASE, netbsd-7-1, netbsd-7-0-RELEASE, netbsd-7-0-RC3, netbsd-7-0-RC2, netbsd-7-0-RC1, netbsd-7-0-2-RELEASE, netbsd-7-0-1-RELEASE, netbsd-7-0, netbsd-7, netbsd-6-base, netbsd-6-1-RELEASE, netbsd-6-1-RC4, netbsd-6-1-RC3, netbsd-6-1-RC2, netbsd-6-1-RC1, netbsd-6-1-5-RELEASE, netbsd-6-1-4-RELEASE, netbsd-6-1-3-RELEASE, netbsd-6-1-2-RELEASE, netbsd-6-1-1-RELEASE, netbsd-6-1, netbsd-6-0-RELEASE, netbsd-6-0-RC2, netbsd-6-0-RC1, netbsd-6-0-6-RELEASE, netbsd-6-0-5-RELEASE, netbsd-6-0-4-RELEASE, netbsd-6-0-3-RELEASE, netbsd-6-0-2-RELEASE, netbsd-6-0-1-RELEASE, netbsd-6-0, netbsd-6, netbsd-5-base, netbsd-5-2-RELEASE, netbsd-5-2-RC1, netbsd-5-2-3-RELEASE, netbsd-5-2-2-RELEASE, netbsd-5-2-1-RELEASE, netbsd-5-2, netbsd-5-1-RELEASE, netbsd-5-1-RC4, netbsd-5-1-RC3, netbsd-5-1-RC2, netbsd-5-1-RC1, netbsd-5-1-5-RELEASE, netbsd-5-1-4-RELEASE, netbsd-5-1-3-RELEASE, netbsd-5-1-2-RELEASE, netbsd-5-1-1-RELEASE, netbsd-5-1, netbsd-5-0-RELEASE, netbsd-5-0-RC4, netbsd-5-0-RC3, netbsd-5-0-RC2, netbsd-5-0-RC1, netbsd-5-0-2-RELEASE, netbsd-5-0-1-RELEASE, netbsd-5-0, netbsd-5, netbsd-4-base, netbsd-4-0-RELEASE, netbsd-4-0-RC5, netbsd-4-0-RC4, netbsd-4-0-RC3, netbsd-4-0-RC2, netbsd-4-0-RC1, netbsd-4-0-1-RELEASE, netbsd-4-0, netbsd-4, netbsd-3-base, netbsd-3-1-RELEASE, netbsd-3-1-RC4, netbsd-3-1-RC3, netbsd-3-1-RC2, netbsd-3-1-RC1, netbsd-3-1-1-RELEASE, netbsd-3-1, netbsd-3-0-RELEASE, netbsd-3-0-RC6, netbsd-3-0-RC5, netbsd-3-0-RC4, netbsd-3-0-RC3, netbsd-3-0-RC2, netbsd-3-0-RC1, netbsd-3-0-3-RELEASE, netbsd-3-0-2-RELEASE, netbsd-3-0-1-RELEASE, netbsd-3-0, netbsd-3, netbsd-2-base, netbsd-2-1-RELEASE, netbsd-2-1-RC6, netbsd-2-1-RC5, netbsd-2-1-RC4, netbsd-2-1-RC3, netbsd-2-1-RC2, netbsd-2-1-RC1, netbsd-2-1, netbsd-2-0-base, netbsd-2-0-RELEASE, netbsd-2-0-RC5, netbsd-2-0-RC4, netbsd-2-0-RC3, netbsd-2-0-RC2, netbsd-2-0-RC1, netbsd-2-0-3-RELEASE, netbsd-2-0-2-RELEASE, netbsd-2-0-1-RELEASE, netbsd-2-0, netbsd-2, netbsd-1-6-base, netbsd-1-6-RELEASE, netbsd-1-6-RC3, netbsd-1-6-RC2, netbsd-1-6-RC1, netbsd-1-6-PATCH002-RELEASE, netbsd-1-6-PATCH002-RC4, netbsd-1-6-PATCH002-RC3, netbsd-1-6-PATCH002-RC2, netbsd-1-6-PATCH002-RC1, netbsd-1-6-PATCH002, netbsd-1-6-PATCH001-RELEASE, netbsd-1-6-PATCH001-RC3, netbsd-1-6-PATCH001-RC2, netbsd-1-6-PATCH001-RC1, netbsd-1-6-PATCH001, netbsd-1-6, mjf-devfs2-base, mjf-devfs2, matt-premerge-20091211, matt-nb8-mediatek-base, matt-nb8-mediatek, matt-nb6-plus-nbase, matt-nb6-plus-base, matt-nb6-plus, matt-nb5-pq3-base, matt-nb5-pq3, matt-nb5-mips64-u2-k2-k4-k7-k8-k9, matt-nb5-mips64-u1-k1-k5, matt-nb5-mips64-premerge-20101231, matt-nb5-mips64-premerge-20091211, matt-nb5-mips64-k15, matt-nb5-mips64, matt-nb4-mips64-k7-u2a-k9b, matt-mips64-premerge-20101231, matt-mips64-base2, matt-mips64-base, matt-mips64, matt-armv6-prevmlocking, matt-armv6-nbase, matt-armv6-base, matt-armv6, localcount-20160914, keiichi-mipv6-base, keiichi-mipv6, jym-xensuspend-nbase, jym-xensuspend-base, jym-xensuspend, is-mlppp-base, is-mlppp, hpcarm-cleanup-nbase, hpcarm-cleanup-base, hpcarm-cleanup, fvdl_fs64_base, cube-autoconf-base, cube-autoconf, cjep_sun2x-base1, cjep_sun2x-base, cjep_sun2x, cjep_staticlib_x-base1, cjep_staticlib_x-base, cjep_staticlib_x, cherry-xenmp-base, cherry-xenmp, bouyer-socketcan-base1, bouyer-socketcan-base, bouyer-socketcan, bouyer-quota2-nbase, bouyer-quota2-base, bouyer-quota2, agc-symver-base, agc-symver, abandoned-netbsd-4-base, abandoned-netbsd-4, HEAD
Changes since 1.1: +1 -1 lines

put TNF 4-clause license.
authors - if you are okay with more relaxed license terms, or the regression
test code was derived from others, pls change them.

/*	$NetBSD: uipc_syscalls_43.c,v 1.2 2002/02/21 07:38:20 itojun Exp $	*/

/*
 * This is regression test for COMPAT_43 code. Tested 4.3 syscalls are:
 * - getsockname(2), getpeername(2)
 * - recv(2), recvfrom(2), recvmsg(2)
 * - send(2), sendmsg(2)
 * 
 * This program uses inetd echo service. You need to configure
 * inetd to provide echo for both tcp and udp in order to run
 * this program successfully, and adjust 'echoserver' to IP address
 * of the machine running the service.
 *
 * Public domain. Do whatever you please with this. Jaromir Dolecek
 */

#include <sys/syscall.h>
#include <unistd.h>
#include <err.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/un.h>
#include <sys/socket.h>
#include <sys/uio.h>

const char *unixd = "unixdomain";
const char *echoserver = "127.0.0.1";
const char *localhost = "127.0.0.1";
const int echoport = 7;

int
main()
{
	int s, descr;
	socklen_t sz;
	struct sockaddr_in sa;
	struct sockaddr_un sun;
	struct osockaddr *osa = (struct osockaddr *) &sa;
	struct omsghdr msg;
	struct iovec iov;
	char buf[10];

	/*
	 * TCP connection, test connect(2), bind(2), recv(2), send(2),
	 * getsockname(2), getpeername(2).
	 */
	if ((s = socket(PF_INET, SOCK_STREAM, 0)) < 0)
		err(1, "socket");
		
	sa.sin_addr.s_addr = inet_addr(echoserver);
	sa.sin_port = htons(echoport);
	osa->sa_family = AF_INET;
	if (connect(s, (struct sockaddr *)&sa, sizeof(sa)) < 0)
		err(1, "connect");

	/* ogetpeername */
	sz = sizeof(sa);
	memset(&sa, '\0', sizeof(sa));
	if (syscall(SYS_compat_43_ogetpeername, s, (struct sockaddr *) &sa, &sz))
		err(1, "getpeername");

	printf("ogetpeername: sz %d:%d name %s port %d family %d\n",
		sizeof(sa), sz,
		inet_ntoa(sa.sin_addr),
		ntohs(sa.sin_port),
		osa->sa_family);
	
	/* ogetsockname */
	sz = sizeof(sa);
	memset(&sa, '\0', sizeof(sa));
	if (syscall(SYS_compat_43_ogetsockname, s, (struct sockaddr *) &sa, &sz))
		err(1, "getsockname");

	printf("osockname: sz %d:%d name %s port %d family %d\n",
		sizeof(sa), sz,
		inet_ntoa(sa.sin_addr),
		ntohs(sa.sin_port),
		osa->sa_family);
	
	/* osend */
	if (syscall(SYS_compat_43_osend, s, "fobj", 4, 0) < 0)
		err(1, "osend");

	/* orecv */
	memset(buf, '\0', sizeof(buf));
	if (syscall(SYS_compat_43_orecv, s, buf, sizeof(buf), 0) < 0)
		err(1, "orecv");

	printf("orecv: %s\n", buf);

	shutdown(s, SHUT_RDWR);
	close(s);

	/* UDP connection, test sendto()/recvfrom() */

	if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
		err(1, "socket");
		
	sa.sin_addr.s_addr = INADDR_ANY;
	sa.sin_port = htons(65533);
	osa->sa_family = AF_INET;
	if (bind(s, (struct sockaddr *) &sa, sizeof(sa)))
		err(1, "bind1");

	/* ogetsockname */
	sz = sizeof(sa);
	memset(&sa, '\0', sizeof(sa));
	if (syscall(SYS_compat_43_ogetsockname, s, (struct sockaddr *) &sa, &sz))
		err(1, "getsockname");

	printf("osockname2: sz %d:%d name %s port %d family %d\n",
		sizeof(sa), sz,
		inet_ntoa(sa.sin_addr),
		ntohs(sa.sin_port),
		osa->sa_family);
	
	sa.sin_addr.s_addr = inet_addr(echoserver);
	sa.sin_port = htons(echoport);
	osa->sa_family = AF_INET;
	/* common sendto(2) - not versioned */
	if (sendto(s, "fob2", 4, 0, (struct sockaddr *) &sa, sizeof(sa)) < 0)
		err(1, "sendto");

	/* orecvfrom */
	memset(buf, '\0', sizeof(buf));
	memset(&sa, '\0', sizeof(sa));
	sz = sizeof(sa);
	if (syscall(SYS_compat_43_orecvfrom, s, buf, sizeof(buf), 0, (struct osockaddr *) &sa, &sz) < 0)
		err(1, "orecvfrom");
	printf("orecvfrom: '%s' sz %d:%d name %s port %d family %d\n",
		buf,
		sizeof(sa), sz,
		inet_ntoa(sa.sin_addr),
		ntohs(sa.sin_port),
		osa->sa_family);

	shutdown(s, SHUT_RDWR);
	close(s);

	/* UDP connection, test sendmsg()/recvmsg() */

	if ((s = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
		err(1, "socket");
		
	sa.sin_addr.s_addr = INADDR_ANY;
	sa.sin_port = htons(65533);
	osa->sa_family = AF_INET;
	if (bind(s, (struct sockaddr *) &sa, sizeof(sa)))
		err(1, "bind2");

	sa.sin_addr.s_addr = inet_addr(echoserver);
	sa.sin_port = htons(echoport);
	osa->sa_family = AF_INET;
	memset(&msg, '\0', sizeof(msg));
	msg.msg_name = (void *) &sa;
	msg.msg_namelen = sizeof(sa);
	iov.iov_base = "fob3";
	iov.iov_len = 4;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	/* osendmsg */
	if (syscall(SYS_compat_43_osendmsg, s, &msg, 0) < 0)
		err(1, "osendmsg");

	/* orecvmsg */
	memset(&sa, '\0', sizeof(sa));
	iov.iov_base = buf;
	iov.iov_len = sizeof(buf);
	if (syscall(SYS_compat_43_orecvmsg, s, &msg, 0) < 0)
		err(1, "orecvmsg");

	printf("orecvmsg: '%s' sz %d:%d name %s port %d family %d\n",
		buf,
		sizeof(sa), msg.msg_namelen,
		inet_ntoa(sa.sin_addr),
		ntohs(sa.sin_port),
		osa->sa_family);

	shutdown(s, SHUT_RDWR);
	close(s);

	/*
	 * Local (unix domain) socket, test sendmsg()/recvmsg() with
	 * accrights
	 */

	if ((s = socket(PF_LOCAL, SOCK_DGRAM, 0)) < 0)
		err(1, "socket");
		
	osa = (struct osockaddr *) &sun;
	strcpy(sun.sun_path, unixd);
	osa->sa_family = AF_LOCAL;
	if (bind(s, (struct sockaddr *) &sun, SUN_LEN(&sun)))
		err(1, "bind3");

	/* osendmsg, old style descriptor passing */
	memset(&msg, '\0', sizeof(msg));
	msg.msg_name = (void *) &sun;
	msg.msg_namelen = sizeof(sun);
	iov.iov_base = "fob4";
	iov.iov_len = 4;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	descr = s;
	msg.msg_accrights = (caddr_t) &descr;
	msg.msg_accrightslen = sizeof(int);
	if (syscall(SYS_compat_43_osendmsg, s, &msg, 0) < 0) {
		unlink(unixd);
		err(1, "osendmsg");
	}

	memset(&sun, '\0', sizeof(sa));
	iov.iov_base = buf;
	iov.iov_len = sizeof(buf);
	descr = -1;

	/* orecvmsg */
	if (syscall(SYS_compat_43_orecvmsg, s, &msg, 0) < 0) {
		unlink(unixd);
		err(1, "orecvmsg");
	}

	printf("orecvmsg: '%s' sz %d:%d name '%s' family %d descr %d\n",
		buf,
		sizeof(sun), msg.msg_namelen, sun.sun_path,
		osa->sa_family, descr);

	unlink(unixd);
	close(s);
	close(descr);

}