94d5d2ab7f35237c8ae8526a4111d3ba70ba6242
[platform/upstream/freerdp.git] / client / common / client.c
1 /**
2  * FreeRDP: A Remote Desktop Protocol Implementation
3  * FreeRDP Client Common
4  *
5  * Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include <freerdp/client.h>
25
26 #include <freerdp/addin.h>
27 #include <freerdp/assistance.h>
28 #include <freerdp/client/file.h>
29 #include <freerdp/client/cmdline.h>
30 #include <freerdp/client/channels.h>
31
32 int freerdp_client_common_new(freerdp* instance, rdpContext* context)
33 {
34         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = instance->pClientEntryPoints;
35         return pEntryPoints->ClientNew(instance, context);
36 }
37
38 void freerdp_client_common_free(freerdp* instance, rdpContext* context)
39 {
40         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = instance->pClientEntryPoints;
41         pEntryPoints->ClientFree(instance, context);
42 }
43
44 /* Common API */
45
46 rdpContext* freerdp_client_context_new(RDP_CLIENT_ENTRY_POINTS* pEntryPoints)
47 {
48         freerdp* instance;
49         rdpContext* context;
50
51         pEntryPoints->GlobalInit();
52
53         instance = freerdp_new();
54         instance->settings = pEntryPoints->settings;
55         instance->ContextSize = pEntryPoints->ContextSize;
56         instance->ContextNew = freerdp_client_common_new;
57         instance->ContextFree = freerdp_client_common_free;
58         instance->pClientEntryPoints = (RDP_CLIENT_ENTRY_POINTS*) malloc(pEntryPoints->Size);
59         CopyMemory(instance->pClientEntryPoints, pEntryPoints, pEntryPoints->Size);
60         freerdp_context_new(instance);
61
62         context = instance->context;
63         context->instance = instance;
64         context->settings = instance->settings;
65
66         freerdp_register_addin_provider(freerdp_channels_load_static_addin_entry, 0);
67
68         return context;
69 }
70
71 void freerdp_client_context_free(rdpContext* context)
72 {
73         freerdp* instance = context->instance;
74
75         if (instance)
76         {
77                 freerdp_context_free(instance);
78                 free(instance->pClientEntryPoints);
79                 freerdp_free(instance);
80         }
81 }
82
83 int freerdp_client_start(rdpContext* context)
84 {
85         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = context->instance->pClientEntryPoints;
86         return pEntryPoints->ClientStart(context);
87 }
88
89 int freerdp_client_stop(rdpContext* context)
90 {
91         RDP_CLIENT_ENTRY_POINTS* pEntryPoints = context->instance->pClientEntryPoints;
92         return pEntryPoints->ClientStop(context);
93 }
94
95 freerdp* freerdp_client_get_instance(rdpContext* context)
96 {
97         return context->instance;
98 }
99
100 HANDLE freerdp_client_get_thread(rdpContext* context)
101 {
102         return ((rdpClientContext*) context)->thread;
103 }
104
105 static BOOL freerdp_client_settings_post_process(rdpSettings* settings)
106 {
107         /* Moved GatewayUseSameCredentials logic outside of cmdline.c, so
108          * that the rdp file also triggers this functionality */
109         if (settings->GatewayEnabled)
110         {
111                 if (settings->GatewayUseSameCredentials)
112                 {
113                         if (settings->Username)
114                         {
115                                 settings->GatewayUsername = _strdup(settings->Username);
116                                 if (!settings->GatewayUsername)
117                                         return FALSE;
118                         }
119                         if (settings->Domain)
120                         {
121                                 settings->GatewayDomain = _strdup(settings->Domain);
122                                 if (!settings->GatewayDomain)
123                                         return FALSE;
124                         }
125                         if (settings->Password)
126                         {
127                                 settings->GatewayPassword = _strdup(settings->Password);
128                                 if (!settings->GatewayPassword)
129                                         return FALSE;
130                         }
131                 }
132         }
133         return TRUE;
134 }
135
136
137 int freerdp_client_settings_parse_command_line(rdpSettings* settings, int argc, char** argv)
138 {
139         int status;
140
141         if (argc < 1)
142                 return 0;
143
144         if (!argv)
145                 return -1;
146
147         status = freerdp_client_settings_parse_command_line_arguments(settings, argc, argv);
148
149         if (settings->ConnectionFile)
150         {
151                 status = freerdp_client_settings_parse_connection_file(settings, settings->ConnectionFile);
152         }
153
154         if (settings->AssistanceFile)
155         {
156                 status = freerdp_client_settings_parse_assistance_file(settings, settings->AssistanceFile);
157         }
158         
159         /* Only call post processing if no status/error was returned*/
160         if (status < 0)
161                 return status;
162
163         /* This function will call logic that is applicable to the settings
164          * from command line parsing AND the rdp file parsing */
165         if(!freerdp_client_settings_post_process(settings))
166                 return -1;
167
168         return status;
169 }
170
171 int freerdp_client_settings_parse_connection_file(rdpSettings* settings, const char* filename)
172 {
173         rdpFile* file;
174
175         file = freerdp_client_rdp_file_new();
176         freerdp_client_parse_rdp_file(file, filename);
177         freerdp_client_populate_settings_from_rdp_file(file, settings);
178         freerdp_client_rdp_file_free(file);
179
180         return 0;
181 }
182
183 int freerdp_client_settings_parse_connection_file_buffer(rdpSettings* settings, const BYTE* buffer, size_t size)
184 {
185         rdpFile* file;
186         int status = -1;
187
188         file = freerdp_client_rdp_file_new();
189
190         if (freerdp_client_parse_rdp_file_buffer(file, buffer, size)
191                         && freerdp_client_populate_settings_from_rdp_file(file, settings))
192         {
193                 status = 0;
194         }
195
196         freerdp_client_rdp_file_free(file);
197
198         return status;
199 }
200
201 int freerdp_client_settings_write_connection_file(const rdpSettings* settings, const char* filename, BOOL unicode)
202 {
203         rdpFile* file;
204
205         file = freerdp_client_rdp_file_new();
206
207         if (!freerdp_client_populate_rdp_file_from_settings(file, settings))
208                 return -1;
209
210         if (!freerdp_client_write_rdp_file(file, filename, unicode))
211                 return -1;
212
213         freerdp_client_rdp_file_free(file);
214
215         return 0;
216 }
217
218 int freerdp_client_settings_parse_assistance_file(rdpSettings* settings, const char* filename)
219 {
220         int status;
221         rdpAssistanceFile* file;
222
223         file = freerdp_assistance_file_new();
224
225         if (!file)
226                 return -1;
227
228         status = freerdp_assistance_parse_file(file, filename);
229
230         if (status < 0)
231                 return -1;
232
233         status = freerdp_client_populate_settings_from_assistance_file(file, settings);
234
235         if (status < 0)
236                 return -1;
237
238         freerdp_assistance_file_free(file);
239
240         return 0;
241 }