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 "socket_server.h"
17
18#include "accesstoken_kit.h"
19
20#include "devicestatus_define.h"
21#include "socket_params.h"
22
23#undef LOG_TAG
24#define LOG_TAG "SocketServer"
25
26namespace OHOS {
27namespace Msdp {
28namespace DeviceStatus {
29
30SocketServer::SocketServer(IContext *context)
31    : context_(context)
32{}
33
34int32_t SocketServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
35{
36    CALL_DEBUG_ENTER;
37    return RET_ERR;
38}
39
40int32_t SocketServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
41{
42    CALL_DEBUG_ENTER;
43    return RET_ERR;
44}
45
46int32_t SocketServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
47{
48    CALL_DEBUG_ENTER;
49    return RET_ERR;
50}
51
52int32_t SocketServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
53{
54    CALL_DEBUG_ENTER;
55    return RET_ERR;
56}
57
58int32_t SocketServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
59{
60    CALL_DEBUG_ENTER;
61    return RET_ERR;
62}
63
64int32_t SocketServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
65{
66    CALL_DEBUG_ENTER;
67    return RET_ERR;
68}
69
70int32_t SocketServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
71{
72    CALL_DEBUG_ENTER;
73    return RET_ERR;
74}
75
76int32_t SocketServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
77{
78    CALL_DEBUG_ENTER;
79    return RET_ERR;
80}
81
82int32_t SocketServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
83{
84    CALL_DEBUG_ENTER;
85    if (id != SocketAction::SOCKET_ACTION_CONNECT) {
86        FI_HILOGE("Unsupported action");
87        return RET_ERR;
88    }
89    AllocSocketPairParam param;
90    if (!param.Unmarshalling(data)) {
91        FI_HILOGE("AllocSocketPairParam::Unmarshalling fail");
92        return RET_ERR;
93    }
94    int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
95    int32_t clientFd { -1 };
96    CHKPR(context_, RET_ERR);
97    int32_t ret = context_->GetSocketSessionManager().AllocSocketFd(
98        param.programName, param.moduleType, tokenType, context.uid, context.pid, clientFd);
99    if (ret != RET_OK) {
100        FI_HILOGE("AllocSocketFd failed");
101        if (clientFd >= 0 && close(clientFd) < 0) {
102            FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
103        }
104        return RET_ERR;
105    }
106    AllocSocketPairReply replyData(tokenType, clientFd);
107    if (!replyData.Marshalling(reply)) {
108        FI_HILOGE("AllocSocketPairReply::Marshalling fail");
109        if (close(clientFd) < 0) {
110            FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
111        }
112        return RET_ERR;
113    }
114    if (close(clientFd) < 0) {
115        FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
116    }
117    return RET_OK;
118}
119} // namespace DeviceStatus
120} // namespace Msdp
121} // namespace OHOS
122