1
1
import SocketServer
2
2
import struct
3
3
import types
4
+ from bitstring import Bits
4
5
5
6
# Hey StackOverflow !
6
7
class bcolors :
@@ -208,9 +209,17 @@ def pack(self):
208
209
packed_data += self .data
209
210
return packed_data
210
211
212
+ class USBIPCMDUnlink (BaseStucture ):
213
+ _fields_ = [
214
+ ('seqnum' , 'I' ),
215
+ ('devid' , 'I' ),
216
+ ('direction' , 'I' ),
217
+ ('ep' , 'I' ),
218
+ ('seqnum2' , 'I' ),
219
+ ]
220
+
211
221
class USBIPCMDSubmit (BaseStucture ):
212
222
_fields_ = [
213
- ('command' , 'I' ),
214
223
('seqnum' , 'I' ),
215
224
('devid' , 'I' ),
216
225
('direction' , 'I' ),
@@ -529,28 +538,40 @@ def handle(self):
529
538
else :
530
539
#print '----------------'
531
540
#print 'handles requests'
532
- cmd = USBIPCMDSubmit ()
533
- data = self .request .recv (cmd .size ())
534
- cmd .unpack (data )
535
- #print "usbip cmd {}".format(cmd.command,'x')
536
- #print "usbip seqnum {}".format(cmd.seqnum,'x')
537
- #print "usbip devid {}".format(cmd.devid,'x')
538
- #print "usbip direction {}".format(cmd.direction,'x')
539
- #print "usbip ep {}".format(cmd.ep,'x')
540
- #print "usbip flags {}".format(cmd.transfer_flags,'x')
541
- #print "usbip number of packets {}".format(cmd.number_of_packets,'x')
542
- #print "usbip interval {}".format(cmd.interval,'x')
543
- #print "usbip setup {}".format(cmd.setup,'x')
544
- #print "usbip buffer lenght {}".format(cmd.transfer_buffer_length,'x')
545
- usb_req = USBRequest (seqnum = cmd .seqnum ,
546
- devid = cmd .devid ,
547
- direction = cmd .direction ,
548
- ep = cmd .ep ,
549
- flags = cmd .transfer_flags ,
550
- numberOfPackets = cmd .number_of_packets ,
551
- interval = cmd .interval ,
552
- setup = cmd .setup ,
553
- data = data )
554
- self .server .usbcontainer .usb_devices [self .attachedBusID ].connection = self .request
555
- self .server .usbcontainer .usb_devices [self .attachedBusID ].handle_usb_request (usb_req )
541
+ command = self .request .recv (4 )
542
+ if (command == 0x00000003 ):
543
+ cmd = USBIPCMDUnlink ()
544
+ data = self .request .recv (cmd .size ())
545
+ cmd .unpack (data )
546
+ print '[' + bcolors .OKBLUE + 'USBIP' + bcolors .ENDC + '] Detaching device with seqnum' , cmd .seqnum
547
+ # We probably don't even need to handle that, the windows client doesn't even send this packet
548
+ else :
549
+ cmd = USBIPCMDSubmit ()
550
+ data = self .request .recv (cmd .size ())
551
+ cmd .unpack (data )
552
+ #print "usbip cmd {}".format(cmd.command,'x')
553
+ #print "usbip seqnum {}".format(cmd.seqnum,'x')
554
+ #print "usbip devid {}".format(cmd.devid,'x')
555
+ #print "usbip direction {}".format(cmd.direction,'x')
556
+ #print "usbip ep {}".format(cmd.ep,'x')
557
+ #print "usbip flags {}".format(cmd.transfer_flags,'x')
558
+ #print "usbip number of packets {}".format(cmd.number_of_packets,'x')
559
+ #print "usbip interval {}".format(cmd.interval,'x')
560
+ #print "usbip setup {}".format(cmd.setup,'x')
561
+ #print "usbip buffer lenght {}".format(cmd.transfer_buffer_length,'x')
562
+ usb_req = USBRequest (seqnum = cmd .seqnum ,
563
+ devid = cmd .devid ,
564
+ direction = cmd .direction ,
565
+ ep = cmd .ep ,
566
+ flags = cmd .transfer_flags ,
567
+ numberOfPackets = cmd .number_of_packets ,
568
+ interval = cmd .interval ,
569
+ setup = cmd .setup ,
570
+ data = data )
571
+ self .server .usbcontainer .usb_devices [self .attachedBusID ].connection = self .request
572
+ try :
573
+ self .server .usbcontainer .usb_devices [self .attachedBusID ].handle_usb_request (usb_req )
574
+ except :
575
+ print '[' + bcolors .FAIL + 'USBIP' + bcolors .ENDC + '] Connection with client ' + str (self .client_address ) + ' ended'
576
+ break
556
577
self .request .close ()
0 commit comments