~ubuntu-branches/ubuntu/quantal/freerdp/quantal

« back to all changes in this revision

Viewing changes to libfreerdp/freerdp.c

  • Committer: Bazaar Package Importer
  • Author(s): Otavio Salvador
  • Date: 2010-06-23 21:39:09 UTC
  • Revision ID: james.westby@ubuntu.com-20100623213909-bb9pvvv03913tdv6
Tags: upstream-0.7.1
ImportĀ upstreamĀ versionĀ 0.7.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
#include <stdio.h>
 
3
#include <stdlib.h>
 
4
#include <string.h>
 
5
#include <stdarg.h>
 
6
#include "frdp.h"
 
7
#include "freerdp.h"
 
8
#include "rdp.h"
 
9
#include "secure.h"
 
10
#include "mcs.h"
 
11
#include "iso.h"
 
12
#include "tcp.h"
 
13
#include "mem.h"
 
14
#include "chan.h"
 
15
 
 
16
#define RDP_FROM_INST(_inst) ((rdpRdp *) (_inst->rdp))
 
17
 
 
18
void
 
19
ui_error(rdpInst * inst, char * format, ...)
 
20
{
 
21
        char * text1;
 
22
        char * text2;
 
23
        va_list ap;
 
24
 
 
25
        text1 = (char *) xmalloc(1024);
 
26
        text2 = (char *) xmalloc(1024);
 
27
        va_start(ap, format);
 
28
        vsnprintf(text1, 1023, format, ap);
 
29
        va_end(ap);
 
30
        text1[1023] = 0;
 
31
        snprintf(text2, 1023, "ERROR: %s", text1);
 
32
        text2[1023] = 0;
 
33
        inst->ui_error(inst, text2);
 
34
        xfree(text1);
 
35
        xfree(text2);
 
36
}
 
37
 
 
38
void
 
39
ui_warning(rdpInst * inst, char * format, ...)
 
40
{
 
41
        char * text1;
 
42
        char * text2;
 
43
        va_list ap;
 
44
 
 
45
        text1 = (char *) xmalloc(1024);
 
46
        text2 = (char *) xmalloc(1024);
 
47
        va_start(ap, format);
 
48
        vsnprintf(text1, 1023, format, ap);
 
49
        va_end(ap);
 
50
        text1[1023] = 0;
 
51
        snprintf(text2, 1023, "WARNING: %s", text1);
 
52
        text2[1023] = 0;
 
53
        inst->ui_warning(inst, text2);
 
54
        xfree(text1);
 
55
        xfree(text2);
 
56
}
 
57
 
 
58
void
 
59
ui_unimpl(rdpInst * inst, char * format, ...)
 
60
{
 
61
        char * text1;
 
62
        char * text2;
 
63
        va_list ap;
 
64
 
 
65
        text1 = (char *) xmalloc(1024);
 
66
        text2 = (char *) xmalloc(1024);
 
67
        va_start(ap, format);
 
68
        vsnprintf(text1, 1023, format, ap);
 
69
        va_end(ap);
 
70
        text1[1023] = 0;
 
71
        snprintf(text2, 1023, "NOT IMPLEMENTED: %s", text1);
 
72
        text2[1023] = 0;
 
73
        inst->ui_unimpl(inst, text2);
 
74
        xfree(text1);
 
75
        xfree(text2);
 
76
}
 
77
 
 
78
void
 
79
hexdump(unsigned char * p, int len)
 
80
{
 
81
        unsigned char *line = p;
 
82
        int i, thisline, offset = 0;
 
83
 
 
84
        while (offset < len)
 
85
        {
 
86
                printf("%04x ", offset);
 
87
                thisline = len - offset;
 
88
                if (thisline > 16)
 
89
                        thisline = 16;
 
90
 
 
91
                for (i = 0; i < thisline; i++)
 
92
                        printf("%02x ", line[i]);
 
93
 
 
94
                for (; i < 16; i++)
 
95
                        printf("   ");
 
96
 
 
97
                for (i = 0; i < thisline; i++)
 
98
                        printf("%c", (line[i] >= 0x20 && line[i] < 0x7f) ? line[i] : '.');
 
99
 
 
100
                printf("\n");
 
101
                offset += thisline;
 
102
                line += thisline;
 
103
        }
 
104
}
 
105
 
 
106
int
 
107
load_licence(unsigned char ** data)
 
108
{
 
109
        return 0;
 
110
}
 
111
 
 
112
RD_BOOL
 
113
rd_lock_file(int fd, int start, int len)
 
114
{
 
115
        return 0;
 
116
}
 
117
 
 
118
int
 
119
rd_lseek_file(int fd, int offset)
 
120
{
 
121
        return 0;
 
122
}
 
123
 
 
124
int
 
125
rd_write_file(int fd, void * ptr, int len)
 
126
{
 
127
        return 0;
 
128
}
 
129
 
 
130
RD_BOOL
 
131
rd_pstcache_mkdir(void)
 
132
{
 
133
        return 0;
 
134
}
 
135
 
 
136
void
 
137
rd_close_file(int fd)
 
138
{
 
139
}
 
140
 
 
141
int
 
142
rd_read_file(int fd, void * ptr, int len)
 
143
{
 
144
        return 0;
 
145
}
 
146
 
 
147
int
 
148
rd_open_file(char * filename)
 
149
{
 
150
        return 0;
 
151
}
 
152
 
 
153
void
 
154
generate_random(uint8 * random)
 
155
{
 
156
        time_t index;
 
157
 
 
158
        index = time(NULL);
 
159
        srand((unsigned int)index);
 
160
        for (index = 0; index < 32; index++)
 
161
        {
 
162
                random[index] = rand();
 
163
        }
 
164
}
 
165
 
 
166
void
 
167
save_licence(unsigned char * data, int length)
 
168
{
 
169
}
 
170
 
 
171
void
 
172
ui_begin_update(rdpInst * inst)
 
173
{
 
174
        inst->ui_begin_update(inst);
 
175
}
 
176
 
 
177
void
 
178
ui_end_update(rdpInst * inst)
 
179
{
 
180
        inst->ui_end_update(inst);
 
181
}
 
182
 
 
183
void
 
184
ui_line(rdpInst * inst, uint8 opcode, int startx, int starty, int endx, int endy, RD_PEN * pen)
 
185
{
 
186
        inst->ui_line(inst, opcode, startx, starty, endx, endy, pen);
 
187
}
 
188
 
 
189
void
 
190
ui_rect(rdpInst * inst, int x, int y, int cx, int cy, int colour)
 
191
{
 
192
        inst->ui_rect(inst, x, y, cx, cy, colour);
 
193
}
 
194
 
 
195
void
 
196
ui_polygon(rdpInst * inst, uint8 opcode, uint8 fillmode, RD_POINT * point, int npoints,
 
197
           RD_BRUSH * brush, int bgcolour, int fgcolour)
 
198
{
 
199
        inst->ui_polygon(inst, opcode, fillmode, point, npoints, brush, bgcolour, fgcolour);
 
200
}
 
201
 
 
202
void
 
203
ui_polyline(rdpInst * inst, uint8 opcode, RD_POINT * points, int npoints, RD_PEN * pen)
 
204
{
 
205
        inst->ui_polyline(inst, opcode, points, npoints, pen);
 
206
}
 
207
 
 
208
void
 
209
ui_ellipse(rdpInst * inst, uint8 opcode, uint8 fillmode, int x, int y, int cx, int cy,
 
210
           RD_BRUSH * brush, int bgcolour, int fgcolour)
 
211
{
 
212
        inst->ui_ellipse(inst, opcode, fillmode, x, y, cx, cy, brush, bgcolour, fgcolour);
 
213
}
 
214
 
 
215
void
 
216
ui_start_draw_glyphs(rdpInst * inst, int bgcolour, int fgcolour)
 
217
{
 
218
        inst->ui_start_draw_glyphs(inst, bgcolour, fgcolour);
 
219
}
 
220
 
 
221
void
 
222
ui_draw_glyph(rdpInst * inst, int x, int y, int cx, int cy, RD_HGLYPH glyph)
 
223
{
 
224
        inst->ui_draw_glyph(inst, x, y, cx, cy, glyph);
 
225
}
 
226
 
 
227
void
 
228
ui_end_draw_glyphs(rdpInst * inst, int x, int y, int cx, int cy)
 
229
{
 
230
        inst->ui_end_draw_glyphs(inst, x, y, cx, cy);
 
231
}
 
232
 
 
233
void
 
234
ui_desktop_save(rdpInst * inst, uint32 offset, int x, int y, int cx, int cy)
 
235
{
 
236
        inst->ui_desktop_save(inst, offset, x, y, cx, cy);
 
237
}
 
238
 
 
239
void
 
240
ui_desktop_restore(rdpInst * inst, uint32 offset, int x, int y, int cx, int cy)
 
241
{
 
242
        inst->ui_desktop_save(inst, offset, x, y, cx, cy);
 
243
}
 
244
 
 
245
uint32
 
246
ui_get_toggle_keys_state(rdpInst * inst)
 
247
{
 
248
        return inst->ui_get_toggle_keys_state(inst);
 
249
}
 
250
 
 
251
void
 
252
ui_bell(rdpInst * inst)
 
253
{
 
254
        inst->ui_bell(inst);
 
255
}
 
256
 
 
257
void
 
258
ui_destblt(rdpInst * inst, uint8 opcode, int x, int y, int cx, int cy)
 
259
{
 
260
        inst->ui_destblt(inst, opcode, x, y, cx, cy);
 
261
}
 
262
 
 
263
void
 
264
ui_patblt(rdpInst * inst, uint8 opcode, int x, int y, int cx, int cy, RD_BRUSH * brush,
 
265
          int bgcolour, int fgcolour)
 
266
{
 
267
        inst->ui_patblt(inst, opcode, x, y, cx, cy, brush, bgcolour, fgcolour);
 
268
}
 
269
 
 
270
void
 
271
ui_screenblt(rdpInst * inst, uint8 opcode, int x, int y, int cx, int cy, int srcx, int srcy)
 
272
{
 
273
        inst->ui_screenblt(inst, opcode, x, y, cx, cy, srcx, srcy);
 
274
}
 
275
 
 
276
void
 
277
ui_memblt(rdpInst * inst, uint8 opcode, int x, int y, int cx, int cy, RD_HBITMAP src,
 
278
          int srcx, int srcy)
 
279
{
 
280
        inst->ui_memblt(inst, opcode, x, y, cx, cy, src, srcx, srcy);
 
281
}
 
282
 
 
283
void
 
284
ui_triblt(rdpInst * inst, uint8 opcode, int x, int y, int cx, int cy, RD_HBITMAP src,
 
285
          int srcx, int srcy, RD_BRUSH * brush, int bgcolour, int fgcolour)
 
286
{
 
287
        inst->ui_triblt(inst, opcode, x, y, cx, cy, src, srcx, srcy, brush, bgcolour, fgcolour);
 
288
}
 
289
 
 
290
RD_HGLYPH
 
291
ui_create_glyph(rdpInst * inst, int width, int height, uint8 * data)
 
292
{
 
293
        return inst->ui_create_glyph(inst, width, height, data);
 
294
}
 
295
 
 
296
void
 
297
ui_destroy_glyph(rdpInst * inst, RD_HGLYPH glyph)
 
298
{
 
299
        inst->ui_destroy_glyph(inst, glyph);
 
300
}
 
301
 
 
302
int
 
303
ui_select(rdpInst * inst, int rdp_socket)
 
304
{
 
305
        return inst->ui_select(inst, rdp_socket);
 
306
}
 
307
 
 
308
void
 
309
ui_set_clip(rdpInst * inst, int x, int y, int cx, int cy)
 
310
{
 
311
        inst->ui_set_clip(inst, x, y, cx, cy);
 
312
}
 
313
 
 
314
void
 
315
ui_reset_clip(rdpInst * inst)
 
316
{
 
317
        inst->ui_reset_clip(inst);
 
318
}
 
319
 
 
320
void
 
321
ui_resize_window(rdpInst * inst)
 
322
{
 
323
        inst->ui_resize_window(inst);
 
324
}
 
325
 
 
326
RD_HCURSOR
 
327
ui_create_cursor(rdpInst * inst, unsigned int x, unsigned int y, int width, int height,
 
328
                 uint8 * andmask, uint8 * xormask, int bpp)
 
329
{
 
330
        return inst->ui_create_cursor(inst, x, y, width, height, andmask, xormask, bpp);
 
331
}
 
332
 
 
333
void
 
334
ui_set_cursor(rdpInst * inst, RD_HCURSOR cursor)
 
335
{
 
336
        inst->ui_set_cursor(inst, cursor);
 
337
}
 
338
 
 
339
void
 
340
ui_set_null_cursor(rdpInst * inst)
 
341
{
 
342
        inst->ui_set_null_cursor(inst);
 
343
}
 
344
 
 
345
void
 
346
ui_set_default_cursor(rdpInst * inst)
 
347
{
 
348
        inst->ui_set_default_cursor(inst);
 
349
}
 
350
 
 
351
void
 
352
ui_destroy_cursor(rdpInst * inst, RD_HCURSOR cursor)
 
353
{
 
354
        inst->ui_destroy_cursor(inst, cursor);
 
355
}
 
356
 
 
357
RD_HBITMAP
 
358
ui_create_bitmap(rdpInst * inst, int width, int height, uint8 * data)
 
359
{
 
360
        return inst->ui_create_bitmap(inst, width, height, data);
 
361
}
 
362
 
 
363
void
 
364
ui_paint_bitmap(rdpInst * inst, int x, int y, int cx, int cy, int width, int height, uint8 * data)
 
365
{
 
366
        inst->ui_paint_bitmap(inst, x, y, cx, cy, width,  height, data);
 
367
}
 
368
 
 
369
void
 
370
ui_destroy_bitmap(rdpInst * inst, RD_HBITMAP bmp)
 
371
{
 
372
        inst->ui_destroy_bitmap(inst, bmp);
 
373
}
 
374
 
 
375
RD_HCOLOURMAP
 
376
ui_create_colourmap(rdpInst * inst, RD_COLOURMAP * colours)
 
377
{
 
378
        return inst->ui_create_colourmap(inst, colours);
 
379
}
 
380
 
 
381
void
 
382
ui_move_pointer(rdpInst * inst, int x, int y)
 
383
{
 
384
        inst->ui_move_pointer(inst, x, y);
 
385
}
 
386
 
 
387
void
 
388
ui_set_colourmap(rdpInst * inst, RD_HCOLOURMAP map)
 
389
{
 
390
        inst->ui_set_colourmap(inst, map);
 
391
}
 
392
 
 
393
RD_HBITMAP
 
394
ui_create_surface(rdpInst * inst, int width, int height, RD_HBITMAP old)
 
395
{
 
396
        return inst->ui_create_surface(inst, width, height, old);
 
397
}
 
398
 
 
399
void
 
400
ui_set_surface(rdpInst * inst, RD_HBITMAP surface)
 
401
{
 
402
        inst->ui_set_surface(inst, surface);
 
403
}
 
404
 
 
405
void
 
406
ui_destroy_surface(rdpInst * inst, RD_HBITMAP surface)
 
407
{
 
408
        inst->ui_destroy_surface(inst, surface);
 
409
}
 
410
 
 
411
void
 
412
ui_channel_data(rdpInst * inst, int chan_id, char * data, int data_size,
 
413
                int flags, int total_size)
 
414
{
 
415
        inst->ui_channel_data(inst, chan_id, data, data_size, flags, total_size);
 
416
}
 
417
 
 
418
/* returns error */
 
419
static int
 
420
l_rdp_connect(rdpInst * inst)
 
421
{
 
422
        rdpRdp * rdp;
 
423
        int index;
 
424
 
 
425
        rdp = RDP_FROM_INST(inst);
 
426
        for (index = 0; index < rdp->settings->num_channels; index++)
 
427
        {
 
428
                rdp->settings->channels[index].chan_id = MCS_GLOBAL_CHANNEL + 1 + index;
 
429
        }
 
430
        if (rdp_connect(rdp))
 
431
        {
 
432
                return 0;
 
433
        }
 
434
        return 1;
 
435
}
 
436
 
 
437
static int
 
438
l_rdp_get_fds(rdpInst * inst, void ** read_fds, int * read_count,
 
439
        void ** write_fds, int * write_count)
 
440
{
 
441
        rdpRdp * rdp;
 
442
 
 
443
        rdp = RDP_FROM_INST(inst);
 
444
#ifdef _WIN32
 
445
        read_fds[*read_count] = (void *) (rdp->sec->mcs->iso->tcp->wsa_event);
 
446
#else
 
447
        read_fds[*read_count] = (void *)(long) (rdp->sec->mcs->iso->tcp->sock);
 
448
#endif
 
449
        (*read_count)++;
 
450
        return 0;
 
451
}
 
452
 
 
453
/* Process receivable fds, return true if connection should live on */
 
454
static int
 
455
l_rdp_check_fds(rdpInst * inst)
 
456
{
 
457
        rdpRdp * rdp;
 
458
        RD_BOOL deactivated;
 
459
        int rv;
 
460
 
 
461
        rdp = RDP_FROM_INST(inst);
 
462
#ifdef _WIN32
 
463
        WSAResetEvent(rdp->sec->mcs->iso->tcp->wsa_event);
 
464
#endif
 
465
        rv = 0;
 
466
        if (tcp_can_recv(rdp->sec->mcs->iso->tcp->sock, 0))
 
467
        {
 
468
                if (!rdp_loop(rdp, &deactivated))
 
469
                {
 
470
                        rv = 1;
 
471
                }
 
472
        }
 
473
        if ((rv != 0) && rdp->redirect)
 
474
        {
 
475
                rdp->redirect = False;
 
476
                rdp_disconnect(rdp);
 
477
                if (rdp_reconnect(rdp))
 
478
                {
 
479
                        rv = 0;
 
480
                }
 
481
        }
 
482
        return rv;
 
483
}
 
484
 
 
485
static int
 
486
l_rdp_send_input(rdpInst * inst, int message_type, int device_flags,
 
487
        int param1, int param2)
 
488
{
 
489
        rdpRdp * rdp;
 
490
 
 
491
        rdp = RDP_FROM_INST(inst);
 
492
        rdp_send_input(rdp, time(NULL), message_type, device_flags, param1, param2);
 
493
        return 0;
 
494
}
 
495
 
 
496
static int
 
497
l_rdp_sync_input(rdpInst * inst, int toggle_flags)
 
498
{
 
499
        rdpRdp * rdp;
 
500
 
 
501
        rdp = (rdpRdp *) (inst->rdp);
 
502
        rdp_sync_input(rdp, time(NULL), toggle_flags);
 
503
        return 0;
 
504
}
 
505
 
 
506
static int
 
507
l_rdp_channel_data(rdpInst * inst, int chan_id, char * data, int data_size)
 
508
{
 
509
        rdpRdp * rdp;
 
510
        rdpChannels * chan;
 
511
 
 
512
        rdp = RDP_FROM_INST(inst);
 
513
        chan = rdp->sec->mcs->chan;
 
514
        return vchan_send(chan, chan_id, data, data_size);
 
515
}
 
516
 
 
517
static void
 
518
l_rdp_disconnect(rdpInst * inst)
 
519
{
 
520
        rdpRdp * rdp;
 
521
 
 
522
        rdp = RDP_FROM_INST(inst);
 
523
        rdp_disconnect(rdp);
 
524
}
 
525
 
 
526
rdpInst *
 
527
freerdp_new(rdpSet * settings)
 
528
{
 
529
        rdpInst * inst;
 
530
 
 
531
        inst = (rdpInst *) xmalloc(sizeof(rdpInst));
 
532
        inst->version = FREERDP_INTERFACE_VERSION;
 
533
        inst->size = sizeof(rdpInst);
 
534
        inst->settings = settings;
 
535
        inst->rdp_connect = l_rdp_connect;
 
536
        inst->rdp_get_fds = l_rdp_get_fds;
 
537
        inst->rdp_check_fds = l_rdp_check_fds;
 
538
        inst->rdp_send_input = l_rdp_send_input;
 
539
        inst->rdp_sync_input = l_rdp_sync_input;
 
540
        inst->rdp_channel_data = l_rdp_channel_data;
 
541
        inst->rdp_disconnect = l_rdp_disconnect;
 
542
        inst->rdp = (void *) rdp_new(settings, inst);
 
543
        return inst;
 
544
}
 
545
 
 
546
void
 
547
freerdp_free(rdpInst * inst)
 
548
{
 
549
        rdpRdp * rdp;
 
550
 
 
551
        if (inst != NULL)
 
552
        {
 
553
                rdp = RDP_FROM_INST(inst);
 
554
                rdp_free(rdp);
 
555
                xfree(inst);
 
556
        }
 
557
}