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 COMMUNICATIONNETSTACK_NET_ADDRESS_H
17 #define COMMUNICATIONNETSTACK_NET_ADDRESS_H
18 
19 #include <arpa/inet.h>
20 #include <string>
21 
22 namespace OHOS::NetStack::Socket {
23 class NetAddress final {
24 public:
25     enum class Family : uint32_t {
26         IPv4 = 1,
27         IPv6 = 2,
28     };
29 
30     NetAddress();
31 
32     NetAddress(const NetAddress &other);
33 
34     ~NetAddress() = default;
35 
36     void SetRawAddress(const std::string &address);
37 
38     void SetIpAddress(const std::string &address);
39 
40     void SetAddress(const std::string &address);
41 
42     void SetFamilyByJsValue(uint32_t family);
43 
44     void SetFamilyBySaFamily(sa_family_t family);
45 
46     void SetPort(uint16_t port);
47 
48     [[nodiscard]] const std::string &GetAddress() const;
49 
50     [[nodiscard]] uint32_t GetJsValueFamily() const;
51 
52     [[nodiscard]] sa_family_t GetSaFamily() const;
53 
54     [[nodiscard]] uint16_t GetPort() const;
55 
56     [[nodiscard]] Family GetFamily() const;
57 
58     NetAddress &operator=(const NetAddress &other);
59 
60 private:
61     void SetIpAddressInner(const std::string &address);
62 
63     std::string address_;
64 
65     Family family_;
66 
67     uint16_t port_;
68 };
69 } // namespace OHOS::NetStack::Socket
70 
71 #endif /* COMMUNICATIONNETSTACK_NET_ADDRESS_H */
72