Annotation of loncom/LondConnection.pm, revision 1.17
1.2 albertel 1: # This module defines and implements a class that represents
2: # a connection to a lond daemon.
3: #
1.17 ! foxr 4: # $Id: LondConnection.pm,v 1.16 2003/11/04 11:11:08 foxr Exp $
1.2 albertel 5: #
6: # Copyright Michigan State University Board of Trustees
7: #
8: # This file is part of the LearningOnline Network with CAPA (LON-CAPA).
9: #
10: # LON-CAPA is free software; you can redistribute it and/or modify
11: # it under the terms of the GNU General Public License as published by
12: # the Free Software Foundation; either version 2 of the License, or
13: # (at your option) any later version.
14: #
15: # LON-CAPA is distributed in the hope that it will be useful,
16: # but WITHOUT ANY WARRANTY; without even the implied warranty of
17: # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18: # GNU General Public License for more details.
19: #
20: # You should have received a copy of the GNU General Public License
21: # along with LON-CAPA; if not, write to the Free Software
22: # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23: #
24: # /home/httpd/html/adm/gpl.txt
25: #
26: # http://www.lon-capa.org/
1.1 foxr 27: #
1.14 foxr 28:
1.1 foxr 29: package LondConnection;
30:
1.10 foxr 31: use strict;
1.1 foxr 32: use IO::Socket;
33: use IO::Socket::INET;
34: use IO::Handle;
35: use IO::File;
36: use Fcntl;
37: use POSIX;
38: use Crypt::IDEA;
1.14 foxr 39:
1.1 foxr 40:
1.12 foxr 41:
42:
43:
1.6 foxr 44: my $DebugLevel=0;
1.12 foxr 45: my %hostshash;
46: my %perlvar;
1.1 foxr 47:
1.14 foxr 48: #
1.16 foxr 49: # Set debugging level
50: #
51: sub SetDebug {
52: $DebugLevel = shift;
53: }
54:
55: #
1.14 foxr 56: # The config read is done in this way to support the read of
57: # the non-default configuration file in the
58: # event we are being used outside of loncapa.
59: #
60:
61: my $ConfigRead = 0;
62:
1.1 foxr 63: # Read the configuration file for apache to get the perl
64: # variable set.
65:
1.12 foxr 66: sub ReadConfig {
1.14 foxr 67: my $perlvarref = read_conf('loncapa.conf');
1.12 foxr 68: %perlvar = %{$perlvarref};
1.14 foxr 69: my $hoststab = read_hosts(
70: "$perlvar{'lonTabDir'}/hosts.tab") ||
71: die "Can't read host table!!";
1.12 foxr 72: %hostshash = %{$hoststab};
1.17 ! foxr 73: $ConfigRead = 1;
1.12 foxr 74:
75: }
76:
1.15 foxr 77: #
78: # Read a foreign configuration.
79: # This sub is intended for the cases where the package
80: # will be read from outside the LonCAPA environment, in that case
81: # the client will need to explicitly provide:
82: # - A file in hosts.tab format.
83: # - Some idea of the 'lonCAPA' name of the local host (for building
84: # the encryption key).
85: #
86: # Parameters:
87: # MyHost - Name of this host as far as LonCAPA is concerned.
88: # Filename - Name of a hosts.tab formatted file that will be used
89: # to build up the hosts table.
90: #
91: sub ReadForeignConfig {
92: my $MyHost = shift;
93: my $Filename = shift;
94:
1.17 ! foxr 95: &Debug(4, "ReadForeignConfig $MyHost $Filename\n");
! 96:
1.15 foxr 97: $perlvar{lonHostID} = $MyHost; # Rmember my host.
98: my $hosttab = read_hosts($Filename) ||
99: die "Can't read hosts table!!";
1.17 ! foxr 100: %hostshash = %{$hosttab};
! 101: if($DebugLevel > 3) {
! 102: foreach my $host (keys %hostshash) {
! 103: print "host $host => $hostshash{$host}\n";
! 104: }
! 105: }
! 106: $ConfigRead = 1;
1.1 foxr 107:
1.15 foxr 108: }
1.1 foxr 109:
110: sub Debug {
111: my $level = shift;
112: my $message = shift;
113: if ($level < $DebugLevel) {
114: print($message."\n");
115: }
116: }
1.3 albertel 117:
118: =pod
119:
120: =head2 Dump
121:
1.12 foxr 122: Dump the internal state of the object: For debugging purposes, to stderr.
1.3 albertel 123:
1.1 foxr 124: =cut
125:
126: sub Dump {
127: my $self = shift;
1.10 foxr 128: my $key;
129: my $value;
1.1 foxr 130: print "Dumping LondConnectionObject:\n";
131: while(($key, $value) = each %$self) {
1.12 foxr 132: print STDERR "$key -> $value\n";
1.1 foxr 133: }
134: print "-------------------------------\n";
135: }
136:
137: =pod
1.3 albertel 138:
139: Local function to do a state transition. If the state transition
140: callback is defined it is called with two parameters: the self and the
141: old state.
142:
1.1 foxr 143: =cut
1.3 albertel 144:
1.1 foxr 145: sub Transition {
146: my $self = shift;
147: my $newstate = shift;
148: my $oldstate = $self->{State};
149: $self->{State} = $newstate;
150: $self->{TimeoutRemaining} = $self->{TimeoutValue};
151: if($self->{TransitionCallback}) {
152: ($self->{TransitionCallback})->($self, $oldstate);
153: }
154: }
155:
1.3 albertel 156:
1.14 foxr 157:
1.1 foxr 158: =pod
1.3 albertel 159:
160: =head2 new
161:
162: Construct a new lond connection.
163:
164: Parameters (besides the class name) include:
165:
166: =item hostname
167:
168: host the remote lond is on. This host is a host in the hosts.tab file
169:
170: =item port
171:
172: port number the remote lond is listening on.
173:
1.1 foxr 174: =cut
1.3 albertel 175:
1.1 foxr 176: sub new {
177: my $class = shift; # class name.
178: my $Hostname = shift; # Name of host to connect to.
179: my $Port = shift; # Port to connect
1.14 foxr 180:
181: if (!$ConfigRead) {
182: ReadConfig();
183: $ConfigRead = 1;
184: }
1.1 foxr 185: &Debug(4,$class."::new( ".$Hostname.",".$Port.")\n");
186:
187: # The host must map to an entry in the hosts table:
188: # We connect to the dns host that corresponds to that
189: # system and use the hostname for the encryption key
190: # negotion. In the objec these become the Host and
191: # LoncapaHim fields of the object respectively.
192: #
193: if (!exists $hostshash{$Hostname}) {
1.16 foxr 194: &Debug(8, "No Such host $Hostname");
1.1 foxr 195: return undef; # No such host!!!
196: }
197: my @ConfigLine = @{$hostshash{$Hostname}};
198: my $DnsName = $ConfigLine[3]; # 4'th item is dns of host.
199: Debug(5, "Connecting to ".$DnsName);
200: # Now create the object...
201: my $self = { Host => $DnsName,
202: LoncapaHim => $Hostname,
203: Port => $Port,
204: State => "Initialized",
205: TransactionRequest => "",
206: TransactionReply => "",
207: InformReadable => 0,
208: InformWritable => 0,
209: TimeoutCallback => undef,
210: TransitionCallback => undef,
211: Timeoutable => 0,
1.9 foxr 212: TimeoutValue => 30,
213: TimeoutRemaining => 0,
1.1 foxr 214: CipherKey => "",
215: Cipher => undef};
216: bless($self, $class);
217: unless ($self->{Socket} = IO::Socket::INET->new(PeerHost => $self->{Host},
218: PeerPort => $self->{Port},
219: Type => SOCK_STREAM,
1.8 foxr 220: Proto => "tcp",
1.13 foxr 221: Timeout => 3)) {
1.1 foxr 222: return undef; # Inidicates the socket could not be made.
223: }
224: #
225: # We're connected. Set the state, and the events we'll accept:
226: #
227: $self->Transition("Connected");
228: $self->{InformWritable} = 1; # When socket is writable we send init
1.9 foxr 229: $self->{Timeoutable} = 1; # Timeout allowed during startup negotiation.
1.1 foxr 230: $self->{TransactionRequest} = "init\n";
231:
232: #
233: # Set socket to nonblocking I/O.
234: #
235: my $socket = $self->{Socket};
1.10 foxr 236: my $flags = fcntl($socket->fileno, F_GETFL,0);
1.1 foxr 237: if($flags == -1) {
238: $socket->close;
239: return undef;
240: }
241: if(fcntl($socket, F_SETFL, $flags | O_NONBLOCK) == -1) {
242: $socket->close;
243: return undef;
244: }
245:
246: # return the object :
247:
248: return $self;
249: }
1.3 albertel 250:
1.1 foxr 251: =pod
1.3 albertel 252:
253: =head2 Readable
254:
255: This member should be called when the Socket becomes readable. Until
256: the read completes, action is state independet. Data are accepted into
257: the TransactionReply until a newline character is received. At that
258: time actionis state dependent:
259:
260: =item Connected
261:
262: in this case we received challenge, the state changes to
263: ChallengeReceived, and we initiate a send with the challenge response.
264:
265: =item ReceivingReply
266:
267: In this case a reply has been received for a transaction, the state
268: goes to Idle and we disable write and read notification.
269:
270: =item ChallengeReeived
271:
272: we just got what should be an ok\n and the connection can now handle
273: transactions.
1.1 foxr 274:
275: =cut
1.3 albertel 276:
1.1 foxr 277: sub Readable {
278: my $self = shift;
279: my $socket = $self->{Socket};
280: my $data = '';
281: my $rv = $socket->recv($data, POSIX::BUFSIZ, 0);
282: my $errno = $! + 0; # Force numeric context.
283:
1.8 foxr 284: unless (defined($rv) && length $data) {# Read failed,
1.1 foxr 285: if(($errno == POSIX::EWOULDBLOCK) ||
286: ($errno == POSIX::EAGAIN) ||
1.8 foxr 287: ($errno == POSIX::EINTR)) {
1.1 foxr 288: return 0;
289: }
290:
291: # Connection likely lost.
292: &Debug(4, "Connection lost");
293: $self->{TransactionRequest} = '';
294: $socket->close();
295: $self->Transition("Disconnected");
296: return -1;
297: }
298: # Append the data to the buffer. And figure out if the read is done:
299:
300: &Debug(9,"Received from host: ".$data);
301: $self->{TransactionReply} .= $data;
302: if($self->{TransactionReply} =~ /(.*\n)/) {
303: &Debug(8,"Readable End of line detected");
304: if ($self->{State} eq "Initialized") { # We received the challenge:
1.10 foxr 305: if($self->{TransactionReply} eq "refused\n") { # Remote doesn't have
1.1 foxr 306:
307: $self->Transition("Disconnected"); # in host tables.
308: $socket->close();
309: return -1;
310: }
311:
312: &Debug(8," Transition out of Initialized");
313: $self->{TransactionRequest} = $self->{TransactionReply};
314: $self->{InformWritable} = 1;
315: $self->{InformReadable} = 0;
316: $self->Transition("ChallengeReceived");
317: $self->{TimeoutRemaining} = $self->{TimeoutValue};
318: return 0;
319: } elsif ($self->{State} eq "ChallengeReplied") { # should be ok.
320: if($self->{TransactionReply} != "ok\n") {
321: $self->Transition("Disconnected");
322: $socket->close();
323: return -1;
324: }
325: $self->Transition("RequestingKey");
326: $self->{InformReadable} = 0;
327: $self->{InformWritable} = 1;
328: $self->{TransactionRequest} = "ekey\n";
329: return 0;
330: } elsif ($self->{State} eq "ReceivingKey") {
331: my $buildkey = $self->{TransactionReply};
332: my $key = $self->{LoncapaHim}.$perlvar{'lonHostID'};
333: $key=~tr/a-z/A-Z/;
334: $key=~tr/G-P/0-9/;
335: $key=~tr/Q-Z/0-9/;
336: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
337: $key=substr($key,0,32);
338: my $cipherkey=pack("H32",$key);
339: $self->{Cipher} = new IDEA $cipherkey;
1.13 foxr 340: if($self->{Cipher} eq undef) {
1.1 foxr 341: $self->Transition("Disconnected");
342: $socket->close();
343: return -1;
344: } else {
345: $self->Transition("Idle");
346: $self->{InformWritable} = 0;
347: $self->{InformReadable} = 0;
348: $self->{Timeoutable} = 0;
349: return 0;
350: }
351: } elsif ($self->{State} eq "ReceivingReply") {
352:
353: # If the data are encrypted, decrypt first.
354:
355: my $answer = $self->{TransactionReply};
356: if($answer =~ /^enc\:/) {
357: $answer = $self->Decrypt($answer);
358: $self->{TransactionReply} = $answer;
359: }
360:
361: # finish the transaction
362:
363: $self->{InformWritable} = 0;
364: $self->{InformReadable} = 0;
365: $self->{Timeoutable} = 0;
366: $self->Transition("Idle");
367: return 0;
368: } elsif ($self->{State} eq "Disconnected") { # No connection.
369: return -1;
370: } else { # Internal error: Invalid state.
371: $self->Transition("Disconnected");
372: $socket->close();
373: return -1;
374: }
375: }
376:
377: return 0;
378:
379: }
380:
381:
382: =pod
1.3 albertel 383:
384: This member should be called when the Socket becomes writable.
385:
386: The action is state independent. An attempt is made to drain the
387: contents of the TransactionRequest member. Once this is drained, we
388: mark the object as waiting for readability.
1.1 foxr 389:
390: Returns 0 if successful, or -1 if not.
1.3 albertel 391:
1.1 foxr 392: =cut
393: sub Writable {
394: my $self = shift; # Get reference to the object.
395: my $socket = $self->{Socket};
396: my $nwritten = $socket->send($self->{TransactionRequest}, 0);
397: my $errno = $! + 0;
398: unless (defined $nwritten) {
399: if($errno != POSIX::EINTR) {
400: $self->Transition("Disconnected");
401: return -1;
402: }
403:
404: }
1.10 foxr 405: if (($nwritten >= 0) ||
1.1 foxr 406: ($errno == POSIX::EWOULDBLOCK) ||
407: ($errno == POSIX::EAGAIN) ||
408: ($errno == POSIX::EINTR) ||
409: ($errno == 0)) {
410: substr($self->{TransactionRequest}, 0, $nwritten) = ""; # rmv written part
411: if(length $self->{TransactionRequest} == 0) {
412: $self->{InformWritable} = 0;
413: $self->{InformReadable} = 1;
414: $self->{TransactionReply} = '';
415: #
416: # Figure out the next state:
417: #
418: if($self->{State} eq "Connected") {
419: $self->Transition("Initialized");
420: } elsif($self->{State} eq "ChallengeReceived") {
421: $self->Transition("ChallengeReplied");
422: } elsif($self->{State} eq "RequestingKey") {
423: $self->Transition("ReceivingKey");
424: $self->{InformWritable} = 0;
425: $self->{InformReadable} = 1;
426: $self->{TransactionReply} = '';
427: } elsif ($self->{State} eq "SendingRequest") {
428: $self->Transition("ReceivingReply");
429: $self->{TimeoutRemaining} = $self->{TimeoutValue};
430: } elsif ($self->{State} eq "Disconnected") {
431: return -1;
432: }
433: return 0;
434: }
435: } else { # The write failed (e.g. partner disconnected).
436: $self->Transition("Disconnected");
437: $socket->close();
438: return -1;
439: }
440:
441: }
442: =pod
1.3 albertel 443:
444: =head2 Tick
445:
1.1 foxr 446: Tick is called every time unit by the event framework. It
1.3 albertel 447:
448: =item 1 decrements the remaining timeout.
449:
450: =item 2 If the timeout is zero, calls TimedOut indicating that the current operation timed out.
1.1 foxr 451:
452: =cut
453:
454: sub Tick {
455: my $self = shift;
456: $self->{TimeoutRemaining}--;
457: if ($self->{TimeoutRemaining} < 0) {
458: $self->TimedOut();
459: }
460: }
1.3 albertel 461:
1.1 foxr 462: =pod
463:
1.3 albertel 464: =head2 TimedOut
465:
466: called on a timeout. If the timeout callback is defined, it is called
467: with $self as its parameters.
468:
469: =cut
470:
1.1 foxr 471: sub TimedOut {
472:
473: my $self = shift;
474: if($self->{TimeoutCallback}) {
475: my $callback = $self->{TimeoutCallback};
476: my @args = ( $self);
477: &$callback(@args);
478: }
479: }
1.3 albertel 480:
1.1 foxr 481: =pod
1.3 albertel 482:
483: =head2 InitiateTransaction
484:
485: Called to initiate a transaction. A transaction can only be initiated
486: when the object is idle... otherwise an error is returned. A
487: transaction consists of a request to the server that will have a
488: reply. This member sets the request data in the TransactionRequest
489: member, makes the state SendingRequest and sets the data to allow a
490: timout, and to request writability notification.
491:
1.1 foxr 492: =cut
1.3 albertel 493:
1.1 foxr 494: sub InitiateTransaction {
495: my $self = shift;
496: my $data = shift;
497:
1.4 foxr 498: Debug(1, "initiating transaction: ".$data);
1.1 foxr 499: if($self->{State} ne "Idle") {
1.4 foxr 500: Debug(0," .. but not idle here\n");
1.1 foxr 501: return -1; # Error indicator.
502: }
503: # if the transaction is to be encrypted encrypt the data:
504:
505: if($data =~ /^encrypt\:/) {
506: $data = $self->Encrypt($data);
507: }
508:
509: # Setup the trasaction
510:
511: $self->{TransactionRequest} = $data;
512: $self->{TransactionReply} = "";
513: $self->{InformWritable} = 1;
514: $self->{InformReadable} = 0;
515: $self->{Timeoutable} = 1;
516: $self->{TimeoutRemaining} = $self->{TimeoutValue};
517: $self->Transition("SendingRequest");
518: }
519:
520:
521: =pod
1.3 albertel 522:
523: =head2 SetStateTransitionCallback
524:
525: Sets a callback for state transitions. Returns a reference to any
526: prior established callback, or undef if there was none:
527:
1.1 foxr 528: =cut
1.3 albertel 529:
1.1 foxr 530: sub SetStateTransitionCallback {
531: my $self = shift;
532: my $oldCallback = $self->{TransitionCallback};
533: $self->{TransitionCallback} = shift;
534: return $oldCallback;
535: }
1.3 albertel 536:
1.1 foxr 537: =pod
1.3 albertel 538:
539: =head2 SetTimeoutCallback
540:
541: Sets the timeout callback. Returns a reference to any prior
542: established callback or undef if there was none.
543:
1.1 foxr 544: =cut
1.3 albertel 545:
1.1 foxr 546: sub SetTimeoutCallback {
547: my $self = shift;
548: my $callback = shift;
549: my $oldCallback = $self->{TimeoutCallback};
550: $self->{TimeoutCallback} = $callback;
551: return $oldCallback;
552: }
553:
554: =pod
1.3 albertel 555:
1.5 foxr 556: =head2 Shutdown:
557:
558: Shuts down the socket.
559:
560: =cut
561:
562: sub Shutdown {
563: my $self = shift;
564: my $socket = $self->GetSocket();
565: $socket->shutdown(2);
566: }
567:
568: =pod
569:
1.3 albertel 570: =head2 GetState
571:
572: selector for the object state.
573:
1.1 foxr 574: =cut
1.3 albertel 575:
1.1 foxr 576: sub GetState {
577: my $self = shift;
578: return $self->{State};
579: }
1.3 albertel 580:
1.1 foxr 581: =pod
1.3 albertel 582:
583: =head2 GetSocket
584:
585: selector for the object socket.
586:
1.1 foxr 587: =cut
1.3 albertel 588:
1.1 foxr 589: sub GetSocket {
590: my $self = shift;
591: return $self->{Socket};
592: }
1.3 albertel 593:
1.5 foxr 594:
1.1 foxr 595: =pod
1.3 albertel 596:
597: =head2 WantReadable
598:
599: Return the state of the flag that indicates the object wants to be
600: called when readable.
601:
1.1 foxr 602: =cut
1.3 albertel 603:
1.1 foxr 604: sub WantReadable {
605: my $self = shift;
606:
607: return $self->{InformReadable};
608: }
1.3 albertel 609:
1.1 foxr 610: =pod
1.3 albertel 611:
612: =head2 WantWritable
613:
614: Return the state of the flag that indicates the object wants write
615: notification.
616:
1.1 foxr 617: =cut
1.3 albertel 618:
1.1 foxr 619: sub WantWritable {
620: my $self = shift;
621: return $self->{InformWritable};
622: }
1.3 albertel 623:
1.1 foxr 624: =pod
1.3 albertel 625:
626: =head2 WantTimeout
627:
628: return the state of the flag that indicates the object wants to be
629: informed of timeouts.
630:
1.1 foxr 631: =cut
1.3 albertel 632:
1.1 foxr 633: sub WantTimeout {
634: my $self = shift;
635: return $self->{Timeoutable};
636: }
637:
638: =pod
1.3 albertel 639:
640: =head2 GetReply
641:
642: Returns the reply from the last transaction.
643:
1.1 foxr 644: =cut
1.3 albertel 645:
1.1 foxr 646: sub GetReply {
647: my $self = shift;
648: return $self->{TransactionReply};
649: }
650:
651: =pod
1.3 albertel 652:
653: =head2 Encrypt
654:
655: Returns the encrypted version of the command string.
656:
657: The command input string is of the form:
658:
1.1 foxr 659: encrypt:command
1.3 albertel 660:
661: The output string can be directly sent to lond as it is of the form:
662:
1.1 foxr 663: enc:length:<encodedrequest>
1.3 albertel 664:
1.1 foxr 665: =cut
1.3 albertel 666:
1.1 foxr 667: sub Encrypt {
668: my $self = shift; # Reference to the object.
669: my $request = shift; # Text to send.
670:
671:
672: # Split the encrypt: off the request and figure out it's length.
673: # the cipher works in blocks of 8 bytes.
674:
675: my $cmd = $request;
676: $cmd =~ s/^encrypt\://; # strip off encrypt:
677: chomp($cmd); # strip off trailing \n
678: my $length=length($cmd); # Get the string length.
679: $cmd .= " "; # Pad with blanks so we can fill out a block.
680:
681: # encrypt the request in 8 byte chunks to create the encrypted
682: # output request.
683:
684: my $Encoded = '';
685: for(my $index = 0; $index <= $length; $index += 8) {
686: $Encoded .=
687: unpack("H16",
688: $self->{Cipher}->encrypt(substr($cmd,
689: $index, 8)));
690: }
691:
692: # Build up the answer as enc:length:$encrequest.
693:
694: $request = "enc:$length:$Encoded\n";
695: return $request;
696:
697:
698: }
1.3 albertel 699:
700: =pod
701:
702: =head2 Decrypt
703:
704: Decrypt a response from the server. The response is in the form:
705:
706: enc:<length>:<encrypted data>
707:
1.1 foxr 708: =cut
1.3 albertel 709:
1.1 foxr 710: sub Decrypt {
711: my $self = shift; # Recover reference to object
712: my $encrypted = shift; # This is the encrypted data.
713:
714: # Bust up the response into length, and encryptedstring:
715:
716: my ($enc, $length, $EncryptedString) = split(/:/,$encrypted);
717: chomp($EncryptedString);
718:
719: # Decode the data in 8 byte blocks. The string is encoded
720: # as hex digits so there are two characters per byte:
721:
1.10 foxr 722: my $decrypted = "";
1.1 foxr 723: for(my $index = 0; $index < length($EncryptedString);
724: $index += 16) {
725: $decrypted .= $self->{Cipher}->decrypt(
726: pack("H16",
727: substr($EncryptedString,
728: $index,
729: 16)));
730: }
731: # the answer may have trailing pads to fill out a block.
732: # $length tells us the actual length of the decrypted string:
733:
734: $decrypted = substr($decrypted, 0, $length);
735:
736: return $decrypted;
737:
738: }
739:
740: =pod
1.3 albertel 741:
742: =head2 GetHostIterator
1.1 foxr 743:
744: Returns a hash iterator to the host information. Each get from
745: this iterator returns a reference to an array that contains
746: information read from the hosts configuration file. Array elements
747: are used as follows:
748:
1.3 albertel 749: [0] - LonCapa host name.
750: [1] - LonCapa domain name.
751: [2] - Loncapa role (e.g. library or access).
752: [3] - DNS name server hostname.
1.11 foxr 753: [4] - IP address (result of e.g. nslookup [3]).
1.3 albertel 754: [5] - Maximum connection count.
755: [6] - Idle timeout for reducing connection count.
756: [7] - Minimum connection count.
1.1 foxr 757:
1.3 albertel 758: =cut
1.1 foxr 759:
760: sub GetHostIterator {
761:
762: return HashIterator->new(\%hostshash);
763: }
1.14 foxr 764:
765: ###########################################################
766: #
767: # The following is an unashamed kludge that is here to
768: # allow LondConnection to be used outside of the
769: # loncapa environment (e.g. by lonManage).
770: #
771: # This is a textual inclusion of pieces of the
772: # Configuration.pm module.
773: #
774:
775:
776: my $confdir='/etc/httpd/conf/';
777:
778: # ------------------- Subroutine read_conf: read LON-CAPA server configuration.
779: # This subroutine reads PerlSetVar values out of specified web server
780: # configuration files.
781: sub read_conf
782: {
783: my (@conf_files)=@_;
784: my %perlvar;
785: foreach my $filename (@conf_files,'loncapa_apache.conf')
786: {
787: open(CONFIG,'<'.$confdir.$filename) or
788: die("Can't read $confdir$filename");
789: while (my $configline=<CONFIG>)
790: {
791: if ($configline =~ /^[^\#]*PerlSetVar/)
792: {
793: my ($unused,$varname,$varvalue)=split(/\s+/,$configline);
794: chomp($varvalue);
795: $perlvar{$varname}=$varvalue;
796: }
797: }
798: close(CONFIG);
799: }
800: my $perlvarref=\%perlvar;
801: return ($perlvarref);
802: }
803:
804: #---------------------- Subroutine read_hosts: Read a LON-CAPA hosts.tab
805: # formatted configuration file.
806: #
807: my $RequiredCount = 5; # Required item count in hosts.tab.
808: my $DefaultMaxCon = 5; # Default value for maximum connections.
809: my $DefaultIdle = 1000; # Default connection idle time in seconds.
810: my $DefaultMinCon = 0; # Default value for minimum connections.
811:
812: sub read_hosts {
813: my $Filename = shift;
814: my %HostsTab;
815:
1.16 foxr 816: open(CONFIG,'<'.$Filename) or die("Can't read $Filename");
1.14 foxr 817: while (my $line = <CONFIG>) {
818: if (!($line =~ /^\s*\#/)) {
819: my @items = split(/:/, $line);
820: if(scalar @items >= $RequiredCount) {
821: if (scalar @items == $RequiredCount) { # Only required items:
822: $items[$RequiredCount] = $DefaultMaxCon;
823: }
824: if(scalar @items == $RequiredCount + 1) { # up through maxcon.
825: $items[$RequiredCount+1] = $DefaultIdle;
826: }
827: if(scalar @items == $RequiredCount + 2) { # up through idle.
828: $items[$RequiredCount+2] = $DefaultMinCon;
829: }
830: {
831: my @list = @items; # probably not needed but I'm unsure of
832: # about the scope of item so...
833: $HostsTab{$list[0]} = \@list;
834: }
835: }
836: }
837: }
838: close(CONFIG);
839: my $hostref = \%HostsTab;
840: return ($hostref);
841: }
842:
1.1 foxr 843:
844: 1;
845:
846: =pod
1.3 albertel 847:
1.1 foxr 848: =head1 Theory
849:
1.3 albertel 850: The lond object is a state machine. It lives through the following states:
851:
852: =item Connected:
853:
854: a TCP connection has been formed, but the passkey has not yet been
855: negotiated.
856:
857: =item Initialized:
858:
859: "init" sent.
860:
861: =item ChallengeReceived:
862:
863: lond sent its challenge to us.
864:
865: =item ChallengeReplied:
866:
867: We replied to lond's challenge waiting for lond's ok.
868:
869: =item RequestingKey:
870:
871: We are requesting an encryption key.
872:
873: =item ReceivingKey:
874:
875: We are receiving an encryption key.
876:
877: =item Idle:
878:
879: Connection was negotiated but no requests are active.
880:
881: =item SendingRequest:
882:
883: A request is being sent to the peer.
884:
885: =item ReceivingReply:
886:
887: Waiting for an entire reply from the peer.
888:
889: =item Disconnected:
890:
891: For whatever reason, the connection was dropped.
892:
893: When we need to be writing data, we have a writable event. When we
894: need to be reading data, a readable event established. Events
895: dispatch through the class functions Readable and Writable, and the
896: watcher contains a reference to the associated object to allow object
897: context to be reached.
1.1 foxr 898:
899: =head2 Member data.
900:
1.3 albertel 901: =item Host
902:
903: Host socket is connected to.
904:
905: =item Port
906:
907: The port the remote lond is listening on.
908:
909: =item Socket
910:
911: Socket open on the connection.
912:
913: =item State
914:
915: The current state.
916:
917: =item TransactionRequest
918:
919: The request being transmitted.
920:
921: =item TransactionReply
922:
923: The reply being received from the transaction.
924:
925: =item InformReadable
926:
927: True if we want to be called when socket is readable.
928:
929: =item InformWritable
930:
931: True if we want to be informed if the socket is writable.
932:
933: =item Timeoutable
934:
935: True if the current operation is allowed to timeout.
936:
937: =item TimeoutValue
938:
939: Number of seconds in the timeout.
940:
941: =item TimeoutRemaining
942:
943: Number of seconds left in the timeout.
944:
945: =item CipherKey
946:
947: The key that was negotiated with the peer.
948:
949: =item Cipher
950:
951: The cipher obtained via the key.
1.1 foxr 952:
953:
954: =head2 The following are callback like members:
1.3 albertel 955:
956: =item Tick:
957:
958: Called in response to a timer tick. Used to managed timeouts etc.
959:
960: =item Readable:
961:
962: Called when the socket becomes readable.
963:
964: =item Writable:
965:
966: Called when the socket becomes writable.
967:
968: =item TimedOut:
969:
970: Called when a timed operation timed out.
971:
1.1 foxr 972:
973: =head2 The following are operational member functions.
1.3 albertel 974:
975: =item InitiateTransaction:
976:
977: Called to initiate a new transaction
978:
979: =item SetStateTransitionCallback:
980:
981: Called to establish a function that is called whenever the object goes
982: through a state transition. This is used by The client to manage the
983: work flow for the object.
984:
985: =item SetTimeoutCallback:
986:
987: Set a function to be called when a transaction times out. The
988: function will be called with the object as its sole parameter.
989:
990: =item Encrypt:
991:
992: Encrypts a block of text according to the cipher negotiated with the
993: peer (assumes the text is a command).
994:
995: =item Decrypt:
996:
997: Decrypts a block of text according to the cipher negotiated with the
998: peer (assumes the block was a reply.
1.5 foxr 999:
1000: =item Shutdown:
1001:
1002: Shuts off the socket.
1.1 foxr 1003:
1004: =head2 The following are selector member functions:
1005:
1.3 albertel 1006: =item GetState:
1007:
1008: Returns the current state
1009:
1010: =item GetSocket:
1011:
1012: Gets the socekt open on the connection to lond.
1013:
1014: =item WantReadable:
1015:
1016: true if the current state requires a readable event.
1017:
1018: =item WantWritable:
1019:
1020: true if the current state requires a writable event.
1021:
1022: =item WantTimeout:
1023:
1024: true if the current state requires timeout support.
1025:
1026: =item GetHostIterator:
1027:
1028: Returns an iterator into the host file hash.
1029:
1.1 foxr 1030: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>