getnameinfo and getaddrinfo, which say in their types what they return and why they failed
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 5/5
- 見積もり時間
- 1週間以上
- 初心者へのやさしさ
- 35/100
- issue の種類
- 機能追加
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- haskell
- 領域
- networking
調査の方向性
Start by reading the existing getNameInfo and getAddrInfo implementations and exports in Network.Socket, along with the network package’s existing error-code patterns. Then trace how each platform returns EAI_* codes and how gai_strerror or the Winsock error table provides descriptions. Done means the new APIs return the specified typed results and GaiError values across supported platforms while the existing APIs remain compatible.
索引モデルが issue の本文から書いたものです。
説明
Since #151, a failing socket-related call, such as bind, listen or accept, can be told apart by its error code: on POSIX it is in ioe_errno, and on Windows the Winsock code is now kept there too (#631), with the IOErrorType it stands for (#632). getNameInfo and getAddrInfo were left out. They report an EAI_* code rather than an errno, so ioe_errno is Nothing, the type is always NoSuchThing, and the reason survives only in the message.
getNameInfo and getAddrInfo each have two problems: the result type does not say what comes back, and a failure loses its reason.
getNameInfo
getNameInfo :: [NameInfoFlag] -> Bool -> Bool -> SockAddr
-> IO (Maybe HostName, Maybe ServiceName)
- Result (#496). A result is
Nothingexactly when itsBoolisFalse, andJustotherwise, but the type does not say so, and callers handle aNothingthat cannot happen. - Failure (#416). Every
EAI_*code is raised as anIOException(NoSuchThing) with only a message. WithNI_NAMEREQD, a name which cannot be found is thrown rather than returned asNothing.
getAddrInfo
getAddrInfo :: GetAddrInfo t
=> Maybe AddrInfo -> Maybe HostName -> Maybe ServiceName
-> IO (t AddrInfo) -- t is [] or NonEmpty
- Result. The shape is chosen through a hidden class, kept for backward compatibility. With
[], callers handle an empty list that cannot happen: getaddrinfo(3) never succeeds with no result. - Failure. The same as
getNameInfo: anEAI_*code becomes anIOExceptionwith only a message.
In both, a caller cannot tell EAI_AGAIN (try again) from EAI_NONAME (there is no such name) without parsing the message.
Proposal
Add getnameinfo and getaddrinfo, sharing one error type, and leave getNameInfo and getAddrInfo as they are. They are named exactly after the C functions because they report failures as the C functions do, by an EAI_* code; the C naming also sets them apart from the camelCase ones at a glance.
-- | An @EAI_*@ code, as getaddrinfo(3) and getnameinfo(3) return it.
-- 'EAI_SYSTEM' carries the errno it stands for.
data GaiError = GaiError CInt | EAI_SYSTEM Errno
-- POSIX
pattern EAI_AGAIN, EAI_BADFLAGS, EAI_FAIL, EAI_FAMILY, EAI_MEMORY,
EAI_NONAME, EAI_OVERFLOW, EAI_SERVICE, EAI_SOCKTYPE :: GaiError
-- not POSIX, but returned by glibc, macOS and (EAI_NODATA) Windows
pattern EAI_NODATA, EAI_ADDRFAMILY :: GaiError
{-# COMPLETE GaiError, EAI_SYSTEM #-}
-- Eq, Show showing the pattern names, and Exception
-- (displayException = gaiErrorString), so `either throwIO return` works.
-- | What the code means, for people.
gaiErrorString :: GaiError -> String
-- | What to look up, and so what comes back.
data NameInfoQuery a where
QueryHost :: NameInfoQuery HostName
QueryService :: NameInfoQuery ServiceName
QueryHostAndService :: NameInfoQuery (HostName, ServiceName)
getnameinfo :: [NameInfoFlag] -> NameInfoQuery a -> SockAddr
-> IO (Either GaiError a)
getaddrinfo :: Maybe AddrInfo -> Maybe HostName -> Maybe ServiceName
-> IO (Either GaiError (NonEmpty AddrInfo))
Use:
r <- getnameinfo [NI_NAMEREQD] QueryHost addr
case r of
Right host -> ... -- host :: HostName, no Maybe
Left EAI_NONAME -> ... -- no name for this address
Left EAI_AGAIN -> ... -- try again later
Left (EAI_SYSTEM errno) -> ... -- the system call failed with errno
Left e -> fail (gaiErrorString e)
r' <- getaddrinfo (Just hints) (Just host) (Just port)
case r' of
Right (ai :| _) -> ... -- the best address
Left EAI_AGAIN -> ... -- retry
Left e -> fail (gaiErrorString e)
Error codes
The codes differ by OS, so the patterns take their values with #const, as network's other patterns do:
| macOS | glibc | Windows | |
|---|---|---|---|
| values | positive (EAI_NONAME = 8) |
negative (EAI_NONAME = -2) |
Winsock codes (EAI_NONAME = WSAHOST_NOT_FOUND) |
EAI_SYSTEM |
yes | yes | no |
EAI_OVERFLOW |
yes | yes | no |
EAI_NODATA, EAI_ADDRFAMILY |
yes | with _GNU_SOURCE |
EAI_NODATA only |
- A code an OS lacks gets a value that is never returned there.
- A code without a pattern is kept as
GaiError n, not lost. - errno is read only for
EAI_SYSTEM; after any other code its value is unspecified. Windows has noEAI_SYSTEM: the returned code is the Winsock error itself, as inioe_errno.
gaiErrorString is pure: it looks a code up and nothing else.
- POSIX: gai_strerror(3), which returns constant strings.
- Windows: network's table of Winsock errors (
getWSErrorDescr), since the codes are Winsock codes; gai_strerror there writes to a static buffer and is not thread-safe. Codes not in the table are formatted in Haskell rather than in that function's static buffer. EAI_SYSTEM: the description of its errno, rather than gai_strerror's "System error".
Notes
- Every failure getnameinfo(3) or getaddrinfo(3) reports, an unsupported address family (
EAI_FAMILY) included, comes back asLeft; nothing about resolving a name is thrown. The only exception left is the one every function taking aSockAddrhas: aSockAddrUnixpath too long to marshal. NameInfoQueryis not in C: the arguments ofgetnameinfodiffer from getnameinfo(3) there, and its documentation will say so. It needsGADTs, which all supported compilers have.- Flags which do not apply to a query, such as
NI_NAMEREQDwithQueryService, are allowed and documented rather than ruled out by the types. - All of these are exported from
Network.Socket, next togetNameInfoandgetAddrInfo. - The API stays compatible, so this does not have to wait for v4.0 (#557).
getNameInfoandgetAddrInfowill not be deprecated: they have far too many users.- A prototype of the types, including
EAI_SYSTEMcarrying itsErrno, compiles cleanly with-Walland is used as above.
- 主要言語
- Haskell
- スター
- 371
- フォーク
- 211
- 平均マージ
- 12時間 42分
- マージ済み PR(30日)
- 26
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
haskell/network のほかの issue
-
難易度 5/5 1週間以上 初心者へのやさしさ 12/100
haskell/network#642 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
Making `gracefulClose` more robust?対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 5/5 1週間以上 初心者へのやさしさ 35/100
haskell/network#618 · コメント 10 件 ·
メンテナーはふだん 1 日以内に返信
-
v4.0オープン
難易度 5/5 1週間以上 初心者へのやさしさ 20/100
haskell/network#557 · コメント 10 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
haskell/network の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 74/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
haskell/bytestring#730 · コメント 1 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 84/100
seal-harness/seal-harness#399 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
fmidue/codeworld-tasks#124 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 62/100
reflex-frp/reflex#563 ·