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

📄 4.t

📁 早期freebsd实现
💻 T
📖 第 1 页 / 共 2 页
字号:
.\" Copyright (c) 1986, 1993.\"	The Regents of the University of California.  All rights reserved..\".\" Redistribution and use in source and binary forms, with or without.\" modification, are permitted provided that the following conditions.\" are met:.\" 1. Redistributions of source code must retain the above copyright.\"    notice, this list of conditions and the following disclaimer..\" 2. Redistributions in binary form must reproduce the above copyright.\"    notice, this list of conditions and the following disclaimer in the.\"    documentation and/or other materials provided with the distribution..\" 3. All advertising materials mentioning features or use of this software.\"    must display the following acknowledgement:.\"	This product includes software developed by the University of.\"	California, Berkeley and its contributors..\" 4. Neither the name of the University nor the names of its contributors.\"    may be used to endorse or promote products derived from this software.\"    without specific prior written permission..\".\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION).\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF.\" SUCH DAMAGE..\".\"	@(#)4.t	8.1 (Berkeley) 6/8/93.\".\".ds RH "Client/Server Model.bp.nr H1 4.nr H2 0.sp 8i.bp.LG.B.ce4. CLIENT/SERVER MODEL.sp 2.R.NL.PPThe most commonly used paradigm in constructing distributed applicationsis the client/server model.  In this scheme client applications requestservices from a server process.  This implies an asymmetry in establishingcommunication between the client and server which has been examinedin section 2.  In this section we will look more closely at the interactionsbetween client and server, and consider some of the problems in developingclient and server applications..PPThe client and server require a well known set of conventions beforeservice may be rendered (and accepted).  This set of conventionscomprises a protocol which must be implemented at both ends of aconnection.  Depending on the situation, the protocol may be symmetricor asymmetric.  In a symmetric protocol, either side may play the master or slave roles.  In an asymmetric protocol, one side isimmutably recognized as the master, with the other as the slave.  An example of a symmetric protocol is the TELNET protocol used inthe Internet for remote terminal emulation.  An exampleof an asymmetric protocol is the Internet file transfer protocol,FTP.  No matter whether the specific protocol used in obtaininga service is symmetric or asymmetric, when accessing a service thereis a \*(lqclient process\*(rq and a \*(lqserver process\*(rq.  Wewill first consider the properties of server processes, thenclient processes..PPA server process normally listens at a well known address forservice requests.  That is, the server process remains dormantuntil a connection is requested by a client's connectionto the server's address.  At such a timethe server process ``wakes up'' and services the client,performing whatever appropriate actions the client requests of it..PPAlternative schemes which use a service servermay be used to eliminate a flock of server processes clogging thesystem while remaining dormant most of the time.  For Internetservers in 4.4BSD,this scheme has been implemented via \fIinetd\fP, the so called``internet super-server.''  \fIInetd\fP listens at a varietyof ports, determined at start-up by reading a configuration file.When a connection is requested to a port on which \fIinetd\fP islistening, \fIinetd\fP executes the appropriate server program to handle theclient.  With this method, clients are unaware that anintermediary such as \fIinetd\fP has played any part in theconnection.  \fIInetd\fP will be described in more detail insection 5..PPA similar alternative scheme is used by most Xerox services.  In general,the Courier dispatch process (if used) accepts connections fromprocesses requesting services of some sort or another.  The clientprocesses request a particular <program number, version number, procedurenumber> triple.  If the dispatcher knows of such a program, it isstarted to handle the request; if not, an error is reported to theclient.  In this way, only one port is required to service a largevariety of different requests.  Again, the Courier facilities arenot available without the use and installation of the Couriercompiler.  The information presented in this section applies onlyto NS clients and services that do not use Courier..NH 2Servers.PPIn 4.4BSD most servers are accessed at well known Internet addressesor UNIX domain names.  Forexample, the remote login server's main loop is of the form shownin Figure 2..KF.if t .ta .5i 1.0i 1.5i 2.0i 2.5i 3.0i 3.5i.if n .ta .7i 1.4i 2.1i 2.8i 3.5i 4.2i 4.9i.sp 0.5i.DSmain(argc, argv)	int argc;	char *argv[];{	int f;	struct sockaddr_in from;	struct servent *sp;	sp = getservbyname("login", "tcp");	if (sp == NULL) {		fprintf(stderr, "rlogind: tcp/login: unknown service\en");		exit(1);	}	...#ifndef DEBUG	/* Disassociate server from controlling terminal */	...#endif	sin.sin_port = sp->s_port;	/* Restricted port -- see section 5 */	...	f = socket(AF_INET, SOCK_STREAM, 0);	...	if (bind(f, (struct sockaddr *) &sin, sizeof (sin)) < 0) {		...	}	...	listen(f, 5);	for (;;) {		int g, len = sizeof (from);		g = accept(f, (struct sockaddr *) &from, &len);		if (g < 0) {			if (errno != EINTR)				syslog(LOG_ERR, "rlogind: accept: %m");			continue;		}		if (fork() == 0) {			close(f);			doit(g, &from);		}		close(g);	}}.DE.ceFigure 2.  Remote login server..sp 0.5i.KE.PPThe first step taken by the server is look up its servicedefinition:.sp 1.nf.in +5.if t .ta .5i 1.0i 1.5i 2.0i.if n .ta .7i 1.4i 2.1i 2.8isp = getservbyname("login", "tcp");if (sp == NULL) {	fprintf(stderr, "rlogind: tcp/login: unknown service\en");	exit(1);}.sp 1.in -5.fiThe result of the \fIgetservbyname\fP callis used in later portions of the code todefine the Internet port at which it listens for servicerequests (indicated by a connection)..KS.PPStep two is to disassociate the server from the controllingterminal of its invoker:.DS	for (i = 0; i < 3; ++i)		close(i);	open("/", O_RDONLY);	dup2(0, 1);	dup2(0, 2);	i = open("/dev/tty", O_RDWR);	if (i >= 0) {		ioctl(i, TIOCNOTTY, 0);		close(i);	}.DE.KEThis step is important as the server willlikely not want to receive signals delivered to the processgroup of the controlling terminal.  Note, however, thatonce a server has disassociated itself it can no longersend reports of errors to a terminal, and must log errorsvia \fIsyslog\fP..PPOnce a server has established a pristine environment, itcreates a socket and begins accepting service requests.The \fIbind\fP call is required to insure the server listensat its expected location.  It should be noted that theremote login server listens at a restricted port number, and musttherefore be runwith a user-id of root.This concept of a ``restricted port number'' is 4BSDspecific, and is covered in section 5..PPThe main body of the loop is fairly simple:.DS.if t .ta .5i 1.0i 1.5i 2.0i.if n .ta .7i 1.4i 2.1i 2.8ifor (;;) {	int g, len = sizeof (from);	g = accept(f, (struct sockaddr *)&from, &len);	if (g < 0) {		if (errno != EINTR)			syslog(LOG_ERR, "rlogind: accept: %m");		continue;	}	if (fork() == 0) {	/* Child */		close(f);		doit(g, &from);	}	close(g);		/* Parent */}.DEAn \fIaccept\fP call blocks the server untila client requests service.  This call could return afailure status if the call is interrupted by a signalsuch as SIGCHLD (to be discussed in section 5).  Therefore,the return value from \fIaccept\fP is checked to insurea connection has actually been established, andan error report is logged via \fIsyslog\fP if an errorhas occurred..PPWith a connectionin hand, the server then forks a child process and invokesthe main body of the remote login protocol processing.  Notehow the socket used by the parent for queuing connectionrequests is closed in the child, while the socket created asa result of the \fIaccept\fP is closed in the parent.  Theaddress of the client is also handed the \fIdoit\fP routinebecause it requires it in authenticating clients..NH 2

⌨️ 快捷键说明

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