1 /*
2  * Copyright (c) 2024 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 #ifndef WIFI_DIRECT_IPV4_INFO_H
16 #define WIFI_DIRECT_IPV4_INFO_H
17 
18 #include "serializable.h"
19 
20 namespace OHOS::SoftBus {
21 static constexpr int DEFAULT_PREFIX_VALUE = 24;
22 static constexpr int SUBNET_SHIFT = 8;
23 
24 class Ipv4Info {
25 public:
26     Ipv4Info() = default;
27     explicit Ipv4Info(const std::string &ip);
28     ~Ipv4Info() = default;
29     bool operator==(const Ipv4Info &other) const;
30 
31     int Marshalling(std::vector<uint8_t> &output) const;
32     int Unmarshalling(const uint8_t *input, size_t size);
33 
34     int FromIpString(const std::string &ipString);
35     std::string ToIpString() const;
36     uint32_t GetSubNet() const;
SetIp(uint32_t ip)37     void SetIp(uint32_t ip)
38     {
39         ip_ = ip;
40     }
SetPrefixLength(int32_t prefixLen)41     void SetPrefixLength(int32_t prefixLen)
42     {
43         prefixLength_ = prefixLen;
44     }
GetPrefixLength()45     int32_t GetPrefixLength() const
46     {
47         return prefixLength_;
48     }
49 
Ipv4InfoSize()50     static constexpr int Ipv4InfoSize() { return sizeof(ip_) + sizeof(prefixLength_); }
51 
52 private:
53     uint32_t ip_ {}; /* net order */
54     uint8_t prefixLength_ { DEFAULT_PREFIX_VALUE };
55 };
56 }
57 #endif
58