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