🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: news.software.nntp
Article #8824

Re: idea: pre-spawned nnrpds

#8824
From: T. William Wells
Date: Thu, 27 Jun 1996 00:00
310 lines
7715 bytes
In article <4quo1b$aom@brasil.moneng.mei.com>,
Joe Greco <jgreco@brasil.moneng.mei.com> wrote:

Another useful feechur I added was to arrange that incoming
connections were compared to the conf file both as host addresses
and as network addresses. That let me redirect entire networks.

: I don't see the problem..  I can't think of a situation where there would be
: deadlock...  I've traditionally avoided select for writes but have buffered
: pending uncompleted writes (instead of calling bufwrite()) ..  I haven't
: observed a problem with either method, and the code is simpler.  :-)

The symptom was loads of connection timeouts; deadlock was the
presumed cause, as when I did select for writes the problem went
away.

Anyhow, my version of connectd is sufficiently short that I've
decided to include it here. This isn't even alpha quality code:
it has no comments. Also, at one point I noticed some oddball
stuff getting syslogged, looks like I missed an argument
somewhere, but I haven't tracked it down. It does work, though....

(System is FreeBSD2.1. I *hate* #ifs!)

#include <fcntl.h>
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#include <netdb.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <ctype.h>
#include <stdlib.h>
#include <syslog.h>

#define BACKLOG         32
#define CONNECTD_CONF   "/home/news/etc/reflector.conf"
#define NNRPD_PATH      "/home/news/inn/bin/in.nnrpd"
#define NNRP_ARG        "-s                                                "
#define MYADDR          "204.213.252.15"
#define MYPORT          119
#define XBYTES          16384
#define MYLOG           LOG_DAEMON
#define SERVERPORT      119

typedef struct {
	int     b_eof;
	int     b_rfd;
	int     b_wfd;
	int     b_start;
	int     b_end;
	char    b_buf[XBYTES];
} BUF;

static void reflector(int afd, char *server);

static void
collect_dead_kids(
	int     val)
{
	int i;

	if (wait3(&i, WNOHANG, NULL) < 0) {
		if (errno != ECHILD) {
			syslog(LOG_ERR, "wait3: %m");
		}
	}
}

static void
signal_pipe(
	int     val)
{
	syslog(LOG_ERR, "killed by SIGPIPE");
}

int
main(void)
{
	int     afd;
	int     lfd;
	int     flag;
	int     len;
	FILE    *fp;
	struct sockaddr_in sin;
	char    peer[64];
	char    net[64];
	char    addr[64];
	char    server[64];

	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	inet_aton(MYADDR, &sin.sin_addr);
	sin.sin_port = htons(MYPORT);
	if ((lfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		perror("socket");
		exit(1);
	}
	len = sizeof(flag);
	if (setsockopt(lfd, SOL_SOCKET, SO_REUSEADDR, (char *)&flag, len)
	    < 0) {
		perror("setsockopt");
		exit(1);
	}
	if (bind(lfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
		perror("bind");
		exit(1);
	}
	if (listen(lfd, BACKLOG) < 0) {
		perror("listen");
		exit(1);
	}
	if (setgid(getgid()) < 0) {
		perror("setgid");
		exit(1);
	}
	if (setuid(getuid()) < 0) {
		perror("setuid");
		exit(1);
	}
	if (signal(SIGCHLD, collect_dead_kids) < 0) {
		perror("signal");
		exit(1);
	}
	if (daemon(0, 0) < 0) {
		perror("daemon");
		exit(1);
	}
	openlog("connectd", LOG_PID, MYLOG);
	while (1) {
		collect_dead_kids(0);
		len = sizeof(sin);
		if ((afd = accept(lfd, (struct sockaddr *)&sin, &len)) < 0) {
			syslog(LOG_ERR, "accept: %m");
			sleep(1);
			continue;
		}
		switch (fork()) {
		default:
			close(afd);
			continue;
		case 0:
			break;
		case -1:
			syslog(LOG_ERR, "fork: %m");
			close(afd);
			continue;
		}
		break;
	}
	close(lfd);
	fp = fopen(CONNECTD_CONF, "r");
	if (fp) {
		strcpy(peer, inet_ntoa(sin.sin_addr));
		strcpy(net,
		    inet_ntoa(inet_makeaddr(inet_netof(sin.sin_addr), 0L)));
		while (!feof(fp)) {
			fscanf(fp, "%s %s", addr, server);
			if (strcmp(addr, peer) == 0
			    || strcmp(addr, net) == 0) {
				fclose(fp);
				syslog(LOG_INFO,
				    "connection from %s for innd", peer);
				reflector(afd, server);
				syslog(LOG_INFO,
				    "connection from %s closed", peer);
				exit(1);
			}
		}
		fclose(fp);
	}
	syslog(LOG_INFO, "connection from %s for nnrp", peer);
	if (dup2(afd, 0) < 0
	    || dup2(afd, 1) < 0
	    || dup2(afd, 2) < 0) {
		syslog(LOG_ERR, "dup2: %m");
		exit(1);
	}
	close(afd);
	execl(NNRPD_PATH, NNRPD_PATH, NNRP_ARG, NULL);
	syslog(LOG_ERR, "execl: %m");
	exit(1);
}

static void
check_write(
	fd_set  *fds,
	BUF     *buf)
{
	int     cnt;

	if (!FD_ISSET(buf->b_wfd, fds)) {
		return;
	}
	cnt = write(buf->b_wfd, buf->b_buf + buf->b_start,
	    buf->b_end - buf->b_start);
	if (cnt < 0) {
		syslog(LOG_ERR, "write: %m");
		exit(1);
	}
	buf->b_start += cnt;
	if (buf->b_start == buf->b_end) {
		buf->b_start = buf->b_end = 0;
	}
}

static void
check_read(
	fd_set  *fds,
	BUF     *buf)
{
	int     cnt;

	if (!FD_ISSET(buf->b_rfd, fds)) {
		return;
	}
	if (buf->b_end == XBYTES) {
		if (buf->b_start == 0) {
			return;
		}
		cnt = buf->b_end - buf->b_start;
		memmove(buf->b_buf, buf->b_buf + buf->b_start, cnt);
		buf->b_start = 0;
		buf->b_end = cnt;
	}
	cnt = read(buf->b_rfd, buf->b_buf + buf->b_end,
	    XBYTES - buf->b_end);
	if (cnt < 0) {
		syslog(LOG_ERR, "read: %m");
		exit(1);
	}
	if (cnt == 0) {
		buf->b_eof = 1;
	}
	buf->b_end += cnt;
}

static void
reflector(
	int     afd,
	char    *server)
{
	int     cfd;
	struct  sockaddr_in sin;
	fd_set  rfds;
	fd_set  wfds;
	int     hfd;
	BUF     Ab;
	BUF     Cb;

	signal(SIGPIPE, signal_pipe);
	if ((cfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
		syslog(LOG_ERR, "socket: %m");
		exit(1);
	}
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	inet_aton(MYADDR, &sin.sin_addr);
	sin.sin_port = 0;
	if (bind(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
		syslog(LOG_ERR, "bind: %m");
		exit(1);
	}
	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	inet_aton(server, &sin.sin_addr);
	sin.sin_port = htons(SERVERPORT);
	if (connect(cfd, (struct sockaddr *)&sin, sizeof(sin)) < 0) {
		syslog(LOG_ERR, "connect: %m");
		exit(1);
	}
	hfd = (cfd > afd ? cfd : afd) + 1;
	Ab.b_rfd = afd; Ab.b_wfd = cfd; Ab.b_start = Ab.b_end = Ab.b_eof = 0;
	Cb.b_rfd = cfd; Cb.b_wfd = afd; Cb.b_start = Cb.b_end = Cb.b_eof = 0;
	while (1) {
		FD_ZERO(&rfds);
		FD_ZERO(&wfds);
		if ((Ab.b_eof || Cb.b_eof)
		   && (Ab.b_eof || Cb.b_start == Cb.b_end)
		   && (Cb.b_eof || Ab.b_start == Ab.b_end)) {
			return;
		}
		if (!Ab.b_eof && Ab.b_end < XBYTES) {
			FD_SET(afd, &rfds);
		}
		if (!Cb.b_eof && Cb.b_end < XBYTES) {
			FD_SET(cfd, &rfds);
		}
		if (Cb.b_start != Cb.b_end) {
			FD_SET(afd, &wfds);
		}
		if (Ab.b_start != Ab.b_end) {
			FD_SET(cfd, &wfds);
		}
		if (select(hfd, &rfds, &wfds, NULL, NULL) < 0) {
			syslog(LOG_ERR, "select: %m");
			exit(1);
		}
		check_write(&wfds, &Cb);
		check_write(&wfds, &Ab);
		check_read(&rfds, &Cb);
		check_read(&rfds, &Ab);
	}
}

Technical Details
Message-ID: <4quuoh$rs8@twwells.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
References: <4p9ta0$1vn@thunder.mgl.ca> <4q8vnk$q6d@brasil.moneng.mei.com> <4qnqci$2i3@twwells.com> <4quo1b$aom@brasil.moneng.mei.com>

Show Full Headers
Complete Article Headers
Message-ID: <4quuoh$rs8@twwells.com>
Subject: Re: idea: pre-spawned nnrpds
Date: Thu, 27 Jun 1996 00:00:00 +0000
From: bill@twwells.com (T. William Wells)
References: <4p9ta0$1vn@thunder.mgl.ca> <4q8vnk$q6d@brasil.moneng.mei.com> <4qnqci$2i3@twwells.com> <4quo1b$aom@brasil.moneng.mei.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!illumos!news.pugleaf.net!.TX!archive.pugleaf.net!archive.newsdeef.eu!mbox2nntp-news.software.nntp.mbox.zip!not-for-mail
X-Deja-AN: 162456804
Organization: None, Mt. Laurel, NJ
Newsgroups: news.software.nntp