This is the first step toward supporting protocol families other than IP in the USB/IP client. This changes the format of the connection records in the filesystem, so all USB/IP devices should be detached before applying this change. The output format for "usbip port" remains the same in the default case. If VSOCK is being used, it will display URLs starting with usbvsock:// instead of usbip://. Signed-off-by: Alyssa Ross <hi@alyssa.is> --- tools/usb/usbip/libsrc/vhci_driver.c | 26 +++++++++++++------------ tools/usb/usbip/src/usbip_attach.c | 29 ++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/tools/usb/usbip/libsrc/vhci_driver.c b/tools/usb/usbip/libsrc/vhci_driver.c index 8159fd98680b..98945078ccf4 100644 --- a/tools/usb/usbip/libsrc/vhci_driver.c +++ b/tools/usb/usbip/libsrc/vhci_driver.c @@ -184,16 +184,17 @@ static int get_ncontrollers(void) * which is needed to properly validate the 3rd part without it being * truncated to an acceptable length. */ -static int read_record(int rhport, char *host, unsigned long host_len, - char *port, unsigned long port_len, char *busid) +static int read_record(int rhport, char *proto, unsigned long proto_len, + char *host, unsigned long host_len, char *port, + unsigned long port_len, char *busid) { int part; FILE *file; char path[PATH_MAX+1]; char *buffer, *start, *end; - char delim[] = {' ', ' ', '\n'}; - int max_len[] = {(int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE}; - size_t buffer_len = host_len + port_len + SYSFS_BUS_ID_SIZE + 4; + char delim[] = {' ', ' ', ' ', '\n'}; + int max_len[] = {(int)proto_len, (int)host_len, (int)port_len, SYSFS_BUS_ID_SIZE}; + size_t buffer_len = proto_len + host_len + port_len + SYSFS_BUS_ID_SIZE + 5; buffer = malloc(buffer_len); if (!buffer) @@ -218,7 +219,7 @@ static int read_record(int rhport, char *host, unsigned long host_len, /* validate the length of each of the 3 parts */ start = buffer; - for (part = 0; part < 3; part++) { + for (part = 0; part < 4; part++) { end = strchr(start, delim[part]); if (end == NULL || (end - start) > max_len[part]) { free(buffer); @@ -227,7 +228,7 @@ static int read_record(int rhport, char *host, unsigned long host_len, start = end + 1; } - if (sscanf(buffer, "%s %s %s\n", host, port, busid) != 3) { + if (sscanf(buffer, "%s %s %s %s\n", proto, host, port, busid) != 4) { err("sscanf"); free(buffer); return -1; @@ -426,6 +427,7 @@ int usbip_vhci_detach_device(uint8_t port) int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev) { char product_name[100]; + char proto[] = "unknown protocol"; char host[NI_MAXHOST] = "unknown host"; char serv[NI_MAXSERV] = "unknown port"; char remote_busid[SYSFS_BUS_ID_SIZE]; @@ -435,8 +437,8 @@ int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev) if (idev->status == VDEV_ST_NULL || idev->status == VDEV_ST_NOTASSIGNED) return 0; - ret = read_record(idev->port, host, sizeof(host), serv, sizeof(serv), - remote_busid); + ret = read_record(idev->port, proto, sizeof(proto), host, sizeof(host), + serv, sizeof(serv), remote_busid); if (ret) { err("read_record"); read_record_error = 1; @@ -452,12 +454,12 @@ int usbip_vhci_imported_device_dump(struct usbip_imported_device *idev) printf(" %s\n", product_name); if (!read_record_error) { - printf("%10s -> usbip://%s:%s/%s\n", idev->udev.busid, - host, serv, remote_busid); + printf("%10s -> usb%s://%s:%s/%s\n", idev->udev.busid, + proto, host, serv, remote_busid); printf("%10s -> remote bus/dev %03d/%03d\n", " ", idev->busnum, idev->devnum); } else { - printf("%10s -> unknown host, remote port and remote busid\n", + printf("%10s -> unknown protocol, host, remote port and remote busid\n", idev->udev.busid); printf("%10s -> remote bus/dev %03d/%03d\n", " ", idev->busnum, idev->devnum); diff --git a/tools/usb/usbip/src/usbip_attach.c b/tools/usb/usbip/src/usbip_attach.c index b4aeb9f1f493..b202c7513bc3 100644 --- a/tools/usb/usbip/src/usbip_attach.c +++ b/tools/usb/usbip/src/usbip_attach.c @@ -8,6 +8,7 @@ */ #include <sys/stat.h> +#include <sys/socket.h> #include <limits.h> #include <stdint.h> @@ -36,7 +37,7 @@ void usbip_attach_usage(void) } #define MAX_BUFF 100 -static int record_connection(char *host, char *port, char *busid, int rhport) +static int record_connection(char *proto, char *host, char *port, char *busid, int rhport) { int fd; char path[PATH_MAX+1]; @@ -64,8 +65,8 @@ static int record_connection(char *host, char *port, char *busid, int rhport) if (fd < 0) return -1; - snprintf(buff, MAX_BUFF, "%s %s %s\n", - host, port, busid); + snprintf(buff, MAX_BUFF, "%s %s %s %s\n", + proto, host, port, busid); ret = write(fd, buff, strlen(buff)); if (ret != (ssize_t) strlen(buff)) { @@ -171,11 +172,28 @@ static int query_import_device(int sockfd, char *busid) return import_device(sockfd, &reply.udev); } +static int get_protocol_family(int fd, char **proto) +{ + struct sockaddr addr; + socklen_t addrlen = sizeof addr; + if (getsockname(fd, &addr, &addrlen) == -1) + return -1; + + switch (addr.sa_family) { + case AF_INET: + case AF_INET6: + *proto = "ip"; + return 0; + } + return -1; +} + static int attach_device(char *host, char *busid) { int sockfd; int rc; int rhport; + char *pf; sockfd = usbip_net_tcp_connect(host, usbip_port_string); if (sockfd < 0) { @@ -183,13 +201,16 @@ static int attach_device(char *host, char *busid) return -1; } + if (get_protocol_family(sockfd, &pf) == -1) + return -1; + rhport = query_import_device(sockfd, busid); if (rhport < 0) return -1; close(sockfd); - rc = record_connection(host, usbip_port_string, busid, rhport); + rc = record_connection(pf, host, usbip_port_string, busid, rhport); if (rc < 0) { err("record connection"); return -1; -- 2.32.0