1/*
2 * Copyright (c) 2024 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 "netfirewall_callback_stub.h"
17#include "net_manager_constants.h"
18#include "netnative_log_wrapper.h"
19#include "netsys_ipc_interface_code.h"
20
21namespace OHOS {
22namespace NetsysNative {
23NetFirewallCallbackStub::NetFirewallCallbackStub()
24{
25    memberFuncMap_[static_cast<uint32_t>(NetFirewallfaceCode::ON_INTERCEPT)] = &NetFirewallCallbackStub::CmdOnIntercept;
26}
27
28int32_t NetFirewallCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
29    MessageOption &option)
30{
31    NETNATIVE_LOGI("Stub call start, code:[%{public}d]", code);
32    std::u16string myDescriptor = NetFirewallCallbackStub::GetDescriptor();
33    std::u16string remoteDescriptor = data.ReadInterfaceToken();
34    if (myDescriptor != remoteDescriptor) {
35        NETNATIVE_LOGE("Descriptor checked failed");
36        return NetManagerStandard::NETMANAGER_ERR_DESCRIPTOR_MISMATCH;
37    }
38
39    auto itFunc = memberFuncMap_.find(code);
40    if (itFunc != memberFuncMap_.end()) {
41        auto requestFunc = itFunc->second;
42        if (requestFunc != nullptr) {
43            return (this->*requestFunc)(data, reply);
44        }
45    }
46
47    NETNATIVE_LOGE("Stub default case, need check");
48    return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
49}
50
51int32_t NetFirewallCallbackStub::CmdOnIntercept(MessageParcel &data, MessageParcel &reply)
52{
53    sptr<NetManagerStandard::InterceptRecord> recored = NetManagerStandard::InterceptRecord::Unmarshalling(data);
54    int32_t result = OnIntercept(recored);
55    if (!reply.WriteInt32(result)) {
56        return NetManagerStandard::NETMANAGER_ERR_WRITE_REPLY_FAIL;
57    }
58    return NetManagerStandard::NETMANAGER_SUCCESS;
59}
60} // namespace NetsysNative
61} // namespace OHOS
62