vimus / libmpd-haskell

A client library for MPD, the Music Player Daemon
MIT License
36 stars 26 forks source link

Only tries to connect to a single address #136

Open infinisil opened 2 years ago

infinisil commented 2 years ago

In https://github.com/vimus/libmpd-haskell/blob/1ec02deba33ce2a16012d8f0954e648eb4b5c485/src/Network/MPD/Core.hs#L123 only the first address out of many is tried. In case of the default host "localhost" however this returns 6 results for me:

ghci> import Network.Socket
ghci> traverse print =<< getAddrInfo (Just defaultHints) (Just "localhost") (Just "6600")
AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Stream, addrProtocol = 6, addrAddress = [::1]:6600, addrCanonName = Nothing}
AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Datagram, addrProtocol = 17, addrAddress = [::1]:6600, addrCanonName = Nothing}
AddrInfo {addrFlags = [], addrFamily = AF_INET6, addrSocketType = Raw, addrProtocol = 0, addrAddress = [::1]:6600, addrCanonName = Nothing}
AddrInfo {addrFlags = [], addrFamily = AF_INET, addrSocketType = Stream, addrProtocol = 6, addrAddress = 127.0.0.1:6600, addrCanonName = Nothing}
AddrInfo {addrFlags = [], addrFamily = AF_INET, addrSocketType = Datagram, addrProtocol = 17, addrAddress = 127.0.0.1:6600, addrCanonName = Nothing}
AddrInfo {addrFlags = [], addrFamily = AF_INET, addrSocketType = Raw, addrProtocol = 0, addrAddress = 127.0.0.1:6600, addrCanonName = Nothing}

In my case this causes it to fail to connect, supposedly because I don't have a local IPv6 interface:

$ mpc --host=::1
MPD error: Connection refused

I think this should be fixed by libmpd trying each of the returned addresses until one succeeds. This is also what libmpdclient does

It also looks like the defaultHints should be adjusted to only return ones with addrSocketType = Stream

psibi commented 2 years ago

Sounds fine. I would be happy to accept a PR for the same.