;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; This file is part of "NOVA": NOVA = search + COCOMO tools ; Copyright, 2008, Tim Menzies tim@menzies.us ; ; NOVA is free software: you can redistribute it and/or modify ; it under the terms of the GNU General Public License as published by ; the Free Software Foundation, either version 3 of the License, or ; (at your option) any later version. ; ; NOVA is distributed in the hope that it will be useful, ; but WITHOUT ANY WARRANTY; without even the implied warranty of ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ; GNU General Public License for more details. ; You should have received a copy of the GNU General Public License ; a long with NOVA. If not, see . ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; (in-package :wvu-lib.tricks) (defun nslookup (hostname) "Performs a DNS look up for HOSTNAME and returns the address as a four element array, suitable for socket-connect. If HOSTNAME is not found, a host-not-found-error condition is thrown." (when hostname (sb-bsd-sockets:host-ent-address (sb-bsd-sockets:get-host-by-name hostname)))) (defun tcp-server (&key (interface (machine-instance)) (port 8000) (backlog 5)) "Returns a socket server bound to PORT on INTERFACE. Defaults to (machine-instance):8000. The queue size BACKLOG defaults to 5 connections." (handler-case (let ((server (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp ))) (sb-bsd-sockets:socket-bind server (nslookup interface ) port) (sb-bsd-sockets:socket-listen server backlog) server) (sb-bsd-sockets:address-in-use-error () (format t "address ~A : ~A is already in use" interface port) (force-output) nil))) (defun tcp-accept (server &key (timeout 30)) "Waits up to TIMEOUT (defaults to 30) seconds for a client to connect to SERVER. A new socket is returned, which is bound to the client connection. If a timeout occured, nil is returned." (when server (unless-timeout timeout (sb-bsd-sockets:socket-accept server)))) (defun tcp-connect (server port &optional (timeout 5)) "Returns a socket connected to SERVER:PORT. If an error occurs, or the attempt times out after TIMOUT (default 5) secons, nil is returned." (when (and server port) (handler-case (unless-timeout timeout (let ((socket (make-instance 'sb-bsd-sockets:inet-socket :type :stream :protocol :tcp))) (sb-bsd-sockets:socket-connect socket (nslookup server) port) socket)) (sb-bsd-sockets:host-not-found-error () (format t "host ~A not found." server) (force-output) nil))))