我有这段代码连接到本地石墨(实际上只是nc -l -p 2023
在localhost上运行):
getCarbonAddr :: Config -> IO SockAddr
getCarbonAddr cfg = do
let host = (graphiteHost . graphiteConfig) cfg
let port = (graphitePort . graphiteConfig) cfg
-- addrInfos <- getAddrInfo (Just defaultHints)
addrInfos <- getAddrInfo Nothing
(Just host)
(Just (show port))
putStrLn $ "addrInfos: " ++ show addrInfos
c <- case addrInfos of
(addrInfo : _) -> return (addrAddress addrInfo)
_ -> unsupportedAddressError host
return c
where
unsupportedAddressError h = ioError $ userError $
"unsupported address: " ++ h
主机和端口的配置值分别为“localhost”和2023。当我在升级到Yosemite后在我的OS X上运行它时 - 我看到以下崩溃:
addrInfos: [AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Datagram, addrProtocol = 17, addrAddress = [::1]:2023, addrCanonName = Nothing},AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Stream, addrProtocol = 6, addrAddress = [::1]:2023, addrCanonName = Nothing},AddrInfo {addrFlags = [], addrFamily = AF_INET, addrSocketType = Datagram, addrProtocol = 17, addrAddress = 127.0.0.1:2023, addrCanonName = Nothing},AddrInfo {addrFlags = [], addrFamily = AF_INET, addrSocketType = Stream, addrProtocol = 6, addrAddress = 127.0.0.1:2023, addrCanonName = Nothing},AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Datagram, addrProtocol = 17, addrAddress = [fe80::1%lo0]:2023, addrCanonName = Nothing},AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Stream, addrProtocol = 6, addrAddress = [fe80::1%lo0]:2023, addrCanonName = Nothing}]
LocalJob: connect: unsupported operation (Address family not supported by protocol family)
这对我来说似乎很奇怪,所以我决定运行这个C程序(google搜索“getaddrinfo”示例,更改主机名和端口,添加了ai_family的打印):
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>
#ifndef NI_MAXHOST
#define NI_MAXHOST 1025
#endif
int main(void)
{
struct addrinfo *result;
struct addrinfo *res;
int error;
/* resolve the domain name into a list of addresses */
error = getaddrinfo("localhost", "2023", NULL, &result);
if (error != 0)
{
fprintf(stderr, "error in getaddrinfo: %s\n", gai_strerror(error));
return EXIT_FAILURE;
}
/* loop over all returned results and do inverse lookup */
for (res = result; res != NULL; res = res->ai_next)
{
char hostname[NI_MAXHOST] = "";
error = getnameinfo(res->ai_addr, res->ai_addrlen, hostname, NI_MAXHOST, NULL, 0, 0);
if (error != 0)
{
fprintf(stderr, "error in getnameinfo: %s\n", gai_strerror(error));
continue;
}
if (*hostname != '\0')
printf("hostname: %s. ai_family: %i\n", hostname, res->ai_family);
}
freeaddrinfo(result);
return EXIT_SUCCESS;
}
启动后,我看到了这个输出:
➜ getaddrinfotest ./main
hostname: localhost. ai_family: 30
hostname: localhost. ai_family: 30
hostname: localhost. ai_family: 2
hostname: localhost. ai_family: 2
hostname: localhost. ai_family: 30
hostname: localhost. ai_family: 30
所以,ai_family 30似乎是一件奇怪的事情。据我所知socket.h sources,它是AF_TIPC协议,这是我以前从未听说过的一件非常罕见的事情。我还打开了haskell的packFamily' sources并且惊讶地看到它没有处理30的值(不知道AF_TIPC)。
我的问题是:现在最好的事情是什么?我是否正确地解决了问题? haskell应该更好地处理未知的ai家庭吗? //谢谢!
更新:我通过提示使用ipv4解决了一个问题:
addrInfos <- getAddrInfo (Just (defaultHints { addrFamily=AF_INET }))
(Just host)
(Just (show port))
但我仍然想知道如何以“正确的方式”解决这个问题。
答案 0 :(得分:3)
默认情况下,getaddrinfo
(及其Haskell绑定getAddrInfo
)返回addrinfo的链接列表,该列表可以包含IPv4地址,IPv6地址或两者的混合。遗憾的是,套接字函数不允许将IPv4套接字连接到IPv6地址,因此当您遍历地址列表时,需要创建一个正确类型的套接字:
addrinfos <- getAddrInfo Nothing (Just hostname) (Just (show port))
let first = head addrinfos
sock <- socket (addrFamily first) Stream defaultProtocol
connect sock (addrAddress first)
注意(addrFamily first)
如何传递给socket
函数,以便在正确的协议族中创建套接字。
在实际代码中,您需要遍历列表addrinfos
并尝试连接到getAddrInfo
返回的所有地址。当您这样做时,不要忘记关闭连接失败的套接字。