From: Alyssa Ross <hi@alyssa.is>
To: devel@spectrum-os.org
Subject: [DEMO PATCH linux 2/3] usbip: tools: usbip: record protocol
Date: Wed, 13 Oct 2021 13:53:12 +0000 [thread overview]
Message-ID: <20211013135313.2494599-3-hi@alyssa.is> (raw)
In-Reply-To: <20211013135313.2494599-1-hi@alyssa.is>
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
next prev parent reply other threads:[~2021-10-13 13:54 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-13 13:53 [DEMO PATCH linux 0/3] usbip: tools: support USB over vsock(7) Alyssa Ross
2021-10-13 13:53 ` [DEMO PATCH linux 1/3] usbip: tools: in.usbipd: add Alyssa Ross
2021-10-13 13:53 ` Alyssa Ross [this message]
2021-10-13 13:53 ` [DEMO PATCH linux 3/3] usbip: tools: usbip: support vsock(7) connections Alyssa Ross
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211013135313.2494599-3-hi@alyssa.is \
--to=hi@alyssa.is \
--cc=devel@spectrum-os.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://spectrum-os.org/git/crosvm
https://spectrum-os.org/git/doc
https://spectrum-os.org/git/mktuntap
https://spectrum-os.org/git/nixpkgs
https://spectrum-os.org/git/spectrum
https://spectrum-os.org/git/ucspi-vsock
https://spectrum-os.org/git/www
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).