added windows support over usbip-win2
This commit is contained in:
@@ -3,16 +3,58 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/duffy/usb-server/internal/protocol"
|
||||
"github.com/duffy/usb-server/internal/usbip"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
// createVHCIAttachment creates a VHCI attachment on Linux using socketpair + sysfs.
|
||||
// Returns the tunnel connection (our end of the socketpair), the VHCI port number, and any error.
|
||||
func createVHCIAttachment(_ context.Context, granted *protocol.DeviceGranted, _ *RemoteDevice) (net.Conn, int, error) {
|
||||
// Create a socketpair - one end for VHCI, one for our tunnel
|
||||
fds, err := createSocketPair()
|
||||
if err != nil {
|
||||
return nil, -1, fmt.Errorf("creating socketpair: %w", err)
|
||||
}
|
||||
|
||||
vhciFD := fds[0]
|
||||
tunnelFD := fds[1]
|
||||
|
||||
// Find a free VHCI port
|
||||
port, err := usbip.FindFreePort(granted.Speed)
|
||||
if err != nil {
|
||||
closeFDs(fds)
|
||||
return nil, -1, fmt.Errorf("finding free VHCI port: %w", err)
|
||||
}
|
||||
|
||||
// Attach to VHCI
|
||||
if err := usbip.AttachDevice(port, vhciFD, granted.DevID, granted.Speed); err != nil {
|
||||
closeFDs(fds)
|
||||
return nil, -1, fmt.Errorf("VHCI attach: %w", err)
|
||||
}
|
||||
|
||||
// The VHCI driver now owns vhciFD, so we don't close it
|
||||
// Create a net.Conn from the tunnel FD
|
||||
tunnelFile := fdToFile(tunnelFD, "usb-tunnel")
|
||||
tunnelConn, err := net.FileConn(tunnelFile)
|
||||
tunnelFile.Close() // FileConn dups the fd
|
||||
if err != nil {
|
||||
usbip.DetachDevice(port)
|
||||
return nil, -1, fmt.Errorf("creating tunnel conn: %w", err)
|
||||
}
|
||||
|
||||
return tunnelConn, port, nil
|
||||
}
|
||||
|
||||
// createSocketPair creates a Unix domain socket pair
|
||||
func createSocketPair() ([2]int, error) {
|
||||
fds, err := unix.Socketpair(unix.AF_UNIX, unix.SOCK_STREAM, 0)
|
||||
|
||||
Reference in New Issue
Block a user