-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathusbip.c
617 lines (524 loc) · 12.2 KB
/
usbip.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
/*
$Id$
*/
#include "usbip.h"
#include "getopt.h"
#define _GNU_SOURCE
#include <WINSOCK2.H>
#include <STDIO.H>
#pragma comment(lib,"ws2_32.lib")
static const char version[] = "usbip for windows ($Id$)";
int usbip_use_debug=0;
int usbip_use_syslog=0;
int usbip_use_stderr=1;
static const struct option longopts[] = {
{"attach", no_argument, NULL, 'a'},
{"attachall", no_argument, NULL, 'x'},
{"detach", no_argument, NULL, 'd'},
{"port", no_argument, NULL, 'p'},
{"list", no_argument, NULL, 'l'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{"debug", no_argument, NULL, 'D'},
{"syslog", no_argument, NULL, 'S'},
{NULL, 0, NULL, 0}
};
enum {
CMD_ATTACH = 1,
CMD_ATTACHALL,
CMD_DETACH,
CMD_PORT,
CMD_LIST,
CMD_HELP,
CMD_VERSION
};
unsigned int parse_opt(int argc, char *argv[])
{
int cmd = 0;
for (;;) {
int c;
int index = 0;
c = getopt_long(argc, argv, "adplvhDSx", longopts, &index);
if (c == -1)
break;
switch(c) {
case 'a':
if (!cmd)
cmd = CMD_ATTACH;
else
cmd = CMD_HELP;
break;
case 'd':
if (!cmd)
cmd = CMD_DETACH;
else
cmd = CMD_HELP;
break;
case 'p':
if (!cmd)
cmd = CMD_PORT;
else
cmd = CMD_HELP;
break;
case 'l':
if (!cmd)
cmd = CMD_LIST;
else
cmd = CMD_HELP;
break;
case 'v':
if (!cmd)
cmd = CMD_VERSION;
else
cmd = CMD_HELP;
break;
case 'x':
if(!cmd)
cmd = CMD_ATTACHALL;
else
cmd = CMD_HELP;
break;
case 'h':
cmd = CMD_HELP;
break;
case 'D':
usbip_use_debug = 1;
break;
case 'S':
usbip_use_syslog = 1;
break;
case '?':
break;
default:
err("getopt");
}
}
if(!cmd)
cmd = CMD_HELP;
return cmd;
}
int query_interface0(SOCKET sockfd, char * busid, struct usb_interface * uinf0)
{
int ret;
struct op_devlist_reply rep;
uint16_t code = OP_REP_DEVLIST;
uint32_t i,j;
char product_name[100];
char class_name[100];
struct usb_device udev;
struct usb_interface uinf;
int found=0;
memset(&rep, 0, sizeof(rep));
ret = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
if (ret < 0) {
err("send op_common");
return -1;
}
ret = usbip_recv_op_common(sockfd, &code);
if (ret < 0) {
err("recv op_common");
return -1;
}
ret = usbip_recv(sockfd, (void *) &rep, sizeof(rep));
if (ret < 0) {
err("recv op_devlist");
return -1;
}
PACK_OP_DEVLIST_REPLY(0, &rep);
dbg("exportable %d devices", rep.ndev);
for (i=0; i < rep.ndev; i++) {
memset(&udev, 0, sizeof(udev));
ret = usbip_recv(sockfd, (void *) &udev, sizeof(udev));
if (ret < 0) {
err("recv usb_device[%d]", i);
return -1;
}
pack_usb_device(0, &udev);
usbip_names_get_product(product_name, sizeof(product_name),
udev.idVendor, udev.idProduct);
usbip_names_get_class(class_name, sizeof(class_name), udev.bDeviceClass,
udev.bDeviceSubClass, udev.bDeviceProtocol);
dbg("%8s: %s", udev.busid, product_name);
dbg("%8s: %s", " ", udev.path);
dbg("%8s: %s", " ", class_name);
for (j=0; j < udev.bNumInterfaces; j++) {
ret = usbip_recv(sockfd, (void *) &uinf, sizeof(uinf));
if (ret < 0) {
err("recv usb_interface[%d]", j);
return -1;
}
pack_usb_interface(0, &uinf);
if(!strcmp(udev.busid, busid)&&j==0){
memcpy(uinf0, &uinf, sizeof(uinf));
found=1;
}
usbip_names_get_class(class_name, sizeof(class_name),
uinf.bInterfaceClass,
uinf.bInterfaceSubClass,
uinf.bInterfaceProtocol);
dbg("%8s: %2d - %s", " ", j, class_name);
}
dbg(" ");
}
if(found)
return 0;
return -1;
}
int import_device(int sockfd, struct usb_device *udev,
struct usb_interface *uinf0,
HANDLE *devfd)
{
HANDLE fd;
int port, ret;
fd = usbip_vbus_open();
if (INVALID_HANDLE_VALUE == fd) {
err("open vbus driver");
return -1;
}
port = usbip_vbus_get_free_port(fd);
printf("port == %d\n",port);
if (port <= 0) {
err("no free port");
CloseHandle(fd);
return -1;
}
dbg("call from attch here\n");
ret = usbip_vbus_attach_device(fd, port, udev, uinf0);
dbg("return from attch here\n");
if (ret < 0) {
err("import device");
CloseHandle(fd);
return -1;
}
dbg("devfd:%p\n",devfd);
*devfd=fd;
return port;
}
int query_import_device(int sockfd, char *busid,
struct usb_interface *uinf0, HANDLE * fd)
{
int ret;
struct op_import_request request;
struct op_import_reply reply;
uint16_t code = OP_REP_IMPORT;
memset(&request, 0, sizeof(request));
memset(&reply, 0, sizeof(reply));
/* send a request */
ret = usbip_send_op_common(sockfd, OP_REQ_IMPORT, 0);
if (ret < 0) {
err("send op_common");
return -1;
}
strncpy(request.busid, busid, sizeof(request.busid));
request.busid[sizeof(request.busid)-1]=0;
PACK_OP_IMPORT_REQUEST(0, &request);
ret = usbip_send(sockfd, (void *) &request, sizeof(request));
if (ret < 0) {
err("send op_import_request");
return -1;
}
/* recieve a reply */
ret = usbip_recv_op_common(sockfd, &code);
if (ret < 0) {
err("recv op_common");
return -1;
}
ret = usbip_recv(sockfd, (void *) &reply, sizeof(reply));
if (ret < 0) {
err("recv op_import_reply");
return -1;
}
PACK_OP_IMPORT_REPLY(0, &reply);
/* check the reply */
if (strncmp(reply.udev.busid, busid, sizeof(reply.udev.busid))) {
err("recv different busid %s", reply.udev.busid);
return -1;
}
/* import a device */
return import_device(sockfd, &reply.udev, uinf0, fd);
}
//static void attach_device(char * host, char * busid)
__declspec(dllexport) int __cdecl attach_device(char * host, char * busid ,char * wip)
{
SOCKET sockfd;
int rhport;
HANDLE devfd=INVALID_HANDLE_VALUE;
struct usb_interface uinf;
printf("attach_device dll test: %s %s\n",host,busid);
sockfd = tcp_connect(host, wip);
if (INVALID_SOCKET == sockfd) {
err("tcp connect");
return -1;
}
if(query_interface0(sockfd, busid, &uinf)){
err("cannot find device");
return -1;
}
closesocket(sockfd);
sockfd = tcp_connect(host, wip);
if (INVALID_SOCKET == sockfd) {
err("tcp connect");
return -1;
}
rhport = query_import_device(sockfd, busid, &uinf, &devfd);
if (rhport < 0) {
err("query");
return -1;
}
info("new usb device attached to usbvbus port %d\n", rhport);
usbip_vbus_forward(sockfd, devfd);
dbg("closing connection to device");
CloseHandle(devfd);
dbg("detaching device");
usbip_vbus_detach_device(devfd,rhport);
dbg("closing connection to peer");
closesocket(sockfd);
dbg("done");
return rhport;
}
const char help_message[] = "\
Usage: %s [options] \n\
-a, --attach [host] [bus_id] \n\
Attach a remote USB device. \n\
\n\
-x, --attachall [host] \n\
Attach all remote USB devices on the specific host. \n\
\n\
-d, --detach [ports] \n\
Detach an imported USB device. \n\
\n\
-l, --list [hosts] \n\
List exported USB devices. \n\
\n\
-p, --port \n\
List virtual USB port status. \n\
\n\
-D, --debug \n\
Print debugging information. \n\
\n\
-v, --version \n\
Show version. \n\
\n\
-h, --help \n\
Print this help. \n";
__declspec(dllexport) void __cdecl show_help(char *name)
{
printf(help_message, name);
}
__declspec(dllexport) int __cdecl detach_port(char *port)
{
signed char addr=atoi(port);
HANDLE fd;
int ret;
fd = usbip_vbus_open();
if (INVALID_HANDLE_VALUE == fd) {
err("open vbus driver");
return -1;
}
ret = usbip_vbus_detach_device(fd, addr);
CloseHandle(fd);
return ret;
}
__declspec(dllexport) int __cdecl show_port_status(void)
{
HANDLE fd;
int i;
char buf[128];
fd = usbip_vbus_open();
if (INVALID_HANDLE_VALUE == fd) {
err("open vbus driver ");
return -1;
}
if(usbip_vbus_get_ports_status(fd, buf, sizeof(buf))){
err("get port status");
return -1;
}
info("max used port:%d\n", buf[0]);
for(i=1; i<=buf[0]; i++){
if(buf[i])
info("port %d: used\n", i);
else
info("port %d: idle\n", i);
}
CloseHandle(fd);
return 0;
}
int query_exported_devices(SOCKET sockfd)
{
int ret;
struct op_devlist_reply rep;
uint16_t code = OP_REP_DEVLIST;
uint32_t i,j;
char product_name[100];
char class_name[100];
struct usb_device udev;
memset(&rep, 0, sizeof(rep));
ret = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
if (ret < 0) {
err("send op_common");
return -1;
}
ret = usbip_recv_op_common(sockfd, &code);
if (ret < 0) {
err("recv op_common");
return -1;
}
ret = usbip_recv(sockfd, (void *) &rep, sizeof(rep));
if (ret < 0) {
err("recv op_devlist");
return -1;
}
PACK_OP_DEVLIST_REPLY(0, &rep);
dbg("exportable %d devices", rep.ndev);
for (i=0; i < rep.ndev; i++) {
memset(&udev, 0, sizeof(udev));
ret = usbip_recv(sockfd, (void *) &udev, sizeof(udev));
if (ret < 0) {
err("recv usb_device[%d]", i);
return -1;
}
pack_usb_device(0, &udev);
usbip_names_get_product(product_name, sizeof(product_name),
udev.idVendor, udev.idProduct);
usbip_names_get_class(class_name, sizeof(class_name), udev.bDeviceClass,
udev.bDeviceSubClass, udev.bDeviceProtocol);
info("%8s: %s", udev.busid, product_name);
info("%8s: %s", " ", udev.path);
info("%8s: %s", " ", class_name);
for (j=0; j < udev.bNumInterfaces; j++) {
struct usb_interface uinf;
ret = usbip_recv(sockfd, (void *) &uinf, sizeof(uinf));
if (ret < 0) {
err("recv usb_interface[%d]", j);
return -1;
}
pack_usb_interface(0, &uinf);
usbip_names_get_class(class_name, sizeof(class_name),
uinf.bInterfaceClass,
uinf.bInterfaceSubClass,
uinf.bInterfaceProtocol);
info("%8s: %2d - %s", " ", j, class_name);
}
info(" ");
}
return rep.ndev;
}
__declspec(dllexport) void __cdecl show_exported_devices(char *host)
{
int ret;
SOCKET sockfd;
sockfd = tcp_connect(host, USBIP_PORT_STRING);
if (INVALID_SOCKET == sockfd){
info("- %s failed", host);
return;
}
info("- %s", host);
ret = query_exported_devices(sockfd);
if (ret < 0) {
err("query");
}
closesocket(sockfd);
}
__declspec(dllexport) void __cdecl attach_devices_all(char *host)
{
return;
}
__declspec(dllexport) int __cdecl init_winsock_dll(void)
{
unsigned short version = 0x202; /* winsock 2.2 */
WSADATA data;
int ret;
ret = WSAStartup( version, &data);
if (ret)
return -1;
return 0;
}
int
main(int argc, char *argv[])
{
int cmd;
if(init_winsock()){
err("can't init winsock");
return 0;
}
cmd = parse_opt(argc, argv);
switch(cmd) {
case CMD_ATTACH:
if (optind == argc - 2)
//attach_device(argv[optind], argv[optind+1]);
attach_device(argv[optind], argv[optind+1],argv[optind+2]);
else
show_help(argv[0]);
break;
case CMD_DETACH:
while (optind < argc)
detach_port(argv[optind++]);
break;
case CMD_PORT:
show_port_status();
break;
case CMD_LIST:
while (optind < argc)
show_exported_devices(argv[optind++]);
break;
case CMD_ATTACHALL:
while(optind < argc)
attach_devices_all(argv[optind++]);
break;
case CMD_VERSION:
printf("%s\n", version);
break;
case CMD_HELP:
show_help(argv[0]);
break;
default:
show_help(argv[0]);
}
return 0;
}
__declspec(dllexport) char* __cdecl Send_Control(SOCKET opens,char *cstring)
{
char buff[255];
int ret;
//memset(buff,0,);
send(opens,cstring,strlen(cstring),0);
//int i = recv(opens,buff,100,0);
ret = recv(opens, buff, 255, 0);
if(ret > 0)
{
buff[ret] = 0x00;
//printf(buff);
return buff;
}
//printf("length =====\n",i);
return buff;
}
__declspec(dllexport) void __cdecl Send_HearBeat(SOCKET s,char *cstring)
{
send(s,cstring,strlen(cstring),0);
}
/*__declspec(dllexport) char* __cdecl Send_Control(char *ip,int port ,char *cstring)
{
char buff[255];
int ret;
//memset(buff,0,sizeof(buff));
SOCKET sclient = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
struct sockaddr_in serAddr;
serAddr.sin_family = AF_INET;
serAddr.sin_port = htons(port);
serAddr.sin_addr.S_un.S_addr = inet_addr(ip);
if (connect(sclient, (struct sockaddr *)&serAddr, sizeof(serAddr)) == SOCKET_ERROR)
{
printf("connect error !");
closesocket(sclient);
return 0;
}
send(sclient,cstring,strlen(cstring),0);
ret = recv(sclient, buff, 255, 0);
if(ret > 0)
{
buff[ret] = 0x00;
printf(buff);
return buff;
}
}*/