-
Notifications
You must be signed in to change notification settings - Fork 14
/
socket.c
628 lines (531 loc) · 13.9 KB
/
socket.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
/*
* 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 "project.h"
#define MAGIC 0xAD9CBCE9
#define EVENT_SIZE sizeof(struct event_record)
#define EV_VM 0x7
#define VM_SEND_TO 0x1
#define VM_TAKE_FROM 0x2
#define VM_ERROR 0x3
struct sock_plugin buffers;
static struct event server_accept_event;
static long server_sock_fd;
static void process_event(struct event_record* r, struct sock_plugin* b);
void send_event(struct sock_plugin* plug, int t, int c, int v);
void kill_connection(struct sock_plugin* buf);
int send_to_plugin(struct sock_plugin* plug,struct event_record* e);
struct event_record* findnext(struct sock_plugin* 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)
info("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;
}
struct event server_recv_event;
static void wrapper_server_recv(int fd, short event, void *opaque)
{
int n;
struct sock_plugin* buf = (struct sock_plugin*) opaque;
char *b = buf->buffer;
memmove(b, &b[buf->position] , buf->bytes_remaining);
buf->position=0;
do
{
n=recv(fd, &b[buf->bytes_remaining], buffersize- buf->bytes_remaining,0);
}
while ((n<0) && (errno==EINTR));
if (n>0)
{
struct event_record* r = NULL;
buf->bytes_remaining+=n;
while ((r=findnext(buf))!=NULL)
{
process_event(r,buf);
}
}
else if (n)
{
info("Recv failed with %s",strerror(errno));
kill_connection(buf);
}
else
{
kill_connection(buf);
info("Client disconnected");
}
}
void kill_connection(struct sock_plugin* buf)
{
event_del(&server_recv_event);
close(buf->s);
if (buf->src)
{
buf->src->plugin=NULL;
buf->src=NULL;
}
buf->s=-1;
free(buf->dev_events);
}
#define E_BADDOMAIN 0x1
#define E_BADCODE 0x2
#define NONE -1
static void send_error(struct sock_plugin* plug, uint8_t code, uint8_t type, uint8_t domain)
{
struct event_record e;
e.magic= MAGIC;
e.itype=EV_VM;
e.icode=VM_ERROR;
e.ivalue= code << (3*8) | type << (2*8) | domain << (1*8);
if (!send_to_plugin(plug ,&e))
{
if (!plug->error)
plug->error=e.ivalue;
}
}
static void send_slot(struct sock_plugin* plug, int slot)
{
struct event_record er;
er.magic= MAGIC;
if (plug->dropped)
{
er.itype=EV_SYN;
er.icode=SYN_DROPPED;
er.ivalue=0;
plug->dropped = ! send_to_plugin(plug, &er);
if (plug->dropped)
{
plug->slot_dropped=true;
return;
}
}
er.magic= MAGIC;
er.itype=EV_DEV;
er.icode=DEV_SET;
er.ivalue=slot;
if (send_to_plugin(plug, &er))
{
plug->slot=slot;
/* If we prevously droppend the slot number, then we know we also dropped the first packet from that slot,
hence we should set dropped. If not, then we've already dealt with dropped, so it should be false. */
plug->dropped = plug->slot_dropped;
plug->slot_dropped=false;
}
else
plug->slot_dropped=true;
}
// called from input.c, when devices are added or removed.
void add_queue_dev_event_conf(struct sock_plugin* plug, int slot)
{
struct dev_event* dev_events = plug->dev_events;
int i;
int size = plug->deq_size;
for (i=0; i<size; i++)
if (dev_events[i].slot==slot)
{
dev_events[i].add=true;
return;
}
dev_events = realloc(dev_events, (size+1)*sizeof(struct dev_event));
dev_events[size].slot=slot;
dev_events[size].remove=0;
dev_events[size].add=1;
plug->deq_size=size+1;
plug->dev_events=dev_events;
}
static void add_queue_dev_event_reset(struct sock_plugin* plug, int slot)
{
int size = plug->deq_size;
int i;
struct dev_event* dv;
if (slot==0xFF)
{
plug->resetall=true;
return;
}
for (i=0; i < size; i++)
{
dv = &plug->dev_events[i];
if (dv->slot==slot)
{
if ( dv->add)
{
dv->add =false;
return;
}
dv->remove=true;
return;
}
}
dv = realloc(plug->dev_events, (size+1)*sizeof(struct dev_event));
dv[size].slot=slot;
dv[size].remove=1;
dv[size].add=0;
plug->dev_events=dv;
plug->deq_size=size+1;
}
static int flush_queued_dev_events(struct sock_plugin* plug)
{
int size = plug->deq_size;
int i;
struct event_record er;
er.magic= MAGIC;
er.itype=EV_DEV;
struct dev_event* dv;
for (i=size-1; i>-1; i--)
{
dv = &plug->dev_events[i];
er.ivalue=dv->slot;
if (dv->remove)
{
er.icode=DEV_RESET;
if(!send_to_plugin(plug, &er))
return false;
dv->remove=false;
}
if (dv->add)
{
er.icode=DEV_CONF;
if(!send_to_plugin(plug, &er))
return false;
dv->add=false;
}
plug->deq_size--;
plug->dev_events=realloc(plug->dev_events, plug->deq_size *sizeof(struct dev_event));
}
return true;
}
void send_plugin_dev_event(struct sock_plugin* plug, int code, int value)
{
struct event_record er;
er.magic= MAGIC;
er.itype=EV_DEV;
bool problem=false;
if (plug->resetall)
{
er.icode=DEV_RESET;
er.ivalue=0xFF;
}
else
{
er.icode=code;
er.ivalue=value;
}
if ((code==DEV_RESET) && (value==0xFF))
{
plug->deq_size=0;
free(plug->dev_events);
plug->dev_events=NULL;
}
else if (plug->deq_size)
{
problem = !flush_queued_dev_events(plug);
}
if (problem || !send_to_plugin(plug, &er))
{
switch (code)
{
case DEV_CONF: add_queue_dev_event_conf(plug,value);
break;
case DEV_RESET: add_queue_dev_event_reset(plug,value);
break;
}
}
else if (plug->resetall)
{ // If resetall, we just transmitted a reset_all event, so now need to
// transmit the original code we where invoked to send.
plug->resetall=false;
send_plugin_dev_event(plug,code,value);
}
}
// send_delayed_to_plugin - calledfrom send_to_plugin
// sends remaining of partial sends.
static int send_delayed_to_plugin(struct sock_plugin* plug)
{
int r;
do
{
r = send(plug->s, plug->partialBuffer, plug->delayed, MSG_NOSIGNAL | MSG_DONTWAIT);
if (r>0)
{
plug->delayed-=r;
if (plug->delayed)
memmove(&plug->partialBuffer, ((char*) ( &plug->partialBuffer)) + r, plug->delayed);
}
}
while ( ((r==-1) && (errno==EINTR)) || ((r>0) && (plug->delayed)) );
if (r==-1)
{
if ((errno != EAGAIN) && (errno != EWOULDBLOCK))
{
info(strerror(errno));
kill_connection(plug);
}
return false;
}
return true;
}
// sends raw event to plugin. Partial sends are delt with.
int send_to_plugin(struct sock_plugin* plug,struct event_record* e)
{
int r;
if (plug->delayed)
{
r = send_delayed_to_plugin(plug);
if (!r)
{
return false;
}
}
do
{
r = send(plug->s, e, sizeof(struct event_record), MSG_NOSIGNAL | MSG_DONTWAIT);
}
while ((r==-1) && (errno==EINTR));
if (r==-1)
{
if ((errno == EAGAIN) || (errno == EWOULDBLOCK))
{
// PACKET LOST;
return false;
}
else
{
info(strerror(errno));
kill_connection(plug);
}
}
else if (r< (int) sizeof(struct event_record)) // partial send
{
plug->delayed=sizeof(struct event_record) - r;
memcpy(&plug->partialBuffer, ((char*)e) + r , plug->delayed);
send_delayed_to_plugin(plug);
}
return true;
}
// sends an event, on the given slot, to the plugin
void send_plugin_event(struct domain *d,int slot, struct input_event *e)
{
struct sock_plugin* plug=d->plugin;
struct event_record er;
er.magic= MAGIC;
if (plug->error)
{
er.itype=EV_VM;
er.icode=VM_ERROR;
er.ivalue=plug->error;
if (!send_to_plugin(plug, &er))
goto cantsend;
plug->error=0;
}
if (plug->resetall)
{
er.itype=EV_DEV;
er.icode=DEV_RESET;
er.ivalue=0xFF;
if (!send_to_plugin(plug, &er))
goto cantsend;
plug->resetall=false;
}
if (plug->deq_size)
if (!flush_queued_dev_events(plug))
goto cantsend;
if (plug->slot!=slot)
send_slot(plug, slot);
if (plug->dropped)
{
er.itype=EV_SYN;
er.icode=SYN_DROPPED;
er.ivalue=0;
plug->dropped = ! send_to_plugin(plug, &er);
if (plug->dropped)
return;
}
er.itype=e->type;
er.icode=e->code;
er.ivalue=e->value;
if (!send_to_plugin(plug, &er))
{
plug->dropped=true;
}
return;
cantsend:
if (plug->slot!=slot)
plug->slot_dropped=true;
else
plug->dropped=true;
return;
}
static void process_event(struct event_record* r, struct sock_plugin* plug)
{
if (r->itype == EV_VM)
{
struct domain* d;
switch(r->icode)
{
case VM_SEND_TO:
if ( !( plug->dest=domain_with_domid(r->ivalue)) )
{
send_error(plug, E_BADDOMAIN, r->icode , r->ivalue);
}
else if (plug->dest->is_pv_domain)
{
send_error(plug, E_BADDOMAIN, r->icode , r->ivalue);
plug->dest=NULL;
}
else
{ // Do we have the right dev slot?
int devslot=plug->recived_slot;
if ((devslot!=INPUTSLOT_INVALID) && (plug->dest->last_devslot!=devslot))
{
send_event(plug, EV_DEV,DEV_SET, devslot);
plug->dest->last_devslot=devslot;
}
}
break;
case VM_TAKE_FROM:
if (plug->src)
plug->src->plugin=NULL;
d=domain_with_domid(r->ivalue);
if (!d)
{
send_error(plug, E_BADDOMAIN, r->icode , r->ivalue);
}
else
{
d->plugin=plug;
plug->src=d;
sock_plugin_sendconfig(plug);
}
break;
default:
send_error(plug,E_BADCODE, r->icode, 0);
}
}
else if (plug->dest)
send_event(plug, r->itype, r->icode, r->ivalue);
else
send_error(plug, E_BADDOMAIN, 0 , 0xff);
}
void send_event(struct sock_plugin* plug, int t, int c, int v)
{
struct msg_dom0_input_event msg;
struct domain* d = plug->dest;
if ((NULL==d) || (NULL == d->client))
return;
if (t==EV_DEV)
{
if(c==DEV_SET)
{
plug->recived_slot=v;
d->last_devslot=v;
}
else // We don't want config events progated, since the VM will already know about it.
return;
}
msg.type = t;
msg.code = c;
msg.value = v;
dom0_input_event(d->client, &msg, sizeof(msg));
}
static void server_accept(int fd, short event, void *opaque)
{
int s = (long)opaque;
int s2;
socklen_t t;
struct sockaddr_un remote;
t = sizeof (remote);
if ((s2 = accept(s, (struct sockaddr *)&remote, &t)) == -1)
{
info("Accept failed with %s",strerror(errno));
return;
}
if (buffers.s==-1)
{
buffers.src=NULL;
buffers.dest=NULL;
buffers.slot=INPUTSLOT_INVALID;
buffers.bytes_remaining=0;
buffers.position=0;
buffers.recived_slot=INPUTSLOT_INVALID;
buffers.dropped=false;
buffers.slot_dropped=false;
buffers.error=0;
buffers.resetall=0;
buffers.deq_size=0;
buffers.dev_events=NULL;
buffers.delayed=0;
buffers.s=s2;
info("Accepting client for Unix domain socket.");
event_set (&server_recv_event, s2, EV_READ | EV_PERSIST, wrapper_server_recv, (void*)&buffers);
event_add (&server_recv_event, NULL);
}
else
{
info("Second connection not allowed\n");
close(s2);
}
}
void socket_server_init(void)
{
int len;
struct sockaddr_un local;
const char sock_path[]="/var/run/input_socket";
info("Opening Unix domain socket in %s",sock_path);
buffers.s=-1;
if ((server_sock_fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
info("socket failed with %s",strerror(errno));
return;
}
local.sun_family = AF_UNIX;
strcpy(local.sun_path, sock_path);
unlink(local.sun_path);
len = strlen(local.sun_path) + sizeof(local.sun_family);
if (bind(server_sock_fd, (struct sockaddr *)&local, len) == -1) {
info("bind failed with %s",strerror(errno));
close(server_sock_fd);
return;
}
if (listen(server_sock_fd, 5) == -1) {
info("listen failed with %s",strerror(errno));
close(server_sock_fd);
return;
}
event_set (&server_accept_event, server_sock_fd, EV_READ | EV_PERSIST, server_accept, (void*)server_sock_fd);
event_add (&server_accept_event, NULL);
}
void socket_server_close()
{
close(server_sock_fd);
free(buffers.dev_events);
}