;;; -*- Mode:Lisp; Readtable:CL; Package:USER; Base:10; Patch-File:T -*-
;;; Patch file for System version 123.28
;;; Reason:
;;;  If there is no Ethernet hardware, Chaos couldn't talk to other processors on backplane.
;;;  Also, when Chaos started on share and loopback devices, include own Chaos address --
;;;  which makes no operational difference, but is nice for Peek
;;; Written 5-Oct-87 16:51:29 by pld at site LMI Cambridge
;;; while running on Jack Flanders from band 2
;;; with Experimental System 123.27, Experimental Local-File 73.0, Experimental FILE-Server 22.0, Experimental Unix-Interface 11.0, Experimental Tape 18.0, Experimental KERMIT 34.0, Experimental ZMail 71.0, Experimental Lambda-Diag 15.0, microcode 1754, SDU Boot Tape 3.12, SDU ROM 8.



; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#389 at 5-Oct-87 16:51:29
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "

(defun setup-chaos (&aux (my-subnet (chaos-subnet-number-from-address my-address)))
  (if background
      (send background :flush)
    (setq background (make-process "Chaos Background" :warm-boot-action nil :priority 25.)))

  (setq *chaos-stream* (make-chaos-network-protocol :keyword :chaos
						    :protocol `((:ethernet . ,net:chaos-ethernet-type)
								(:backplane . 0)
								(:loopback . ,net:chaos-ethernet-type))
						    :address-length 2
						    :big-endian-address-p nil
						    :disable-function 'disable
						    :enable-function 'enable
						    :interrupt-function 'receive-chaos-pkt
						    :special-address-function 'chaos-special-addresses
						    :packet-length-function 'chaos-packet-length
						    :gauge-name "Chaos"
						    ))
  (setup-my-address)
  (send *chaos-stream* :open
	`(("ONE" ,my-subnet ,my-address)	;3com interface
	  ("LOOPBACK" nil ,my-address)		;loopback
	  ("SHARE" nil ,my-address)		;backplane
	  ))
  (send *chaos-stream* :enable)
  )

))

; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#389 at 5-Oct-87 16:56:41
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "

(defun transmit-int-pkt (int-pkt &optional host subnet (broadcast-if-necessary t)
			 &aux byte-count address interface)
  (if (null host) (setq host (pkt-dest-address int-pkt)))
  (if (null subnet) (setq subnet (pkt-dest-subnet int-pkt)))
  
  (COND ((AND (NOT (= SUBNET MY-SUBNET))
	      (NOT (MEMBER SUBNET MY-OTHER-SUBNETS)))
	 (AND ( SUBNET (ARRAY-LENGTH ROUTING-TABLE))
	      (SETQ SUBNET 0))
	 (SETQ HOST (aref ROUTING-TABLE SUBNET))))
  
  (setq byte-count (* 2 (pkt-nwords int-pkt)))
  
  (unless (net:check-packet (net:original-array int-pkt))
    (ferror "Attempt to transmit non-interrupt packet ~A." INT-PKT))
  
  (if (BIT-TEST #o200 (PKT-OPCODE INT-PKT))
      (incf DATA-PKTS-OUT))
  
  (cond ((and broadcast-if-necessary
	      (zerop host)
	      (zerop (pkt-dest-address int-pkt)))
	 (incf pkts-transmitted)		;Increment counter looked at by Hostat
	 (send *chaos-stream*
	       :broadcast
	       int-pkt
	       byte-count))
	
	;;Maybe this case should be handled by some sort of "default gateway" mechanism.
	((and si:*ethernet-hardware-controller*
	      (not (eq si:*ethernet-hardware-controller* si:*my-op*))
	      (si:share-mode-active-p))
	 ;;Here if there is Ethernet hardware and it is owned by another processor
	 (incf pkts-transmitted)		;Increment counter looked at by Hostat
	 (send *chaos-stream*
	       :send
	       int-pkt
	       byte-count
	       si:share-interface
	       si:*ethernet-hardware-controller*))
	
	((multiple-value-setq (address interface)
	   (send *chaos-stream* :translate-address host nil (pkt-source-address int-pkt)))
	 ;;Here if there is no Ethernet hardware, but packet goes to processor on our backplane,
	 ;;OR there is Ethernet hardware and we own it,
	 ;;OR the packet is addressed to this host
	 (incf pkts-transmitted)		;Increment counter looked at by Hostat
	 (send *chaos-stream*
	       :send
	       int-pkt
	       byte-count
	       interface
	       address))
	
	(t
	 (incf (chaos-packets-sent-discarded *chaos-stream*))
	 (incf (chaos-bytes-sent-discarded *chaos-stream*) byte-count)
	 (net:free-packet int-pkt)))
  )

))
