xref: /aosp_15_r20/system/libbase/parsenetaddress.cpp (revision 8f0ba417480079999ba552f1087ae592091b9d02)
1*8f0ba417SAndroid Build Coastguard Worker /*
2*8f0ba417SAndroid Build Coastguard Worker  * Copyright (C) 2016 The Android Open Source Project
3*8f0ba417SAndroid Build Coastguard Worker  *
4*8f0ba417SAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*8f0ba417SAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*8f0ba417SAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*8f0ba417SAndroid Build Coastguard Worker  *
8*8f0ba417SAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*8f0ba417SAndroid Build Coastguard Worker  *
10*8f0ba417SAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*8f0ba417SAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*8f0ba417SAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*8f0ba417SAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*8f0ba417SAndroid Build Coastguard Worker  * limitations under the License.
15*8f0ba417SAndroid Build Coastguard Worker  */
16*8f0ba417SAndroid Build Coastguard Worker 
17*8f0ba417SAndroid Build Coastguard Worker #include "android-base/parsenetaddress.h"
18*8f0ba417SAndroid Build Coastguard Worker 
19*8f0ba417SAndroid Build Coastguard Worker #include <algorithm>
20*8f0ba417SAndroid Build Coastguard Worker 
21*8f0ba417SAndroid Build Coastguard Worker #include "android-base/stringprintf.h"
22*8f0ba417SAndroid Build Coastguard Worker #include "android-base/strings.h"
23*8f0ba417SAndroid Build Coastguard Worker 
24*8f0ba417SAndroid Build Coastguard Worker namespace android {
25*8f0ba417SAndroid Build Coastguard Worker namespace base {
26*8f0ba417SAndroid Build Coastguard Worker 
ParseNetAddress(const std::string & address,std::string * host,int * port,std::string * canonical_address,std::string * error)27*8f0ba417SAndroid Build Coastguard Worker bool ParseNetAddress(const std::string& address, std::string* host, int* port,
28*8f0ba417SAndroid Build Coastguard Worker                      std::string* canonical_address, std::string* error) {
29*8f0ba417SAndroid Build Coastguard Worker   host->clear();
30*8f0ba417SAndroid Build Coastguard Worker 
31*8f0ba417SAndroid Build Coastguard Worker   bool ipv6 = true;
32*8f0ba417SAndroid Build Coastguard Worker   bool saw_port = false;
33*8f0ba417SAndroid Build Coastguard Worker   size_t colons = std::count(address.begin(), address.end(), ':');
34*8f0ba417SAndroid Build Coastguard Worker   size_t dots = std::count(address.begin(), address.end(), '.');
35*8f0ba417SAndroid Build Coastguard Worker   std::string port_str;
36*8f0ba417SAndroid Build Coastguard Worker   if (address[0] == '[') {
37*8f0ba417SAndroid Build Coastguard Worker     // [::1]:123
38*8f0ba417SAndroid Build Coastguard Worker     if (address.rfind("]:") == std::string::npos) {
39*8f0ba417SAndroid Build Coastguard Worker       *error = StringPrintf("bad IPv6 address '%s'", address.c_str());
40*8f0ba417SAndroid Build Coastguard Worker       return false;
41*8f0ba417SAndroid Build Coastguard Worker     }
42*8f0ba417SAndroid Build Coastguard Worker     *host = address.substr(1, (address.find("]:") - 1));
43*8f0ba417SAndroid Build Coastguard Worker     port_str = address.substr(address.rfind("]:") + 2);
44*8f0ba417SAndroid Build Coastguard Worker     saw_port = true;
45*8f0ba417SAndroid Build Coastguard Worker   } else if (dots == 0 && colons >= 2 && colons <= 7) {
46*8f0ba417SAndroid Build Coastguard Worker     // ::1
47*8f0ba417SAndroid Build Coastguard Worker     *host = address;
48*8f0ba417SAndroid Build Coastguard Worker   } else if (colons <= 1) {
49*8f0ba417SAndroid Build Coastguard Worker     // 1.2.3.4 or some.accidental.domain.com
50*8f0ba417SAndroid Build Coastguard Worker     ipv6 = false;
51*8f0ba417SAndroid Build Coastguard Worker     std::vector<std::string> pieces = Split(address, ":");
52*8f0ba417SAndroid Build Coastguard Worker     *host = pieces[0];
53*8f0ba417SAndroid Build Coastguard Worker     if (pieces.size() > 1) {
54*8f0ba417SAndroid Build Coastguard Worker       port_str = pieces[1];
55*8f0ba417SAndroid Build Coastguard Worker       saw_port = true;
56*8f0ba417SAndroid Build Coastguard Worker     }
57*8f0ba417SAndroid Build Coastguard Worker   }
58*8f0ba417SAndroid Build Coastguard Worker 
59*8f0ba417SAndroid Build Coastguard Worker   if (host->empty()) {
60*8f0ba417SAndroid Build Coastguard Worker     *error = StringPrintf("no host in '%s'", address.c_str());
61*8f0ba417SAndroid Build Coastguard Worker     return false;
62*8f0ba417SAndroid Build Coastguard Worker   }
63*8f0ba417SAndroid Build Coastguard Worker 
64*8f0ba417SAndroid Build Coastguard Worker   if (saw_port) {
65*8f0ba417SAndroid Build Coastguard Worker     if (sscanf(port_str.c_str(), "%d", port) != 1 || *port <= 0 ||
66*8f0ba417SAndroid Build Coastguard Worker         *port > 65535) {
67*8f0ba417SAndroid Build Coastguard Worker       *error = StringPrintf("bad port number '%s' in '%s'", port_str.c_str(),
68*8f0ba417SAndroid Build Coastguard Worker                             address.c_str());
69*8f0ba417SAndroid Build Coastguard Worker       return false;
70*8f0ba417SAndroid Build Coastguard Worker     }
71*8f0ba417SAndroid Build Coastguard Worker   }
72*8f0ba417SAndroid Build Coastguard Worker 
73*8f0ba417SAndroid Build Coastguard Worker   if (canonical_address != nullptr) {
74*8f0ba417SAndroid Build Coastguard Worker     *canonical_address =
75*8f0ba417SAndroid Build Coastguard Worker         StringPrintf(ipv6 ? "[%s]:%d" : "%s:%d", host->c_str(), *port);
76*8f0ba417SAndroid Build Coastguard Worker   }
77*8f0ba417SAndroid Build Coastguard Worker 
78*8f0ba417SAndroid Build Coastguard Worker   return true;
79*8f0ba417SAndroid Build Coastguard Worker }
80*8f0ba417SAndroid Build Coastguard Worker 
81*8f0ba417SAndroid Build Coastguard Worker }  // namespace base
82*8f0ba417SAndroid Build Coastguard Worker }  // namespace android
83