⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tlssrv.c

📁 这是一个同样来自贝尔实验室的和UNIX有着渊源的操作系统, 其简洁的设计和实现易于我们学习和理解
💻 C
字号:
#include <u.h>#include <libc.h>#include <bio.h>#include <mp.h>#include <libsec.h>enum{ BufSize = 8192 };char *remotesys, *logfile;int debug, p[2];voiddeath(void *, char *){	int pid;	close(0);	close(1);	close(p[1]);	pid = getpid();	postnote(PNGROUP, pid, "die");	postnote(PNGROUP, pid, "die");	postnote(PNGROUP, pid, "die");	_exits(0);}static voiddump(int fd, uchar *buf, int n, char *label){	Biobuf bout;	int i;	Binit(&bout, fd, OWRITE);	Bprint(&bout, "%s<%d>: ", label, n);	if(n > 64)		n = 64;	for(i = 0; i < n; i++)		Bprint(&bout, "%2.2x ", buf[i]);	Bprint(&bout, "\n");	Bterm(&bout);}static voidxfer(int from, int to, int cfd, char *label){	uchar buf[BufSize];	int n;	if(fork() == 0)		return;	close(cfd);	for(;;){		n = read(from, buf, sizeof(buf));		if(n <= 0){			fprint(2, "%s EOF\n", label);			close(to);			close(from);			death(nil, nil);		}		dump(2, buf, n, label);		n = write(to, buf, n);		if(n < 0){			fprint(2, "%s write err\n", label);			close(to);			close(from);			death(nil, nil);		}	}}static intdumper(int fd){	int p[2];	if(pipe(p) < 0)		sysfatal("can't make pipe: %r");	xfer(fd, p[0], p[1], "read");	xfer(p[0], fd, p[1], "write");	close(p[0]);	return p[1];}static intreporter(char *fmt, ...){	va_list ap;	char buf[2000];	va_start(ap, fmt);	if(logfile){		vsnprint(buf, sizeof buf, fmt, ap);		syslog(0, logfile, "%s tls reports %s", remotesys, buf);	}else{		fprint(2, "%s: %s tls reports ", argv0, remotesys);		vfprint(2, fmt, ap);		fprint(2, "\n");	}	va_end(ap);	return 0;}voidusage(void){	fprint(2, "usage: tlssrv -c cert [-D] [-l logfile] [-r remotesys] [cmd args...]\n");	fprint(2, "  after  auth/secretpem key.pem > /mnt/factotum/ctl\n");	exits("usage");}voidmain(int argc, char *argv[]){	TLSconn *conn;	uchar buf[BufSize];	char *cert;	int n, fd, clearfd;	debug = 0;	remotesys = nil;	cert = nil;	logfile = nil;	ARGBEGIN{	case 'D':		debug++;		break;	case 'c':		cert = EARGF(usage());		break;	case 'l':		logfile = EARGF(usage());		break;	case 'r':		remotesys = EARGF(usage());		break;	default:		usage();	}ARGEND	if(cert == nil)		sysfatal("no certificate specified");	if(remotesys == nil)		remotesys = "";	conn = (TLSconn*)mallocz(sizeof *conn, 1);	if(conn == nil)		sysfatal("out of memory");	conn->chain = readcertchain(cert);	if (conn->chain == nil)		sysfatal("can't read certificate");	conn->cert = conn->chain->pem;	conn->certlen = conn->chain->pemlen;	conn->chain = conn->chain->next;	if(debug)		conn->trace = reporter;	clearfd = 0;	fd = 1;	if(debug > 1)		fd = dumper(fd);	fd = tlsServer(fd, conn);	if(fd < 0){		reporter("failed: %r");		exits(0);	}	reporter("open\n");	if(argc > 0){		if(pipe(p) < 0)			exits("pipe");		switch(fork()){		case 0:			close(fd);			dup(p[0], 0);			dup(p[0], 1);			close(p[1]);			close(p[0]);			exec(argv[0], argv);			reporter("can't exec %s: %r", argv[0]);			_exits("exec");		case -1:			exits("fork");		default:			close(p[0]);			clearfd = p[1];			break;		}	}	rfork(RFNOTEG);	notify(death);	switch(rfork(RFPROC)){	case -1:		sysfatal("can't fork");	case 0:		for(;;){			n = read(clearfd, buf, BufSize);			if(n <= 0)				break;			if(write(fd, buf, n) != n)				break;		}		break;	default:		for(;;){			n = read(fd, buf, BufSize);			if(n <= 0)				break;			if(write(clearfd, buf, n) != n)				break;		}		break;	}	death(nil, nil);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -