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 #include <netdb.h>
17 #include "net_address.h"
18 #include "netstack_log.h"
19 #include "securec.h"
20
21 namespace OHOS::NetStack::Socket {
22
NetAddress()23 NetAddress::NetAddress() : family_(Family::IPv4), port_(0) {}
24
SetAddress(const std::string & address)25 void NetAddress::SetAddress(const std::string &address)
26 {
27 if (family_ == Family::IPv4) {
28 struct in_addr ipv4;
29 if (inet_pton(AF_INET, address.c_str(), &(ipv4.s_addr)) > 0) {
30 address_ = address;
31 return;
32 }
33 } else {
34 struct in6_addr ipv6;
35 if (inet_pton(AF_INET6, address.c_str(), &ipv6) > 0) {
36 address_ = address;
37 return;
38 }
39 }
40
41 struct addrinfo hints;
42 sa_family_t saFamily = GetSaFamily();
43 if (memset_s(&hints, sizeof hints, 0, sizeof hints) != EOK) {
44 NETSTACK_LOGE("memory operation fail");
45 }
46 hints.ai_family = saFamily;
47 char ipStr[INET6_ADDRSTRLEN];
48 struct addrinfo *res = nullptr;
49 int status = getaddrinfo(address.c_str(), nullptr, &hints, &res);
50 if (status != 0 || res == nullptr) {
51 NETSTACK_LOGE("getaddrinfo status is %{public}d, error is %{public}s", status, gai_strerror(status));
52 return;
53 }
54
55 void *addr = nullptr;
56 if (res->ai_family == AF_INET) {
57 auto *ipv4 = reinterpret_cast<struct sockaddr_in *>(res->ai_addr);
58 addr = &(ipv4->sin_addr);
59 } else {
60 struct sockaddr_in6 *ipv6 = reinterpret_cast<struct sockaddr_in6 *>(res->ai_addr);
61 addr = &(ipv6->sin6_addr);
62 }
63 inet_ntop(res->ai_family, addr, ipStr, sizeof ipStr);
64 address_ = ipStr;
65 freeaddrinfo(res);
66 }
67
SetFamilyByJsValue(uint32_t family)68 void NetAddress::SetFamilyByJsValue(uint32_t family)
69 {
70 if (static_cast<Family>(family) == Family::IPv6) {
71 family_ = Family::IPv6;
72 }
73 }
74
SetFamilyBySaFamily(sa_family_t family)75 void NetAddress::SetFamilyBySaFamily(sa_family_t family)
76 {
77 if (family == AF_INET6) {
78 family_ = Family::IPv6;
79 }
80 }
81
SetPort(uint16_t port)82 void NetAddress::SetPort(uint16_t port)
83 {
84 port_ = port;
85 }
86
GetAddress() const87 const std::string &NetAddress::GetAddress() const
88 {
89 return address_;
90 }
91
GetSaFamily() const92 sa_family_t NetAddress::GetSaFamily() const
93 {
94 if (family_ == Family::IPv6) {
95 return AF_INET6;
96 }
97 return AF_INET;
98 }
99
GetJsValueFamily() const100 uint32_t NetAddress::GetJsValueFamily() const
101 {
102 return static_cast<uint32_t>(family_);
103 }
104
GetPort() const105 uint16_t NetAddress::GetPort() const
106 {
107 return port_;
108 }
109 } // namespace OHOS::NetStack::Socket
110