1 /*
2 * Copyright (c) 2023 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_interface_config.h"
17
18 #include "net_mgr_log_wrapper.h"
19
20 namespace OHOS {
21 namespace NetManagerStandard {
22 namespace {
23 constexpr size_t MAX_INTERFACE_CONFIG_SIZE = 16;
24 constexpr const char *IFACE_LINK_UP = "up";
25 constexpr const char *IFACE_RUNNING = "running";
26 } // namespace
27
IsInterfaceUp()28 bool NetInterfaceConfiguration::IsInterfaceUp()
29 {
30 return (std::find(flags_.begin(), flags_.end(), IFACE_LINK_UP) != flags_.end());
31 }
32
IsInterfaceRunning()33 bool NetInterfaceConfiguration::IsInterfaceRunning()
34 {
35 return (std::find(flags_.begin(), flags_.end(), IFACE_RUNNING) != flags_.end());
36 }
37
Marshalling(Parcel & parcel) const38 bool NetInterfaceConfiguration::Marshalling(Parcel &parcel) const
39 {
40 if (!parcel.WriteString(ifName_)) {
41 return false;
42 }
43
44 if (!parcel.WriteString(hwAddr_)) {
45 return false;
46 }
47
48 if (!parcel.WriteString(ipv4Addr_)) {
49 return false;
50 }
51
52 if (!parcel.WriteInt32(prefixLength_)) {
53 return false;
54 }
55
56 if (!parcel.WriteInt32(static_cast<int32_t>(std::min(MAX_INTERFACE_CONFIG_SIZE, flags_.size())))) {
57 return false;
58 }
59
60 size_t size = 0;
61 for (const auto &flag : flags_) {
62 if (!parcel.WriteString(flag)) {
63 return false;
64 }
65 ++size;
66 if (size >= MAX_INTERFACE_CONFIG_SIZE) {
67 return true;
68 }
69 }
70 return true;
71 }
72
Unmarshalling(Parcel & parcel,NetInterfaceConfiguration & config)73 bool NetInterfaceConfiguration::Unmarshalling(Parcel &parcel, NetInterfaceConfiguration &config)
74 {
75 if (!parcel.ReadString(config.ifName_)) {
76 return false;
77 }
78 if (!parcel.ReadString(config.hwAddr_)) {
79 return false;
80 }
81 if (!parcel.ReadString(config.ipv4Addr_)) {
82 return false;
83 }
84 if (!parcel.ReadInt32(config.prefixLength_)) {
85 return false;
86 }
87 int32_t tmpSize = 0;
88 if (!parcel.ReadInt32(tmpSize)) {
89 return false;
90 }
91 size_t size = static_cast<size_t>(tmpSize);
92 size = (size > MAX_INTERFACE_CONFIG_SIZE) ? MAX_INTERFACE_CONFIG_SIZE : size;
93 for (size_t i = 0; i < size; i++) {
94 std::string flag;
95 if (!parcel.ReadString(flag)) {
96 return false;
97 }
98 config.flags_.push_back(flag);
99 }
100 return true;
101 }
102 } // namespace NetManagerStandard
103 } // namespace OHOS
104