;;; -*- Mode:Lisp; Readtable:CL; Package:USER; Base:10; Patch-File:T -*-
;;; Patch file for System version 123.192
;;; Reason:
;;;  Abstract all of the Network Interface / Network Protocol cross-knowledge
;;;  into one variable: net:*network-configuration*.  Now you don't need to
;;;  modify (ip:setup-ip), etc. to talk to a new device driver.
;;; Written 25-Jan-88 14:55:45 by pld (Peter L. DeWolf) at site Gigamos Cambridge
;;; while running on Jack Flanders from band 2
;;; with Experimental System 123.191, Experimental Local-File 73.3, Experimental FILE-Server 22.1, Experimental Unix-Interface 11.0, Experimental Tape 18.0, Experimental KERMIT 34.3, 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.KERNEL; CONFIGURE.LISP#91 at 25-Jan-88 14:56:32
#10R NETWORK#: 
(COMPILER-LET ((*PACKAGE* (GLOBAL:PKG-FIND-PACKAGE "NETWORK")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERNEL; CONFIGURE  "

(defvar *protocol-configuration*
	`((:ethernet  (:internet ,ip-ethernet-type (:address :subnet) :address)
		      (:chaos ,chaos-ethernet-type :subnet :address)
		      (:arp ,arp-ethernet-type nil nil))
	  (:loopback  (:internet ,ip-ethernet-type (#x7f000000 #xff000000) #x7f000000)
		      (:chaos ,chaos-ethernet-type nil :address))
	  (:backplane (:internet ,ip-ethernet-type nil :address)
		      (:chaos 0 nil :address)))
  "List of (hardware-type protocol...) for network interfaces")

))

; From modified file DJ: L.NETWORK.KERNEL; NETWORK-PROTOCOL.LISP#141 at 25-Jan-88 14:56:42
#10R NETWORK#: 
(COMPILER-LET ((*PACKAGE* (GLOBAL:PKG-FIND-PACKAGE "NETWORK")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERNEL; NETWORK-PROTOCOL  "

(defop (network-protocol :open) (address subnet)
  "open a network-protocol."
  (unless (np-opened self)
    (dolist (stream *network-protocol-streams*)
      (when (eq (np-keyword stream) (np-keyword self))
	(send stream :close)
	(return t)))
    (push self *network-protocol-streams*)
    (let ((func (let ((stream self))
		  #'(lambda (int-pkt interface sender receiver broadcast-p)
		      (network-protocol-packet-receiver int-pkt interface stream sender receiver broadcast-p))))
	  list
	  result)
      (labels ((make-address-form (l)
		 (cond ((consp l)
			(cons (make-address-form (car l))
			      (make-address-form (cdr l))))
		       ((numberp l) l)
		       ((eq l :address) address)
		       ((eq l :subnet) subnet)
		       (t nil))))
	(dolist (ni *network-interfaces*)
	  (dolist (item *protocol-configuration*)
	    (when (and (eq (ni-keyword ni) (first item))
		       (setq list (assoc (np-keyword self) (cdr item))))
	      (pushnew (cons (first item) (second list)) (np-protocol self) :test #'equal)
	      (delete-from-alist (np-keyword self) (ni-network-alist ni))
	      (push (cons (np-keyword self) (make-address-form (third list))) (ni-network-alist ni))
	      (delete-from-alist (np-keyword self) (ni-address-alist ni))
	      (let ((address (make-address-form (fourth list))))
		(push (cons (np-keyword self) (ncons address)) (ni-address-alist ni))
		(setq result (nconc result (ncons address))))
	      (delete-from-alist (second list) (ni-protocol-alist ni))
	      (push (cons (second list) func) (ni-protocol-alist ni))
	      (return t)))))
      (setf (np-addresses self) result))
    (when (np-addresses self)
      (setf (get (np-keyword self) 'si::address-unparser) (np-address-printer self)))
    (add-network-statistics-block (np-statistics-block self))
    (setf (np-opened self) t)))

))

; From modified file DJ: L.NETWORK.IP-TCP.KERNEL; IP.LISP#282 at 25-Jan-88 14:57:26
#10R INTERNET#: 
(COMPILER-LET ((*PACKAGE* (GLOBAL:PKG-FIND-PACKAGE "INTERNET")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; IP-TCP; KERNEL; IP  "

(net:create-included-methods 'ip:ip-network-protocol)

(defun setup-ip ()
  (multiple-value-bind (internet-address-list subnet-mask-list)
      (setup-my-internet-address)
    (when internet-address-list
      (let ((my-internet-address (first internet-address-list))
	    (my-subnet-mask (first subnet-mask-list)))
	;;;***Ideally, there should be a way to match multiple internet addresses with
	;;;***multiple network interfaces -- Internet Gateway functions!
	(setq *ip-stream*
	      (make-ip-network-protocol :keyword :internet
					:address-length 4
					:big-endian-address-p t
					:interrupt-function 'receive-ip-packet
					:address-printer 'canonical-ip
					:get-header-function 'make-ip-header
					:free-header-function 'free-ip-header
					:send-packet-function 'send-ip-packet
					:broadcast-packet-function 'broadcast-ip-packet
					:special-address-function 'ip-special-addresses
					:packet-length-function 'ip-length
					:gauge-name "IP"
					:default-address my-internet-address
					))
	(send *ip-stream* :open my-internet-address my-subnet-mask)))
      (initialize-route-table internet-address-list)
    (initialize-ip-background-process)
    (send *ip-stream* :enable)
    t))

))

; From modified file DJ: L.NETWORK.CHAOS; CHSNCP.LISP#404 at 25-Jan-88 14:58:43
#10R CHAOS#: 
(COMPILER-LET ((*PACKAGE* (PKG-FIND-PACKAGE "CHAOS")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; CHAOS; CHSNCP  "

(net:create-included-methods 'chaos:chaos-network-protocol)

(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
						    :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 my-address my-subnet)
  (send *chaos-stream* :enable)
  )

))

; From modified file DJ: L.NETWORK.KERNEL; ARP.LISP#128 at 25-Jan-88 14:59:28
#10R ARP#: 
(COMPILER-LET ((*PACKAGE* (GLOBAL:PKG-FIND-PACKAGE "ARP")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; KERNEL; ARP  "

(defun setup-arp ()
  (setq *arp-stream* (make-network-protocol :keyword :arp
					    :addresses nil
					    :packet-length-function 'arp-int-pkt-length
					    :interrupt-function 'receive-arp-pkt
					    :gauge-name "ARP"
					    ))
  
  (send *arp-stream* :open nil nil)
  (send *arp-stream* :enable)
  )
))
