Note: This is taken from the Chicken Wiki, where a more recent version could be available.
This is version 1.1 of the hostinfo extension library for Chicken Scheme.
Look up host, protocol, and service information
This extension performs host, protocol and service information lookups via underlying calls to gethostbyname(3), getprotobyname(3), and getservbyname(3). Depending on your system, this may consult DNS, NIS, /etc/hosts, /etc/services, /etc/protocols, and so on.
A simple interface is provided for the most commmon queries. Also provided is a more comprehensive interface using records, which contain all data available in a lookup.
IP addresses are represented by 4 (IPv4) or 16 (IPv6) byte u8vectors. The interface requires, and returns, addresses in this format; functions are provided to convert between the string and u8vector representations. However, the "do what I want" procedures (e.g. host-information) will do the conversion for you.
Quickly perform the most common lookups. Convenient and efficient for one-off use, but perform a new lookup each time. They return #f on failure.
procedure: (hostname->ip HOSTNAME)
Look up string HOSTNAME and return IP address as u8vector.
procedure: (ip->hostname IPADDR)
Look up u8vector IPADDR and return hostname as string.
procedure: (protocol-name->number PROTOCOL-NAME)
Look up string PROTOCOL-NAME and return protocol number.
procedure: (protocol-number->name PROTOCOL-NUMBER)
Look up PROTOCOL-NUMBER and return protocol name as string.
procedure: (service-port->name SERVICE-PORT [PROTO])
Look up SERVICE-PORT number and return service name as string. Optional PROTO argument, which must be a string, constrains lookup to that protocol.
procedure: (service-name->port SERVICE-NAME [PROTO])
Look up string SERVICE-NAME and return the canonical port for that service. Optional PROTO argument as above.
Some lookups return a host, protocol, or service record. These records print nicely on the screen, for convenient interactive use.
procedure: (hostinfo-address h)
Retrieves the address field of the hostinfo record h. Accessors are similar for other records and their fields.
A vector of one or more u8vector IP addresses
A vector of any alternate hostnames
The first IP address (u8vector) in addresses
'AF_INET (IPv4) or 'AF_INET6 (IPv6)
IP address length in bytes
Vector of alternate names for this protocol
Vector of alternate names for this service
Name of protocol this service uses
procedure: (hostname->hostinfo NAME) procedure: (ip->hostinfo IPADDR) procedure: (service-name->servinfo NAME) procedure: (service-port->servinfo NUM) procedure: (protocol-name->protoinfo NAME) procedure: (protocol-number->protoinfo NUM)
These lookups correspond to those described in Short and sweet, but return a full record. The entire record is filled in a single system call.
These decipher your argument, call the appropriate lookup, and return an information record.
procedure: (host-information HOST)
Look up and return a hostinfo record, or #f. HOST is a string hostname, a string numeric IP address, or a u8vector IP address.
procedure: (protocol-information PROTO)
Look up and return a protoinfo record, or #f. PROTO is a protocol number or string name.
procedure: (service-information SERVICE [PROTO])
Look up and return a servinfo record, or #f. SERVICE is a service number or string name. PROTO is an optional protocol number or string name, which will constrain lookups to that particular protocol.
NOTE: if the protocol number is illegal, an error is thrown, since this was probably unintentional.
procedure: (string->ip IP-STRING)
Convert an IPv4 or IPv6 address string in canonical format to a u8vector, which can be considered an "IP address object". Returns #f on failure.
procedure: (ip->string IPADDR)
Convert a 4 (IPv4) or 16 (IPv6) element u8vector to a string in canonical format. Throws an error if the u8vector is not 4 or 16 bytes long. This call should only fail on system error, in which case it will return #f (perhaps not the best behaviour).
procedure: (current-hostname [HOSTNAME])
Get the standard host name for the current processor; synonym for get-host-name.
Set the standard host name when a HOSTNAME is specified; assuming permission and argument validity. Aborts with an error upon constraint violation.
IPv6 lookup is not yet supported. However, IPv6↔string conversion works fine.
System errors return failure (#f) and so are indistinguishable from failed lookups. They should probably signal an error or an exception.
(host-information "www.call-with-current-continuation.org") (host-information '#u8(194 97 107 133)) (host-information "184.108.40.206") ; => #,(hostinfo name: "www003.lifemedien.de" ; addresses: #(#u8(194 97 107 133)) ; aliases: #("www.call-with-current-continuation.org")) (ip->hostname '#u8(194 97 107 133)) ; "www003.lifemedien.de" (string->ip "0708::0901") ; #u8(7 8 0 0 0 0 0 0 0 0 0 0 0 0 9 1) (ip->string '#u8(127 0 0 1)) ; "127.0.0.1" (hostinfo-aliases (hostname->hostinfo (ip->hostname (hostname->ip (hostinfo-name (host-information "www.call-with-current-continuation.org")))))) ; => #("www.call-with-current-continuation.org") (protocol-information 17) ; => #,(protoinfo name: "udp" number: 17 aliases: #("UDP")) (protoinfo-name (protocol-information 2)) ; => "igmp" (protoinfo-aliases (protocol-name->protoinfo (protocol-number->name (protoinfo-number (protocol-information "ospf"))))) ; => #("OSPFIGP") (protocol-name->number "OSPFIGP") ; 89 (you can look up aliases, too) (servinfo-protocol (service-name->servinfo (service-port->name (servinfo-port (service-information "ssh"))))) ; => "udp" (yes, really) (service-information "ssh" "tcp") ; => #,(servinfo name: "ssh" port: 22 aliases: #() protocol: "tcp") (service-information "ssh" "tco") ; => #f (service-information 512 "tcp") ; #,(servinfo name: "exec" port: 512 aliases: #() protocol: "tcp") (service-information 512 "udp") ; #,(servinfo name: "comsat" port: 512 aliases: #("biff") protocol: "udp") (service-information 512 17) ; same as previous (service-information 512 170000) ; Error: (service-information) illegal protocol number: 170000
Copyright (c) 2005, 2006 Jim "Zb" Ursetto. All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. Neither the name of the author nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.