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