;;; -*- Mode:Lisp; Readtable:CL; Package:USER; Base:10; Patch-File:T -*-
;;; Patch file for System version 123.11
;;; Reason:
;;;  Increment chaos:pkts-transmitted and chaos:pkts-received, which are statistics sent in
;;;  STS packets.  (New Network kept statistics elsewhere so didn't bother with old ones)
;;; Written 3-Sep-87 11:19:27 by pld (Peter L. DeWolf) at site LMI Cambridge
;;; while running on Azathoth from band 2
;;; with Experimental System 123.10, 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, Experimental Site Data Editor 6.0, microcode 1754, SDU Boot Tape 3.12, SDU ROM 102.



; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#385 at 3-Sep-87 11:19:39
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "


(defun receive-chaos-pkt (int-pkt interface stream source destination broadcast-p)
  (declare (ignore stream))
  (declare (ignore destination))
  (declare (ignore broadcast-p))
  (incf pkts-received)				;Increment counter looked at by Hostat
  (let* ((packet (net:original-array int-pkt))
	 (header (net:ni-rcvd-header-length interface))
	 (trailer (net:ni-rcvd-trailer-length interface))
	 (total-words (/ (+ header trailer) 2)))
    (unless (zerop header)
      (setq int-pkt (make-array (- (array-length packet) total-words)
				:element-type '(unsigned-byte 16)
				:displaced-to packet
				:displaced-index-offset (/ header 2)
				:fill-pointer (- (fill-pointer int-pkt) total-words)))))
  (cond ((set-word-count int-pkt)
	 (and (/= (pkt-dest-address int-pkt) my-address)	;Packet to be forwarded
	      (eq interface si:share-interface)	                ;And came from other processor
	      (let ((elt (assoc source net:*processor-forwarding-alist*)))
		(when elt
		  (setf (cdr elt) t))))		;Note that we are that processor's gateway
	 (without-interrupts
	   (receive-int-pkt int-pkt)))
	(t
	 ;;packet appears to be a loser.
	 (net:free-packet int-pkt))))

))

; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#385 at 3-Sep-87 11:19:57
#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 (not (eq si:*ethernet-hardware-controller* si:*my-op*))
	      (si:share-mode-active-p))
	 (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)))
	 (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)))
  )

;;;; KLUDGES and assorted random functions

))
