;;; -*- Mode:Lisp; Readtable:CL; Package:USER; Base:10; Patch-File:T -*-
;;; Patch file for System version 126.11
;;; Reason:
;;;  a udp-socket now lets you specify :gauge-name on open
;;; Written 3-Aug-88 18:30:36 by pld (Peter L. DeWolf) at site Gigamos Cambridge
;;; while running on Azathoth from band 1
;;; with Experimental System 126.7, ZWEI 125.10, ZMail 73.0, Local-File 75.2, File-Server 24.1, Unix-Interface 13.0, Tape 24.2, Lambda-Diag 17.0, microcode 1762, SDU Boot Tape 3.14, SDU ROM 8, the old ones.



; From modified file DJ: L.NETWORK.IP-TCP.KERNEL; UDP.LISP#115 at 3-Aug-88 18:30:36
#10R UDP#: 
(COMPILER-LET ((*PACKAGE* (GLOBAL:PKG-FIND-PACKAGE "UDP")))
  (COMPILER::PATCH-SOURCE-FILE "SYS: NETWORK; IP-TCP; KERNEL; UDP  "

(defop (udp-socket :open) (&key local-port remote-port remote-address (initial-gauges nil ig-p) gauge-name
				&aux parsed-remote-address)
  (unless (udp-user-enabled self)
    (when (udp-enabled *udp-stream*)
      (assert (or (setq parsed-remote-address nil)	;This branch always fails, but reinitializes local
		  (cond ((null remote-port)
			 ;;If no remote-port, must be no remote-address
			 (null remote-address))
			((setq parsed-remote-address (parse-internet-address remote-address))
			 ;;If have a remote-port, remote-address must be valid AND remote-port must be valid
			 (typep remote-port '(unsigned-byte 16)))
			(t
			 ;;If have a remote-port and remote-address invalid, error
			 nil)))
	      (remote-port remote-address)
	      "~[REMOTE-PORT ~D out of range~;REMOTE-ADDRESS ~S unknown~;REMOTE-PORT and REMOTE-ADDRESS must be both nil or both non-nil~]"
	      (cond ((null remote-port) 2)
		    (parsed-remote-address 0)
		    (t 1))
	      (if parsed-remote-address remote-port remote-address))
      (if local-port
	  (assert (and (typep local-port '(unsigned-byte 16))
		       (unused-local-port local-port remote-port))
		  (local-port)
		  "LOCAL-PORT ~D is ~:[out of range~;already in use~]"
		  local-port
		  (typep local-port '(unsigned-byte 16)))
	(setq local-port (assign-local-port)))
      (setf (udp-user-local-port self) local-port)
      (setf (udp-user-remote-port self) remote-port)
      (setf (udp-user-remote-address self) parsed-remote-address)
      (setf (udp-user-packet-list self) (make-fifo))
      (array-initialize (udp-user-statistics-block self) 0)
      (setf (udp-user-gauge-name self) (or gauge-name (format nil "UDP ~D" (udp-user-local-port self))))
      (dolist (g (udp-user-inactive-gauges self))
	(send (cdr g) :set-margin-name (udp-user-gauge-name self)))
      (when ig-p
	;;If initial-gauges specified, NIL means no gauges, T means default, anything else specifies gauges
	;;The reason to add no gauges is to have the statistics block looked at by the clock function
	(if (eq initial-gauges t)
	    (setq initial-gauges '(:apr :aps :abr :abs)))
	(set-gauges (udp-user-statistics-block self)
		    (locf (udp-user-active-gauges self))
		    (locf (udp-user-inactive-gauges self))
		    (udp-user-gauge-name self)
		    initial-gauges)
	(add-network-statistics-block (udp-user-statistics-block self)))
      (do ((blip (pop-fifo (udp-user-packet-list self))
		 (pop-fifo (udp-user-packet-list self))))
	  ((null blip))
	(unless (eq (first blip) :close)
	  (free-ip-header (second blip)))
	(if (eq (first blip) :data)
	    (free-udp-buffer (third blip))))
      (push (cons local-port self) (udp-user-socket-alist *udp-stream*))
      local-port)))

))
