Annotation of loncom/LondConnection.pm, revision 1.24
1.2 albertel 1: # This module defines and implements a class that represents
2: # a connection to a lond daemon.
3: #
1.24 ! foxr 4: # $Id: LondConnection.pm,v 1.23 2004/01/06 09:35:22 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(
1.21 foxr 70: "$perlvar{lonTabDir}/hosts.tab") ||
1.14 foxr 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.22 foxr 130: print STDERR "Dumping LondConnectionObject:\n";
1.1 foxr 131: while(($key, $value) = each %$self) {
1.22 foxr 132: print STDERR "$key -> $value\n";
1.1 foxr 133: }
1.23 foxr 134: print STDERR "-------------------------------\n";
1.1 foxr 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,
1.24 ! foxr 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,
! 212: TimeoutValue => 30,
! 213: TimeoutRemaining => 0,
! 214: CipherKey => "",
! 215: LondVersion => "Unknown",
! 216: Cipher => undef};
1.1 foxr 217: bless($self, $class);
218: unless ($self->{Socket} = IO::Socket::INET->new(PeerHost => $self->{Host},
219: PeerPort => $self->{Port},
220: Type => SOCK_STREAM,
1.8 foxr 221: Proto => "tcp",
1.13 foxr 222: Timeout => 3)) {
1.1 foxr 223: return undef; # Inidicates the socket could not be made.
224: }
225: #
226: # We're connected. Set the state, and the events we'll accept:
227: #
228: $self->Transition("Connected");
229: $self->{InformWritable} = 1; # When socket is writable we send init
1.9 foxr 230: $self->{Timeoutable} = 1; # Timeout allowed during startup negotiation.
1.1 foxr 231: $self->{TransactionRequest} = "init\n";
232:
233: #
234: # Set socket to nonblocking I/O.
235: #
236: my $socket = $self->{Socket};
1.10 foxr 237: my $flags = fcntl($socket->fileno, F_GETFL,0);
1.1 foxr 238: if($flags == -1) {
239: $socket->close;
240: return undef;
241: }
242: if(fcntl($socket, F_SETFL, $flags | O_NONBLOCK) == -1) {
243: $socket->close;
244: return undef;
245: }
246:
247: # return the object :
248:
249: return $self;
250: }
1.3 albertel 251:
1.1 foxr 252: =pod
1.3 albertel 253:
254: =head2 Readable
255:
256: This member should be called when the Socket becomes readable. Until
257: the read completes, action is state independet. Data are accepted into
258: the TransactionReply until a newline character is received. At that
259: time actionis state dependent:
260:
261: =item Connected
262:
263: in this case we received challenge, the state changes to
264: ChallengeReceived, and we initiate a send with the challenge response.
265:
266: =item ReceivingReply
267:
268: In this case a reply has been received for a transaction, the state
269: goes to Idle and we disable write and read notification.
270:
271: =item ChallengeReeived
272:
273: we just got what should be an ok\n and the connection can now handle
274: transactions.
1.1 foxr 275:
276: =cut
1.3 albertel 277:
1.1 foxr 278: sub Readable {
279: my $self = shift;
280: my $socket = $self->{Socket};
281: my $data = '';
282: my $rv = $socket->recv($data, POSIX::BUFSIZ, 0);
283: my $errno = $! + 0; # Force numeric context.
284:
1.8 foxr 285: unless (defined($rv) && length $data) {# Read failed,
1.1 foxr 286: if(($errno == POSIX::EWOULDBLOCK) ||
287: ($errno == POSIX::EAGAIN) ||
1.8 foxr 288: ($errno == POSIX::EINTR)) {
1.1 foxr 289: return 0;
290: }
291:
292: # Connection likely lost.
293: &Debug(4, "Connection lost");
294: $self->{TransactionRequest} = '';
295: $socket->close();
296: $self->Transition("Disconnected");
297: return -1;
298: }
299: # Append the data to the buffer. And figure out if the read is done:
300:
301: &Debug(9,"Received from host: ".$data);
302: $self->{TransactionReply} .= $data;
303: if($self->{TransactionReply} =~ /(.*\n)/) {
304: &Debug(8,"Readable End of line detected");
305: if ($self->{State} eq "Initialized") { # We received the challenge:
1.10 foxr 306: if($self->{TransactionReply} eq "refused\n") { # Remote doesn't have
1.1 foxr 307:
308: $self->Transition("Disconnected"); # in host tables.
309: $socket->close();
310: return -1;
311: }
312:
313: &Debug(8," Transition out of Initialized");
314: $self->{TransactionRequest} = $self->{TransactionReply};
315: $self->{InformWritable} = 1;
316: $self->{InformReadable} = 0;
317: $self->Transition("ChallengeReceived");
318: $self->{TimeoutRemaining} = $self->{TimeoutValue};
319: return 0;
1.24 ! foxr 320: } elsif ($self->{State} eq "ChallengeReplied") {
! 321: if($self->{TransactionReply} ne "ok\n") {
! 322: $self->Transition("Disconnected");
! 323: $socket->close();
! 324: return -1;
! 325: }
! 326: $self->Transition("RequestingVersion");
! 327: $self->{InformReadable} = 0;
! 328: $self->{InformWritable} = 1;
! 329: $self->{TransactionRequest} = "version\n";
! 330: return 0;
! 331: } elsif ($self->{State} eq "ReadingVersionString") {
! 332: $self->{LondVersion} = chomp($self->{TransactionReply});
! 333: $self->Transition("SetHost");
! 334: $self->{InformReadable} = 0;
! 335: $self->{InformWritable} = 1;
! 336: my $peer = $self->{LoncapaHim};
! 337: $self->{TransactionRequest}= "sethost:$peer\n";
! 338: return 0;
! 339: } elsif ($self->{State} eq "HostSet") { # should be ok.
! 340: if($self->{TransactionReply} ne "ok\n") {
! 341: $self->Transition("Disconnected");
! 342: $socket->close();
! 343: return -1;
! 344: }
! 345: $self->Transition("RequestingKey");
! 346: $self->{InformReadable} = 0;
! 347: $self->{InformWritable} = 1;
! 348: $self->{TransactionRequest} = "ekey\n";
! 349: return 0;
1.1 foxr 350: } elsif ($self->{State} eq "ReceivingKey") {
351: my $buildkey = $self->{TransactionReply};
352: my $key = $self->{LoncapaHim}.$perlvar{'lonHostID'};
353: $key=~tr/a-z/A-Z/;
354: $key=~tr/G-P/0-9/;
355: $key=~tr/Q-Z/0-9/;
356: $key=$key.$buildkey.$key.$buildkey.$key.$buildkey;
357: $key=substr($key,0,32);
358: my $cipherkey=pack("H32",$key);
359: $self->{Cipher} = new IDEA $cipherkey;
1.13 foxr 360: if($self->{Cipher} eq undef) {
1.1 foxr 361: $self->Transition("Disconnected");
362: $socket->close();
363: return -1;
364: } else {
365: $self->Transition("Idle");
366: $self->{InformWritable} = 0;
367: $self->{InformReadable} = 0;
368: $self->{Timeoutable} = 0;
369: return 0;
370: }
371: } elsif ($self->{State} eq "ReceivingReply") {
372:
373: # If the data are encrypted, decrypt first.
374:
375: my $answer = $self->{TransactionReply};
376: if($answer =~ /^enc\:/) {
377: $answer = $self->Decrypt($answer);
378: $self->{TransactionReply} = $answer;
379: }
380:
381: # finish the transaction
382:
383: $self->{InformWritable} = 0;
384: $self->{InformReadable} = 0;
385: $self->{Timeoutable} = 0;
386: $self->Transition("Idle");
387: return 0;
388: } elsif ($self->{State} eq "Disconnected") { # No connection.
389: return -1;
390: } else { # Internal error: Invalid state.
391: $self->Transition("Disconnected");
392: $socket->close();
393: return -1;
394: }
395: }
396:
397: return 0;
398:
399: }
400:
401:
402: =pod
1.3 albertel 403:
404: This member should be called when the Socket becomes writable.
405:
406: The action is state independent. An attempt is made to drain the
407: contents of the TransactionRequest member. Once this is drained, we
408: mark the object as waiting for readability.
1.1 foxr 409:
410: Returns 0 if successful, or -1 if not.
1.3 albertel 411:
1.1 foxr 412: =cut
413: sub Writable {
414: my $self = shift; # Get reference to the object.
415: my $socket = $self->{Socket};
416: my $nwritten = $socket->send($self->{TransactionRequest}, 0);
417: my $errno = $! + 0;
418: unless (defined $nwritten) {
419: if($errno != POSIX::EINTR) {
420: $self->Transition("Disconnected");
421: return -1;
422: }
423:
424: }
1.10 foxr 425: if (($nwritten >= 0) ||
1.1 foxr 426: ($errno == POSIX::EWOULDBLOCK) ||
427: ($errno == POSIX::EAGAIN) ||
428: ($errno == POSIX::EINTR) ||
429: ($errno == 0)) {
430: substr($self->{TransactionRequest}, 0, $nwritten) = ""; # rmv written part
1.24 ! foxr 431: if(length $self->{TransactionRequest} == 0) {
! 432: $self->{InformWritable} = 0;
! 433: $self->{InformReadable} = 1;
! 434: $self->{TransactionReply} = '';
! 435: #
! 436: # Figure out the next state:
! 437: #
! 438: if($self->{State} eq "Connected") {
! 439: $self->Transition("Initialized");
! 440: } elsif($self->{State} eq "ChallengeReceived") {
! 441: $self->Transition("ChallengeReplied");
! 442: } elsif($self->{State} eq "RequestingVersion") {
! 443: $self->Transition("ReadingVersionString");
! 444: } elsif ($self->{State} eq "SetHost") {
! 445: $self->Transition("HostSet");
! 446: } elsif($self->{State} eq "RequestingKey") {
! 447: $self->Transition("ReceivingKey");
! 448: # $self->{InformWritable} = 0;
! 449: # $self->{InformReadable} = 1;
! 450: # $self->{TransactionReply} = '';
! 451: } elsif ($self->{State} eq "SendingRequest") {
! 452: $self->Transition("ReceivingReply");
! 453: $self->{TimeoutRemaining} = $self->{TimeoutValue};
! 454: } elsif ($self->{State} eq "Disconnected") {
! 455: return -1;
! 456: }
! 457: return 0;
! 458: }
! 459: } else { # The write failed (e.g. partner disconnected).
! 460: $self->Transition("Disconnected");
! 461: $socket->close();
! 462: return -1;
! 463: }
1.1 foxr 464:
465: }
466: =pod
1.3 albertel 467:
468: =head2 Tick
469:
1.1 foxr 470: Tick is called every time unit by the event framework. It
1.3 albertel 471:
472: =item 1 decrements the remaining timeout.
473:
474: =item 2 If the timeout is zero, calls TimedOut indicating that the current operation timed out.
1.1 foxr 475:
476: =cut
477:
478: sub Tick {
479: my $self = shift;
480: $self->{TimeoutRemaining}--;
481: if ($self->{TimeoutRemaining} < 0) {
482: $self->TimedOut();
483: }
484: }
1.3 albertel 485:
1.1 foxr 486: =pod
487:
1.3 albertel 488: =head2 TimedOut
489:
490: called on a timeout. If the timeout callback is defined, it is called
491: with $self as its parameters.
492:
493: =cut
494:
1.1 foxr 495: sub TimedOut {
496:
497: my $self = shift;
498: if($self->{TimeoutCallback}) {
499: my $callback = $self->{TimeoutCallback};
500: my @args = ( $self);
501: &$callback(@args);
502: }
503: }
1.3 albertel 504:
1.1 foxr 505: =pod
1.3 albertel 506:
507: =head2 InitiateTransaction
508:
509: Called to initiate a transaction. A transaction can only be initiated
510: when the object is idle... otherwise an error is returned. A
511: transaction consists of a request to the server that will have a
512: reply. This member sets the request data in the TransactionRequest
513: member, makes the state SendingRequest and sets the data to allow a
514: timout, and to request writability notification.
515:
1.1 foxr 516: =cut
1.3 albertel 517:
1.1 foxr 518: sub InitiateTransaction {
519: my $self = shift;
520: my $data = shift;
521:
1.4 foxr 522: Debug(1, "initiating transaction: ".$data);
1.1 foxr 523: if($self->{State} ne "Idle") {
1.4 foxr 524: Debug(0," .. but not idle here\n");
1.1 foxr 525: return -1; # Error indicator.
526: }
527: # if the transaction is to be encrypted encrypt the data:
528:
529: if($data =~ /^encrypt\:/) {
530: $data = $self->Encrypt($data);
531: }
532:
533: # Setup the trasaction
534:
535: $self->{TransactionRequest} = $data;
536: $self->{TransactionReply} = "";
537: $self->{InformWritable} = 1;
538: $self->{InformReadable} = 0;
539: $self->{Timeoutable} = 1;
540: $self->{TimeoutRemaining} = $self->{TimeoutValue};
541: $self->Transition("SendingRequest");
542: }
543:
544:
545: =pod
1.3 albertel 546:
547: =head2 SetStateTransitionCallback
548:
549: Sets a callback for state transitions. Returns a reference to any
550: prior established callback, or undef if there was none:
551:
1.1 foxr 552: =cut
1.3 albertel 553:
1.1 foxr 554: sub SetStateTransitionCallback {
555: my $self = shift;
556: my $oldCallback = $self->{TransitionCallback};
557: $self->{TransitionCallback} = shift;
558: return $oldCallback;
559: }
1.3 albertel 560:
1.1 foxr 561: =pod
1.3 albertel 562:
563: =head2 SetTimeoutCallback
564:
565: Sets the timeout callback. Returns a reference to any prior
566: established callback or undef if there was none.
567:
1.1 foxr 568: =cut
1.3 albertel 569:
1.1 foxr 570: sub SetTimeoutCallback {
571: my $self = shift;
572: my $callback = shift;
573: my $oldCallback = $self->{TimeoutCallback};
574: $self->{TimeoutCallback} = $callback;
575: return $oldCallback;
576: }
577:
578: =pod
1.3 albertel 579:
1.5 foxr 580: =head2 Shutdown:
581:
582: Shuts down the socket.
583:
584: =cut
585:
586: sub Shutdown {
587: my $self = shift;
588: my $socket = $self->GetSocket();
1.20 albertel 589: Debug(5,"socket is -$socket-");
590: if ($socket) {
591: # Ask lond to exit too. Non blocking so
592: # there is no cost for failure.
593: eval {
594: $socket->send("exit\n", 0);
595: $socket->shutdown(2);
596: }
597: }
1.5 foxr 598: }
599:
600: =pod
601:
1.3 albertel 602: =head2 GetState
603:
604: selector for the object state.
605:
1.1 foxr 606: =cut
1.3 albertel 607:
1.1 foxr 608: sub GetState {
609: my $self = shift;
610: return $self->{State};
611: }
1.3 albertel 612:
1.1 foxr 613: =pod
1.3 albertel 614:
615: =head2 GetSocket
616:
617: selector for the object socket.
618:
1.1 foxr 619: =cut
1.3 albertel 620:
1.1 foxr 621: sub GetSocket {
622: my $self = shift;
623: return $self->{Socket};
624: }
1.3 albertel 625:
1.5 foxr 626:
1.1 foxr 627: =pod
1.3 albertel 628:
629: =head2 WantReadable
630:
631: Return the state of the flag that indicates the object wants to be
632: called when readable.
633:
1.1 foxr 634: =cut
1.3 albertel 635:
1.1 foxr 636: sub WantReadable {
637: my $self = shift;
638:
639: return $self->{InformReadable};
640: }
1.3 albertel 641:
1.1 foxr 642: =pod
1.3 albertel 643:
644: =head2 WantWritable
645:
646: Return the state of the flag that indicates the object wants write
647: notification.
648:
1.1 foxr 649: =cut
1.3 albertel 650:
1.1 foxr 651: sub WantWritable {
652: my $self = shift;
653: return $self->{InformWritable};
654: }
1.3 albertel 655:
1.1 foxr 656: =pod
1.3 albertel 657:
658: =head2 WantTimeout
659:
660: return the state of the flag that indicates the object wants to be
661: informed of timeouts.
662:
1.1 foxr 663: =cut
1.3 albertel 664:
1.1 foxr 665: sub WantTimeout {
666: my $self = shift;
667: return $self->{Timeoutable};
668: }
669:
670: =pod
1.3 albertel 671:
672: =head2 GetReply
673:
674: Returns the reply from the last transaction.
675:
1.1 foxr 676: =cut
1.3 albertel 677:
1.1 foxr 678: sub GetReply {
679: my $self = shift;
680: return $self->{TransactionReply};
681: }
682:
683: =pod
1.3 albertel 684:
685: =head2 Encrypt
686:
687: Returns the encrypted version of the command string.
688:
689: The command input string is of the form:
690:
1.1 foxr 691: encrypt:command
1.3 albertel 692:
693: The output string can be directly sent to lond as it is of the form:
694:
1.1 foxr 695: enc:length:<encodedrequest>
1.3 albertel 696:
1.1 foxr 697: =cut
1.3 albertel 698:
1.1 foxr 699: sub Encrypt {
700: my $self = shift; # Reference to the object.
701: my $request = shift; # Text to send.
702:
703:
704: # Split the encrypt: off the request and figure out it's length.
705: # the cipher works in blocks of 8 bytes.
706:
707: my $cmd = $request;
708: $cmd =~ s/^encrypt\://; # strip off encrypt:
709: chomp($cmd); # strip off trailing \n
710: my $length=length($cmd); # Get the string length.
711: $cmd .= " "; # Pad with blanks so we can fill out a block.
712:
713: # encrypt the request in 8 byte chunks to create the encrypted
714: # output request.
715:
716: my $Encoded = '';
717: for(my $index = 0; $index <= $length; $index += 8) {
718: $Encoded .=
719: unpack("H16",
720: $self->{Cipher}->encrypt(substr($cmd,
721: $index, 8)));
722: }
723:
724: # Build up the answer as enc:length:$encrequest.
725:
726: $request = "enc:$length:$Encoded\n";
727: return $request;
728:
729:
730: }
1.3 albertel 731:
732: =pod
733:
734: =head2 Decrypt
735:
736: Decrypt a response from the server. The response is in the form:
737:
738: enc:<length>:<encrypted data>
739:
1.1 foxr 740: =cut
1.3 albertel 741:
1.1 foxr 742: sub Decrypt {
743: my $self = shift; # Recover reference to object
744: my $encrypted = shift; # This is the encrypted data.
745:
746: # Bust up the response into length, and encryptedstring:
747:
748: my ($enc, $length, $EncryptedString) = split(/:/,$encrypted);
749: chomp($EncryptedString);
750:
751: # Decode the data in 8 byte blocks. The string is encoded
752: # as hex digits so there are two characters per byte:
753:
1.10 foxr 754: my $decrypted = "";
1.1 foxr 755: for(my $index = 0; $index < length($EncryptedString);
756: $index += 16) {
757: $decrypted .= $self->{Cipher}->decrypt(
758: pack("H16",
759: substr($EncryptedString,
760: $index,
761: 16)));
762: }
763: # the answer may have trailing pads to fill out a block.
764: # $length tells us the actual length of the decrypted string:
765:
766: $decrypted = substr($decrypted, 0, $length);
767:
768: return $decrypted;
769:
770: }
771:
772: =pod
1.3 albertel 773:
774: =head2 GetHostIterator
1.1 foxr 775:
776: Returns a hash iterator to the host information. Each get from
777: this iterator returns a reference to an array that contains
778: information read from the hosts configuration file. Array elements
779: are used as follows:
780:
1.3 albertel 781: [0] - LonCapa host name.
782: [1] - LonCapa domain name.
783: [2] - Loncapa role (e.g. library or access).
784: [3] - DNS name server hostname.
1.11 foxr 785: [4] - IP address (result of e.g. nslookup [3]).
1.3 albertel 786: [5] - Maximum connection count.
787: [6] - Idle timeout for reducing connection count.
788: [7] - Minimum connection count.
1.1 foxr 789:
1.3 albertel 790: =cut
1.1 foxr 791:
792: sub GetHostIterator {
793:
794: return HashIterator->new(\%hostshash);
795: }
1.14 foxr 796:
797: ###########################################################
798: #
799: # The following is an unashamed kludge that is here to
800: # allow LondConnection to be used outside of the
801: # loncapa environment (e.g. by lonManage).
802: #
803: # This is a textual inclusion of pieces of the
804: # Configuration.pm module.
805: #
806:
807:
808: my $confdir='/etc/httpd/conf/';
809:
810: # ------------------- Subroutine read_conf: read LON-CAPA server configuration.
811: # This subroutine reads PerlSetVar values out of specified web server
812: # configuration files.
813: sub read_conf
814: {
815: my (@conf_files)=@_;
816: my %perlvar;
817: foreach my $filename (@conf_files,'loncapa_apache.conf')
818: {
1.21 foxr 819: if($DebugLevel > 3) {
820: print("Going to read $confdir.$filename\n");
821: }
1.14 foxr 822: open(CONFIG,'<'.$confdir.$filename) or
823: die("Can't read $confdir$filename");
824: while (my $configline=<CONFIG>)
825: {
826: if ($configline =~ /^[^\#]*PerlSetVar/)
827: {
828: my ($unused,$varname,$varvalue)=split(/\s+/,$configline);
829: chomp($varvalue);
830: $perlvar{$varname}=$varvalue;
831: }
832: }
833: close(CONFIG);
834: }
1.21 foxr 835: if($DebugLevel > 3) {
836: print "Dumping perlvar:\n";
837: foreach my $var (keys %perlvar) {
838: print "$var = $perlvar{$var}\n";
839: }
840: }
1.14 foxr 841: my $perlvarref=\%perlvar;
1.21 foxr 842: return $perlvarref;
843: }
1.14 foxr 844:
845: #---------------------- Subroutine read_hosts: Read a LON-CAPA hosts.tab
846: # formatted configuration file.
847: #
848: my $RequiredCount = 5; # Required item count in hosts.tab.
849: my $DefaultMaxCon = 5; # Default value for maximum connections.
850: my $DefaultIdle = 1000; # Default connection idle time in seconds.
851: my $DefaultMinCon = 0; # Default value for minimum connections.
852:
853: sub read_hosts {
854: my $Filename = shift;
855: my %HostsTab;
856:
1.16 foxr 857: open(CONFIG,'<'.$Filename) or die("Can't read $Filename");
1.14 foxr 858: while (my $line = <CONFIG>) {
859: if (!($line =~ /^\s*\#/)) {
860: my @items = split(/:/, $line);
861: if(scalar @items >= $RequiredCount) {
862: if (scalar @items == $RequiredCount) { # Only required items:
863: $items[$RequiredCount] = $DefaultMaxCon;
864: }
865: if(scalar @items == $RequiredCount + 1) { # up through maxcon.
866: $items[$RequiredCount+1] = $DefaultIdle;
867: }
868: if(scalar @items == $RequiredCount + 2) { # up through idle.
869: $items[$RequiredCount+2] = $DefaultMinCon;
870: }
871: {
872: my @list = @items; # probably not needed but I'm unsure of
873: # about the scope of item so...
874: $HostsTab{$list[0]} = \@list;
875: }
876: }
877: }
878: }
879: close(CONFIG);
880: my $hostref = \%HostsTab;
881: return ($hostref);
882: }
1.24 ! foxr 883: #
! 884: # Get the version of our peer. Note that this is only well
! 885: # defined if the state machine has hit the idle state at least
! 886: # once (well actually if it has transitioned out of
! 887: # ReadingVersionString The member data LondVersion is returned.
! 888: #
! 889: sub PeerVersion {
! 890: my $self = shift;
! 891:
! 892: return $self->{LondVersion};
! 893: }
1.1 foxr 894:
895: 1;
896:
897: =pod
1.3 albertel 898:
1.1 foxr 899: =head1 Theory
900:
1.3 albertel 901: The lond object is a state machine. It lives through the following states:
902:
903: =item Connected:
904:
905: a TCP connection has been formed, but the passkey has not yet been
906: negotiated.
907:
908: =item Initialized:
909:
910: "init" sent.
911:
912: =item ChallengeReceived:
913:
914: lond sent its challenge to us.
915:
916: =item ChallengeReplied:
917:
918: We replied to lond's challenge waiting for lond's ok.
919:
920: =item RequestingKey:
921:
922: We are requesting an encryption key.
923:
924: =item ReceivingKey:
925:
926: We are receiving an encryption key.
927:
928: =item Idle:
929:
930: Connection was negotiated but no requests are active.
931:
932: =item SendingRequest:
933:
934: A request is being sent to the peer.
935:
936: =item ReceivingReply:
937:
938: Waiting for an entire reply from the peer.
939:
940: =item Disconnected:
941:
942: For whatever reason, the connection was dropped.
943:
944: When we need to be writing data, we have a writable event. When we
945: need to be reading data, a readable event established. Events
946: dispatch through the class functions Readable and Writable, and the
947: watcher contains a reference to the associated object to allow object
948: context to be reached.
1.1 foxr 949:
950: =head2 Member data.
951:
1.3 albertel 952: =item Host
953:
954: Host socket is connected to.
955:
956: =item Port
957:
958: The port the remote lond is listening on.
959:
960: =item Socket
961:
962: Socket open on the connection.
963:
964: =item State
965:
966: The current state.
967:
968: =item TransactionRequest
969:
970: The request being transmitted.
971:
972: =item TransactionReply
973:
974: The reply being received from the transaction.
975:
976: =item InformReadable
977:
978: True if we want to be called when socket is readable.
979:
980: =item InformWritable
981:
982: True if we want to be informed if the socket is writable.
983:
984: =item Timeoutable
985:
986: True if the current operation is allowed to timeout.
987:
988: =item TimeoutValue
989:
990: Number of seconds in the timeout.
991:
992: =item TimeoutRemaining
993:
994: Number of seconds left in the timeout.
995:
996: =item CipherKey
997:
998: The key that was negotiated with the peer.
999:
1000: =item Cipher
1001:
1002: The cipher obtained via the key.
1.1 foxr 1003:
1004:
1005: =head2 The following are callback like members:
1.3 albertel 1006:
1007: =item Tick:
1008:
1009: Called in response to a timer tick. Used to managed timeouts etc.
1010:
1011: =item Readable:
1012:
1013: Called when the socket becomes readable.
1014:
1015: =item Writable:
1016:
1017: Called when the socket becomes writable.
1018:
1019: =item TimedOut:
1020:
1021: Called when a timed operation timed out.
1022:
1.1 foxr 1023:
1024: =head2 The following are operational member functions.
1.3 albertel 1025:
1026: =item InitiateTransaction:
1027:
1028: Called to initiate a new transaction
1029:
1030: =item SetStateTransitionCallback:
1031:
1032: Called to establish a function that is called whenever the object goes
1033: through a state transition. This is used by The client to manage the
1034: work flow for the object.
1035:
1036: =item SetTimeoutCallback:
1037:
1038: Set a function to be called when a transaction times out. The
1039: function will be called with the object as its sole parameter.
1040:
1041: =item Encrypt:
1042:
1043: Encrypts a block of text according to the cipher negotiated with the
1044: peer (assumes the text is a command).
1045:
1046: =item Decrypt:
1047:
1048: Decrypts a block of text according to the cipher negotiated with the
1049: peer (assumes the block was a reply.
1.5 foxr 1050:
1051: =item Shutdown:
1052:
1053: Shuts off the socket.
1.1 foxr 1054:
1055: =head2 The following are selector member functions:
1056:
1.3 albertel 1057: =item GetState:
1058:
1059: Returns the current state
1060:
1061: =item GetSocket:
1062:
1063: Gets the socekt open on the connection to lond.
1064:
1065: =item WantReadable:
1066:
1067: true if the current state requires a readable event.
1068:
1069: =item WantWritable:
1070:
1071: true if the current state requires a writable event.
1072:
1073: =item WantTimeout:
1074:
1075: true if the current state requires timeout support.
1076:
1077: =item GetHostIterator:
1078:
1079: Returns an iterator into the host file hash.
1080:
1.1 foxr 1081: =cut
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>