Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 1 | // Copyright 2019 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "arc/network/net_util.h" |
| 6 | |
Long Cheng | e4c8676 | 2019-09-24 18:52:40 +0000 | [diff] [blame] | 7 | #include <arpa/inet.h> |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 8 | |
| 9 | namespace arc_networkd { |
| 10 | |
| 11 | std::string IPv4AddressToString(uint32_t addr) { |
| 12 | char buf[INET_ADDRSTRLEN] = {0}; |
| 13 | struct in_addr ia; |
| 14 | ia.s_addr = addr; |
| 15 | return !inet_ntop(AF_INET, &ia, buf, sizeof(buf)) ? "" : buf; |
| 16 | } |
| 17 | |
| 18 | std::string IPv4AddressToCidrString(uint32_t addr, uint32_t prefix_length) { |
| 19 | return IPv4AddressToString(addr) + "/" + std::to_string(prefix_length); |
| 20 | } |
| 21 | |
Hugo Benichi | 2ac4d07 | 2019-05-28 14:51:23 +0900 | [diff] [blame] | 22 | } // namespace arc_networkd |