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