Copyright | (c) Winterland 2018 |
---|---|
License | BSD |
Maintainer | drkoster@qq.com |
Stability | experimental |
Portability | non-portable |
Safe Haskell | None |
Language | Haskell2010 |
This module provides getAddrInfo
and getNameInfo
. getnameinfo and getaddrinfo equivalent.
Synopsis
- getAddrInfo :: Maybe AddrInfo -> HostName -> ServiceName -> IO [AddrInfo]
- type HostName = CBytes
- type ServiceName = CBytes
- data AddrInfoFlag
- addrInfoFlagImplemented :: AddrInfoFlag -> Bool
- addrInfoFlagMapping :: [(AddrInfoFlag, CInt)]
- data AddrInfo = AddrInfo {}
- defaultHints :: AddrInfo
- followAddrInfo :: Ptr AddrInfo -> IO [AddrInfo]
- getNameInfo :: [NameInfoFlag] -> Bool -> Bool -> SocketAddr -> IO (HostName, ServiceName)
- data NameInfoFlag
- nameInfoFlagMapping :: [(NameInfoFlag, CInt)]
name to ip
:: Maybe AddrInfo | preferred socket type or protocol |
-> HostName | host name to look up |
-> ServiceName | service name to look up |
-> IO [AddrInfo] | resolved addresses, with "best" first |
Resolve a host or service name to one or more addresses.
The AddrInfo
values that this function returns contain SocketAddr
values that you can use to init TCP connection.
This function is protocol independent. It can return both IPv4 and IPv6 address information.
The AddrInfo
argument specifies the preferred query behaviour,
socket options, or protocol. You can override these conveniently
using Haskell's record update syntax on defaultHints
, for example
as follows:
>>>
let hints = defaultHints { addrFlags = [AI_NUMERICHOST], addrSocketType = Stream }
You must provide non empty value for at least one of the HostName
or ServiceName
arguments. HostName
can be either a numeric
network address (dotted quad for IPv4, colon-separated hex for
IPv6) or a hostname. In the latter case, its addresses will be
looked up unless AI_NUMERICHOST
is specified as a hint. If you
do not provide a HostName
value and do not set AI_PASSIVE
as
a hint, network addresses in the result will contain the address of
the loopback interface.
If the query fails, this function throws an IO exception instead of
returning an empty list. Otherwise, it returns a non-empty list
of AddrInfo
values.
There are several reasons why a query might result in several values. For example, the queried-for host could be multihomed, or the service might be available via several protocols.
Note: the order of arguments is slightly different to that defined
for getaddrinfo
in RFC 2553. The AddrInfo
parameter comes first
to make partial application easier.
>>>
addr:_ <- getAddrInfo (Just hints) "127.0.0.1" "http"
>>>
addrAddress addr
127.0.0.1:80
type HostName = CBytes Source #
Either a host name e.g., "haskell.org"
or a numeric host
address string consisting of a dotted decimal IPv4 address or an
IPv6 address e.g., "192.168.0.1"
.
type ServiceName = CBytes Source #
Either a service name e.g., "http"
or a numeric port number.
data AddrInfoFlag Source #
Flags that control the querying behaviour of getAddrInfo
.
For more information, see https://tools.ietf.org/html/rfc3493#page-25
AI_ADDRCONFIG | The list of returned |
AI_ALL | If |
AI_CANONNAME | The |
AI_NUMERICHOST | The |
AI_NUMERICSERV | The |
AI_PASSIVE | If no |
AI_V4MAPPED | If an IPv6 lookup is performed, and no IPv6 addresses are found, IPv6-mapped IPv4 addresses will be returned. (Only some platforms support this.) |
Instances
addrInfoFlagImplemented :: AddrInfoFlag -> Bool Source #
Indicate whether the given AddrInfoFlag
will have any effect on this system.
addrInfoFlagMapping :: [(AddrInfoFlag, CInt)] Source #
Address info
Instances
defaultHints :: AddrInfo Source #
Default hints for address lookup with getAddrInfo
.
>>>
addrFlags defaultHints
[]>>>
addrFamily defaultHints
AF_UNSPEC>>>
addrSocketType defaultHints
NoSocketType>>>
addrProtocol defaultHints
0
ip to name
:: [NameInfoFlag] | flags to control lookup behaviour |
-> Bool | whether to look up a hostname |
-> Bool | whether to look up a service name |
-> SocketAddr | the address to look up |
-> IO (HostName, ServiceName) |
Resolve an address to a host or service name.
This function is protocol independent.
The list of NameInfoFlag
values controls query behaviour.
If a host or service's name cannot be looked up, then the numeric form of the address or service will be returned.
If the query fails, this function throws an IO exception.
>>>
addr:_ <- getAddrInfo (Just defaultHints) "127.0.0.1" "http"
>>>
getNameInfo [NI_NUMERICHOST, NI_NUMERICSERV] True True $ addrAddress addr
("127.0.0.1", "80")
data NameInfoFlag Source #
Flags that control the querying behaviour of getNameInfo
.
For more information, see https://tools.ietf.org/html/rfc3493#page-30
NI_DGRAM | Resolve a datagram-based service name. This is required only for the few protocols that have different port numbers for their datagram-based versions than for their stream-based versions. |
NI_NAMEREQD | If the hostname cannot be looked up, an IO error is thrown. |
NI_NOFQDN | If a host is local, return only the hostname part of the FQDN. |
NI_NUMERICHOST | The name of the host is not looked up. Instead, a numeric representation of the host's address is returned. For an IPv4 address, this will be a dotted-quad string. For IPv6, it will be colon-separated hexadecimal. |
NI_NUMERICSERV | The name of the service is not looked up. Instead, a numeric representation of the service is returned. |
Instances
Eq NameInfoFlag Source # | |
Defined in Z.IO.Network.DNS (==) :: NameInfoFlag -> NameInfoFlag -> Bool # (/=) :: NameInfoFlag -> NameInfoFlag -> Bool # | |
Read NameInfoFlag Source # | |
Defined in Z.IO.Network.DNS readsPrec :: Int -> ReadS NameInfoFlag # readList :: ReadS [NameInfoFlag] # | |
Show NameInfoFlag Source # | |
Defined in Z.IO.Network.DNS showsPrec :: Int -> NameInfoFlag -> ShowS # show :: NameInfoFlag -> String # showList :: [NameInfoFlag] -> ShowS # |
nameInfoFlagMapping :: [(NameInfoFlag, CInt)] Source #