1 /*
2  * Copyright (C) 2018 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 "netdutils/InternetAddresses.h"
18 
19 #include <stdlib.h>
20 #include <string>
21 
22 #include <android-base/stringprintf.h>
23 #include <arpa/inet.h>
24 #include <netdb.h>
25 #include <sys/socket.h>
26 #include <sys/types.h>
27 
28 namespace android {
29 
30 using base::StringPrintf;
31 
32 namespace netdutils {
33 
toString() const34 std::string IPAddress::toString() const noexcept {
35     char repr[INET6_ADDRSTRLEN] = "\0";
36 
37     switch (mData.family) {
38         case AF_UNSPEC:
39             return "<unspecified>";
40         case AF_INET: {
41             const in_addr v4 = mData.ip.v4;
42             inet_ntop(AF_INET, &v4, repr, sizeof(repr));
43             break;
44         }
45         case AF_INET6: {
46             const in6_addr v6 = mData.ip.v6;
47             inet_ntop(AF_INET6, &v6, repr, sizeof(repr));
48             break;
49         }
50         default:
51             return "<unknown_family>";
52     }
53 
54     if (mData.family == AF_INET6 && mData.scope_id > 0) {
55         return StringPrintf("%s%%%u", repr, mData.scope_id);
56     }
57 
58     return repr;
59 }
60 
forString(const std::string & repr,IPAddress * ip)61 bool IPAddress::forString(const std::string& repr, IPAddress* ip) {
62     const addrinfo hints = {
63             .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
64     };
65     addrinfo* res;
66     const int ret = getaddrinfo(repr.c_str(), nullptr, &hints, &res);
67     ScopedAddrinfo res_cleanup(res);
68     if (ret != 0) {
69         return false;
70     }
71 
72     bool rval = true;
73     switch (res[0].ai_family) {
74         case AF_INET: {
75             sockaddr_in* sin = (sockaddr_in*) res[0].ai_addr;
76             if (ip) *ip = IPAddress(sin->sin_addr);
77             break;
78         }
79         case AF_INET6: {
80             sockaddr_in6* sin6 = (sockaddr_in6*) res[0].ai_addr;
81             if (ip) *ip = IPAddress(sin6->sin6_addr, sin6->sin6_scope_id);
82             break;
83         }
84         default:
85             rval = false;
86             break;
87     }
88 
89     return rval;
90 }
91 
IPPrefix(const IPAddress & ip,int length)92 IPPrefix::IPPrefix(const IPAddress& ip, int length) : IPPrefix(ip) {
93     // Silently treat CIDR lengths like "-1" as meaning the full bit length
94     // appropriate to the address family.
95     if (length < 0) return;
96     if (length >= mData.cidrlen) return;
97 
98     switch (mData.family) {
99         case AF_UNSPEC:
100             break;
101         case AF_INET: {
102             const in_addr_t mask = (length > 0) ? (~0U) << (IPV4_ADDR_BITS - length) : 0U;
103             mData.ip.v4.s_addr &= htonl(mask);
104             mData.cidrlen = static_cast<uint8_t>(length);
105             break;
106         }
107         case AF_INET6: {
108             // The byte in which this CIDR length falls.
109             const int which = length / 8;
110             const int mask = (length % 8 == 0) ? 0 : 0xff << (8 - length % 8);
111             mData.ip.v6.s6_addr[which] &= mask;
112             for (int i = which + 1; i < IPV6_ADDR_LEN; i++) {
113                 mData.ip.v6.s6_addr[i] = 0U;
114             }
115             mData.cidrlen = static_cast<uint8_t>(length);
116             break;
117         }
118         default:
119             // TODO: Complain bitterly about possible data corruption?
120             return;
121     }
122 }
123 
isUninitialized() const124 bool IPPrefix::isUninitialized() const noexcept {
125     static const internal_::compact_ipdata empty{};
126     return mData == empty;
127 }
128 
forString(const std::string & repr,IPPrefix * prefix)129 bool IPPrefix::forString(const std::string& repr, IPPrefix* prefix) {
130     size_t index = repr.find('/');
131     if (index == std::string::npos) return false;
132 
133     // Parse the IP address.
134     IPAddress ip;
135     if (!IPAddress::forString(repr.substr(0, index), &ip)) return false;
136 
137     // Parse the prefix length. Can't use base::ParseUint because it accepts non-base 10 input.
138     const char* prefixString = repr.c_str() + index + 1;
139     if (!isdigit(*prefixString)) return false;
140     char* endptr;
141     unsigned long prefixlen = strtoul(prefixString, &endptr, 10);
142     if (*endptr != '\0') return false;
143 
144     uint8_t maxlen = (ip.family() == AF_INET) ? 32 : 128;
145     if (prefixlen > maxlen) return false;
146 
147     *prefix = IPPrefix(ip, prefixlen);
148     return true;
149 }
150 
toString() const151 std::string IPPrefix::toString() const noexcept {
152     return StringPrintf("%s/%d", ip().toString().c_str(), mData.cidrlen);
153 }
154 
toString() const155 std::string IPSockAddr::toString() const noexcept {
156     switch (mData.family) {
157         case AF_INET6:
158             return StringPrintf("[%s]:%u", ip().toString().c_str(), mData.port);
159         default:
160             return StringPrintf("%s:%u", ip().toString().c_str(), mData.port);
161     }
162 }
163 
164 }  // namespace netdutils
165 }  // namespace android
166