• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2025 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         DOMAIN_NAME = 3,
29     };
30 
31     NetAddress();
32 
33     NetAddress(const NetAddress &other);
34 
35     ~NetAddress() = default;
36 
37     void SetRawAddress(const std::string &address);
38 
39     void SetIpAddress(const std::string &address);
40 
41     void SetAddress(const std::string &address);
42 
43     void SetAddress(const std::string &address, bool resolveDns);
44 
45     void SetFamilyByJsValue(uint32_t family);
46 
47     void SetFamilyBySaFamily(sa_family_t family);
48 
49     void SetPort(uint16_t port);
50 
51     [[nodiscard]] const std::string &GetAddress() const;
52 
53     [[nodiscard]] uint32_t GetJsValueFamily() const;
54 
55     [[nodiscard]] sa_family_t GetSaFamily() const;
56 
57     [[nodiscard]] uint16_t GetPort() const;
58 
59     [[nodiscard]] Family GetFamily() const;
60 
61     NetAddress &operator=(const NetAddress &other);
62 
63 private:
64     void SetIpAddressInner(const std::string &address);
65 
66     std::string address_;
67 
68     Family family_;
69 
70     uint16_t port_;
71 };
72 } // namespace OHOS::NetStack::Socket
73 
74 #endif /* COMMUNICATIONNETSTACK_NET_ADDRESS_H */
75