Initial commit - from Precise source
[freerdp-ubuntu-pcb-backport.git] / channels / cliprdr / cliprdr_main.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol client.
3  * Clipboard Virtual Channel
4  *
5  * Copyright 2009-2011 Jay Sorg
6  * Copyright 2010-2011 Vic Lee
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *     http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <freerdp/types.h>
25 #include <freerdp/constants.h>
26 #include <freerdp/utils/memory.h>
27 #include <freerdp/utils/unicode.h>
28 #include <freerdp/utils/svc_plugin.h>
29 #include <freerdp/plugins/cliprdr.h>
30
31 #include "cliprdr_constants.h"
32 #include "cliprdr_main.h"
33 #include "cliprdr_format.h"
34
35 static const char* const CB_MSG_TYPE_STRINGS[] =
36 {
37         "",
38         "CB_MONITOR_READY",
39         "CB_FORMAT_LIST",
40         "CB_FORMAT_LIST_RESPONSE",
41         "CB_FORMAT_DATA_REQUEST",
42         "CB_FORMAT_DATA_RESPONSE",
43         "CB_TEMP_DIRECTORY",
44         "CB_CLIP_CAPS",
45         "CB_FILECONTENTS_REQUEST",
46         "CB_FILECONTENTS_RESPONSE",
47         "CB_LOCK_CLIPDATA"
48         "CB_UNLOCK_CLIPDATA"
49 };
50
51 STREAM* cliprdr_packet_new(uint16 msgType, uint16 msgFlags, uint32 dataLen)
52 {
53         STREAM* s;
54
55         s = stream_new(dataLen + 8);
56         stream_write_uint16(s, msgType);
57         stream_write_uint16(s, msgFlags);
58         /* Write actual length after the entire packet has been constructed. */
59         stream_seek(s, 4);
60
61         return s;
62 }
63
64 void cliprdr_packet_send(cliprdrPlugin* cliprdr, STREAM* s)
65 {
66         int pos;
67         uint32 dataLen;
68
69         pos = stream_get_pos(s);
70         dataLen = pos - 8;
71         stream_set_pos(s, 4);
72         stream_write_uint32(s, dataLen);
73         stream_set_pos(s, pos);
74
75         svc_plugin_send((rdpSvcPlugin*) cliprdr, s);
76 }
77
78 static void cliprdr_process_connect(rdpSvcPlugin* plugin)
79 {
80         DEBUG_CLIPRDR("connecting");
81
82         ((cliprdrPlugin*) plugin)->uniconv = freerdp_uniconv_new();
83 }
84
85 void cliprdr_print_general_capability_flags(uint32 flags)
86 {
87         printf("generalFlags (0x%08X) {\n", flags);
88
89         if (flags & CB_USE_LONG_FORMAT_NAMES)
90                 printf("\tCB_USE_LONG_FORMAT_NAMES\n");
91         if (flags & CB_STREAM_FILECLIP_ENABLED)
92                 printf("\tCB_STREAM_FILECLIP_ENABLED\n");
93         if (flags & CB_FILECLIP_NO_FILE_PATHS)
94                 printf("\tCB_FILECLIP_NO_FILE_PATHS\n");
95         if (flags & CB_CAN_LOCK_CLIPDATA)
96                 printf("\tCB_CAN_LOCK_CLIPDATA\n");
97
98         printf("}\n");
99 }
100
101 static void cliprdr_process_general_capability(cliprdrPlugin* cliprdr, STREAM* s)
102 {
103         uint32 version;
104         uint32 generalFlags;
105
106         stream_read_uint32(s, version); /* version (4 bytes) */
107         stream_read_uint32(s, generalFlags); /* generalFlags (4 bytes) */
108
109         DEBUG_CLIPRDR("Version: %d", version);
110
111 #ifdef WITH_DEBUG_CLIPRDR
112         cliprdr_print_general_capability_flags(generalFlags);
113 #endif
114
115         if (generalFlags & CB_USE_LONG_FORMAT_NAMES)
116                 cliprdr->use_long_format_names = true;
117
118         if (generalFlags & CB_STREAM_FILECLIP_ENABLED)
119                 cliprdr->stream_fileclip_enabled = true;
120
121         if (generalFlags & CB_FILECLIP_NO_FILE_PATHS)
122                 cliprdr->fileclip_no_file_paths = true;
123
124         if (generalFlags & CB_CAN_LOCK_CLIPDATA)
125                 cliprdr->can_lock_clipdata = true;
126
127         cliprdr->received_caps = true;
128 }
129
130 static void cliprdr_process_clip_caps(cliprdrPlugin* cliprdr, STREAM* s, uint16 length, uint16 flags)
131 {
132         int i;
133         uint16 lengthCapability;
134         uint16 cCapabilitiesSets;
135         uint16 capabilitySetType;
136
137         stream_read_uint16(s, cCapabilitiesSets); /* cCapabilitiesSets (2 bytes) */
138         stream_seek_uint16(s); /* pad1 (2 bytes) */
139
140         DEBUG_CLIPRDR("cCapabilitiesSets %d", cCapabilitiesSets);
141
142         for (i = 0; i < cCapabilitiesSets; i++)
143         {
144                 stream_read_uint16(s, capabilitySetType); /* capabilitySetType (2 bytes) */
145                 stream_read_uint16(s, lengthCapability); /* lengthCapability (2 bytes) */
146
147                 switch (capabilitySetType)
148                 {
149                         case CB_CAPSTYPE_GENERAL:
150                                 cliprdr_process_general_capability(cliprdr, s);
151                                 break;
152
153                         default:
154                                 DEBUG_WARN("unknown cliprdr capability set: %d", capabilitySetType);
155                                 break;
156                 }
157         }
158 }
159
160 static void cliprdr_send_clip_caps(cliprdrPlugin* cliprdr)
161 {
162         STREAM* s;
163         uint32 flags;
164
165         s = cliprdr_packet_new(CB_CLIP_CAPS, 0, 4 + CB_CAPSTYPE_GENERAL_LEN);
166
167         DEBUG_CLIPRDR("Sending Capabilities");
168
169         flags = CB_USE_LONG_FORMAT_NAMES;
170
171         stream_write_uint16(s, 1); /* cCapabilitiesSets */
172         stream_write_uint16(s, 0); /* pad1 */
173         stream_write_uint16(s, CB_CAPSTYPE_GENERAL); /* capabilitySetType */
174         stream_write_uint16(s, CB_CAPSTYPE_GENERAL_LEN); /* lengthCapability */
175         stream_write_uint32(s, CB_CAPS_VERSION_2); /* version */
176         stream_write_uint32(s, flags); /* generalFlags */
177
178         cliprdr_packet_send(cliprdr, s);
179 }
180
181 static void cliprdr_process_monitor_ready(cliprdrPlugin* cliprdr, STREAM* s, uint16 length, uint16 flags)
182 {
183         RDP_EVENT* event;
184
185         if (cliprdr->received_caps)
186                 cliprdr_send_clip_caps(cliprdr);
187
188         event = freerdp_event_new(RDP_EVENT_CLASS_CLIPRDR, RDP_EVENT_TYPE_CB_MONITOR_READY, NULL, NULL);
189         svc_plugin_send_event((rdpSvcPlugin*) cliprdr, event);
190 }
191
192 static void cliprdr_process_receive(rdpSvcPlugin* plugin, STREAM* s)
193 {
194         uint16 msgType;
195         uint16 msgFlags;
196         uint32 dataLen;
197         cliprdrPlugin* cliprdr = (cliprdrPlugin*) plugin;
198
199         stream_read_uint16(s, msgType);
200         stream_read_uint16(s, msgFlags);
201         stream_read_uint32(s, dataLen);
202
203         DEBUG_CLIPRDR("msgType: %s (%d), msgFlags: %d dataLen: %d",
204                 CB_MSG_TYPE_STRINGS[msgType], msgType, msgFlags, dataLen);
205
206         switch (msgType)
207         {
208                 case CB_CLIP_CAPS:
209                         cliprdr_process_clip_caps(cliprdr, s, dataLen, msgFlags);
210                         break;
211
212                 case CB_MONITOR_READY:
213                         cliprdr_process_monitor_ready(cliprdr, s, dataLen, msgFlags);
214                         break;
215
216                 case CB_FORMAT_LIST:
217                         cliprdr_process_format_list(cliprdr, s, dataLen, msgFlags);
218                         break;
219
220                 case CB_FORMAT_LIST_RESPONSE:
221                         cliprdr_process_format_list_response(cliprdr, s, dataLen, msgFlags);
222                         break;
223
224                 case CB_FORMAT_DATA_REQUEST:
225                         cliprdr_process_format_data_request(cliprdr, s, dataLen, msgFlags);
226                         break;
227
228                 case CB_FORMAT_DATA_RESPONSE:
229                         cliprdr_process_format_data_response(cliprdr, s, dataLen, msgFlags);
230                         break;
231
232                 default:
233                         DEBUG_WARN("unknown msgType %d", msgType);
234                         break;
235         }
236
237         stream_free(s);
238 }
239
240 static void cliprdr_process_event(rdpSvcPlugin* plugin, RDP_EVENT* event)
241 {
242         switch (event->event_type)
243         {
244                 case RDP_EVENT_TYPE_CB_FORMAT_LIST:
245                         cliprdr_process_format_list_event((cliprdrPlugin*) plugin, (RDP_CB_FORMAT_LIST_EVENT*) event);
246                         break;
247
248                 case RDP_EVENT_TYPE_CB_DATA_REQUEST:
249                         cliprdr_process_format_data_request_event((cliprdrPlugin*) plugin, (RDP_CB_DATA_REQUEST_EVENT*) event);
250                         break;
251
252                 case RDP_EVENT_TYPE_CB_DATA_RESPONSE:
253                         cliprdr_process_format_data_response_event((cliprdrPlugin*) plugin, (RDP_CB_DATA_RESPONSE_EVENT*) event);
254                         break;
255
256                 default:
257                         DEBUG_WARN("unknown event type %d", event->event_type);
258                         break;
259         }
260
261         freerdp_event_free(event);
262 }
263
264 static void cliprdr_process_terminate(rdpSvcPlugin* plugin)
265 {
266         cliprdrPlugin* cliprdr_plugin = (cliprdrPlugin*) plugin;
267
268         if (cliprdr_plugin->uniconv != NULL)
269                 freerdp_uniconv_free(cliprdr_plugin->uniconv);
270
271         xfree(plugin);
272 }
273
274 DEFINE_SVC_PLUGIN(cliprdr, "cliprdr",
275         CHANNEL_OPTION_INITIALIZED | CHANNEL_OPTION_ENCRYPT_RDP |
276         CHANNEL_OPTION_COMPRESS_RDP | CHANNEL_OPTION_SHOW_PROTOCOL)