blob: f8c80dcb4d1888a4aa93318aeb7fca2d3524c3a3 [file] [log] [blame]
Robert Quattlebaum6316f5b2017-01-04 13:25:14 -08001/*
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
26namespace android {
27
28namespace net {
29
30/*
31 * C++ implementation of the Java class android.net.IpPrefix
32 */
33class IpPrefix : public Parcelable {
34public:
35 IpPrefix() = default;
36 virtual ~IpPrefix() = default;
37 IpPrefix(const IpPrefix& prefix) = default;
38
39 IpPrefix(const struct in6_addr& addr, int32_t plen):
40 mUnion(addr), mPrefixLength(plen), mIsIpv6(true) { }
41
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
67public:
68 // Overrides
69 status_t writeToParcel(Parcel* parcel) const override;
70 status_t readFromParcel(const Parcel* parcel) override;
71
72private:
73 union InternalUnion {
74 InternalUnion(const struct in6_addr &addr):mIn6Addr(addr) { };
75 InternalUnion(const struct in_addr &addr):mInAddr(addr) { };
76 struct in6_addr mIn6Addr;
77 struct in_addr mInAddr;
78 } mUnion;
79 int32_t mPrefixLength;
80 bool mIsIpv6;
81};
82
83} // namespace net
84
85} // namespace android
86
87#endif // ANDROID_IP_PREFIX_H