• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 #ifndef ANDROID_IP_PREFIX_H
18 #define ANDROID_IP_PREFIX_H
19 
20 #include <netinet/in.h>
21 
22 #include <binder/Parcelable.h>
23 #include <utils/String16.h>
24 #include <utils/StrongPointer.h>
25 
26 namespace android {
27 
28 namespace net {
29 
30 /*
31  * C++ implementation of the Java class android.net.IpPrefix
32  */
33 class IpPrefix : public Parcelable {
34 public:
35     IpPrefix() = default;
36     virtual ~IpPrefix() = default;
37     IpPrefix(const IpPrefix& prefix) = default;
38 
IpPrefix(const struct in6_addr & addr,int32_t plen)39     IpPrefix(const struct in6_addr& addr, int32_t plen):
40         mUnion(addr), mPrefixLength(plen), mIsIpv6(true) { }
41 
IpPrefix(const struct in_addr & addr,int32_t plen)42     IpPrefix(const struct in_addr& addr, int32_t plen):
43         mUnion(addr), mPrefixLength(plen), mIsIpv6(false) { }
44 
45     bool getAddressAsIn6Addr(struct in6_addr* addr) const;
46     bool getAddressAsInAddr(struct in_addr* addr) const;
47 
48     const struct in6_addr& getAddressAsIn6Addr() const;
49     const struct in_addr& getAddressAsInAddr() const;
50 
51     bool isIpv6() const;
52     bool isIpv4() const;
53 
54     int32_t getPrefixLength() const;
55 
56     void setAddress(const struct in6_addr& addr);
57     void setAddress(const struct in_addr& addr);
58 
59     void setPrefixLength(int32_t prefix);
60 
61     friend bool operator==(const IpPrefix& lhs, const IpPrefix& rhs);
62 
63     friend bool operator!=(const IpPrefix& lhs, const IpPrefix& rhs) {
64         return !(lhs == rhs);
65     }
66 
67 public:
68     // Overrides
69     status_t writeToParcel(Parcel* parcel) const override;
70     status_t readFromParcel(const Parcel* parcel) override;
71 
72 private:
73     union InternalUnion {
74         InternalUnion() = default;
InternalUnion(const struct in6_addr & addr)75         InternalUnion(const struct in6_addr &addr):mIn6Addr(addr) { };
InternalUnion(const struct in_addr & addr)76         InternalUnion(const struct in_addr &addr):mInAddr(addr) { };
77         struct in6_addr mIn6Addr;
78         struct in_addr mInAddr;
79     } mUnion;
80     int32_t mPrefixLength;
81     bool mIsIpv6;
82 };
83 
84 }  // namespace net
85 
86 }  // namespace android
87 
88 #endif  // ANDROID_IP_PREFIX_H
89