1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#include "socket_server.h"
17f857971dSopenharmony_ci
18f857971dSopenharmony_ci#include "accesstoken_kit.h"
19f857971dSopenharmony_ci
20f857971dSopenharmony_ci#include "devicestatus_define.h"
21f857971dSopenharmony_ci#include "socket_params.h"
22f857971dSopenharmony_ci
23f857971dSopenharmony_ci#undef LOG_TAG
24f857971dSopenharmony_ci#define LOG_TAG "SocketServer"
25f857971dSopenharmony_ci
26f857971dSopenharmony_cinamespace OHOS {
27f857971dSopenharmony_cinamespace Msdp {
28f857971dSopenharmony_cinamespace DeviceStatus {
29f857971dSopenharmony_ci
30f857971dSopenharmony_ciSocketServer::SocketServer(IContext *context)
31f857971dSopenharmony_ci    : context_(context)
32f857971dSopenharmony_ci{}
33f857971dSopenharmony_ci
34f857971dSopenharmony_ciint32_t SocketServer::Enable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
35f857971dSopenharmony_ci{
36f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
37f857971dSopenharmony_ci    return RET_ERR;
38f857971dSopenharmony_ci}
39f857971dSopenharmony_ci
40f857971dSopenharmony_ciint32_t SocketServer::Disable(CallingContext &context, MessageParcel &data, MessageParcel &reply)
41f857971dSopenharmony_ci{
42f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
43f857971dSopenharmony_ci    return RET_ERR;
44f857971dSopenharmony_ci}
45f857971dSopenharmony_ci
46f857971dSopenharmony_ciint32_t SocketServer::Start(CallingContext &context, MessageParcel &data, MessageParcel &reply)
47f857971dSopenharmony_ci{
48f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
49f857971dSopenharmony_ci    return RET_ERR;
50f857971dSopenharmony_ci}
51f857971dSopenharmony_ci
52f857971dSopenharmony_ciint32_t SocketServer::Stop(CallingContext &context, MessageParcel &data, MessageParcel &reply)
53f857971dSopenharmony_ci{
54f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
55f857971dSopenharmony_ci    return RET_ERR;
56f857971dSopenharmony_ci}
57f857971dSopenharmony_ci
58f857971dSopenharmony_ciint32_t SocketServer::AddWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
59f857971dSopenharmony_ci{
60f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
61f857971dSopenharmony_ci    return RET_ERR;
62f857971dSopenharmony_ci}
63f857971dSopenharmony_ci
64f857971dSopenharmony_ciint32_t SocketServer::RemoveWatch(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
65f857971dSopenharmony_ci{
66f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
67f857971dSopenharmony_ci    return RET_ERR;
68f857971dSopenharmony_ci}
69f857971dSopenharmony_ci
70f857971dSopenharmony_ciint32_t SocketServer::SetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
71f857971dSopenharmony_ci{
72f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
73f857971dSopenharmony_ci    return RET_ERR;
74f857971dSopenharmony_ci}
75f857971dSopenharmony_ci
76f857971dSopenharmony_ciint32_t SocketServer::GetParam(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
77f857971dSopenharmony_ci{
78f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
79f857971dSopenharmony_ci    return RET_ERR;
80f857971dSopenharmony_ci}
81f857971dSopenharmony_ci
82f857971dSopenharmony_ciint32_t SocketServer::Control(CallingContext &context, uint32_t id, MessageParcel &data, MessageParcel &reply)
83f857971dSopenharmony_ci{
84f857971dSopenharmony_ci    CALL_DEBUG_ENTER;
85f857971dSopenharmony_ci    if (id != SocketAction::SOCKET_ACTION_CONNECT) {
86f857971dSopenharmony_ci        FI_HILOGE("Unsupported action");
87f857971dSopenharmony_ci        return RET_ERR;
88f857971dSopenharmony_ci    }
89f857971dSopenharmony_ci    AllocSocketPairParam param;
90f857971dSopenharmony_ci    if (!param.Unmarshalling(data)) {
91f857971dSopenharmony_ci        FI_HILOGE("AllocSocketPairParam::Unmarshalling fail");
92f857971dSopenharmony_ci        return RET_ERR;
93f857971dSopenharmony_ci    }
94f857971dSopenharmony_ci    int32_t tokenType = Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(context.tokenId);
95f857971dSopenharmony_ci    int32_t clientFd { -1 };
96f857971dSopenharmony_ci    CHKPR(context_, RET_ERR);
97f857971dSopenharmony_ci    int32_t ret = context_->GetSocketSessionManager().AllocSocketFd(
98f857971dSopenharmony_ci        param.programName, param.moduleType, tokenType, context.uid, context.pid, clientFd);
99f857971dSopenharmony_ci    if (ret != RET_OK) {
100f857971dSopenharmony_ci        FI_HILOGE("AllocSocketFd failed");
101f857971dSopenharmony_ci        if (clientFd >= 0 && close(clientFd) < 0) {
102f857971dSopenharmony_ci            FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
103f857971dSopenharmony_ci        }
104f857971dSopenharmony_ci        return RET_ERR;
105f857971dSopenharmony_ci    }
106f857971dSopenharmony_ci    AllocSocketPairReply replyData(tokenType, clientFd);
107f857971dSopenharmony_ci    if (!replyData.Marshalling(reply)) {
108f857971dSopenharmony_ci        FI_HILOGE("AllocSocketPairReply::Marshalling fail");
109f857971dSopenharmony_ci        if (close(clientFd) < 0) {
110f857971dSopenharmony_ci            FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
111f857971dSopenharmony_ci        }
112f857971dSopenharmony_ci        return RET_ERR;
113f857971dSopenharmony_ci    }
114f857971dSopenharmony_ci    if (close(clientFd) < 0) {
115f857971dSopenharmony_ci        FI_HILOGE("Close client fd failed, error:%{public}s, clientFd:%{public}d", strerror(errno), clientFd);
116f857971dSopenharmony_ci    }
117f857971dSopenharmony_ci    return RET_OK;
118f857971dSopenharmony_ci}
119f857971dSopenharmony_ci} // namespace DeviceStatus
120f857971dSopenharmony_ci} // namespace Msdp
121f857971dSopenharmony_ci} // namespace OHOS
122