util.cpp (72412c99f292d85d4c016ce1093b1462a00347cd) | util.cpp (97b5dc68bf301a5a97e0169217de2916bd91a0de) |
---|---|
1#include "config.h" 2 3#include "util.hpp" 4 5#include "config_parser.hpp" 6#include "types.hpp" 7 8#include <arpa/inet.h> --- 137 unchanged lines hidden (view full) --- 146 } 147 else if (family == AF_INET6) 148 { 149 return visitor.template operator()<AF_INET6>(); 150 } 151 throw std::invalid_argument("Invalid addr family"); 152} 153 | 1#include "config.h" 2 3#include "util.hpp" 4 5#include "config_parser.hpp" 6#include "types.hpp" 7 8#include <arpa/inet.h> --- 137 unchanged lines hidden (view full) --- 146 } 147 else if (family == AF_INET6) 148 { 149 return visitor.template operator()<AF_INET6>(); 150 } 151 throw std::invalid_argument("Invalid addr family"); 152} 153 |
154InAddrAny addrFromBuf(int addressFamily, std::string_view buf) | 154template <int family> 155typename FamilyTraits<family>::addr addrFromBuf(std::string_view buf) |
155{ | 156{ |
156 if (addressFamily == AF_INET) 157 { 158 struct in_addr ret; 159 if (buf.size() != sizeof(ret)) 160 { 161 throw std::runtime_error("Buf not in_addr sized"); 162 } 163 memcpy(&ret, buf.data(), sizeof(ret)); 164 return ret; 165 } 166 else if (addressFamily == AF_INET6) 167 { 168 struct in6_addr ret; 169 if (buf.size() != sizeof(ret)) 170 { 171 throw std::runtime_error("Buf not in6_addr sized"); 172 } 173 memcpy(&ret, buf.data(), sizeof(ret)); 174 return ret; 175 } | 157 return stdplus::raw::copyFromStrict<typename FamilyTraits<family>::addr>( 158 buf); 159} |
176 | 160 |
177 throw std::runtime_error("Unsupported address family"); | 161InAddrAny addrFromBuf(int family, std::string_view buf) 162{ 163 return familyVisit( 164 [=]<int f>() -> InAddrAny { return addrFromBuf<f>(buf); }, family); |
178} 179 180std::string toString(const struct in_addr& addr) 181{ 182 std::string ip(INET_ADDRSTRLEN, '\0'); 183 if (inet_ntop(AF_INET, &addr, ip.data(), ip.size()) == nullptr) 184 { 185 throw std::runtime_error("Failed to convert IP4 to string"); --- 383 unchanged lines hidden --- | 165} 166 167std::string toString(const struct in_addr& addr) 168{ 169 std::string ip(INET_ADDRSTRLEN, '\0'); 170 if (inet_ntop(AF_INET, &addr, ip.data(), ip.size()) == nullptr) 171 { 172 throw std::runtime_error("Failed to convert IP4 to string"); --- 383 unchanged lines hidden --- |