Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
M
mpd
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Иван Мажукин
mpd
Commits
bcb7e954
Commit
bcb7e954
authored
Jan 12, 2021
by
Max Kellermann
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net/Resolver: add simple getaddrinfo() wrapper
parent
866c87c6
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
26 deletions
+29
-26
Resolver.cxx
src/net/Resolver.cxx
+21
-26
Resolver.hxx
src/net/Resolver.hxx
+8
-0
No files found.
src/net/Resolver.cxx
View file @
bcb7e954
...
@@ -49,6 +49,21 @@
...
@@ -49,6 +49,21 @@
#include <stdio.h>
#include <stdio.h>
AddressInfoList
Resolve
(
const
char
*
node
,
const
char
*
service
,
const
struct
addrinfo
*
hints
)
{
struct
addrinfo
*
ai
;
int
error
=
getaddrinfo
(
node
,
service
,
hints
,
&
ai
);
if
(
error
!=
0
)
throw
FormatRuntimeError
(
"Failed to resolve '%s':'%s': %s"
,
node
==
nullptr
?
""
:
node
,
service
==
nullptr
?
""
:
service
,
gai_strerror
(
error
));
return
AddressInfoList
(
ai
);
}
static
inline
bool
static
inline
bool
ai_is_passive
(
const
struct
addrinfo
*
ai
)
ai_is_passive
(
const
struct
addrinfo
*
ai
)
{
{
...
@@ -82,10 +97,9 @@ FindAndResolveInterfaceName(char *host, size_t size)
...
@@ -82,10 +97,9 @@ FindAndResolveInterfaceName(char *host, size_t size)
#endif
#endif
static
in
t
AddressInfoLis
t
Resolve
(
const
char
*
host_and_port
,
int
default_port
,
Resolve
(
const
char
*
host_and_port
,
int
default_port
,
const
struct
addrinfo
*
hints
,
const
struct
addrinfo
*
hints
)
struct
addrinfo
**
ai_r
)
{
{
const
char
*
host
,
*
port
;
const
char
*
host
,
*
port
;
char
buffer
[
256
],
port_string
[
16
];
char
buffer
[
256
],
port_string
[
16
];
...
@@ -93,16 +107,10 @@ Resolve(const char *host_and_port, int default_port,
...
@@ -93,16 +107,10 @@ Resolve(const char *host_and_port, int default_port,
if
(
host_and_port
!=
nullptr
)
{
if
(
host_and_port
!=
nullptr
)
{
const
auto
eh
=
ExtractHost
(
host_and_port
);
const
auto
eh
=
ExtractHost
(
host_and_port
);
if
(
eh
.
HasFailed
())
if
(
eh
.
HasFailed
())
return
EAI_NONAME
;
throw
std
::
runtime_error
(
"Failed to extract host name"
)
;
if
(
eh
.
host
.
size
>=
sizeof
(
buffer
))
{
if
(
eh
.
host
.
size
>=
sizeof
(
buffer
))
#ifdef _WIN32
throw
std
::
runtime_error
(
"Host name too long"
);
return
EAI_MEMORY
;
#else
errno
=
ENAMETOOLONG
;
return
EAI_SYSTEM
;
#endif
}
memcpy
(
buffer
,
eh
.
host
.
data
,
eh
.
host
.
size
);
memcpy
(
buffer
,
eh
.
host
.
data
,
eh
.
host
.
size
);
buffer
[
eh
.
host
.
size
]
=
0
;
buffer
[
eh
.
host
.
size
]
=
0
;
...
@@ -131,20 +139,7 @@ Resolve(const char *host_and_port, int default_port,
...
@@ -131,20 +139,7 @@ Resolve(const char *host_and_port, int default_port,
port
=
port_string
;
port
=
port_string
;
}
}
return
getaddrinfo
(
host
,
port
,
hints
,
ai_r
);
return
Resolve
(
host
,
port
,
hints
);
}
AddressInfoList
Resolve
(
const
char
*
host_and_port
,
int
default_port
,
const
struct
addrinfo
*
hints
)
{
struct
addrinfo
*
ai
;
int
result
=
Resolve
(
host_and_port
,
default_port
,
hints
,
&
ai
);
if
(
result
!=
0
)
throw
FormatRuntimeError
(
"Failed to resolve '%s': %s"
,
host_and_port
,
gai_strerror
(
result
));
return
AddressInfoList
(
ai
);
}
}
AddressInfoList
AddressInfoList
...
...
src/net/Resolver.hxx
View file @
bcb7e954
...
@@ -36,6 +36,14 @@
...
@@ -36,6 +36,14 @@
class
AddressInfoList
;
class
AddressInfoList
;
/**
/**
* Thin wrapper for getaddrinfo() which throws on error and returns a
* RAII object.
*/
AddressInfoList
Resolve
(
const
char
*
node
,
const
char
*
service
,
const
struct
addrinfo
*
hints
);
/**
* Resolve the given host name (which may include a port), and fall
* Resolve the given host name (which may include a port), and fall
* back to the given default port.
* back to the given default port.
*
*
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment