4.2bsd IPC interface

Chris Torek chris at umcp-cs.UUCP
Fri Oct 25 16:14:39 AEST 1985


Note that setsockopt()s are preferable to ioctl()s on sockets, if the
operation is socket-specific; and all the more so if the operation is
protocol specific as well.  E.g., I might write something like this:

	int on = 1;
	...
	/*
	 * Get a socket and bind it to our address, so that we may
	 * begin accepting connections.  Turn on delayed accepts so
	 * that new connections are not acknowledged by the kernel
	 * until a TCP_ACCEPT operation is done.  (Connections may
	 * be silently ignored by simply closing the new socket,
	 * or actively rejected by doing a TCP_ACCEPT with a value
	 * of zero.  Or perhaps you may wish to do this another way.)
	 */
	if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0)
		error(1, errno, "socket");
	if (setsockopt(s, IPPROTO_TCP, TCP_DELAYEDACCEPT,
		       (caddr_t)&on, sizeof on))
		error(1, errno, "setsockopt (TCP_DELAYEDACCEPT)");
	if (bind(s, (caddr_t)&myaddr, sizeof myaddr))
		error(1, errno, "bind");
	if (listen(s, 5))
		error(1, errno, "listen");
	for (;;) {
		client = accept(s, (caddr_t)&sin, sizeof sin);
		if (client < 0) {
			if (errno == EINTR)	/* ick */
				continue;
			error(0, errno, "accept");
			continue;
		}
		/*
		 * Fiddle with `sin' to decide whether we are willing
		 * to accept the connection.  If not, actively reject it.
		 */
		if (badclient(&sin)) {
			int off = 0;

			if (setsockopt(client, IPPROTO_TCP, TCP_ACCEPT,
				       (caddr_t)&off, sizeof off))
				error(0, errno, "setsockopt (!TCP_ACCEPT)");
			(void) close(client);
			continue;
		}
		/*
		 * Accept it.
		 */
		if (setsockopt(client, IPPROTO_TCP, TCP_ACCEPT,
			       (caddr_t)&on, sizeof on)) {
			error(0, errno, "setsockopt (TCP_ACCEPT)");
			(void) close(client);
			continue;
		}
		/*
		 * Connection has now been accepted and communication is
		 * possible.
		 */
		...
	}

This code is quite feasible and would not take much work on the kernel,
now that 4.3BSD allows socket operations at the protocol levels.
-- 
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 4251)
UUCP:	seismo!umcp-cs!chris
CSNet:	chris at umcp-cs		ARPA:	chris at mimsy.umd.edu



More information about the Comp.unix.wizards mailing list