Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
U
UERANSIM
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
UERANSIM
Commits
dd821784
Commit
dd821784
authored
Jul 01, 2021
by
aligungr
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
GetIp4 improvements for network interface IP addresses.
parent
375d5382
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
51 additions
and
5 deletions
+51
-5
src/utils/io.cpp
src/utils/io.cpp
+35
-0
src/utils/io.hpp
src/utils/io.hpp
+2
-0
src/utils/yaml_utils.cpp
src/utils/yaml_utils.cpp
+14
-5
No files found.
src/utils/io.cpp
View file @
dd821784
...
...
@@ -14,7 +14,12 @@
#include <queue>
#include <stack>
#include <arpa/inet.h>
#include <dirent.h>
#include <net/if.h>
#include <netinet/in.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
...
...
@@ -183,4 +188,34 @@ void AppendPath(std::string &source, const std::string &target)
source
+=
target
;
}
std
::
string
GetIp4OfInterface
(
const
std
::
string
&
ifName
)
{
std
::
string
res
;
struct
ifreq
ifr
=
{};
int
fd
=
socket
(
AF_INET
,
SOCK_DGRAM
,
0
);
if
(
fd
<=
0
)
return
""
;
ifr
.
ifr_addr
.
sa_family
=
AF_INET
;
strncpy
(
ifr
.
ifr_name
,
ifName
.
c_str
(),
IFNAMSIZ
-
1
);
if
(
ioctl
(
fd
,
SIOCGIFADDR
,
&
ifr
))
{
close
(
fd
);
return
""
;
}
close
(
fd
);
auto
address
=
((
struct
sockaddr_in
*
)
&
ifr
.
ifr_addr
)
->
sin_addr
;
char
str
[
INET_ADDRSTRLEN
]
=
{
0
};
if
(
inet_ntop
(
AF_INET
,
&
address
,
str
,
INET_ADDRSTRLEN
)
==
nullptr
)
return
""
;
return
std
::
string
{
str
};
}
}
// namespace io
src/utils/io.hpp
View file @
dd821784
...
...
@@ -40,4 +40,6 @@ std::string GetStem(const std::string &path);
void
AppendPath
(
std
::
string
&
source
,
const
std
::
string
&
target
);
std
::
string
GetIp4OfInterface
(
const
std
::
string
&
ifName
);
}
// namespace io
src/utils/yaml_utils.cpp
View file @
dd821784
...
...
@@ -8,6 +8,7 @@
#include "yaml_utils.hpp"
#include "common.hpp"
#include "io.hpp"
#include <cctype>
#include <stdexcept>
...
...
@@ -143,11 +144,19 @@ int64_t GetInt64(const YAML::Node &node, const std::string &name, std::optional<
std
::
string
GetIp4
(
const
YAML
::
Node
&
node
,
const
std
::
string
&
name
)
{
std
::
string
ip
=
GetString
(
node
,
name
);
int
version
=
utils
::
GetIpVersion
(
ip
);
if
(
version
!=
4
)
FieldError
(
name
,
"must be a valid IPv4 address"
);
return
ip
;
std
::
string
s
=
GetString
(
node
,
name
);
int
version
=
utils
::
GetIpVersion
(
s
);
if
(
version
==
6
)
FieldError
(
name
,
"must be a valid IPv4 address or a valid network interface with a IPv4 address"
);
if
(
version
==
4
)
return
s
;
auto
ipFromIf
=
io
::
GetIp4OfInterface
(
s
);
if
(
ipFromIf
.
empty
())
FieldError
(
name
,
"must be a valid IPv4 address or a valid network interface with a IPv4 address"
);
return
ipFromIf
;
}
void
AssertHasBool
(
const
YAML
::
Node
&
node
,
const
std
::
string
&
name
)
...
...
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