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 |
{ |
66 |
WTSVirtualChannelClose(g_vchannel); |
67 |
g_vchannel = NULL; |
68 |
return -1; |
69 |
} |
70 |
|
71 |
return 0; |
72 |
} |
73 |
|
74 |
void |
75 |
vchannel_close() |
76 |
{ |
77 |
if (g_mutex) |
78 |
CloseHandle(g_mutex); |
79 |
|
80 |
if (g_vchannel) |
81 |
WTSVirtualChannelClose(g_vchannel); |
82 |
|
83 |
g_mutex = NULL; |
84 |
g_vchannel = NULL; |
85 |
} |
86 |
|
87 |
int |
88 |
vchannel_is_open() |
89 |
{ |
90 |
if (g_vchannel == NULL) |
91 |
return 0; |
92 |
else |
93 |
return 1; |
94 |
} |
95 |
|
96 |
int |
97 |
vchannel_read(char *line, size_t length) |
98 |
{ |
99 |
static BOOL overflow_mode = FALSE; |
100 |
static char buffer[VCHANNEL_MAX_LINE]; |
101 |
static size_t size = 0; |
102 |
|
103 |
char *newline; |
104 |
int line_size; |
105 |
|
106 |
BOOL result; |
107 |
ULONG bytes_read; |
108 |
|
109 |
result = WTSVirtualChannelRead(g_vchannel, 0, buffer + size, |
110 |
sizeof(buffer) - size, &bytes_read); |
111 |
|
112 |
if (!result) |
113 |
{ |
114 |
errno = EIO; |
115 |
return -1; |
116 |
} |
117 |
|
118 |
if (overflow_mode) |
119 |
{ |
120 |
newline = strchr(buffer, '\n'); |
121 |
if (newline && (newline - buffer) < bytes_read) |
122 |
{ |
123 |
size = bytes_read - (newline - buffer) - 1; |
124 |
memmove(buffer, newline + 1, size); |
125 |
overflow_mode = FALSE; |
126 |
} |
127 |
} |
128 |
else |
129 |
size += bytes_read; |
130 |
|
131 |
if (overflow_mode) |
132 |
{ |
133 |
errno = -EAGAIN; |
134 |
return -1; |
135 |
} |
136 |
|
137 |
newline = strchr(buffer, '\n'); |
138 |
if (!newline || (newline - buffer) >= size) |
139 |
{ |
140 |
if (size == sizeof(buffer)) |
141 |
{ |
142 |
overflow_mode = TRUE; |
143 |
size = 0; |
144 |
} |
145 |
errno = -EAGAIN; |
146 |
return -1; |
147 |
} |
148 |
|
149 |
if ((newline - buffer) >= length) |
150 |
{ |
151 |
errno = ENOMEM; |
152 |
return -1; |
153 |
} |
154 |
|
155 |
*newline = '\0'; |
156 |
|
157 |
strcpy(line, buffer); |
158 |
line_size = newline - buffer; |
159 |
|
160 |
size -= newline - buffer + 1; |
161 |
memmove(buffer, newline + 1, size); |
162 |
|
163 |
return 0; |
164 |
} |
165 |
|
166 |
int |
167 |
vchannel_write(char *format, ...) |
168 |
{ |
169 |
BOOL result; |
170 |
va_list argp; |
171 |
char buf[VCHANNEL_MAX_LINE]; |
172 |
int size; |
173 |
ULONG bytes_written; |
174 |
|
175 |
assert(vchannel_is_open()); |
176 |
|
177 |
va_start(argp, format); |
178 |
size = _vsnprintf(buf, sizeof(buf), format, argp); |
179 |
va_end(argp); |
180 |
|
181 |
assert(size < sizeof(buf)); |
182 |
|
183 |
WaitForSingleObject(g_mutex, INFINITE); |
184 |
result = WTSVirtualChannelWrite(g_vchannel, buf, (ULONG) strlen(buf), &bytes_written); |
185 |
result = WTSVirtualChannelWrite(g_vchannel, "\n", (ULONG) 1, &bytes_written); |
186 |
ReleaseMutex(g_mutex); |
187 |
|
188 |
if (!result) |
189 |
return -1; |
190 |
|
191 |
return bytes_written; |
192 |
} |
193 |
|
194 |
void |
195 |
vchannel_block() |
196 |
{ |
197 |
WaitForSingleObject(g_mutex, INFINITE); |
198 |
} |
199 |
|
200 |
void |
201 |
vchannel_unblock() |
202 |
{ |
203 |
ReleaseMutex(g_mutex); |
204 |
} |