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 "net_address.h" 17 18 namespace OHOS::NetStack { NetAddress()19NetAddress::NetAddress() : family_(Family::IPv4), port_(0) {} 20 SetAddress(const std::string & address)21void NetAddress::SetAddress(const std::string &address) 22 { 23 address_ = address; 24 } 25 SetFamilyByJsValue(uint32_t family)26void NetAddress::SetFamilyByJsValue(uint32_t family) 27 { 28 if (static_cast<Family>(family) == Family::IPv6) { 29 family_ = Family::IPv6; 30 } 31 } 32 SetFamilyBySaFamily(sa_family_t family)33void NetAddress::SetFamilyBySaFamily(sa_family_t family) 34 { 35 if (family == AF_INET6) { 36 family_ = Family::IPv6; 37 } 38 } 39 SetPort(uint16_t port)40void NetAddress::SetPort(uint16_t port) 41 { 42 port_ = port; 43 } 44 GetAddress() const45const std::string &NetAddress::GetAddress() const 46 { 47 return address_; 48 } 49 GetSaFamily() const50sa_family_t NetAddress::GetSaFamily() const 51 { 52 if (family_ == Family::IPv6) { 53 return AF_INET6; 54 } 55 return AF_INET; 56 } 57 GetJsValueFamily() const58uint32_t NetAddress::GetJsValueFamily() const 59 { 60 return static_cast<uint32_t>(family_); 61 } 62 GetPort() const63uint16_t NetAddress::GetPort() const 64 { 65 return port_; 66 } 67 } // namespace OHOS::NetStack 68