/* * network.cpp * * This source file is part of the FoundationDB open source project * * Copyright 2013-2018 Apple Inc. and the FoundationDB project authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #include "network.h" #include "flow.h" NetworkAddress NetworkAddress::parse( std::string const& s ) { bool isTLS = false; std::string f; if( s.size() > 4 && strcmp(s.c_str() + s.size() - 4, ":tls") == 0 ) { isTLS = true; f = s.substr(0, s.size() - 4); } else f = s; int a,b,c,d,port,count=-1; if (sscanf(f.c_str(), "%d.%d.%d.%d:%d%n", &a,&b,&c,&d, &port, &count)<5 || count != f.size()) throw connection_string_invalid(); return NetworkAddress( (a<<24)+(b<<16)+(c<<8)+d, port, true, isTLS ); } std::vector NetworkAddress::parseList( std::string const& addrs ) { // Split addrs on ',' and parse them individually std::vector coord; for(int p = 0; p <= addrs.size(); ) { int pComma = addrs.find_first_of(',', p); if (pComma == addrs.npos) pComma = addrs.size(); NetworkAddress parsedAddress = NetworkAddress::parse( addrs.substr(p, pComma-p) ); coord.push_back( parsedAddress ); p = pComma + 1; } return coord; } std::string NetworkAddress::toString() const { return format( "%d.%d.%d.%d:%d%s", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff, port, isTLS() ? ":tls" : "" ); } std::string toIPString(uint32_t ip) { return format( "%d.%d.%d.%d", (ip>>24)&0xff, (ip>>16)&0xff, (ip>>8)&0xff, ip&0xff ); } std::string toIPVectorString(std::vector ips) { std::string output; const char* space = ""; for (auto ip : ips) { output += format("%s%d.%d.%d.%d", space, (ip >> 24) & 0xff, (ip >> 16) & 0xff, (ip >> 8) & 0xff, ip & 0xff); space = " "; } return output; }