3 ;;; System-specific functions
5 ;;; (c) 2008 Straylight/Edgeware
8 ;;;----- Licensing notice ---------------------------------------------------
10 ;;; This program is free software; you can redistribute it and/or modify
11 ;;; it under the terms of the GNU General Public License as published by
12 ;;; the Free Software Foundation; either version 2 of the License, or
13 ;;; (at your option) any later version.
15 ;;; This program is distributed in the hope that it will be useful,
16 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;;; GNU General Public License for more details.
20 ;;; You should have received a copy of the GNU General Public License
21 ;;; along with this program; if not, write to the Free Software Foundation,
22 ;;; Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 (cl:defpackage #:net-sys
26 (:export #:gethostname #:resolve-hostname #:canonify-hostname))
27 (cl:in-package #:net-sys)
29 ;;;--------------------------------------------------------------------------
30 ;;; Functions provided.
33 (cffi:defcfun gethostname :int
38 "Return the hostname (not necessarily canonical) of the current host."
41 (unix:unix-gethostname)
44 (sb-unix:unix-gethostname)
50 (cffi:with-foreign-pointer-as-string (buffer 256 len)
51 (let ((rc (gethostname buffer len)))
53 (error "gethostname(2) failed (rc = ~A)." rc))))
55 #-(or cmu sbcl clisp ecl)
58 (defun resolve-hostname (name)
59 "Resolve a hostname to an IP address using the DNS, or return nil."
62 (let ((he (ext:lookup-host-entry name)))
63 (and he (ext:host-entry-addr he)))
67 (let* ((he (sb-bsd-sockets:get-host-by-name name))
68 (addr (sb-bsd-sockets:host-ent-address he)))
69 (reduce (lambda (acc byte) (logior (ash acc 8) byte)) addr))
70 (sb-bsd-sockets:name-service-error () nil))
73 (let ((he (ext:resolve-host-ipaddr name)))
74 (and he (string-ipaddr (car (ext:hostent-addr-list he)))))
77 (nth-value 2 (ext:lookup-host-entry name))
79 #-(or cmu sbcl clisp ecl)
82 (defun canonify-hostname (name)
83 "Resolve a hostname to canonical form using the DNS, or return nil."
86 (let ((he (ext:lookup-host-entry name)))
87 (and he (ext:host-entry-name he)))
91 (let ((he (sb-bsd-sockets:get-host-by-name name)))
92 (sb-bsd-sockets:host-ent-name he))
93 (sb-bsd-sockets:name-service-error () nil))
96 (let ((he (ext:resolve-host-ipaddr name)))
97 (and he (ext:hostent-name he)))
100 (nth-value 0 (ext:lookup-host-entry name))
102 #-(or cmu sbcl clisp ecl)
105 ;;;----- That's all, folks --------------------------------------------------