This patch allows to substitute send, receive and shutdown routines for both a) request/response PDUs among utilities and b) user space URBs transmission. usbip_sock_t is introduced instead of sockfd. it includes function pointers of send/receive/shutdown routines, an argument for the routines, and a sockfd. The argument is needed for the routines. The sockfd is needed to bind connection to USB device. Succeeding SSL and WebSocket patch use this feature. Signed-off-by: Nobuo Iwata <nobuo.iwata@xxxxxxxxxxxxxxx> --- tools/usb/usbip/libsrc/usbip_common.c | 14 +++++ tools/usb/usbip/libsrc/usbip_common.h | 14 +++++ tools/usb/usbip/libsrc/usbip_ux.c | 24 ++++++-- tools/usb/usbip/libsrc/usbip_ux.h | 4 +- tools/usb/usbip/src/usbip_attach.c | 30 +++++----- tools/usb/usbip/src/usbip_connect.c | 30 +++++----- tools/usb/usbip/src/usbip_disconnect.c | 26 ++++----- tools/usb/usbip/src/usbip_list.c | 27 +++++---- tools/usb/usbip/src/usbip_network.c | 78 +++++++++++++++++++------- tools/usb/usbip/src/usbip_network.h | 12 ++-- tools/usb/usbip/src/usbipd.c | 14 +++-- tools/usb/usbip/src/usbipd_app.c | 36 ++++++------ tools/usb/usbip/src/usbipd_dev.c | 40 ++++++------- 13 files changed, 214 insertions(+), 135 deletions(-) diff --git a/tools/usb/usbip/libsrc/usbip_common.c b/tools/usb/usbip/libsrc/usbip_common.c index ac73710..dc0712c 100644 --- a/tools/usb/usbip/libsrc/usbip_common.c +++ b/tools/usb/usbip/libsrc/usbip_common.c @@ -1,5 +1,6 @@ /* * Copyright (C) 2005-2007 Takahiro Hirofuchi + * Copyright (C) 2015 Nobuo Iwata */ #include <libudev.h> @@ -283,3 +284,16 @@ void usbip_names_get_class(char *buff, size_t size, uint8_t class, snprintf(buff, size, "%s / %s / %s (%02x/%02x/%02x)", c, s, p, class, subclass, protocol); } + +void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg, + ssize_t (*send)(void *arg, void *buf, size_t len), + ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all), + void (*shutdown)(void *arg)) +{ + sock->fd = fd; + sock->arg = arg; + sock->send = send; + sock->recv = recv; + sock->shutdown = shutdown; +} + diff --git a/tools/usb/usbip/libsrc/usbip_common.h b/tools/usb/usbip/libsrc/usbip_common.h index 15fe792..0dcbd99 100644 --- a/tools/usb/usbip/libsrc/usbip_common.h +++ b/tools/usb/usbip/libsrc/usbip_common.h @@ -1,5 +1,6 @@ /* * Copyright (C) 2005-2007 Takahiro Hirofuchi + * Copyright (C) 2015 Nobuo Iwata */ #ifndef __USBIP_COMMON_H @@ -134,4 +135,17 @@ void usbip_names_get_product(char *buff, size_t size, uint16_t vendor, void usbip_names_get_class(char *buff, size_t size, uint8_t class, uint8_t subclass, uint8_t protocol); +typedef struct usbip_sock { + int fd; + void *arg; + ssize_t (*send)(void *arg, void *buf, size_t len); + ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all); + void (*shutdown)(void *arg); +} usbip_sock_t; + +void usbip_sock_init(usbip_sock_t *sock, int fd, void *arg, + ssize_t (*send)(void *arg, void *buf, size_t len), + ssize_t (*recv)(void *arg, void *buf, size_t len, int wait_all), + void (*shutdown)(void *arg)); + #endif /* __USBIP_COMMON_H */ diff --git a/tools/usb/usbip/libsrc/usbip_ux.c b/tools/usb/usbip/libsrc/usbip_ux.c index 3ac45a72..aa3d863 100644 --- a/tools/usb/usbip/libsrc/usbip_ux.c +++ b/tools/usb/usbip/libsrc/usbip_ux.c @@ -57,7 +57,11 @@ static void *usbip_ux_rx(void *arg) char buf[BLEN]; while(good) { - received = recv(ux->sockfd, buf, BLEN, 0); + if (ux->sock->recv) { + received = ux->sock->recv(ux->sock->arg, buf, BLEN, 0); + } else { + received = recv(ux->sock->fd, buf, BLEN, 0); + } if (received == 0) { dbg("connection closed on sock:%p", ux->kaddr.sock); break; @@ -101,7 +105,11 @@ static void *usbip_ux_tx(void *arg) break; } dump_buff(buf, reads, "ux sending"); - sent = send(ux->sockfd, buf, reads, 0); + if (ux->sock->send) { + sent = ux->sock->send(ux->sock->arg, buf, reads); + } else { + sent = send(ux->sock->fd, buf, reads, 0); + } if (sent < 0) { dbg("connection closed on sock:%p", ux->kaddr.sock); break; @@ -112,7 +120,11 @@ static void *usbip_ux_tx(void *arg) } } dbg("end of ux-tx for sock:%p", ux->kaddr.sock); - shutdown(ux->sockfd, SHUT_RDWR); + if (ux->sock->shutdown) { + ux->sock->shutdown(ux->sock->arg); + } else { + shutdown(ux->sock->fd, SHUT_RDWR); + } return 0; } @@ -120,7 +132,7 @@ static void *usbip_ux_tx(void *arg) * Setup user space mode. * Null will be set in ux if usbip_ux.ko is not installed. */ -int usbip_ux_setup(int sockfd, usbip_ux_t **uxp) +int usbip_ux_setup(usbip_sock_t *sock, usbip_ux_t **uxp) { usbip_ux_t *ux; int fd, ret; @@ -140,8 +152,8 @@ int usbip_ux_setup(int sockfd, usbip_ux_t **uxp) } ux->devfd = fd; ux->started = 0; - ux->sockfd = sockfd; - ret = ioctl(ux->devfd, USBIP_UX_IOCSETSOCKFD, sockfd); + ux->sock = sock; + ret = ioctl(ux->devfd, USBIP_UX_IOCSETSOCKFD, sock->fd); if (ret) { dbg("failed to set sock fd"); goto err_free; diff --git a/tools/usb/usbip/libsrc/usbip_ux.h b/tools/usb/usbip/libsrc/usbip_ux.h index 943008e..222fce5 100644 --- a/tools/usb/usbip/libsrc/usbip_ux.h +++ b/tools/usb/usbip/libsrc/usbip_ux.h @@ -12,14 +12,14 @@ #include "usbip_common.h" typedef struct usbip_ux { - int sockfd; + usbip_sock_t *sock; int devfd; int started; pthread_t tx, rx; struct usbip_ux_kaddr kaddr; } usbip_ux_t; -int usbip_ux_setup(int sockfd, usbip_ux_t **uxp); +int usbip_ux_setup(usbip_sock_t *sock, usbip_ux_t **uxp); void usbip_ux_cleanup(usbip_ux_t **ux); int usbip_ux_start(usbip_ux_t *ux); void usbip_ux_join(usbip_ux_t *ux); diff --git a/tools/usb/usbip/src/usbip_attach.c b/tools/usb/usbip/src/usbip_attach.c index 0441e17..ae0ca6e 100644 --- a/tools/usb/usbip/src/usbip_attach.c +++ b/tools/usb/usbip/src/usbip_attach.c @@ -45,7 +45,7 @@ void usbip_attach_usage(void) printf("usage: %s", usbip_attach_usage_string); } -static int import_device(int sockfd, struct usbip_usb_device *udev) +static int import_device(usbip_sock_t *sock, struct usbip_usb_device *udev) { int rc; int port; @@ -63,7 +63,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev) return -1; } - rc = usbip_vhci_attach_device(port, sockfd, udev->busnum, + rc = usbip_vhci_attach_device(port, sock->fd, udev->busnum, udev->devnum, udev->speed); if (rc < 0) { err("import device"); @@ -76,7 +76,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev) return port; } -static int query_import_device(int sockfd, char *busid) +static int query_import_device(usbip_sock_t *sock, char *busid) { int rc; struct op_import_request request; @@ -87,7 +87,7 @@ static int query_import_device(int sockfd, char *busid) memset(&reply, 0, sizeof(reply)); /* send a request */ - rc = usbip_net_send_op_common(sockfd, OP_REQ_IMPORT, 0); + rc = usbip_net_send_op_common(sock, OP_REQ_IMPORT, 0); if (rc < 0) { err("send op_common"); return -1; @@ -97,20 +97,20 @@ static int query_import_device(int sockfd, char *busid) PACK_OP_IMPORT_REQUEST(0, &request); - rc = usbip_net_send(sockfd, (void *) &request, sizeof(request)); + rc = usbip_net_send(sock, (void *) &request, sizeof(request)); if (rc < 0) { err("send op_import_request"); return -1; } /* receive a reply */ - rc = usbip_net_recv_op_common(sockfd, &code); + rc = usbip_net_recv_op_common(sock, &code); if (rc < 0) { err("recv op_common"); return -1; } - rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply)); + rc = usbip_net_recv(sock, (void *) &reply, sizeof(reply)); if (rc < 0) { err("recv op_import_reply"); return -1; @@ -125,29 +125,29 @@ static int query_import_device(int sockfd, char *busid) } /* import a device */ - return import_device(sockfd, &reply.udev); + return import_device(sock, &reply.udev); } static int attach_device(char *host, char *busid) { - int sockfd; + usbip_sock_t *sock; usbip_ux_t *ux; int rc; int rhport; - sockfd = usbip_net_tcp_connect(host, usbip_port_string); - if (sockfd < 0) { + sock = usbip_net_tcp_connect(host, usbip_port_string); + if (!sock) { err("tcp connect"); goto err_out; } - rc = usbip_ux_setup(sockfd, &ux); + rc = usbip_ux_setup(sock, &ux); if (rc) { err("ux setup"); goto err_close_conn; } - rhport = query_import_device(sockfd, busid); + rhport = query_import_device(sock, busid); if (rhport < 0) { err("query"); goto err_cleanup_ux; @@ -164,13 +164,13 @@ static int attach_device(char *host, char *busid) usbip_ux_join(ux); } usbip_ux_cleanup(&ux); - close(sockfd); + usbip_net_tcp_close(sock); return 0; err_cleanup_ux: usbip_ux_cleanup(&ux); err_close_conn: - close(sockfd); + usbip_net_tcp_close(sock); err_out: return -1; } diff --git a/tools/usb/usbip/src/usbip_connect.c b/tools/usb/usbip/src/usbip_connect.c index 89caf93..5f9505c 100644 --- a/tools/usb/usbip/src/usbip_connect.c +++ b/tools/usb/usbip/src/usbip_connect.c @@ -43,7 +43,7 @@ void usbip_connect_usage(void) printf("usage: %s", usbip_connect_usage_string); } -static int send_export_device(int sockfd, struct usbip_usb_device *udev) +static int send_export_device(usbip_sock_t *sock, struct usbip_usb_device *udev) { int rc; struct op_export_request request; @@ -54,7 +54,7 @@ static int send_export_device(int sockfd, struct usbip_usb_device *udev) memset(&reply, 0, sizeof(reply)); /* send a request */ - rc = usbip_net_send_op_common(sockfd, OP_REQ_EXPORT, 0); + rc = usbip_net_send_op_common(sock, OP_REQ_EXPORT, 0); if (rc < 0) { err("send op_common"); return -1; @@ -64,20 +64,20 @@ static int send_export_device(int sockfd, struct usbip_usb_device *udev) PACK_OP_EXPORT_REQUEST(0, &request); - rc = usbip_net_send(sockfd, (void *) &request, sizeof(request)); + rc = usbip_net_send(sock, (void *) &request, sizeof(request)); if (rc < 0) { err("send op_export_request"); return -1; } /* receive a reply */ - rc = usbip_net_recv_op_common(sockfd, &code); + rc = usbip_net_recv_op_common(sock, &code); if (rc < 0) { err("recv op_common"); return -1; } - rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply)); + rc = usbip_net_recv(sock, (void *) &reply, sizeof(reply)); if (rc < 0) { err("recv op_export_reply"); return -1; @@ -94,7 +94,7 @@ static int send_export_device(int sockfd, struct usbip_usb_device *udev) return 0; } -static int export_device(char *busid, int sockfd) +static int export_device(char *busid, usbip_sock_t *sock) { int rc; struct usbip_exported_device *edev; @@ -119,14 +119,14 @@ static int export_device(char *busid, int sockfd) return -1; } - rc = send_export_device(sockfd, &edev->udev); + rc = send_export_device(sock, &edev->udev); if (rc < 0) { err("send export"); usbip_host_driver_close(); return -1; } - rc = usbip_host_export_device(edev, sockfd); + rc = usbip_host_export_device(edev, sock->fd); if (rc < 0) { err("export device"); usbip_host_driver_close(); @@ -140,7 +140,7 @@ static int export_device(char *busid, int sockfd) static int connect_device(char *host, char *busid) { - int sockfd; + usbip_sock_t *sock; usbip_ux_t *ux; int rc; @@ -150,19 +150,19 @@ static int connect_device(char *host, char *busid) goto err_out; } - sockfd = usbip_net_tcp_connect(host, usbip_port_string); - if (sockfd < 0) { + sock = usbip_net_tcp_connect(host, usbip_port_string); + if (!sock) { err("tcp connect"); goto err_unbind_device; } - rc = usbip_ux_setup(sockfd, &ux); + rc = usbip_ux_setup(sock, &ux); if (rc) { err("ux setup"); goto err_close_conn; } - rc = export_device(busid, sockfd); + rc = export_device(busid, sock); if (rc < 0) { err("export"); goto err_cleanup_ux; @@ -174,13 +174,13 @@ static int connect_device(char *host, char *busid) usbip_unbind_device(busid); } usbip_ux_cleanup(&ux); - close(sockfd); + usbip_net_tcp_close(sock); return 0; err_cleanup_ux: usbip_ux_cleanup(&ux); err_close_conn: - close(sockfd); + usbip_net_tcp_close(sock); err_unbind_device: usbip_unbind_device(busid); err_out: diff --git a/tools/usb/usbip/src/usbip_disconnect.c b/tools/usb/usbip/src/usbip_disconnect.c index 8adabe6..8da1717 100644 --- a/tools/usb/usbip/src/usbip_disconnect.c +++ b/tools/usb/usbip/src/usbip_disconnect.c @@ -43,7 +43,7 @@ void usbip_disconnect_usage(void) printf("usage: %s", usbip_disconnect_usage_string); } -static int send_unexport_device(int sockfd, struct usbip_usb_device *udev) +static int send_unexport_device(usbip_sock_t *sock, struct usbip_usb_device *udev) { int rc; struct op_unexport_request request; @@ -54,7 +54,7 @@ static int send_unexport_device(int sockfd, struct usbip_usb_device *udev) memset(&reply, 0, sizeof(reply)); /* send a request */ - rc = usbip_net_send_op_common(sockfd, OP_REQ_UNEXPORT, 0); + rc = usbip_net_send_op_common(sock, OP_REQ_UNEXPORT, 0); if (rc < 0) { err("send op_common"); return -1; @@ -64,20 +64,20 @@ static int send_unexport_device(int sockfd, struct usbip_usb_device *udev) PACK_OP_UNEXPORT_REQUEST(0, &request); - rc = usbip_net_send(sockfd, (void *) &request, sizeof(request)); + rc = usbip_net_send(sock, (void *) &request, sizeof(request)); if (rc < 0) { err("send op_export_request"); return -1; } /* receive a reply */ - rc = usbip_net_recv_op_common(sockfd, &code); + rc = usbip_net_recv_op_common(sock, &code); if (rc < 0) { err("recv op_common"); return -1; } - rc = usbip_net_recv(sockfd, (void *) &reply, sizeof(reply)); + rc = usbip_net_recv(sock, (void *) &reply, sizeof(reply)); if (rc < 0) { err("recv op_unexport_reply"); return -1; @@ -94,7 +94,7 @@ static int send_unexport_device(int sockfd, struct usbip_usb_device *udev) return 0; } -static int unexport_device(char *busid, int sockfd) +static int unexport_device(char *busid, usbip_sock_t *sock) { int rc; struct usbip_exported_device *edev; @@ -119,7 +119,7 @@ static int unexport_device(char *busid, int sockfd) return -1; } - rc = send_unexport_device(sockfd, &edev->udev); + rc = send_unexport_device(sock, &edev->udev); if (rc < 0) { err("send unexport"); usbip_host_driver_close(); @@ -133,23 +133,23 @@ static int unexport_device(char *busid, int sockfd) static int disconnect_device(char *host, char *busid) { - int sockfd; + usbip_sock_t *sock; int rc; - sockfd = usbip_net_tcp_connect(host, usbip_port_string); - if (sockfd < 0) { + sock = usbip_net_tcp_connect(host, usbip_port_string); + if (!sock) { err("tcp connect"); return -1; } - rc = unexport_device(busid, sockfd); + rc = unexport_device(busid, sock); if (rc < 0) { err("unexport"); - close(sockfd); + usbip_net_tcp_close(sock); return -1; } - close(sockfd); + usbip_net_tcp_close(sock); if (!usbip_ux_installed()) { rc = usbip_unbind_device(busid); diff --git a/tools/usb/usbip/src/usbip_list.c b/tools/usb/usbip/src/usbip_list.c index acbd9b5..cc86132 100644 --- a/tools/usb/usbip/src/usbip_list.c +++ b/tools/usb/usbip/src/usbip_list.c @@ -28,7 +28,6 @@ #include <string.h> #include <getopt.h> -#include <netdb.h> #include <unistd.h> #include "usbip_common.h" @@ -46,7 +45,7 @@ void usbip_list_usage(void) printf("usage: %s", usbip_list_usage_string); } -static int get_importable_devices(char *host, int sockfd) +static int get_importable_devices(char *host, usbip_sock_t *sock) { char product_name[100]; char class_name[100]; @@ -57,20 +56,20 @@ static int get_importable_devices(char *host, int sockfd) unsigned int i; int rc, j; - rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0); + rc = usbip_net_send_op_common(sock, OP_REQ_DEVLIST, 0); if (rc < 0) { dbg("usbip_net_send_op_common failed"); return -1; } - rc = usbip_net_recv_op_common(sockfd, &code); + rc = usbip_net_recv_op_common(sock, &code); if (rc < 0) { dbg("usbip_net_recv_op_common failed"); return -1; } memset(&reply, 0, sizeof(reply)); - rc = usbip_net_recv(sockfd, &reply, sizeof(reply)); + rc = usbip_net_recv(sock, &reply, sizeof(reply)); if (rc < 0) { dbg("usbip_net_recv_op_devlist failed"); return -1; @@ -89,7 +88,7 @@ static int get_importable_devices(char *host, int sockfd) for (i = 0; i < reply.ndev; i++) { memset(&udev, 0, sizeof(udev)); - rc = usbip_net_recv(sockfd, &udev, sizeof(udev)); + rc = usbip_net_recv(sock, &udev, sizeof(udev)); if (rc < 0) { dbg("usbip_net_recv failed: usbip_usb_device[%d]", i); return -1; @@ -106,7 +105,7 @@ static int get_importable_devices(char *host, int sockfd) printf("%11s: %s\n", "", class_name); for (j = 0; j < udev.bNumInterfaces; j++) { - rc = usbip_net_recv(sockfd, &uintf, sizeof(uintf)); + rc = usbip_net_recv(sock, &uintf, sizeof(uintf)); if (rc < 0) { err("usbip_net_recv failed: usbip_usb_intf[%d]", j); @@ -131,24 +130,24 @@ static int get_importable_devices(char *host, int sockfd) static int list_importable_devices(char *host) { int rc; - int sockfd; + usbip_sock_t *sock; - sockfd = usbip_net_tcp_connect(host, usbip_port_string); - if (sockfd < 0) { + sock = usbip_net_tcp_connect(host, usbip_port_string); + if (!sock) { err("could not connect to %s:%s: %s", host, - usbip_port_string, gai_strerror(sockfd)); + usbip_port_string, usbip_net_gai_strerror(sock->fd)); return -1; } dbg("connected to %s:%s", host, usbip_port_string); - rc = get_importable_devices(host, sockfd); + rc = get_importable_devices(host, sock); if (rc < 0) { err("failed to get device list from %s", host); - close(sockfd); + usbip_net_tcp_close(sock); return -1; } - close(sockfd); + usbip_net_tcp_close(sock); return 0; } diff --git a/tools/usb/usbip/src/usbip_network.c b/tools/usb/usbip/src/usbip_network.c index b4c37e7..7673e1a 100644 --- a/tools/usb/usbip/src/usbip_network.c +++ b/tools/usb/usbip/src/usbip_network.c @@ -1,5 +1,6 @@ /* - * Copyright (C) 2011 matt mooney <mfm@xxxxxxxxxxxxx> + * Copyright (C) 2015 Nobuo Iwata + * 2011 matt mooney <mfm@xxxxxxxxxxxxx> * 2005-2007 Takahiro Hirofuchi * * This program is free software: you can redistribute it and/or modify @@ -104,7 +105,7 @@ void usbip_net_pack_usb_interface(int pack __attribute__((unused)), /* uint8_t members need nothing */ } -static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen, +static ssize_t usbip_net_xmit(usbip_sock_t *sock, void *buff, size_t bufflen, int sending) { ssize_t nbytes; @@ -114,13 +115,22 @@ static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen, return 0; do { - if (sending) - nbytes = send(sockfd, buff, bufflen, 0); - else - nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL); - - if (nbytes <= 0) + if (sending) { + if (sock->send) + nbytes = sock->send(sock->arg, buff, bufflen); + else + nbytes = send(sock->fd, buff, bufflen, 0); + } else { + if (sock->recv) + nbytes = sock->recv(sock->arg, buff, bufflen, 1); + else + nbytes = recv(sock->fd, buff, bufflen, MSG_WAITALL); + } + if (nbytes <= 0) { + if (!sending && nbytes == 0) + dbg("received zero - broken connection?"); return -1; + } buff = (void *)((intptr_t) buff + nbytes); bufflen -= nbytes; @@ -131,17 +141,17 @@ static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen, return total; } -ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen) +ssize_t usbip_net_recv(usbip_sock_t *sock, void *buff, size_t bufflen) { - return usbip_net_xmit(sockfd, buff, bufflen, 0); + return usbip_net_xmit(sock, buff, bufflen, 0); } -ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen) +ssize_t usbip_net_send(usbip_sock_t *sock, void *buff, size_t bufflen) { - return usbip_net_xmit(sockfd, buff, bufflen, 1); + return usbip_net_xmit(sock, buff, bufflen, 1); } -int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status) +int usbip_net_send_op_common(usbip_sock_t *sock, uint32_t code, uint32_t status) { struct op_common op_common; int rc; @@ -154,7 +164,7 @@ int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status) PACK_OP_COMMON(1, &op_common); - rc = usbip_net_send(sockfd, &op_common, sizeof(op_common)); + rc = usbip_net_send(sock, &op_common, sizeof(op_common)); if (rc < 0) { dbg("usbip_net_send failed: %d", rc); return -1; @@ -163,14 +173,14 @@ int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status) return 0; } -int usbip_net_recv_op_common(int sockfd, uint16_t *code) +int usbip_net_recv_op_common(usbip_sock_t *sock, uint16_t *code) { struct op_common op_common; int rc; memset(&op_common, 0, sizeof(op_common)); - rc = usbip_net_recv(sockfd, &op_common, sizeof(op_common)); + rc = usbip_net_recv(sock, &op_common, sizeof(op_common)); if (rc < 0) { dbg("usbip_net_recv failed: %d", rc); goto err; @@ -258,10 +268,11 @@ int usbip_net_set_v6only(int sockfd) /* * IPv6 Ready */ -int usbip_net_tcp_connect(char *hostname, char *service) +usbip_sock_t *usbip_net_tcp_connect(char *hostname, char *service) { struct addrinfo hints, *res, *rp; int sockfd; + usbip_sock_t *sock; int ret; memset(&hints, 0, sizeof(hints)); @@ -272,8 +283,8 @@ int usbip_net_tcp_connect(char *hostname, char *service) ret = getaddrinfo(hostname, service, &hints, &res); if (ret < 0) { dbg("getaddrinfo: %s service %s: %s", hostname, service, - gai_strerror(ret)); - return ret; + usbip_net_gai_strerror(ret)); + return NULL; } /* try the addresses */ @@ -297,7 +308,32 @@ int usbip_net_tcp_connect(char *hostname, char *service) freeaddrinfo(res); if (!rp) - return EAI_SYSTEM; + return NULL; + + sock = (usbip_sock_t*)malloc(sizeof(usbip_sock_t)); + if (!sock) { + dbg("Fail to malloc usbip_sock"); + close(sockfd); + return NULL; + } + usbip_sock_init(sock, sockfd, NULL, NULL, NULL, NULL); + + return sock; +} + +void usbip_net_tcp_close(usbip_sock_t *sock) +{ + close(sock->fd); + free(sock); +} - return sockfd; +static const char *s_unknown_error = "?"; + +const char *usbip_net_gai_strerror(int errcode) +{ + const char *s = gai_strerror(errcode); + if (s == NULL) { + return s_unknown_error; + } + return s; } diff --git a/tools/usb/usbip/src/usbip_network.h b/tools/usb/usbip/src/usbip_network.h index 2fe806d..f84775e 100644 --- a/tools/usb/usbip/src/usbip_network.h +++ b/tools/usb/usbip/src/usbip_network.h @@ -173,14 +173,16 @@ void usbip_net_pack_uint16_t(int pack, uint16_t *num); void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev); void usbip_net_pack_usb_interface(int pack, struct usbip_usb_interface *uinf); -ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen); -ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen); -int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status); -int usbip_net_recv_op_common(int sockfd, uint16_t *code); +ssize_t usbip_net_recv(usbip_sock_t *sock, void *buff, size_t bufflen); +ssize_t usbip_net_send(usbip_sock_t *sock, void *buff, size_t bufflen); +int usbip_net_send_op_common(usbip_sock_t *sock, uint32_t code, uint32_t status); +int usbip_net_recv_op_common(usbip_sock_t *sock, uint16_t *code); int usbip_net_set_reuseaddr(int sockfd); int usbip_net_set_nodelay(int sockfd); int usbip_net_set_keepalive(int sockfd); int usbip_net_set_v6only(int sockfd); -int usbip_net_tcp_connect(char *hostname, char *port); +usbip_sock_t *usbip_net_tcp_connect(char *hostname, char *port); +void usbip_net_tcp_close(usbip_sock_t *sock); +const char *usbip_net_gai_strerror(int errcode); #endif /* __USBIP_NETWORK_H */ diff --git a/tools/usb/usbip/src/usbipd.c b/tools/usb/usbip/src/usbipd.c index 3337328..9185b27 100644 --- a/tools/usb/usbip/src/usbipd.c +++ b/tools/usb/usbip/src/usbipd.c @@ -56,7 +56,7 @@ extern char *usbip_default_pid_file; static const char usbip_version_string[] = PACKAGE_STRING; -static const char usbipd_help_string = +static const char usbipd_help_string[] = "usage: %s [options]\n" "\n" " -4, --ipv4\n" @@ -123,7 +123,7 @@ static int do_accept(int listenfd, char *host, char *port) rc = getnameinfo((struct sockaddr *)&ss, len, host, NI_MAXHOST, port, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV); if (rc) - err("getnameinfo: %s", gai_strerror(rc)); + err("getnameinfo: %s", usbip_net_gai_strerror(rc)); #ifdef HAVE_LIBWRAP rc = tcpd_auth(connfd); @@ -138,12 +138,13 @@ static int do_accept(int listenfd, char *host, char *port) return connfd; } -extern int usbip_recv_pdu(int connfd, char *host, char *port); +extern int usbip_recv_pdu(usbip_sock_t *sock, char *host, char *port); int process_request(int listenfd) { pid_t childpid; int connfd; + usbip_sock_t sock; char host[NI_MAXHOST], port[NI_MAXSERV]; connfd = do_accept(listenfd, host, port); @@ -152,7 +153,8 @@ int process_request(int listenfd) childpid = fork(); if (childpid == 0) { close(listenfd); - usbip_recv_pdu(connfd, host, port); + usbip_sock_init(&sock, connfd, NULL, NULL, NULL, NULL); + usbip_recv_pdu(&sock, host, port); close(connfd); exit(0); } @@ -172,7 +174,7 @@ static void addrinfo_to_text(struct addrinfo *ai, char buf[], rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); if (rc) - err("getnameinfo: %s", gai_strerror(rc)); + err("getnameinfo: %s", usbip_net_gai_strerror(rc)); snprintf(buf, buf_size, "%s:%s", hbuf, sbuf); } @@ -246,7 +248,7 @@ static struct addrinfo *do_getaddrinfo(char *host, int ai_family) rc = getaddrinfo(host, usbip_port_string, &hints, &ai_head); if (rc) { err("failed to get a network address %s: %s", usbip_port_string, - gai_strerror(rc)); + usbip_net_gai_strerror(rc)); return NULL; } diff --git a/tools/usb/usbip/src/usbipd_app.c b/tools/usb/usbip/src/usbipd_app.c index 16af965..53fdfc7 100644 --- a/tools/usb/usbip/src/usbipd_app.c +++ b/tools/usb/usbip/src/usbipd_app.c @@ -52,12 +52,12 @@ void usbip_driver_close(void) usbip_vhci_driver_close(); } -static int import_device(int sockfd, struct usbip_usb_device *udev) +static int import_device(usbip_sock_t *sock, struct usbip_usb_device *udev) { int rc; int port; - dbg("Sockfd:%d", sockfd); + dbg("Sockfd:%d", sock->fd); port = usbip_vhci_get_free_port(); if (port < 0) { err("no free port"); @@ -65,7 +65,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev) } dump_usb_device(udev); - rc = usbip_vhci_attach_device(port, sockfd, udev->busnum, + rc = usbip_vhci_attach_device(port, sock->fd, udev->busnum, udev->devnum, udev->speed); if (rc < 0) { err("import device"); @@ -75,7 +75,7 @@ static int import_device(int sockfd, struct usbip_usb_device *udev) return port; } -static int recv_request_export(int sockfd, char *host, char *port) +static int recv_request_export(usbip_sock_t *sock, char *host, char *port) { struct op_export_request req; struct op_export_reply reply; @@ -87,26 +87,26 @@ static int recv_request_export(int sockfd, char *host, char *port) memset(&req, 0, sizeof(req)); memset(&reply, 0, sizeof(reply)); - rc = usbip_net_recv(sockfd, &req, sizeof(req)); + rc = usbip_net_recv(sock, &req, sizeof(req)); if (rc < 0) { dbg("usbip_net_recv failed: export request"); return -1; } PACK_OP_EXPORT_REQUEST(0, &req); - rc = usbip_ux_setup(sockfd, &ux); + rc = usbip_ux_setup(sock, &ux); if (rc) { dbg("usbip_ux_setup failed: export"); return -1; } - rhport = import_device(sockfd, &req.udev); + rhport = import_device(sock, &req.udev); if (rhport < 0) { dbg("export request busid %s: failed", req.udev.busid); error = 1; } - rc = usbip_net_send_op_common(sockfd, OP_REP_EXPORT, + rc = usbip_net_send_op_common(sock, OP_REP_EXPORT, (!error ? ST_OK : ST_NA)); if (rc < 0) { dbg("usbip_net_send_op_common failed: %#0x", OP_REP_EXPORT); @@ -120,7 +120,7 @@ static int recv_request_export(int sockfd, char *host, char *port) } PACK_OP_EXPORT_REPLY(0, &rep); - rc = usbip_net_send(sockfd, &reply, sizeof(reply)); + rc = usbip_net_send(sock, &reply, sizeof(reply)); if (rc < 0) { dbg("usbip_net_send failed: export reply"); return -1; @@ -162,7 +162,7 @@ static int unimport_device(char *host, struct usbip_usb_device *udev) return idev->port; } -static int recv_request_unexport(int sockfd, char *host) +static int recv_request_unexport(usbip_sock_t *sock, char *host) { struct op_unexport_request req; struct op_unexport_reply reply; @@ -173,7 +173,7 @@ static int recv_request_unexport(int sockfd, char *host) memset(&req, 0, sizeof(req)); memset(&reply, 0, sizeof(reply)); - rc = usbip_net_recv(sockfd, &req, sizeof(req)); + rc = usbip_net_recv(sock, &req, sizeof(req)); if (rc < 0) { dbg("usbip_net_recv failed: unexport request"); return -1; @@ -185,7 +185,7 @@ static int recv_request_unexport(int sockfd, char *host) error = 1; } - rc = usbip_net_send_op_common(sockfd, OP_REP_UNEXPORT, + rc = usbip_net_send_op_common(sock, OP_REP_UNEXPORT, (!error ? ST_OK : ST_NA)); if (rc < 0) { dbg("usbip_net_send_op_common failed: %#0x", OP_REP_UNEXPORT); @@ -201,7 +201,7 @@ static int recv_request_unexport(int sockfd, char *host) } PACK_OP_UNEXPORT_REPLY(0, &rep); - rc = usbip_net_send(sockfd, &reply, sizeof(reply)); + rc = usbip_net_send(sock, &reply, sizeof(reply)); if (rc < 0) { dbg("usbip_net_send failed: unexport reply"); return -1; @@ -214,12 +214,12 @@ static int recv_request_unexport(int sockfd, char *host) return 0; } -int usbip_recv_pdu(int connfd, char *host, char *port) +int usbip_recv_pdu(usbip_sock_t *sock, char *host, char *port) { uint16_t code = OP_UNSPEC; int ret; - ret = usbip_net_recv_op_common(connfd, &code); + ret = usbip_net_recv_op_common(sock, &code); if (ret < 0) { dbg("could not receive opcode: %#0x", code); return -1; @@ -231,13 +231,13 @@ int usbip_recv_pdu(int connfd, char *host, char *port) return -1; } - info("received request: %#0x(%d)", code, connfd); + info("received request: %#0x(%d)", code, sock->fd); switch (code) { case OP_REQ_EXPORT: - ret = recv_request_export(connfd, host, port); + ret = recv_request_export(sock, host, port); break; case OP_REQ_UNEXPORT: - ret = recv_request_unexport(connfd, host); + ret = recv_request_unexport(sock, host); break; default: err("received an unknown opcode: %#0x", code); diff --git a/tools/usb/usbip/src/usbipd_dev.c b/tools/usb/usbip/src/usbipd_dev.c index 7d124c2..11988b4 100644 --- a/tools/usb/usbip/src/usbipd_dev.c +++ b/tools/usb/usbip/src/usbipd_dev.c @@ -57,7 +57,7 @@ void usbip_driver_close(void) usbip_host_driver_close(); } -static int recv_request_import(int sockfd) +static int recv_request_import(usbip_sock_t *sock) { struct op_import_request req; struct op_common reply; @@ -72,7 +72,7 @@ static int recv_request_import(int sockfd) memset(&req, 0, sizeof(req)); memset(&reply, 0, sizeof(reply)); - rc = usbip_net_recv(sockfd, &req, sizeof(req)); + rc = usbip_net_recv(sock, &req, sizeof(req)); if (rc < 0) { dbg("usbip_net_recv failed: import request"); return -1; @@ -89,15 +89,15 @@ static int recv_request_import(int sockfd) } if (found) { - rc = usbip_ux_setup(sockfd, &ux); + rc = usbip_ux_setup(sock, &ux); if (rc) { error = 1; } else { /* should set TCP_NODELAY for usbip */ - usbip_net_set_nodelay(sockfd); + usbip_net_set_nodelay(sock->fd); /* export device needs a TCP/IP socket descriptor */ - rc = usbip_host_export_device(edev, sockfd); + rc = usbip_host_export_device(edev, sock->fd); if (rc < 0) { usbip_ux_cleanup(&ux); error = 1; @@ -108,7 +108,7 @@ static int recv_request_import(int sockfd) error = 1; } - rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, + rc = usbip_net_send_op_common(sock, OP_REP_IMPORT, (!error ? ST_OK : ST_NA)); if (rc < 0) { dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT); @@ -125,7 +125,7 @@ static int recv_request_import(int sockfd) memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev)); usbip_net_pack_usb_device(1, &pdu_udev); - rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev)); + rc = usbip_net_send(sock, &pdu_udev, sizeof(pdu_udev)); if (rc < 0) { dbg("usbip_net_send failed: devinfo"); usbip_ux_cleanup(&ux); @@ -143,7 +143,7 @@ static int recv_request_import(int sockfd) return 0; } -static int send_reply_devlist(int connfd) +static int send_reply_devlist(usbip_sock_t *sock) { struct usbip_exported_device *edev; struct usbip_usb_device pdu_udev; @@ -159,14 +159,14 @@ static int send_reply_devlist(int connfd) } info("exportable devices: %d", reply.ndev); - rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK); + rc = usbip_net_send_op_common(sock, OP_REP_DEVLIST, ST_OK); if (rc < 0) { dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST); return -1; } PACK_OP_DEVLIST_REPLY(1, &reply); - rc = usbip_net_send(connfd, &reply, sizeof(reply)); + rc = usbip_net_send(sock, &reply, sizeof(reply)); if (rc < 0) { dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST); return -1; @@ -178,7 +178,7 @@ static int send_reply_devlist(int connfd) memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev)); usbip_net_pack_usb_device(1, &pdu_udev); - rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev)); + rc = usbip_net_send(sock, &pdu_udev, sizeof(pdu_udev)); if (rc < 0) { dbg("usbip_net_send failed: pdu_udev"); return -1; @@ -189,7 +189,7 @@ static int send_reply_devlist(int connfd) memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf)); usbip_net_pack_usb_interface(1, &pdu_uinf); - rc = usbip_net_send(connfd, &pdu_uinf, + rc = usbip_net_send(sock, &pdu_uinf, sizeof(pdu_uinf)); if (rc < 0) { err("usbip_net_send failed: pdu_uinf"); @@ -201,20 +201,20 @@ static int send_reply_devlist(int connfd) return 0; } -static int recv_request_devlist(int connfd) +static int recv_request_devlist(usbip_sock_t *sock) { struct op_devlist_request req; int rc; memset(&req, 0, sizeof(req)); - rc = usbip_net_recv(connfd, &req, sizeof(req)); + rc = usbip_net_recv(sock, &req, sizeof(req)); if (rc < 0) { dbg("usbip_net_recv failed: devlist request"); return -1; } - rc = send_reply_devlist(connfd); + rc = send_reply_devlist(sock); if (rc < 0) { dbg("send_reply_devlist failed"); return -1; @@ -223,12 +223,12 @@ static int recv_request_devlist(int connfd) return 0; } -int usbip_recv_pdu(int connfd, char *host, char *port) +int usbip_recv_pdu(usbip_sock_t *sock, char *host, char *port) { uint16_t code = OP_UNSPEC; int ret; - ret = usbip_net_recv_op_common(connfd, &code); + ret = usbip_net_recv_op_common(sock, &code); if (ret < 0) { dbg("could not receive opcode: %#0x", code); return -1; @@ -240,13 +240,13 @@ int usbip_recv_pdu(int connfd, char *host, char *port) return -1; } - info("received request: %#0x(%d)", code, connfd); + info("received request: %#0x(%d)", code, sock->fd); switch (code) { case OP_REQ_DEVLIST: - ret = recv_request_devlist(connfd); + ret = recv_request_devlist(sock); break; case OP_REQ_IMPORT: - ret = recv_request_import(connfd); + ret = recv_request_import(sock); break; case OP_REQ_DEVINFO: case OP_REQ_CRYPKEY: -- 2.1.0 -- To unsubscribe from this list: send the line "unsubscribe linux-usb" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html