• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "hook_socket_client.h"
17 
18 #include "unix_socket_client.h"
19 
20 uint32_t g_filterSize;
21 
HookSocketClient(int pid)22 HookSocketClient::HookSocketClient(int pid) : pid_(pid)
23 {
24     unixSocketClient_ = nullptr;
25     serviceName_ = "HookService";
26     Connect(DEFAULT_UNIX_SOCKET_HOOK_PATH);
27 }
28 
~HookSocketClient()29 HookSocketClient::~HookSocketClient()
30 {
31     unixSocketClient_ = nullptr;
32     stackWriter_ = nullptr;
33 }
34 
Connect(const std::string addrname)35 bool HookSocketClient::Connect(const std::string addrname)
36 {
37     if (unixSocketClient_ != nullptr) {
38         return false;
39     }
40     unixSocketClient_ = std::make_shared<UnixSocketClient>();
41     if (!unixSocketClient_->Connect(addrname, *this)) {
42         unixSocketClient_ = nullptr;
43         return false;
44     }
45 
46     unixSocketClient_->SendHookConfig(pid_);
47     return true;
48 }
49 
ProtocolProc(SocketContext & context,uint32_t pnum,const int8_t * buf,const uint32_t size)50 bool HookSocketClient::ProtocolProc(SocketContext &context, uint32_t pnum, const int8_t *buf, const uint32_t size)
51 {
52     if (size != sizeof(uint64_t)) {
53         printf("HookSocketClient::config failed!\n");
54         return true;
55     }
56     uint64_t config = *(uint64_t *)buf;
57     uint32_t smbSize = (uint32_t)config;
58     uint32_t filterSize = config >> 32;
59 
60     smbFd_ = context.ReceiveFileDiscriptor();
61     eventFd_ = context.ReceiveFileDiscriptor();
62     g_filterSize = filterSize;
63 
64     stackWriter_ = std::make_shared<StackWriter>("hooknativesmb", smbSize, smbFd_, eventFd_);
65     return true;
66 }
67 
SendStack(const void * data,size_t size)68 bool HookSocketClient::SendStack(const void* data, size_t size)
69 {
70     if (stackWriter_ == nullptr) {
71         return false;
72     }
73 
74     stackWriter_->Write(data, size);
75     stackWriter_->Flush();
76 
77     return true;
78 }