-
Notifications
You must be signed in to change notification settings - Fork 1
/
rd-thread.c
216 lines (188 loc) · 3.57 KB
/
rd-thread.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <thread.h>
#include <keyboard.h>
#include <mouse.h>
#include <cursor.h>
#include <auth.h>
#include "dat.h"
#include "fns.h"
#define STACK 8192
Rdp conn = {
.fd = -1,
.depth = 16,
.windom = "",
.passwd = "",
.shell = "",
.rwd = "",
};
Mousectl *mousectl;
Keyboardctl *keyboardctl;
char Eshort[]= "short data";
char Esmall[]= "buffer too small";
char Ebignum[]= "number too big";
void sendmouse(Rdp* c, Mouse m);
static void keyboardthread(void*);
static void mousethread(void*);
static void snarfthread(void*);
static void
usage(void)
{
fprint(2, "usage: rd [-0A] [-T title] [-a depth] [-c wdir] [-d dom] [-k keyspec] [-n term] [-s shell] [net!]server[!port]\n");
threadexitsall("usage");
}
void
threadmain(int argc, char *argv[])
{
int doauth;
char *server, *addr, *keyspec;
UserPasswd *creds;
Rdp* c;
c = &conn;
keyspec = "";
doauth = 1;
ARGBEGIN{
case 'A':
doauth = 0;
break;
case 'k':
keyspec = EARGF(usage());
break;
case 'T':
c->label = strdup(EARGF(usage()));
break;
case 'd':
c->windom = strdup(EARGF(usage()));
break;
case 's':
c->shell = strdup(EARGF(usage()));
break;
case 'c':
c->rwd = strdup(EARGF(usage()));
break;
case 'a':
c->depth = atol(EARGF(usage()));
break;
case '0':
c->wantconsole = 1;
break;
default:
usage();
}ARGEND
if (argc != 1)
usage();
server = argv[0];
c->local = getenv("sysname");
c->user = getenv("user");
if(c->local == nil)
sysfatal("set $sysname\n");
if(c->user == nil)
sysfatal("set $user");
if(doauth){
creds = auth_getuserpasswd(auth_getkey, "proto=pass service=rdp %s", keyspec);
if(creds == nil)
fprint(2, "factotum: %r\n");
else {
c->user = creds->user;
c->passwd = creds->passwd;
}
}else
c->user = "";
if(c->label == nil)
c->label = smprint("rd %s", server);
initvc(c);
addr = netmkaddr(server, "tcp", "3389");
c->fd = dial(addr, nil, nil, nil);
if(c->fd < 0)
sysfatal("dial %s: %r", addr);
if(x224handshake(c) < 0)
sysfatal("X.224 handshake: %r");
initscreen(c);
if(rdphandshake(c) < 0)
sysfatal("handshake: %r");
mousectl = initmouse(nil, screen);
if(mousectl == nil){
fprint(2, "rd: can't initialize mouse: %r\n");
threadexitsall("mouse");
}
keyboardctl = initkeyboard(nil);
if(keyboardctl == nil){
fprint(2, "rd: can't initialize keyboard: %r\n");
threadexitsall("keyboard");
}
proccreate(keyboardthread, c, STACK);
proccreate(mousethread, c, STACK);
proccreate(snarfthread, c, STACK);
threadsetname("mainthread");
readnet(c);
x224hangup(c);
if(!c->active)
threadexitsall(nil);
if(c->hupreason)
sysfatal("disconnect reason code %d", c->hupreason);
sysfatal("hangup");
}
static void
keyboardthread(void* v)
{
Rune r;
Rdp* c;
c = v;
threadsetname("keyboardthread");
for(;;){
recv(keyboardctl->c, &r);
sendkbd(c, r);
}
}
static void
mousethread(void* v)
{
Rdp* c;
enum { MResize, MMouse, NMALT };
static Alt alts[NMALT+1];
Mouse m;
c = v;
threadsetname("mousethread");
alts[MResize].c = mousectl->resizec;
alts[MResize].v = nil;
alts[MResize].op = CHANRCV;
alts[MMouse].c = mousectl->c;
alts[MMouse].v = &m;
alts[MMouse].op = CHANRCV;
for(;;){
switch(alt(alts)){
case MResize:
eresized(c, 1);
break;
case MMouse:
sendmouse(c, m);
break;
}
}
}
void
warpmouse(int x, int y)
{
moveto(mousectl, Pt(x, y));
}
static void
snarfthread(void* v)
{
Rdp* c;
c = v;
threadsetname("snarfthread");
initsnarf();
pollsnarf(c);
threadexits("snarf eof");
}
void
readnet(Rdp* c)
{
Msg r;
for(;;){
if(readmsg(c, &r) <= 0)
break;
apply(c, &r);
}
}