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

📄 4.t

📁 早期freebsd实现
💻 T
📖 第 1 页 / 共 2 页
字号:
Clients.PPThe client side of the remote login service was shownearlier in Figure 1.One can see the separate, asymmetric roles of the clientand server clearly in the code.  The server is a passive entity,listening for client connections, while the client process isan active entity, initiating a connection when invoked.  .PPLet us consider more closely the steps takenby the client remote login process.  As in the server process,the first step is to locate the service definition for a remotelogin:.DSsp = getservbyname("login", "tcp");if (sp == NULL) {	fprintf(stderr, "rlogin: tcp/login: unknown service\en");	exit(1);}.DENext the destination host is looked up with a\fIgethostbyname\fP call:.DShp = gethostbyname(argv[1]);if (hp == NULL) {	fprintf(stderr, "rlogin: %s: unknown host\en", argv[1]);	exit(2);}.DEWith this accomplished, all that is required is to establish aconnection to the server at the requested host and start up theremote login protocol.  The address buffer is cleared, then filledin with the Internet address of the foreign host and the portnumber at which the login process resides on the foreign host:.DSbzero((char *)&server, sizeof (server));bcopy(hp->h_addr, (char *) &server.sin_addr, hp->h_length);server.sin_family = hp->h_addrtype;server.sin_port = sp->s_port;.DEA socket is created, and a connection initiated.  Notethat \fIconnect\fP implicitly performs a \fIbind\fPcall, since \fIs\fP is unbound..DSs = socket(hp->h_addrtype, SOCK_STREAM, 0);if (s < 0) {	perror("rlogin: socket");	exit(3);} ...if (connect(s, (struct sockaddr *) &server, sizeof (server)) < 0) {	perror("rlogin: connect");	exit(4);}.DEThe details of the remote login protocol will not be considered here..NH 2Connectionless servers.PPWhile connection-based services are the norm, some servicesare based on the use of datagram sockets.  One, in particular,is the \*(lqrwho\*(rq service which provides users with statusinformation for hosts connected to a local areanetwork.  This service, while predicated on the ability to\fIbroadcast\fP information to all hosts connected to a particularnetwork, is of interest as an example usage of datagram sockets..PPA user on any machine running the rwho server may find outthe current status of a machine with the \fIruptime\fP(1) program.The output generated is illustrated in Figure 3..KF.DS B.TSl r l l l l l.arpa	up	9:45,	5 users, load	1.15,	1.39,	1.31cad	up	2+12:04,	8 users, load	4.67,	5.13,	4.59calder	up	10:10,	0 users, load	0.27,	0.15,	0.14dali	up	2+06:28,	9 users, load	1.04,	1.20,	1.65degas	up	25+09:48,	0 users, load	1.49,	1.43,	1.41ear	up	5+00:05,	0 users, load	1.51,	1.54,	1.56ernie	down	0:24esvax	down	17:04ingres	down	0:26kim	up	3+09:16,	8 users, load	2.03,	2.46,	3.11matisse	up	3+06:18,	0 users, load	0.03,	0.03,	0.05medea	up	3+09:39,	2 users, load	0.35,	0.37,	0.50merlin	down	19+15:37miro	up	1+07:20,	7 users, load	4.59,	3.28,	2.12monet	up	1+00:43,	2 users, load	0.22,	0.09,	0.07oz	down	16:09statvax	up	2+15:57,	3 users, load	1.52,	1.81,	1.86ucbvax	up	9:34,	2 users, load	6.08,	5.16,	3.28.TE.DE.ceFigure 3. ruptime output..sp.KE.PPStatus information for each host is periodically broadcastby rwho server processes on each machine.  The same serverprocess also receives the status information and uses itto update a database.  This database is then interpretedto generate the status information for each host.  Serversoperate autonomously, coupled only by the local network andits broadcast capabilities..PPNote that the use of broadcast for such a task is fairly inefficient,as all hosts must process each message, whether or not using an rwho server.Unless such a service is sufficiently universal and is frequently used,the expense of periodic broadcasts outweighs the simplicity..PPMulticasting is an alternative to broadcasting.Setting up multicast sockets is described in Section 5.10..PPThe rwho server, in a simplified form, is pictured in Figure4.  There are two separate tasks performed by the server.  Thefirst task is to act as a receiver of status information broadcastby other hosts on the network.  This job is carried out in themain loop of the program.  Packets received at the rwho portare interrogated to insure they've been sent by another rwhoserver process, then are time stamped with their arrival timeand used to update a file indicating the status of the host.When a host has not been heard from for an extended period oftime, the database interpretation routines assume the host isdown and indicate such on the status reports.  This algorithmis prone to error as a server may be down while a host is actuallyup, but serves our current needs..KF.DS.if t .ta .5i 1.0i 1.5i 2.0i.if n .ta .7i 1.4i 2.1i 2.8imain(){	...	sp = getservbyname("who", "udp");	net = getnetbyname("localnet");	sin.sin_addr = inet_makeaddr(INADDR_ANY, net);	sin.sin_port = sp->s_port;	...	s = socket(AF_INET, SOCK_DGRAM, 0);	...	on = 1;	if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &on, sizeof(on)) < 0) {		syslog(LOG_ERR, "setsockopt SO_BROADCAST: %m");		exit(1);	}	bind(s, (struct sockaddr *) &sin, sizeof (sin));	...	signal(SIGALRM, onalrm);	onalrm();	for (;;) {		struct whod wd;		int cc, whod, len = sizeof (from);		cc = recvfrom(s, (char *)&wd, sizeof (struct whod), 0,		    (struct sockaddr *)&from, &len);		if (cc <= 0) {			if (cc < 0 && errno != EINTR)				syslog(LOG_ERR, "rwhod: recv: %m");			continue;		}		if (from.sin_port != sp->s_port) {			syslog(LOG_ERR, "rwhod: %d: bad from port",				ntohs(from.sin_port));			continue;		}		...		if (!verify(wd.wd_hostname)) {			syslog(LOG_ERR, "rwhod: malformed host name from %x",				ntohl(from.sin_addr.s_addr));			continue;		}		(void) sprintf(path, "%s/whod.%s", RWHODIR, wd.wd_hostname);		whod = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0666);		...		(void) time(&wd.wd_recvtime);		(void) write(whod, (char *)&wd, cc);		(void) close(whod);	}}.DE.ceFigure 4.  rwho server..sp.KE.PPThe second task performed by the server is to supply informationregarding the status of its host.  This involves periodicallyacquiring system status information, packaging it up in a messageand broadcasting it on the local network for other rwho serversto hear.  The supply function is triggered by a timer and runs off a signal.  Locating the system statusinformation is somewhat involved, but uninteresting.  Decidingwhere to transmit the resultant packetis somewhat problematical, however..PPStatus information must be broadcast on the local network.For networks which do not support the notion of broadcast anotherscheme must be used to simulate orreplace broadcasting.  One possibility is to enumerate theknown neighbors (based on the status messages receivedfrom other rwho servers).  This, unfortunately,requires some bootstrapping information,for a server will have no idea what machines are itsneighbors until it receives status messages from them.Therefore, if all machines on a net are freshly booted,no machine will have anyknown neighbors and thus never receive, or send, any status information.This is the identical problem faced by the routing table managementprocess in propagating routing status information.  The standardsolution, unsatisfactory as it may be, is to inform one or more serversof known neighbors and request that they always communicate withthese neighbors.  If each server has at least one neighbor suppliedto it, status information may then propagate througha neighbor to hosts whichare not (possibly) directly neighbors.  If the server is able tosupport networks which provide a broadcast capability, as well asthose which do not, then networks with anarbitrary topology may share status information*..FS* One must, however, be concerned about \*(lqloops\*(rq.That is, if a host is connected to multiple networks, itwill receive status information from itself.  This can leadto an endless, wasteful, exchange of information..FE.PPIt is important that software operating in a distributedenvironment not have any site-dependent information compiled into it.This would require a separate copy of the server at each host andmake maintenance a severe headache.  4.4BSD attempts to isolatehost-specific information from applications by providing systemcalls which return the necessary information*..FS* An example of such a system call is the \fIgethostname\fP(2)call which returns the host's \*(lqofficial\*(rq name..FEA mechanism exists, in the form of an \fIioctl\fP call,for finding the collectionof networks to which a host is directly connected.Further, a local network broadcasting mechanismhas been implemented at the socket level.Combining these two features allows a processto broadcast on any directly connected localnetwork which supports the notion of broadcastingin a site independent manner.  This allows 4.4BSDto solve the problem of deciding how to propagatestatus information in the case of \fIrwho\fP, ormore generally in broadcasting:Such status information is broadcast to connectednetworks at the socket level, where the connected networkshave been obtained via the appropriate \fIioctl\fPcalls.The specifics ofsuch broadcastings are complex, however, and willbe covered in section 5.

⌨️ 快捷键说明

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