1 |
/* -*- c-basic-offset: 8 -*- |
2 |
rdesktop: A Remote Desktop Protocol client. |
3 |
Seamless windows - Virtual channel handling |
4 |
|
5 |
Copyright (C) Pierre Ossman <ossman@cendio.se> 2006 |
6 |
|
7 |
This program is free software; you can redistribute it and/or modify |
8 |
it under the terms of the GNU General Public License as published by |
9 |
the Free Software Foundation; either version 2 of the License, or |
10 |
(at your option) any later version. |
11 |
|
12 |
This program is distributed in the hope that it will be useful, |
13 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
16 |
|
17 |
You should have received a copy of the GNU General Public License |
18 |
along with this program; if not, write to the Free Software |
19 |
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
20 |
*/ |
21 |
|
22 |
#include <assert.h> |
23 |
#include <stdio.h> |
24 |
#include <stdarg.h> |
25 |
#include <errno.h> |
26 |
|
27 |
#include <windows.h> |
28 |
#include <wtsapi32.h> |
29 |
#include <cchannel.h> |
30 |
|
31 |
#include "vchannel.h" |
32 |
|
33 |
#define CHANNELNAME "seamrdp" |
34 |
|
35 |
static HANDLE g_mutex = NULL; |
36 |
static HANDLE g_vchannel = NULL; |
37 |
|
38 |
void |
39 |
debug(char *format, ...) |
40 |
{ |
41 |
va_list argp; |
42 |
char buf[256]; |
43 |
|
44 |
sprintf(buf, "DEBUG1,"); |
45 |
|
46 |
va_start(argp, format); |
47 |
_vsnprintf(buf + sizeof("DEBUG1,") - 1, sizeof(buf) - sizeof("DEBUG1,") + 1, format, argp); |
48 |
va_end(argp); |
49 |
|
50 |
vchannel_write(buf); |
51 |
} |
52 |
|
53 |
|
54 |
int |
55 |
vchannel_open() |
56 |
{ |
57 |
g_vchannel = WTSVirtualChannelOpen(WTS_CURRENT_SERVER_HANDLE, |
58 |
WTS_CURRENT_SESSION, CHANNELNAME); |
59 |
|
60 |
if (g_vchannel == NULL) |
61 |
return -1; |
62 |
|
63 |
g_mutex = CreateMutex(NULL, FALSE, "Local\\SeamlessChannel"); |
64 |
if (!g_mutex) { |
65 |
WTSVirtualChannelClose(g_vchannel); |
66 |
g_vchannel = NULL; |
67 |
return -1; |
68 |
} |
69 |
|
70 |
return 0; |
71 |
} |
72 |
|
73 |
void |
74 |
vchannel_close() |
75 |
{ |
76 |
if (g_mutex) |
77 |
CloseHandle(g_mutex); |
78 |
|
79 |
if (g_vchannel) |
80 |
WTSVirtualChannelClose(g_vchannel); |
81 |
|
82 |
g_mutex = NULL; |
83 |
g_vchannel = NULL; |
84 |
} |
85 |
|
86 |
int |
87 |
vchannel_is_open() |
88 |
{ |
89 |
if (g_vchannel == NULL) |
90 |
return 0; |
91 |
else |
92 |
return 1; |
93 |
} |
94 |
|
95 |
int |
96 |
vchannel_read(char *line, size_t length) |
97 |
{ |
98 |
static BOOL overflow_mode = FALSE; |
99 |
static char buffer[VCHANNEL_MAX_LINE]; |
100 |
static size_t size = 0; |
101 |
|
102 |
char *newline; |
103 |
int line_size; |
104 |
|
105 |
BOOL result; |
106 |
ULONG bytes_read; |
107 |
|
108 |
result = WTSVirtualChannelRead(g_vchannel, 0, buffer + size, |
109 |
sizeof(buffer) - size, &bytes_read); |
110 |
|
111 |
if (!result) { |
112 |
errno = EIO; |
113 |
return -1; |
114 |
} |
115 |
|
116 |
if (overflow_mode) { |
117 |
newline = strchr(buffer, '\n'); |
118 |
if (newline && (newline - buffer) < bytes_read) { |
119 |
size = bytes_read - (newline - buffer) - 1; |
120 |
memmove(buffer, newline + 1, size); |
121 |
overflow_mode = FALSE; |
122 |
} |
123 |
} |
124 |
else |
125 |
size += bytes_read; |
126 |
|
127 |
if (overflow_mode) { |
128 |
errno = -EAGAIN; |
129 |
return -1; |
130 |
} |
131 |
|
132 |
newline = strchr(buffer, '\n'); |
133 |
if (!newline || (newline - buffer) >= size) { |
134 |
if (size == sizeof(buffer)) { |
135 |
overflow_mode = TRUE; |
136 |
size = 0; |
137 |
} |
138 |
errno = -EAGAIN; |
139 |
return -1; |
140 |
} |
141 |
|
142 |
if ((newline - buffer) >= length) { |
143 |
errno = ENOMEM; |
144 |
return -1; |
145 |
} |
146 |
|
147 |
*newline = '\0'; |
148 |
|
149 |
strcpy(line, buffer); |
150 |
line_size = newline - buffer; |
151 |
|
152 |
size -= newline - buffer + 1; |
153 |
memmove(buffer, newline + 1, size); |
154 |
|
155 |
return 0; |
156 |
} |
157 |
|
158 |
int |
159 |
vchannel_write(char *format, ...) |
160 |
{ |
161 |
BOOL result; |
162 |
va_list argp; |
163 |
char buf[VCHANNEL_MAX_LINE]; |
164 |
int size; |
165 |
ULONG bytes_written; |
166 |
|
167 |
assert(vchannel_is_open()); |
168 |
|
169 |
va_start(argp, format); |
170 |
size = _vsnprintf(buf, sizeof(buf), format, argp); |
171 |
va_end(argp); |
172 |
|
173 |
assert(size < sizeof(buf)); |
174 |
|
175 |
WaitForSingleObject(g_mutex, INFINITE); |
176 |
result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written); |
177 |
result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written); |
178 |
ReleaseMutex(g_mutex); |
179 |
|
180 |
if (!result) |
181 |
return -1; |
182 |
|
183 |
return bytes_written; |
184 |
} |