Outdated egg!
This is an egg for CHICKEN 4, the unsupported old release. You're almost certainly looking for the CHICKEN 5 version of this egg, if it exists.
If it does not exist, there may be equivalent functionality provided by another egg; have a look at the egg index. Otherwise, please consider porting this egg to the current version of CHICKEN.
Hostinfo
This is version 1.1 of the hostinfo extension library for Chicken Scheme.
TOC »
Description
Look up host, protocol, and service information
Documentation
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.
Short and sweet
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.
- hostname->ip HOSTNAMEprocedure
Look up string HOSTNAME and return IP address as u8vector.
- ip->hostname IPADDRprocedure
Look up u8vector IPADDR and return hostname as string.
- protocol-name->number PROTOCOL-NAMEprocedure
Look up string PROTOCOL-NAME and return protocol number.
- protocol-number->name PROTOCOL-NUMBERprocedure
Look up PROTOCOL-NUMBER and return protocol name as string.
- service-port->name SERVICE-PORT #!optional PROTOprocedure
Look up SERVICE-PORT number and return service name as string. Optional PROTO argument, which must be a string, constrains lookup to that protocol.
- service-name->port SERVICE-NAME #!optional PROTOprocedure
Look up string SERVICE-NAME and return the canonical port for that service. Optional PROTO argument as above.
Records
Some lookups return a host, protocol, or service record. These records print nicely on the screen, for convenient interactive use.
- hostinfo-address hprocedure
Retrieves the address field of the hostinfo record h. Accessors are similar for other records and their fields.
- hostinforecord
name Hostname addresses A vector of one or more u8vector IP addresses aliases A vector of any alternate hostnames address The first IP address (u8vector) in addresses type 'AF_INET (IPv4) or 'AF_INET6 (IPv6) length IP address length in bytes
- protoinforecord
name Protocol name number Protocol number aliases Vector of alternate names for this protocol
- servinforecord
name Service name number Service number aliases Vector of alternate names for this service protocol Name of protocol this service uses
Record lookup
- hostname->hostinfo NAMEprocedure
- ip->hostinfo IPADDRprocedure
- service-name->servinfo NAMEprocedure
- service-port->servinfo NUMprocedure
- protocol-name->protoinfo NAMEprocedure
- protocol-number->protoinfo NUMprocedure
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.
One-stop shops
These decipher your argument, call the appropriate lookup, and return an information record.
- host-information HOSTprocedure
Look up and return a hostinfo record, or #f. HOST is a string hostname, a string numeric IP address, or a u8vector IP address.
- protocol-information PROTOprocedure
Look up and return a protoinfo record, or #f. PROTO is a protocol number or string name.
- service-information SERVICE #!optional PROTOprocedure
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.
Utility functions
- string->ip IP-STRINGprocedure
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.
- ip->string IPADDRprocedure
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).
- current-hostname #!optional HOSTNAMEprocedure
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.
Bugs
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.
Examples
(host-information "www.call-with-current-continuation.org") (host-information '#u8(194 97 107 133)) (host-information "194.97.107.133") ; => #,(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
About this egg
Author
Version history
- 1.3
- Fix for FreeBSD [by Jesper Louis Andersen]
- 1.1
- Experimental Windows support [Daishi Kato]
- 1.0
- Initial release
Requirements
vector-lib
License
Copyright (c) 2005-2010 Jim 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.