Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
41b8022f
Unverified
Commit
41b8022f
authored
Feb 29, 2020
by
Igor [hyperxor]
Committed by
GitHub
Feb 28, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Code imprivements in Address class and more logs (#722)
parent
0bb01515
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
41 additions
and
42 deletions
+41
-42
src/common/net.cc
src/common/net.cc
+38
-42
tests/http_server_test.cc
tests/http_server_test.cc
+3
-0
No files found.
src/common/net.cc
View file @
41b8022f
...
...
@@ -20,24 +20,41 @@
namespace
Pistache
{
namespace
{
bool
IsIPv4HostName
(
const
std
::
string
&
host
)
{
IP
GetIPv4
(
const
std
::
string
&
host
)
{
in_addr
addr
;
char
buff
[
INET_ADDRSTRLEN
+
1
]
=
{
0
,
};
std
::
copy
(
host
.
begin
(),
host
.
end
(),
buff
);
int
res
=
inet_pton
(
AF_INET
,
buff
,
&
addr
);
return
res
;
int
res
=
inet_pton
(
AF_INET
,
host
.
c_str
(),
&
addr
);
if
(
res
==
0
)
{
throw
std
::
invalid_argument
(
"Invalid IPv4 network address"
);
}
else
if
(
res
<
0
)
{
throw
std
::
invalid_argument
(
strerror
(
errno
));
}
else
{
struct
sockaddr_in
s_addr
=
{
0
};
s_addr
.
sin_family
=
AF_INET
;
static_assert
(
sizeof
(
s_addr
.
sin_addr
.
s_addr
)
>=
sizeof
(
uint32_t
));
memcpy
(
&
(
s_addr
.
sin_addr
.
s_addr
),
&
addr
.
s_addr
,
sizeof
(
uint32_t
));
return
IP
(
reinterpret_cast
<
struct
sockaddr
*>
(
&
s_addr
));
}
}
bool
IsIPv6HostName
(
const
std
::
string
&
host
)
{
IP
GetIPv6
(
const
std
::
string
&
host
)
{
in6_addr
addr6
;
char
buff6
[
INET6_ADDRSTRLEN
+
1
]
=
{
0
,
};
std
::
copy
(
host
.
begin
(),
host
.
end
(),
buff6
);
int
res
=
inet_pton
(
AF_INET6
,
buff6
,
&
(
addr6
.
s6_addr16
));
return
res
;
int
res
=
inet_pton
(
AF_INET6
,
host
.
c_str
(),
&
(
addr6
.
s6_addr16
));
if
(
res
==
0
)
{
throw
std
::
invalid_argument
(
"Invalid IPv6 network address"
);
}
else
if
(
res
<
0
)
{
throw
std
::
invalid_argument
(
strerror
(
errno
));
}
else
{
struct
sockaddr_in6
s_addr
=
{
0
};
s_addr
.
sin6_family
=
AF_INET6
;
static_assert
(
sizeof
(
s_addr
.
sin6_addr
.
s6_addr16
)
>=
8
*
sizeof
(
uint16_t
));
memcpy
(
&
(
s_addr
.
sin6_addr
.
s6_addr16
),
&
addr6
.
s6_addr16
,
8
*
sizeof
(
uint16_t
));
return
IP
(
reinterpret_cast
<
struct
sockaddr
*>
(
&
s_addr
));
}
}
}
// namespace
...
...
@@ -267,17 +284,7 @@ void Address::init(const std::string &addr) {
assert
(
raw_host
.
size
()
>
2
);
host_
=
addr
.
substr
(
1
,
raw_host
.
size
()
-
2
);
if
(
!
IsIPv6HostName
(
host_
))
{
throw
std
::
invalid_argument
(
"Invalid IPv6 address"
);
}
struct
in6_addr
addr
;
inet_pton
(
AF_INET6
,
host_
.
c_str
(),
&
addr
);
struct
sockaddr_in6
s_addr
=
{
0
};
s_addr
.
sin6_family
=
AF_INET6
;
memcpy
(
&
(
s_addr
.
sin6_addr
.
s6_addr16
),
&
addr
.
s6_addr16
,
8
*
sizeof
(
uint16_t
));
ip_
=
IP
(
reinterpret_cast
<
struct
sockaddr
*>
(
&
s_addr
));
ip_
=
GetIPv6
(
host_
);
}
else
if
(
family_
==
AF_INET
)
{
host_
=
parser
.
rawHost
();
...
...
@@ -290,25 +297,14 @@ void Address::init(const std::string &addr) {
struct
hostent
*
hp
=
::
gethostbyname
(
host_
.
c_str
());
if
(
hp
)
{
struct
in_addr
**
addr_list
;
addr_list
=
(
struct
in_addr
**
)
hp
->
h_addr_list
;
for
(
int
i
=
0
;
addr_list
[
i
]
!=
NULL
;
i
++
)
{
// Just take the first IP address
host_
=
std
::
string
(
inet_ntoa
(
*
addr_list
[
i
]));
break
;
}
char
**
addr
=
hp
->
h_addr_list
;
while
(
*
addr
)
{
host_
=
std
::
string
(
inet_ntoa
(
*
reinterpret_cast
<
struct
in_addr
*>
(
*
addr
)));
break
;
}
}
if
(
!
IsIPv4HostName
(
host_
))
{
throw
std
::
invalid_argument
(
"Invalid IPv4 address"
);
}
struct
in_addr
addr
;
inet_pton
(
AF_INET
,
host_
.
c_str
(),
&
addr
);
struct
sockaddr_in
s_addr
=
{
0
};
s_addr
.
sin_family
=
AF_INET
;
memcpy
(
&
(
s_addr
.
sin_addr
.
s_addr
),
&
addr
.
s_addr
,
sizeof
(
uint32_t
));
ip_
=
IP
(
reinterpret_cast
<
struct
sockaddr
*>
(
&
s_addr
));
ip_
=
GetIPv4
(
host_
);
}
else
{
assert
(
false
);
}
...
...
tests/http_server_test.cc
View file @
41b8022f
...
...
@@ -136,6 +136,8 @@ TEST(http_server_test,
auto
flags
=
Tcp
::
Options
::
ReuseAddr
;
auto
server_opts
=
Http
::
Endpoint
::
options
().
flags
(
flags
);
server
.
init
(
server_opts
);
std
::
cout
<<
"Trying to run server...
\n
"
;
const
int
ONE_SECOND_TIMEOUT
=
1
;
const
int
SIX_SECONDS_DELAY
=
6
;
server
.
setHandler
(
...
...
@@ -164,6 +166,7 @@ TEST(
auto
server_opts
=
Http
::
Endpoint
::
options
().
flags
(
flags
);
server
.
init
(
server_opts
);
std
::
cout
<<
"Trying to run server...
\n
"
;
const
int
ONE_SECOND_TIMEOUT
=
1
;
const
int
SIX_SECONDS_DELAY
=
6
;
server
.
setHandler
(
...
...
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