Ratan Gupta | 8804feb | 2017-05-25 10:49:57 +0530 | [diff] [blame] | 1 | #include <arpa/inet.h> |
| 2 | #include <dirent.h> |
| 3 | #include <net/if.h> |
| 4 | |
| 5 | #include <iostream> |
| 6 | #include <list> |
| 7 | #include <string> |
| 8 | #include <algorithm> |
| 9 | #include <phosphor-logging/log.hpp> |
| 10 | |
| 11 | namespace phosphor |
| 12 | { |
| 13 | namespace network |
| 14 | { |
| 15 | namespace |
| 16 | { |
| 17 | |
| 18 | using namespace phosphor::logging; |
| 19 | |
| 20 | uint8_t toV6Cidr(const std::string& subnetMask) |
| 21 | { |
| 22 | uint8_t pos = 0; |
| 23 | uint8_t prevPos = 0; |
| 24 | uint8_t cidr = 0; |
| 25 | uint16_t buff {}; |
| 26 | do |
| 27 | { |
| 28 | //subnet mask look like ffff:ffff:: |
| 29 | // or ffff:c000:: |
| 30 | pos = subnetMask.find(":", prevPos); |
| 31 | if (pos == std::string::npos) |
| 32 | { |
| 33 | break; |
| 34 | } |
| 35 | |
| 36 | auto str = subnetMask.substr(prevPos, (pos - prevPos)); |
| 37 | prevPos = pos + 1; |
| 38 | |
| 39 | // String length is 0 |
| 40 | if (!str.length()) |
| 41 | { |
| 42 | return cidr; |
| 43 | } |
| 44 | //converts it into number. |
| 45 | if (sscanf(str.c_str(), "%hx", &buff) <= 0) |
| 46 | { |
| 47 | log<level::ERR>("Invalid Mask", |
| 48 | entry("SUBNETMASK=%s", subnetMask)); |
| 49 | |
| 50 | return 0; |
| 51 | } |
| 52 | |
| 53 | // convert the number into bitset |
| 54 | // and check for how many ones are there. |
| 55 | // if we don't have all the ones then make |
| 56 | // sure that all the ones should be left justify. |
| 57 | |
| 58 | if (__builtin_popcount(buff) != 16) |
| 59 | { |
| 60 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) != __builtin_popcount(buff)) |
| 61 | { |
| 62 | log<level::ERR>("Invalid Mask", |
| 63 | entry("SUBNETMASK=%s", subnetMask)); |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | cidr += __builtin_popcount(buff); |
| 68 | return cidr; |
| 69 | } |
| 70 | |
| 71 | cidr += 16; |
| 72 | } |
| 73 | while (1); |
| 74 | |
| 75 | return cidr; |
| 76 | } |
| 77 | }// anonymous namespace |
| 78 | |
| 79 | uint8_t toCidr(int addressFamily, const std::string& subnetMask) |
| 80 | { |
| 81 | if (addressFamily == AF_INET6) |
| 82 | { |
| 83 | return toV6Cidr(subnetMask); |
| 84 | } |
| 85 | |
| 86 | uint32_t buff; |
| 87 | |
| 88 | auto rc = inet_pton(addressFamily, subnetMask.c_str(), &buff); |
| 89 | if (rc <= 0) |
| 90 | { |
| 91 | log<level::ERR>("inet_pton failed:", |
| 92 | entry("SUBNETMASK=%s", subnetMask)); |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | buff = be32toh(buff); |
| 97 | // total no of bits - total no of leading zero == total no of ones |
| 98 | if (((sizeof(buff) * 8) - (__builtin_ctz(buff))) == __builtin_popcount(buff)) |
| 99 | { |
| 100 | return __builtin_popcount(buff); |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | log<level::ERR>("Invalid Mask", |
| 105 | entry("SUBNETMASK=%s", subnetMask)); |
| 106 | return 0; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | std::string toMask(int addressFamily, uint8_t prefix) |
| 111 | { |
| 112 | if (addressFamily == AF_INET6) |
| 113 | { |
| 114 | //TODO:- conversion for v6 |
| 115 | return ""; |
| 116 | } |
| 117 | |
| 118 | if (prefix < 1 || prefix > 30) |
| 119 | { |
| 120 | log<level::ERR>("Invalid Prefix", |
| 121 | entry("PREFIX=%d", prefix)); |
| 122 | return ""; |
| 123 | } |
| 124 | /* Create the netmask from the number of bits */ |
| 125 | unsigned long mask = 0; |
| 126 | for (auto i = 0 ; i < prefix ; i++) |
| 127 | { |
| 128 | mask |= 1 << (31 - i); |
| 129 | } |
| 130 | struct in_addr netmask; |
| 131 | netmask.s_addr = htonl(mask); |
| 132 | return inet_ntoa(netmask); |
| 133 | } |
| 134 | |
| 135 | bool isLinkLocal(const std::string& address) |
| 136 | { |
| 137 | std::string linklocal = "fe80"; |
| 138 | return std::mismatch(linklocal.begin(), linklocal.end(), |
| 139 | address.begin()).first == linklocal.end() ? |
| 140 | true : false; |
| 141 | } |
| 142 | |
| 143 | }//namespace network |
| 144 | }//namespace phosphor |