OLD | NEW |
---|---|
(Empty) | |
1 /* | |
2 * Copyright 2015 The WebRTC Project Authors. All rights reserved. | |
pthatcher1
2015/12/18 19:51:21
The pattern for other mac-specific files is macfoo
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <net/if.h> | |
12 #include <sys/ioctl.h> | |
13 #include <unistd.h> | |
14 | |
15 #include "webrtc/base/checks.h" | |
16 #include "webrtc/base/ifaddrs_converter.h" | |
17 #include "webrtc/base/scoped_ptr.h" | |
18 | |
19 #if !defined(WEBRTC_IOS) | |
20 #include <net/if_media.h> | |
21 #include <netinet/in_var.h> | |
22 #else // WEBRTC_IOS | |
23 #define SCOPE6_ID_MAX 16 | |
24 | |
25 struct in6_addrlifetime { | |
26 time_t ia6t_expire; /* valid lifetime expiration time */ | |
27 time_t ia6t_preferred; /* preferred lifetime expiration time */ | |
28 u_int32_t ia6t_vltime; /* valid lifetime */ | |
29 u_int32_t ia6t_pltime; /* prefix lifetime */ | |
30 }; | |
31 | |
32 struct in6_ifstat { | |
33 u_quad_t ifs6_in_receive; /* # of total input datagram */ | |
34 u_quad_t ifs6_in_hdrerr; /* # of datagrams with invalid hdr */ | |
35 u_quad_t ifs6_in_toobig; /* # of datagrams exceeded MTU */ | |
36 u_quad_t ifs6_in_noroute; /* # of datagrams with no route */ | |
37 u_quad_t ifs6_in_addrerr; /* # of datagrams with invalid dst */ | |
38 u_quad_t ifs6_in_protounknown; /* # of datagrams with unknown proto */ | |
39 /* NOTE: increment on final dst if */ | |
40 u_quad_t ifs6_in_truncated; /* # of truncated datagrams */ | |
41 u_quad_t ifs6_in_discard; /* # of discarded datagrams */ | |
42 /* NOTE: fragment timeout is not here */ | |
43 u_quad_t ifs6_in_deliver; /* # of datagrams delivered to ULP */ | |
44 /* NOTE: increment on final dst if */ | |
45 u_quad_t ifs6_out_forward; /* # of datagrams forwarded */ | |
46 /* NOTE: increment on outgoing if */ | |
47 u_quad_t ifs6_out_request; /* # of outgoing datagrams from ULP */ | |
48 /* NOTE: does not include forwrads */ | |
49 u_quad_t ifs6_out_discard; /* # of discarded datagrams */ | |
50 u_quad_t ifs6_out_fragok; /* # of datagrams fragmented */ | |
51 u_quad_t ifs6_out_fragfail; /* # of datagrams failed on fragment */ | |
52 u_quad_t ifs6_out_fragcreat; /* # of fragment datagrams */ | |
53 /* NOTE: this is # after fragment */ | |
54 u_quad_t ifs6_reass_reqd; /* # of incoming fragmented packets */ | |
55 /* NOTE: increment on final dst if */ | |
56 u_quad_t ifs6_reass_ok; /* # of reassembled packets */ | |
57 /* NOTE: this is # after reass */ | |
58 /* NOTE: increment on final dst if */ | |
59 u_quad_t ifs6_reass_fail; /* # of reass failures */ | |
60 /* NOTE: may not be packet count */ | |
61 /* NOTE: increment on final dst if */ | |
62 u_quad_t ifs6_in_mcast; /* # of inbound multicast datagrams */ | |
63 u_quad_t ifs6_out_mcast; /* # of outbound multicast datagrams */ | |
64 }; | |
65 struct icmp6_ifstat { | |
66 /* | |
67 * Input statistics | |
68 */ | |
69 /* ipv6IfIcmpInMsgs, total # of input messages */ | |
70 u_quad_t ifs6_in_msg; | |
71 /* ipv6IfIcmpInErrors, # of input error messages */ | |
72 u_quad_t ifs6_in_error; | |
73 /* ipv6IfIcmpInDestUnreachs, # of input dest unreach errors */ | |
74 u_quad_t ifs6_in_dstunreach; | |
75 /* ipv6IfIcmpInAdminProhibs, # of input admin. prohibited errs */ | |
76 u_quad_t ifs6_in_adminprohib; | |
77 /* ipv6IfIcmpInTimeExcds, # of input time exceeded errors */ | |
78 u_quad_t ifs6_in_timeexceed; | |
79 /* ipv6IfIcmpInParmProblems, # of input parameter problem errors */ | |
80 u_quad_t ifs6_in_paramprob; | |
81 /* ipv6IfIcmpInPktTooBigs, # of input packet too big errors */ | |
82 u_quad_t ifs6_in_pkttoobig; | |
83 /* ipv6IfIcmpInEchos, # of input echo requests */ | |
84 u_quad_t ifs6_in_echo; | |
85 /* ipv6IfIcmpInEchoReplies, # of input echo replies */ | |
86 u_quad_t ifs6_in_echoreply; | |
87 /* ipv6IfIcmpInRouterSolicits, # of input router solicitations */ | |
88 u_quad_t ifs6_in_routersolicit; | |
89 /* ipv6IfIcmpInRouterAdvertisements, # of input router advertisements */ | |
90 u_quad_t ifs6_in_routeradvert; | |
91 /* ipv6IfIcmpInNeighborSolicits, # of input neighbor solicitations */ | |
92 u_quad_t ifs6_in_neighborsolicit; | |
93 /* ipv6IfIcmpInNeighborAdvertisements, # of input neighbor advs. */ | |
94 u_quad_t ifs6_in_neighboradvert; | |
95 /* ipv6IfIcmpInRedirects, # of input redirects */ | |
96 u_quad_t ifs6_in_redirect; | |
97 /* ipv6IfIcmpInGroupMembQueries, # of input MLD queries */ | |
98 u_quad_t ifs6_in_mldquery; | |
99 /* ipv6IfIcmpInGroupMembResponses, # of input MLD reports */ | |
100 u_quad_t ifs6_in_mldreport; | |
101 /* ipv6IfIcmpInGroupMembReductions, # of input MLD done */ | |
102 u_quad_t ifs6_in_mlddone; | |
103 | |
104 /* | |
105 * Output statistics. We should solve unresolved routing problem... | |
106 */ | |
107 /* ipv6IfIcmpOutMsgs, total # of output messages */ | |
108 u_quad_t ifs6_out_msg; | |
109 /* ipv6IfIcmpOutErrors, # of output error messages */ | |
110 u_quad_t ifs6_out_error; | |
111 /* ipv6IfIcmpOutDestUnreachs, # of output dest unreach errors */ | |
112 u_quad_t ifs6_out_dstunreach; | |
113 /* ipv6IfIcmpOutAdminProhibs, # of output admin. prohibited errs */ | |
114 u_quad_t ifs6_out_adminprohib; | |
115 /* ipv6IfIcmpOutTimeExcds, # of output time exceeded errors */ | |
116 u_quad_t ifs6_out_timeexceed; | |
117 /* ipv6IfIcmpOutParmProblems, # of output parameter problem errors */ | |
118 u_quad_t ifs6_out_paramprob; | |
119 /* ipv6IfIcmpOutPktTooBigs, # of output packet too big errors */ | |
120 u_quad_t ifs6_out_pkttoobig; | |
121 /* ipv6IfIcmpOutEchos, # of output echo requests */ | |
122 u_quad_t ifs6_out_echo; | |
123 /* ipv6IfIcmpOutEchoReplies, # of output echo replies */ | |
124 u_quad_t ifs6_out_echoreply; | |
125 /* ipv6IfIcmpOutRouterSolicits, # of output router solicitations */ | |
126 u_quad_t ifs6_out_routersolicit; | |
127 /* ipv6IfIcmpOutRouterAdvertisements, # of output router advs. */ | |
128 u_quad_t ifs6_out_routeradvert; | |
129 /* ipv6IfIcmpOutNeighborSolicits, # of output neighbor solicitations */ | |
130 u_quad_t ifs6_out_neighborsolicit; | |
131 /* ipv6IfIcmpOutNeighborAdvertisements, # of output neighbor advs. */ | |
132 u_quad_t ifs6_out_neighboradvert; | |
133 /* ipv6IfIcmpOutRedirects, # of output redirects */ | |
134 u_quad_t ifs6_out_redirect; | |
135 /* ipv6IfIcmpOutGroupMembQueries, # of output MLD queries */ | |
136 u_quad_t ifs6_out_mldquery; | |
137 /* ipv6IfIcmpOutGroupMembResponses, # of output MLD reports */ | |
138 u_quad_t ifs6_out_mldreport; | |
139 /* ipv6IfIcmpOutGroupMembReductions, # of output MLD done */ | |
140 u_quad_t ifs6_out_mlddone; | |
141 }; | |
142 | |
143 struct in6_ifreq { | |
144 char ifr_name[IFNAMSIZ]; | |
145 union { | |
146 struct sockaddr_in6 ifru_addr; | |
147 struct sockaddr_in6 ifru_dstaddr; | |
148 int ifru_flags; | |
149 int ifru_flags6; | |
150 int ifru_metric; | |
151 int ifru_intval; | |
152 caddr_t ifru_data; | |
153 struct in6_addrlifetime ifru_lifetime; | |
154 struct in6_ifstat ifru_stat; | |
155 struct icmp6_ifstat ifru_icmp6stat; | |
156 u_int32_t ifru_scope_id[SCOPE6_ID_MAX]; | |
157 } ifr_ifru; | |
158 }; | |
159 | |
160 #define SIOCGIFAFLAG_IN6 _IOWR('i', 73, struct in6_ifreq) | |
161 | |
162 #define IN6_IFF_ANYCAST 0x0001 /* anycast address */ | |
163 #define IN6_IFF_TENTATIVE 0x0002 /* tentative address */ | |
164 #define IN6_IFF_DUPLICATED 0x0004 /* DAD detected duplicate */ | |
165 #define IN6_IFF_DETACHED 0x0008 /* may be detached from the link */ | |
166 #define IN6_IFF_DEPRECATED 0x0010 /* deprecated address */ | |
167 #define IN6_IFF_TEMPORARY 0x0080 /* temporary (anonymous) address. */ | |
168 | |
169 #endif // WEBRTC_IOS | |
170 | |
171 namespace rtc { | |
172 | |
173 namespace { | |
174 | |
175 class IPAttributesGetter { | |
176 public: | |
177 IPAttributesGetter(); | |
178 virtual ~IPAttributesGetter(); | |
179 bool IsInitialized() const; | |
180 bool GetIPAttributes(const char* ifname, | |
181 const sockaddr* sock_addr, | |
182 int* native_attributes); | |
183 | |
184 private: | |
185 int ioctl_socket_; | |
186 }; | |
187 | |
188 IPAttributesGetter::IPAttributesGetter() | |
189 : ioctl_socket_(socket(AF_INET6, SOCK_DGRAM, 0)) { | |
pthatcher1
2015/12/18 19:51:21
Should the class be called Ipv6AttributesGetter, s
pthatcher1
2015/12/18 19:51:21
Can you leave a comment explaining what the 0 mean
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
190 RTC_DCHECK_GE(ioctl_socket_, 0); | |
191 } | |
192 | |
193 bool IPAttributesGetter::IsInitialized() const { | |
194 return ioctl_socket_ >= 0; | |
195 } | |
196 | |
197 IPAttributesGetter::~IPAttributesGetter() { | |
198 if (ioctl_socket_ >= 0) { | |
199 close(ioctl_socket_); | |
200 } | |
pthatcher1
2015/12/18 19:51:21
Maybe:
if (!initialized()) {
return;
}
close(io
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
201 } | |
202 | |
203 bool IPAttributesGetter::GetIPAttributes(const char* ifname, | |
204 const sockaddr* sock_addr, | |
205 int* native_attributes) { | |
pthatcher1
2015/12/18 19:51:20
Should we check that it's initialized?
if (!initi
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
206 struct in6_ifreq ifr = {}; | |
207 strncpy(ifr.ifr_name, ifname, sizeof(ifr.ifr_name) - 1); | |
208 memcpy(&ifr.ifr_ifru.ifru_addr, sock_addr, sock_addr->sa_len); | |
209 int rv = ioctl(ioctl_socket_, SIOCGIFAFLAG_IN6, &ifr); | |
210 if (rv >= 0) { | |
211 *native_attributes = ifr.ifr_ifru.ifru_flags; | |
pthatcher1
2015/12/18 19:51:20
Why not just do the conversion here?
guoweis_webrtc
2015/12/21 20:26:33
I copied the code from chrome. In chrome, each pla
| |
212 } | |
213 return (rv >= 0); | |
214 } | |
215 | |
216 // When returning true, the platform native IPv6 address attributes were | |
217 // successfully converted to net IP address attributes. Otherwise, returning | |
218 // false and the caller should drop the IP address which can't be used by the | |
219 // application layer. | |
pthatcher1
2015/12/18 19:51:21
This would be more clear as "Converts native IPv6
| |
220 bool TryConvertNativeToNetIPAttributes(int native_attributes, | |
221 int* net_attributes) { | |
pthatcher1
2015/12/18 19:51:21
I don't think you need the "try" in the name here.
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
222 // For MacOSX, we disallow addresses with attributes IN6_IFF_ANYCASE, | |
223 // IN6_IFF_DUPLICATED, IN6_IFF_TENTATIVE, and IN6_IFF_DETACHED as these are | |
224 // still progressing through duplicated address detection (DAD) or are not | |
225 // suitable to be used in an one-to-one communication and shouldn't be used | |
226 // by the application layer. | |
pthatcher1
2015/12/18 19:51:21
The last part might be more clear if shorter: "not
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
227 if (native_attributes & (IN6_IFF_ANYCAST | IN6_IFF_DUPLICATED | | |
228 IN6_IFF_TENTATIVE | IN6_IFF_DETACHED)) { | |
229 return false; | |
pthatcher1
2015/12/18 19:51:21
Would it make sense to make a flag set called "uns
guoweis_webrtc
2015/12/21 20:26:33
From the readability point of view, I think they a
| |
230 } | |
231 | |
232 if (native_attributes & IN6_IFF_TEMPORARY) { | |
233 *net_attributes |= IPV6_ADDRESS_FLAG_TEMPORARY; | |
234 } | |
235 | |
236 if (native_attributes & IN6_IFF_DEPRECATED) { | |
237 *net_attributes |= IPV6_ADDRESS_FLAG_DEPRECATED; | |
238 } | |
239 | |
240 return true; | |
241 } | |
242 | |
243 class IfAddrsConverterMac : public IfAddrsConverter { | |
pthatcher1
2015/12/18 19:51:21
The current pattern for mac-specific classes is cu
guoweis_webrtc
2015/12/21 20:26:33
Done.
| |
244 public: | |
245 IfAddrsConverterMac() : ip_attribute_getter_(new IPAttributesGetter()) {} | |
246 ~IfAddrsConverterMac() override {} | |
247 | |
248 bool ConvertNativeToIPAttributes(const struct ifaddrs* interface, | |
pthatcher1
2015/12/18 19:51:21
It seems like we could make this structure more si
guoweis_webrtc
2015/12/21 20:26:33
This might not always be true. For example, the vm
| |
249 int* ip_attributes) override { | |
250 int native_attributes; | |
251 if (!ip_attribute_getter_->GetIPAttributes( | |
252 interface->ifa_name, interface->ifa_addr, &native_attributes)) { | |
253 return false; | |
254 } | |
pthatcher1
2015/12/18 19:51:21
Seeing that this is really about *getting* attribu
guoweis_webrtc
2015/12/21 20:26:33
I guess this comment is related to the one above?
| |
255 | |
256 if (!TryConvertNativeToNetIPAttributes(native_attributes, ip_attributes)) { | |
257 return false; | |
258 } | |
259 | |
260 return true; | |
261 } | |
262 | |
263 private: | |
264 rtc::scoped_ptr<IPAttributesGetter> ip_attribute_getter_; | |
265 }; | |
266 | |
267 } // namespace | |
268 | |
269 IfAddrsConverter* CreateIfAddrsConverter() { | |
270 return new IfAddrsConverterMac(); | |
271 } | |
272 | |
273 } // namespace rtc | |
OLD | NEW |