blob: c8e4c4cfb83815e10e7e41cc1369e60cd72d328d [file] [log] [blame]
Erik Klined26a2c22018-05-11 19:33:19 +09001/*
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 <android-base/stringprintf.h>
20#include <arpa/inet.h>
21#include <netdb.h>
22#include <sys/socket.h>
23#include <sys/types.h>
24
25namespace android {
26
27using base::StringPrintf;
28
29namespace netdutils {
30
31std::string IPAddress::toString() const noexcept {
32 char repr[INET6_ADDRSTRLEN] = {0};
33
34 switch (mData.family) {
35 case AF_UNSPEC:
36 return "<unspecified>";
37 case AF_INET: {
38 // Address of packed member may not have correct alignment.
39 const in_addr v4 = mData.ip.v4;
40 inet_ntop(AF_INET, &v4, repr, sizeof(repr));
41 break;
42 }
43 case AF_INET6: {
44 // Address of packed member may not have correct alignment.
45 const in6_addr v6 = mData.ip.v6;
46 inet_ntop(AF_INET6, &v6, repr, sizeof(repr));
47 break;
48 }
49 default:
50 return "<unknown_family>";
51 }
52
53 if (mData.family == AF_INET6 && mData.scope_id > 0) {
54 return StringPrintf("%s%%%u", repr, mData.scope_id);
55 }
56
57 return repr;
58}
59
60bool IPAddress::forString(const std::string& repr, IPAddress* ip) {
61 const addrinfo hints = {
62 .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV,
63 };
64 addrinfo* res;
65 const int ret = getaddrinfo(repr.c_str(), nullptr, &hints, &res);
66 // TODO: move ScopedAddrinfo into libnetdutils and use it here.
67 if (ret != 0) {
68 freeaddrinfo(res);
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 freeaddrinfo(res);
90 return rval;
91}
92
93IPPrefix::IPPrefix(const IPAddress& ip, int length) : IPPrefix(ip) {
94 // Silently treat CIDR lengths like "-1" as meaning the full bit length
95 // appropriate to the address family.
96 if (length < 0) return;
97 if (length >= mData.cidrlen) return;
98
99 switch (mData.family) {
100 case AF_UNSPEC:
101 break;
102 case AF_INET: {
103 const in_addr_t mask = (length > 0) ? (~0U) << (IPV4_ADDR_BITS - length) : 0U;
104 mData.ip.v4.s_addr &= htonl(mask);
105 mData.cidrlen = static_cast<uint8_t>(length);
106 break;
107 }
108 case AF_INET6: {
109 // The byte in which this CIDR length falls.
110 const int which = (length == 0) ? 0 : length / 8;
111 const int mask = (length % 8 == 0) ? 0 : 0xff << (8 - length % 8);
112 mData.ip.v6.s6_addr[which] &= mask;
113 for (int i = which + 1; i < IPV6_ADDR_LEN; i++) {
114 mData.ip.v6.s6_addr[i] = 0U;
115 }
116 mData.cidrlen = static_cast<uint8_t>(length);
117 break;
118 }
119 default:
120 // TODO: Complain bitterly about possible data corruption?
121 return;
122 }
123}
124
125std::string IPPrefix::toString() const noexcept {
126 return StringPrintf("%s/%d", ip().toString().c_str(), mData.cidrlen);
127}
128
129std::string IPSockAddr::toString() const noexcept {
130 switch (mData.family) {
131 case AF_INET6:
132 return StringPrintf("[%s]:%u", ip().toString().c_str(), mData.port);
133 default:
134 return StringPrintf("%s:%u", ip().toString().c_str(), mData.port);
135 }
136}
137
138} // namespace netdutils
139} // namespace android