summaryrefslogtreecommitdiff
path: root/open-vm-tools/services/plugins/dndcp/dndGuest/fileTransferRpcV4.cc
blob: b30025627f6264d9c0634b8ce7871fd6f44bd4c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*********************************************************
 * Copyright (C) 2010 VMware, Inc. All rights reserved.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation version 2.1 and no later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the Lesser GNU General Public
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
 *
 *********************************************************/

/**
 * @fileTransferRpcV4.cc --
 *
 * Implementation of common layer file transfer rpc version 4 object.
 */

#include "fileTransferRpcV4.hh"
#include "dndCPTransport.h"

extern "C" {
   #include "dndMsg.h"
   #include "dndCPMsgV4.h"
   #include "hgfsServer.h"
   #include "str.h"
   #include "util.h"

#if defined VMX86_TOOLS
   #include "debug.h"
   #define LOG(level, msg) (Debug msg)
#else
   #define LOGLEVEL_MODULE dnd
   #include "loglevel_user.h"
#endif
}


/**
 * Create transport object and register callback.
 *
 * @param[in] transport for sending/receiving messages.
 */

FileTransferRpcV4::FileTransferRpcV4(DnDCPTransport *transport)
   : mTransport(transport),
#ifdef VMX86_TOOLS
     mTransportInterface(TRANSPORT_GUEST_CONTROLLER_FT)
#else
     mTransportInterface(TRANSPORT_HOST_CONTROLLER_FT)
#endif
{
   ASSERT(mTransport);

#ifdef VMX86_TOOLS
   mUtil.Init(this, DND_CP_MSG_SRC_GUEST, DND_CP_MSG_TYPE_FT);
#else
   mUtil.Init(this, DND_CP_MSG_SRC_HOST, DND_CP_MSG_TYPE_FT);
#endif
}


/**
 * Init. Register the rpc with transport. Send a ping message to controller to
 * to let it know our version and capability.
 *
 * XXX Capability is not implemented yet.
 */

void
FileTransferRpcV4::Init(void)
{
   ASSERT(mTransport);
   mTransport->RegisterRpc(this, mTransportInterface);
}


/**
 * Sends hgfs packet to peer.
 *
 * @param[in] sessionId DnD/CopyPaste session id.
 * @param[in] packet packet data.
 * @param[in] packetSize packet size.
 *
 * @return true on success, false otherwise.
 */

bool
FileTransferRpcV4::SendHgfsPacket(uint32 sessionId,
                                  const uint8 *packet,
                                  uint32 packetSize)
{
   RpcParams params;

   memset(&params, 0, sizeof params);
   params.addrId = DEFAULT_CONNECTION_ID;
   params.cmd = FT_CMD_HGFS_REQUEST;
   params.sessionId = sessionId;

   return mUtil.SendMsg(&params, packet, packetSize);
}


/**
 * Sends hgfs reply back to peer.
 *
 * @param[in] sessionId DnD/CopyPaste session id.
 * @param[in] packet reply packet data.
 * @param[in] packetSize reply packet size.
 *
 * @return true on success, false otherwise.
 */

bool
FileTransferRpcV4::SendHgfsReply(uint32 sessionId,
                                 const uint8 *packet,
                                 uint32 packetSize)
{
   RpcParams params;

   memset(&params, 0, sizeof params);
   params.addrId = DEFAULT_CONNECTION_ID;
   params.cmd = FT_CMD_HGFS_REPLY;
   params.sessionId = sessionId;

   return mUtil.SendMsg(&params, packet, packetSize);
}


/**
 * Send a packet.
 *
 * @param[in] destId destination address id.
 * @param[in] packet
 * @param[in] length packet length
 *
 * @return true on success, false otherwise.
 */

bool
FileTransferRpcV4::SendPacket(uint32 destId,
                              const uint8 *packet,
                              size_t length)
{
   return mTransport->SendPacket(destId, mTransportInterface, packet, length);
}


/**
 * Handle a received message.
 *
 * @param[in] params parameter list for received message.
 * @param[in] binary attached binary data for received message.
 * @param[in] binarySize
 */

void
FileTransferRpcV4::HandleMsg(RpcParams *params,
                             const uint8 *binary,
                             uint32 binarySize)
{
   ASSERT(params);

   LOG(4, ("%s: Got %s[%d], sessionId %d, srcId %d, binary size %d.\n",
           __FUNCTION__, DnDCPMsgV4_LookupCmd(params->cmd), params->cmd,
           params->sessionId, params->addrId, binarySize));

   switch (params->cmd) {
   case FT_CMD_HGFS_REQUEST:
      HgfsPacketReceived.emit(params->sessionId, binary, binarySize);
      break;
   case FT_CMD_HGFS_REPLY:
      HgfsReplyReceived.emit(params->sessionId, binary, binarySize);
      break;
   case DNDCP_CMD_PING_REPLY:
      break;
   default:
      LOG(0, ("%s: Got unknown command %d.\n", __FUNCTION__, params->cmd));
      break;
   }
}


/**
 * Callback from transport layer after received a packet from srcId.
 *
 * @param[in] srcId addressId where the packet is from
 * @param[in] packet
 * @param[in] packetSize
 */

void
FileTransferRpcV4::OnRecvPacket(uint32 srcId,
                                const uint8 *packet,
                                size_t packetSize)
{
   mUtil.OnRecvPacket(srcId, packet, packetSize);
}