perm filename UPDATE.FAI[HST,NET]1 blob sn#722342 filedate 1983-07-30 generic text, type C, neo UTF8
COMMENT ⊗   VALID 00007 PAGES
C REC  PAGE   DESCRIPTION
C00001 00001
C00002 00002	TITLE UPDATE  A B C D P PDLEN BUFLEN DIN DOUT DVER DSKBLK VERFIL VERPPN HSTFIL HSTPPN NICVER DSKVER HSTSIZ PDL DIBUF DOBUF BUF NIORTS SRINIC HNSPRT
C00005 00003	 NETSTR NETST1 CPOPJ1 CPOPJ ERROR
C00006 00004	 VERNIC VERNI1 VERNI2 VERNI3 VERNI4
C00009 00005	 VERIN VEROUT
C00011 00006	 NEWNIC
C00015 00007	 TOPLEV UPDATE
C00017 ENDMK
C⊗;
TITLE UPDATE ;⊗ A B C D P PDLEN BUFLEN DIN DOUT DVER DSKBLK VERFIL VERPPN HSTFIL HSTPPN NICVER DSKVER HSTSIZ PDL DIBUF DOBUF BUF NIORTS SRINIC HNSPRT

;This is a program to automatically keep the binary host table up-to-date.
;NOT YET READY!!  -- JJW 30-Jul-83.

A←1
B←2
C←3
D←4
P←17
PDLEN←←20
BUFLEN←←40

DIN←←3				;Disk input channel for HOSTS.TXT file
DOUT←←4				;Disk output channel for HOSTS.TXT file
DVER←←5				;Disk channel for NICVER file
DSKBLK←←19			;Number of disk buffers to ask for

VERFIL:	SIXBIT/NICVER/		;LOOKUP/ENTER block for file with NIC version no.
	0
	0
	0
VERPPN:	SIXBIT/HSTNET/

HSTFIL:	SIXBIT/HOSTS/		;LOOKUP/ENTER block for NIC host table
	SIXBIT/TXT/
	0
	0
HSTPPN:	SIXBIT/HSTNET/

NICVER:	BLOCK 1			;Version number from NIC
DSKVER:	BLOCK 1			;Version number on disk
HSTSIZ:	BLOCK 1			;Size of HOSTS.TXT on disk in words

PDL:	BLOCK PDLEN
DIBUF:	BLOCK 3			;Buffer headers
DOBUF:	BLOCK 3
BUF:	BLOCK BUFLEN		;Temporary buffer

NIORTS←←-1			;Use network I/O routines
.INSERT NETWRK.FAI[S,NET]	;Wonderful network I/O package

;Host number for SRI-NIC.  We could use HSTNAM, but then we'd lose if
;there was something wrong with the host table.
SRINIC←←<BYTE (4)0(8)=10,0,0,=73>

HNSPRT←←=101			;Host Name Server port number

DEFINE MESSAG(MESS) <
	JRST [  MOVEI A,[ASCIZ\MESS
\]
		JRST MESTYP]
>
;⊗ NETSTR NETST1 CPOPJ1 CPOPJ ERROR

;Output a string to current network connection.  Call:
;	MOVEI A,[ASCIZ/string/]
;	PUSHJ P,NETSTR
;	<error return>
;	<normal return>
NETSTR:	HRLI A,440700		;Make byte ptr
NETST1:	ILDB 0,A		;Get char
	JUMPE 0,CPOPJ1		;All done
	PUSHJ P,NETOCH		;Send char
	 POPJ P,		;Error return
	JRST NETST1

CPOPJ1:	AOS (P)
CPOPJ:	POPJ P,

;Here on error messages.  Non-skip return usually causes error return
;from whatever procedure we were executing.
MESTYP:	OUTSTR (A)
	POPJ P,
;⊗ VERNIC VERNI1 VERNI2 VERNI3 VERNI4

;Here to get version number from the NIC Host Name Server.  Call:
;	PUSHJ P,VERNIC
;	<error return>
;	<success, version number in A>
VERNIC:	MOVE A,[SRINIC]		;Set host number
	MOVEM A,HOST
	MOVEI A,HNSPRT		;And port number
	MOVEM A,ICPSKT
	PUSHJ P,CONECT
	 MESSAG Can't connect to NIC for version request.
	 MESSAG Can't connect to NIC for version request.
	MOVEI A,[ASCIZ/VERSION
/]
	PUSHJ P,NETSTR		;Send a version request
	 MESSAG Error in sending version request.
	PUSHJ P,NETSND		;Force output out
	 MESSAG Error in sending version request.
	;Now read in characters until connection closes.
	MOVE C,[POINT 7,BUF]	;Ptr to start depositing chars
VERNI1:	PUSHJ P,NETICW		;Get a char
	 JRST [	TRNN 0,20000	;IODEND bit set (connection closed)?
		MESSAG I/O error in version request reply.
		JRST VERNI2]
	IDPB 0,C		;Store char
	CAMN C,[POINT 7,BUF+BUFLEN-1,35] ;Check for overflow
	MESSAG Buffer overflow in version request reply.
	JRST VERNI1		;Back for more

VERNI2:	MOVEI 0,0		;End with a null
	IDPB 0,C
	;Skip up to first digit
	MOVE C,[POINT 7,BUF]
VERNI3:	ILDB B,C
	SKIPN B
	MESSAG No digits in version request reply
	CAIL B,"0"
	CAILE B,"9"
	JRST VERNI3		;Not a digit; keep looking
	MOVEI A,-"0"(B)		;Accumulate version number in A
VERNI4:	ILDB B,C		;Get next char
	JUMPE B,CPOPJ1		;All done if end of string
	CAIL B,"0"
	CAILE B,"9"
	JRST CPOPJ1		;Or if non-digit seen
	IMULI A,=10
	ADDI A,-"0"(B)		;Accumulate version number
	JRST VERNI4
;⊗ VERIN VEROUT

;Here to get the version number of the current NIC host table on disk.  Call:
;	PUSHJ P,VERIN
;	<error return>
;	<success, version number in DSKVER>
VERIN:	INIT DVER,17		;Open disk in dump mode
	 SIXBIT/DSK/
	 0
	 MESSAG INIT error for version file input.
	MOVE A,VERPPN
	MOVEM A,VERFIL+3
	LOOKUP DVER,VERFIL
	 MESSAG LOOKUP error for version file input.
	IN DVER,[IOWD 1,DSKVER ↔ 0] ;Get version number from first word
	JRST CPOPJ1		;Success
	MESSAG Input error on version file.

;Here to output NIC version number to the file.  Call:
;	<call VERIN; leave DVER channel open>
;	 ...
;	PUSHJ P,VEROUT
;	<error return>
;	<success return>
VEROUT:	MOVE B,VERPPN
	MOVEM B,VERFIL+3
	ENTER DVER,VERFIL	;Open in Read-Alter mode
	 MESSAG ENTER error for version file output.
	USETO DVER,0		;Position at beginning
	OUT DVER,[IOWD 1,NICVER ↔ 0] ;Do the output
	JRST CPOPJ1		;Success
	MESSAG Output error on version file.
;⊗ NEWNIC

;Here to read in a new table from the NIC Host Name Server.  Call:
;	PUSHJ P,NEWNIC
;	<error return>
;	<success>
NEWNIC:	INIT DIN,0		;Open current HOSTS.TXT
	 SIXBIT/DSK/
	 0,,DIBUF
	 MESSAG INIT error for host table input.
	MOVE A,HSTPPN
	MOVEM A,HSTFIL+3
	LOOKUP DIN,HSTFIL
	 MESSAG LOOKUP error for host table input.
	MOVS A,HSTFIL+3		;Get size in words
	MOVNM A,HSTSIZ		;Remember for later comparison
	RELEAS DIN,

	MOVE A,[SRINIC]		;Set host number
	MOVEM A,HOST
	MOVEI A,HNSPRT		;And port number
	MOVEM A,ICPSKT
	PUSHJ P,CONECT
	 MESSAG Can't connect to NIC for host table request.
	 MESSAG Can't connect to NIC for host table request.
	MOVEI A,[ASCIZ/ALL
/]
	PUSHJ P,NETSTR		;Ask for the whole table
	 MESSAG Error in sending host table request.
	PUSHJ P,NETSND		;Force output out
	 MESSAG Error in sending host table request.

	INIT DOUT,0		;Get ready to write new file
	 SIXBIT/DSK/
	 DOBUF,,0
	 MESSAG INIT error for host table output.
	MOVE A,HSTPPN
	MOVEM A,HSTFIL+3
	ENTER DOUT,HSTFIL
	 MESSAG ENTER error for host table output.
	OUTBUF DOUT,DSKBLK
	MOVEI C,0		;Initialize byte count

NEWNI1:	PUSHJ P,NETICW		;Get a character from the network
	 JRST [ TRNN 0,20000	;Test for connection close
		MESSAG I/O error in host table request reply.
		JRST NEWNI2]
	SOSG DOBUF+3		;Output the character
	OUT DOUT,
	CAIA
	MESSAG Output error in writing host table file.
	IDPB 0,DOBUF+1
	AOJA C,NEWNI1		;Go back for more

NEWNI2:	OUT DOUT,		;Do last output
	CAIA
	MESSAG Output error in writing host table file.
	MOVE A,HSTSIZ		;Get back previous size in words
	IMULI A,=500		;Convert to bytes and multiply by 100
	DIV A,C			;Get ratio of old size to new size
	CAIL A,=125		;Did file shrink by more than 20%?
	 MESSAG Host table got more than 20% smaller.  Not updated.
	CAIG A,=33		;Or did it get more than 3 times as big?
	 MESSAG Host table got more than 300% larger.  Not updated.
	RELEAS DOUT,		;Put new file in place
	JRST CPOPJ1		;And take success return.
;⊗ TOPLEV UPDATE

TOPLEV:	PUSHJ P,VERNIC		;Get NIC directory version number
	 POPJ P,		;Error of some kind
	MOVEM A,NICVER		;Store result
	PUSHJ P,VERIN		;Get version from disk file
	 POPJ P,		;Error
	MOVE A,NICVER
	CAMN A,DSKVER		;Compare version numbers
	 JRST NONEW		;No update needed
	PUSHJ P,NEWNIC		;Read in the new host table
	 POPJ P,		;Some error
	PUSHJ P,VEROUT		;Output version number to file
	 POPJ P,		;Some error
	OUTSTR [ASCIZ/New host table successfully installed.
/]
	EXIT

NONEW:	OUTSTR [ASCIZ/No update of NIC host table needed.
/]
	EXIT

UPDATE:	RESET			;Program starts here
	MOVE P,[IOWD PDLEN,PDL]
	PUSHJ P,TOPLEV		;Only returns if some kind of error
	MOVEI A,[ASCIZ/
UPDATE aborting due to errors.
/]
	RELEAS DVER,3		;Don't write any files!
	RELEAS DOUT,3
	EXIT

	END UPDATE