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