Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
code
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ReadMailReallyFast
code
Commits
2d1f0d2d
Commit
2d1f0d2d
authored
2 years ago
by
Benny Baumann
Browse files
Options
Downloads
Patches
Plain Diff
chg: Refactor address resolution into separate function
parent
19af90d4
No related branches found
No related tags found
1 merge request
!1
First unit tests
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/net/tcp_client.cpp
+9
-32
9 additions, 32 deletions
src/net/tcp_client.cpp
with
9 additions
and
32 deletions
src/net/tcp_client.cpp
+
9
−
32
View file @
2d1f0d2d
...
...
@@ -14,10 +14,11 @@
#include
<netinet/in.h>
#include
<sys/socket.h>
#include
<utility>
#include
<
deque
>
#include
<
list
>
#include
"net/netio_exception.hpp"
#include
"net/socketaddress.hpp"
#include
"net/sock_address_factory.hpp"
namespace
rmrf
::
net
{
...
...
@@ -69,6 +70,12 @@ tcp_client::tcp_client(
io
{},
write_queue
{}
{
std
::
list
<
socketaddr
>
connection_candidates
=
get_socketaddr_list
(
peer_address_
,
service_or_port
,
socket_t
::
TCP
);
if
(
ip_addr_family
==
AF_UNSPEC
)
{
ip_addr_family
=
connection_candidates
.
front
().
family
();
}
if
(
!
(
ip_addr_family
==
AF_INET
||
ip_addr_family
==
AF_INET6
))
{
throw
netio_exception
(
"Invalid IP address family."
);
}
...
...
@@ -80,38 +87,8 @@ tcp_client::tcp_client(
throw
netio_exception
(
"Failed to request socket fd from kernel."
);
}
// TODO Extract DNS/service resolution into separate library
// TODO build another nice HL structure wrapper for outbound connections
std
::
deque
<
socketaddr
>
connection_candidates
;
int
status
;
{
//Reduce scope of locally declared variables
//May be candidate for extraction into own method
addrinfo
hints
;
addrinfo
*
servinfo
=
nullptr
;
memset
(
&
hints
,
0
,
sizeof
hints
);
hints
.
ai_family
=
ip_addr_family
;
hints
.
ai_socktype
=
SOCK_STREAM
;
if
((
status
=
getaddrinfo
(
peer_address_
.
c_str
(),
service_or_port
.
c_str
(),
&
hints
,
&
servinfo
))
!=
0
)
{
throw
netio_exception
(
"Failed to resolve address '"
+
peer_address_
+
"' with service '"
+
service_or_port
+
"': "
+
gai_strerror
(
status
));
}
// TODO: Prefer IPv6 over IPv4
for
(
auto
p
=
servinfo
;
p
!=
NULL
;
p
=
p
->
ai_next
)
{
if
(
p
->
ai_family
==
AF_INET
)
{
socketaddr
sa
{(
sockaddr_in
*
)
p
->
ai_addr
};
connection_candidates
.
push_back
(
sa
);
}
else
if
(
p
->
ai_family
==
AF_INET6
)
{
socketaddr
sa
{(
sockaddr_in6
*
)
p
->
ai_addr
};
connection_candidates
.
push_front
(
sa
);
}
}
freeaddrinfo
(
servinfo
);
}
status
=
1
;
int
status
=
1
;
do
{
if
(
connection_candidates
.
empty
())
{
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment