forked from OpenXT/input
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestsocketclient.c
489 lines (408 loc) · 9.45 KB
/
testsocketclient.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/*
* Copyright (c) 2013 Citrix Systems, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <stdint.h>
#include <event.h>
#include <linux/input.h>
#define SOCK_PATH "/var/run/input_socket"
#define MAGIC 0xAD9CBCE9
#define EV_DEV 0x6
char typestr[][4] = { "SYN", "KEY", "REL", "ABS", "MSC", "SW " };
char devstr[][45] =
{ "\nChange to device %d\n\n", "\nNew device %d on system!\n\n", "\nDevice %d was removed from the system!\n\n" };
char keys[] =
{ '\0', 'E', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', 'B', '\t', 'q', 'w', 'e', 'r', 't', 'y',
'u', 'i', 'o', 'p', '{', '}',
'M', 'C', 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '~', 'S', '\\', 'z', 'x', 'c', 'v', 'b', 'n', 'm',
',', '.', '/', '\0', '\0', 'A', ' '
};
struct event_record
{
uint32_t magic;
uint16_t itype;
uint16_t icode;
uint32_t ivalue;
} __attribute__ ((__packed__));
#define EVENT_SIZE (sizeof(struct event_record))
#define buffersize (EVENT_SIZE*20)
struct buffer_t
{
char buffer[buffersize];
unsigned int bytes_remaining;
int position;
int s;
int copy;
int block;
} buffers;
struct event_record *findnext (struct buffer_t *b);
static void process_event (struct event_record *r, struct buffer_t *b);
static void stop ();
static void recv_callback (int fd, short event, void *opaque)
{
int n;
struct buffer_t *buf = &buffers;
char *b = buf->buffer;
memmove (b, &b[buf->position], buf->bytes_remaining);
buf->position = 0;
n = recv (fd, &b[buf->bytes_remaining], buffersize - buf->bytes_remaining, 0);
if (n > 0)
{
struct event_record *r = NULL;
buf->bytes_remaining += n;
while ((r = findnext (buf)) != NULL)
{
process_event (r, buf);
}
}
else if (n)
printf ("Error %d\n", n);
else
stop ();
}
static void process_event (struct event_record *r, struct buffer_t *b);
struct event_record *findnext (struct buffer_t *b)
{
struct event_record *r = NULL;
int start = b->position;
// Skip junk
while (b->bytes_remaining >= EVENT_SIZE &&
(r = (struct event_record *) &b->buffer[b->position]) && r->magic != MAGIC)
{
b->bytes_remaining--;
b->position++;
}
if (start != b->position)
printf ("Warning: Encountered %d bytes of junk.\n", b->position - start);
if (b->bytes_remaining >= EVENT_SIZE)
{
b->bytes_remaining -= EVENT_SIZE;
b->position += EVENT_SIZE;
return r;
}
else
return NULL;
}
show_code (struct event_record * r)
{
if (r->itype == EV_DEV)
{
if ((r->icode < 4) && (r->icode > 0))
printf (devstr[r->icode - 1], r->ivalue);
else
printf ("Invalid EV_DEV code %x, value %x\n", r->icode, r->ivalue);
}
else
{
printf ("event %d (%s), %d %d\n", r->itype, (r->itype < 6) ? typestr[r->itype] : "???", r->icode, r->ivalue);
if ((r->itype == EV_SYN) && (r->icode == SYN_REPORT))
printf ("\n");
}
}
static void process_event (struct event_record *r, struct buffer_t *b)
{
if (r->itype == 0x7)
{
if (r->icode == 0x3)
{
printf ("Error %d, %d, %d\n", (r->ivalue & 0xFF000000) >> (8 * 3), (r->ivalue & 0x00FF0000) >> (8 * 2),
(r->ivalue & 0x0000FF00) >> (8 * 1));
}
else
printf ("unexpected event %d, %d %d\n", r->itype, r->icode, r->ivalue);
}
else
{
show_code (r);
if (b->copy)
{
if (send (b->s, r, sizeof (struct event_record), 0) == -1)
{
perror ("send");
exit (1);
}
}
}
}
void set_domain (int s, int d)
{
struct event_record e;
e.magic = MAGIC;
e.itype = 7;
e.icode = 0x1;
e.ivalue = d;
if (send (s, &e, sizeof (struct event_record), 0) == -1)
{
perror ("send");
exit (1);
}
}
void suck (int s, int d)
{
struct event_record e;
e.magic = MAGIC;
e.itype = 7;
e.icode = 0x2;
e.ivalue = d;
if (send (s, &e, sizeof (struct event_record), 0) == -1)
{
perror ("send");
exit (1);
}
}
void send_string (int s, char *str)
{
struct event_record e;
int key;
int i;
int v;
e.magic = MAGIC;
e.itype = EV_KEY;
for (i = 0; (i < 255) && (str[i] != 0); i++)
for (key = 0; key < sizeof (keys); key++)
if (str[i] == keys[key])
{
e.icode = key;
for (v = 1; v >= 0; v--)
{
e.ivalue = v;
if (send (s, &e, sizeof (struct event_record), 0) == -1)
{
perror ("send");
exit (1);
}
}
break;
}
}
void send_event (int s, int v)
{
struct event_record e;
e.magic = MAGIC;
e.itype = 5;
e.icode = v;
e.ivalue = 1;
if (send (s, &e, sizeof (struct event_record), 0) == -1)
{
perror ("send");
exit (1);
}
}
#define NUM_COMMANDS 9
const char commands[NUM_COMMANDS][8] = { "exit", "send", "blow", "suck", "copy", "stop", "block", "unblock","help" };
enum command_ids
{
eExit = 0,
eSend,
eBlow,
eSuck,
eCopy,
eStop,
eBlock,
eUnblock,
eHelp,
eMax
};
#define eNone eMax
struct event recv_event;
struct event recv_stdin;
static void stop ()
{
event_del (&recv_event);
event_del (&recv_stdin);
}
void getcommand (char *str, int len, char **com, char **arg)
{
int i;
int leading = 1;
*arg = NULL;
*com = str;
str[len] = '\0';
if (len == 0)
return;
if (str[len - 1] == '\n')
{
len--;
str[len] = '\0';
}
for (i = 0; i < len; i++)
{
if (str[i] == ' ')
{
if (leading)
{
*com = &str[i + 1];
}
else
{
str[i] = '\0';
*arg = &str[i + 1];
break;
}
}
else
leading = 0;
}
}
enum command_ids conv_com (char *str)
{
enum command_ids com;
printf ("%s: ", str);
for (com = eExit; com < eMax; com++)
if (strcmp (str, commands[com]) == 0)
break;
return com;
}
void do_command (enum command_ids com, char *arg, int s)
{
switch (com)
{
case eSend:
if (!arg)
printf ("Error - Bad Input!");
else
{
send_string (s, arg);
printf ("By your command.");
}
break;
case eBlow:
{
int d = strtol (arg, NULL, 0);
if (errno)
printf ("Error - Bad domain number.");
else
{
set_domain (s, d);
printf ("Right you are!");
}
}
break;
case eSuck:
{
int d = strtol (arg, NULL, 0);
if (errno)
printf ("Error - Bad domain number.");
else
{
printf ("Puckering up!");
suck (s, d);
}
}
break;
case eCopy:
buffers.copy = 1;
printf ("will do!");
break;
case eStop:
buffers.copy = 0;
printf ("ok");
break;
case eExit:
stop ();
break;
case eBlock:
if (!buffers.block)
{
buffers.block = 1;
printf ("That'll be popular!");
event_del (&recv_event);
}
else
printf ("We're already doing that.");
break;
case eUnblock:
if (buffers.block)
{
buffers.block = 0;
printf ("Stand back!");
event_add (&recv_event, NULL);
}
else
printf ("We wern't blocked!");
break;
case eHelp:
printf("Command available:\n suck <d>\t- Requests all events for domain <d>\n blow <d>\t- Sends events to domain <d>\n copy\t\t- All input should be copied to output\n stop\t\t- Don't copy\n send <text>\t- Send text as keypresses. Only lowercase for alpha keys, M = enter, B = backspace\n block\t\t- Disable reading - block the socket.\n unblock\t- Re-enable reading\n help\t\t- This text.\n exit\t\t- Exit this client.\n");
break;
default:
printf ("Hu?");
}
printf ("\n>\n");
}
static void stdin_callback (int fd, short event, void *opaque)
{
int *s = (int *) opaque;
char str[100];
char *command;
char *arg;
enum command_ids com;
int n;
n = read (fd, &str, sizeof (str), 0);
if (n > 0)
{
getcommand (str, n, &command, &arg);
com = conv_com (command);
do_command (com, arg, *s);
}
else if (n)
{
printf ("Error %d\n", n);
exit (1);
}
else
stop ();
}
int main (void)
{
int s, t, len;
struct sockaddr_un remote;
char str[100];
if ((s = socket (AF_UNIX, SOCK_STREAM, 0)) == -1)
{
perror ("socket");
exit (1);
}
printf ("Trying to connect...\n");
remote.sun_family = AF_UNIX;
strcpy (remote.sun_path, SOCK_PATH);
len = strlen (remote.sun_path) + sizeof (remote.sun_family);
if (connect (s, (struct sockaddr *) &remote, len) == -1)
{
perror ("connect");
exit (1);
}
printf ("Connected.\n");
buffers.bytes_remaining = 0;
buffers.position = 0;
buffers.copy = 0;
buffers.block = 0;
buffers.s = s;
event_init ();
event_set (&recv_event, s, EV_READ | EV_PERSIST, recv_callback, NULL);
event_add (&recv_event, NULL);
event_set (&recv_stdin, 0, EV_READ | EV_PERSIST, stdin_callback, &s);
event_add (&recv_stdin, NULL);
event_dispatch ();
close (s);
return 0;
}