1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef OHOS_BASE_ADDRESS_H
17 #define OHOS_BASE_ADDRESS_H
18 
19 #include <cstddef>
20 #include <string>
21 #include <sys/socket.h>
22 
23 namespace OHOS {
24 namespace Wifi {
25 class BaseAddress {
26 public:
27     enum class FamilyType {
28         FAMILY_UNSPEC = AF_UNSPEC, /* wrong type */
29         FAMILY_INET = AF_INET,     /* ipv4 type */
30         FAMILY_INET6 = AF_INET6,   /* ipv6 type */
31     };
32 
33 public:
34     /**
35      * @Description  delete default construct method
36      *
37      * @param None
38      * @return None
39      */
40     BaseAddress() = delete;
41     /**
42      * @Description  unique construction method
43      *
44      * @param None
45      * @return None
46      */
47     BaseAddress(const std::string &ip, size_t prefixLength, FamilyType family);
48 
49     /**
50      * @Description  The == operator is reloaded to determine whether two
51                      IP addresses are the same but not the prefix length.
52                      (The string description of IPv6 in the code has only
53                      one abbreviation.).
54      *
55      * @param None
56      * @return None
57      */
58     bool operator==(const BaseAddress &) const;
59     /**
60      * @Description  destructor methods.
61      *
62      * @param None
63      * @return None
64      */
65     virtual ~BaseAddress();
66 
67     /**
68      * @Description  Check whether the current IP address is valid.
69      *
70      * @param None
71      * @return true - legal       false - Illegal
72      */
73     virtual bool IsValid() const = 0;
74 
75     /**
76      * @Description  Dump member data.
77      *
78      * @param None
79      * @return None
80      */
81     virtual void Dump() const;
82 
83     /**
84      * @Description  Obtains the IP address type.
85      *
86      * @param None
87      * @return IPV4 - the current object is an IPv4 address.
88                IPV6 - the current object is an IPv6 address.
89      */
GetFamilyType()90     inline FamilyType GetFamilyType() const
91     {
92         return family_;
93     }
94 
95     /**
96      * @Description  Obtain the address.
97      *
98      * @param None
99      * @return string - Character description address in the current object
100      */
GetAddressWithString()101     inline const std::string &GetAddressWithString() const
102     {
103         return ipAddress_;
104     }
105 
106     /**
107      * @Description  Obtains the prefix length of the current address.
108      *
109      * @param None
110      * @return size_t - prefix length
111      */
GetAddressPrefixLength()112     inline size_t GetAddressPrefixLength() const
113     {
114         return prefixLength_;
115     }
116 
117 private:
118     /**
119      * Address family, which indicates the current IP version.
120      * the length of the network prefix. The number of bits of IPv4 mask or
121      * the number of IPv6 prefixes.
122      */
123     FamilyType family_;
124     size_t prefixLength_;
125 
126     /* Description of an IPv4 or IPv6 address string */
127     std::string ipAddress_;
128 };
129 }  // namespace Wifi
130 }  // namespace OHOS
131 
132 #endif /* OHOS_BASE_ADDRESS_H */
133