1 /* 2 * Copyright (C) 2021 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 #ifndef HDC_FORWARD_H 16 #define HDC_FORWARD_H 17 #include "common.h" 18 19 namespace Hdc { 20 class HdcForwardBase : public HdcTaskBase { 21 public: 22 explicit HdcForwardBase(HTaskInfo hTaskInfo); 23 virtual ~HdcForwardBase(); 24 bool CommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize) override; 25 bool BeginForward(const string &command, string &sError); 26 void StopTask() override; 27 bool ReadyForRelease() override; 28 29 protected: 30 enum FORWARD_TYPE { 31 FORWARD_TCP, 32 FORWARD_DEVICE, 33 FORWARD_JDWP, 34 FORWARD_ABSTRACT, 35 FORWARD_RESERVED, 36 FORWARD_FILESYSTEM, 37 }; 38 struct ContextForward { 39 FORWARD_TYPE type; 40 bool masterSlave; 41 bool checkPoint; 42 bool ready; 43 bool finish; 44 int fd; 45 uint32_t id; 46 uv_tcp_t tcp; 47 uv_pipe_t pipe; 48 HdcFileDescriptor *fdClass; 49 HdcForwardBase *thisClass; 50 string path; 51 string lastError; 52 string localArgs[2]; 53 string remoteArgs[2]; 54 string remoteParamenters; 55 }; 56 using HCtxForward = struct ContextForward *; 57 struct ContextForwardIO { 58 HCtxForward ctxForward; 59 uint8_t *bufIO; 60 }; 61 SetupJdwpPoint(HCtxForward ctxPoint)62 virtual bool SetupJdwpPoint(HCtxForward ctxPoint) 63 { 64 return false; 65 } 66 bool SetupPointContinue(HCtxForward ctx, int status); 67 68 private: 69 static void ListenCallback(uv_stream_t *server, const int status); 70 static void ConnectTarget(uv_connect_t *connection, int status); 71 static void ReadForwardBuf(uv_stream_t *stream, ssize_t nread, const uv_buf_t *buf); 72 static void AllocForwardBuf(uv_handle_t *handle, size_t sizeSuggested, uv_buf_t *buf); 73 static void SendCallbackForwardBuf(uv_write_t *req, int status); 74 static void OnFdRead(uv_fs_t *req); 75 76 bool SetupPoint(HCtxForward ctxPoint); 77 void *MallocContext(bool masterSlave); 78 bool SlaveConnect(uint8_t *bufCmd, bool bCheckPoint, string &sError); 79 bool SendToTask(const uint32_t cid, const uint16_t command, uint8_t *bufPtr, const int bufSize); 80 bool FilterCommand(uint8_t *bufCmdIn, uint32_t *idOut, uint8_t **pContentBuf); 81 void *AdminContext(const uint8_t op, const uint32_t id, HCtxForward hInput); 82 bool DoForwardBegin(HCtxForward ctx); 83 int SendForwardBuf(HCtxForward ctx, uint8_t *bufPtr, const int size); 84 bool CheckNodeInfo(const char *nodeInfo, string as[2]); 85 void FreeContext(HCtxForward ctxIn, const uint32_t id, bool bNotifyRemote); 86 int LoopFdRead(HCtxForward ctx); 87 void FreeContextCallBack(HCtxForward ctx); 88 void FreeJDWP(HCtxForward ctx); 89 void OnAccept(uv_stream_t *server, HCtxForward ctxClient, uv_stream_t *client); 90 bool DetechForwardType(HCtxForward ctxPoint); 91 bool SetupTCPPoint(HCtxForward ctxPoint); 92 bool SetupDevicePoint(HCtxForward ctxPoint); 93 bool SetupFilePoint(HCtxForward ctxPoint); 94 bool ForwardCommandDispatch(const uint16_t command, uint8_t *payload, const int payloadSize); 95 bool CommandForwardCheckResult(HCtxForward ctx, uint8_t *payload); 96 bool LocalAbstractConnect(uv_pipe_t *pipe, string &sNodeCfg); 97 98 map<uint32_t, HCtxForward> mapCtxPoint; 99 string taskCommand; 100 const uint8_t forwardParameterBufSize = 8; 101 const string filesystemSocketPrefix = "/tmp/"; 102 const string harmonyReservedSocketPrefix = "/dev/socket/"; 103 // set true to enable slave check when forward create 104 const bool slaveCheckWhenBegin = false; 105 std::mutex ctxPointMutex; 106 std::mutex ctxFreeMutex; 107 }; 108 } // namespace Hdc 109 #endif