Skip to content

Commit 77003ff

Browse files
committed
Fixed NAT service and modified utils function. We need to investigate if the problem persists for future transparent services
1 parent d8d1554 commit 77003ff

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

src/libs/polycube/include/polycube/services/utils.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,12 @@ namespace polycube {
2828
namespace service {
2929
namespace utils {
3030

31-
/* ip (a.b.c.d) to string and viceversa
31+
/* IP string (a.b.c.d) or IP prefix (a.b.c.d/m) to nbo uint. If (a.b.c.d/m) only IP will be processed
3232
* Number is in network byte order (nbo), i.e., big endian */
3333
uint32_t ip_string_to_nbo_uint(const std::string &ip);
34+
35+
/* IP (a.b.c.d) to string
36+
* Number is in network byte order (nbo), i.e., big endian */
3437
std::string nbo_uint_to_ip_string(uint32_t ip);
3538

3639
/* mac (aa:bb:cc:dd:ee:ff) to string and vicersa
@@ -54,7 +57,7 @@ uint64_t hex_string_to_uint(const std::string &str);
5457
std::string get_random_mac();
5558

5659
/* Take in ingress a string like 192.168.0.1/24 and return only the ip
57-
* 192.168.0.1 */
60+
* 192.168.0.1 . If no prefix it will return the same input string*/
5861
std::string get_ip_from_string(const std::string &ipv_net);
5962

6063
/* Take in ingress a string like 192.168.0.1/24 and return only the "prefix

src/libs/polycube/src/utils.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,15 @@ namespace service {
3434
namespace utils {
3535

3636
// new set of functions
37+
3738
uint32_t ip_string_to_nbo_uint(const std::string &ip) {
3839
unsigned char a[4];
3940
int last = -1;
40-
int rc = std::sscanf(ip.c_str(), "%hhu.%hhu.%hhu.%hhu%n", a + 0, a + 1, a + 2,
41+
std::string IP_address = get_ip_from_string(ip);
42+
43+
int rc = std::sscanf(IP_address.c_str(), "%hhu.%hhu.%hhu.%hhu%n", a + 0, a + 1, a + 2,
4144
a + 3, &last);
42-
if (rc != 4 || ip.size() != last)
45+
if (rc != 4 || IP_address.size() != last)
4346
throw std::runtime_error("Not an ipv4 address " + ip);
4447

4548
return uint32_t(a[3]) << 24 | uint32_t(a[2]) << 16 | uint32_t(a[1]) << 8 |
@@ -230,7 +233,7 @@ uint64_t hex_string_to_uint(const std::string &str) {
230233
std::string get_ip_from_string(const std::string &ipv_net) {
231234
size_t pos = ipv_net.find("/");
232235
if (pos == std::string::npos) {
233-
return std::string(); // throw?
236+
return ipv_net;
234237
}
235238
return ipv_net.substr(0, pos);
236239
}

src/services/pcn-nat/src/Nat.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ Nat::Nat(const std::string name, const NatJsonObject &conf)
3434
//addNattingTableList(conf.getNattingTable());
3535

3636
ParameterEventCallback cb = [&](const std::string &parameter, const std::string &value) {
37-
logger()->debug("parent IP has been updated to {}", value);
38-
external_ip_ = value;
37+
external_ip_ = utils::get_ip_from_string(value);
38+
logger()->debug("parent IP has been updated to {}", external_ip_);
3939
if (rule_->getMasquerade()->getEnabled()) {
4040
rule_->getMasquerade()->inject(utils::ip_string_to_nbo_uint(external_ip_));
4141
}

0 commit comments

Comments
 (0)