[PATCH v6 09/11] usbip: deriving functions as libraries

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



To utilize core parts of USB/IP to application protocol 
implementations, this patch derives libraries by exposing some 
functions of utilities and removing some unnecessary portions.

Following functions are exposed.
For command:
- usbip_attach_device()
- usbip_detach_port()
- usbip_bind_device()
- usbip_unbind_device()
- usbip_list_imported_devices() : port command
- usbip_list_importable_devices() : list --remote
- usbip_list_devices() : list --local
- usbip_connect_device()
- usbip_disconnect_device()
For daemon:
- usbip_recv_pdu() - processes accepted a connection
- usbip_break_connections() - breaks send/receive threads
- usbip_driver_open() - open host or vhci driver
- usbip_driver_close() - close host or vhci driver

main() and option processing are removed. AS_LIBRARY macro is used to 
remove option processing.

Following libraries are generated.
- libusbip.la : for commnad
- libusbipa.la : for application-side daemon
- libusbipd.la : for device-side daemon

Succeeding WebSocket patch uses these libraries.

Signed-off-by: Nobuo Iwata <nobuo.iwata@xxxxxxxxxxxxxxx>
---
 tools/usb/usbip/libsrc/Makefile.am     | 10 +--
 tools/usb/usbip/src/Makefile.am        | 22 +++++-
 tools/usb/usbip/src/usbip.h            |  7 ++
 tools/usb/usbip/src/usbip_attach.c     | 14 ++--
 tools/usb/usbip/src/usbip_bind.c       |  6 ++
 tools/usb/usbip/src/usbip_connect.c    | 12 +++-
 tools/usb/usbip/src/usbip_detach.c     | 15 ++--
 tools/usb/usbip/src/usbip_disconnect.c | 12 +++-
 tools/usb/usbip/src/usbip_list.c       | 70 +++++++++++-------
 tools/usb/usbip/src/usbip_netconn.c    | 98 ++++++++++++++++++++++++++
 tools/usb/usbip/src/usbip_network.c    | 67 ------------------
 tools/usb/usbip/src/usbip_port.c       |  9 ++-
 tools/usb/usbip/src/usbip_unbind.c     |  6 ++
 tools/usb/usbip/src/usbipd.c           | 13 +---
 tools/usb/usbip/src/usbipd.h           | 38 ++++++++++
 tools/usb/usbip/src/usbipd_app.c       |  1 +
 tools/usb/usbip/src/usbipd_dev.c       |  1 +
 17 files changed, 274 insertions(+), 127 deletions(-)

diff --git a/tools/usb/usbip/libsrc/Makefile.am b/tools/usb/usbip/libsrc/Makefile.am
index 5754425..356a6c0 100644
--- a/tools/usb/usbip/libsrc/Makefile.am
+++ b/tools/usb/usbip/libsrc/Makefile.am
@@ -1,9 +1,9 @@
-libusbip_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
-libusbip_la_CFLAGS   = @EXTRA_CFLAGS@
-libusbip_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
+libusbiplib_la_CPPFLAGS = -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
+libusbiplib_la_CFLAGS   = @EXTRA_CFLAGS@
+libusbiplib_la_LDFLAGS  = -version-info @LIBUSBIP_VERSION@
 
-lib_LTLIBRARIES := libusbip.la
-libusbip_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h \
+lib_LTLIBRARIES := libusbiplib.la
+libusbiplib_la_SOURCES := names.c names.h usbip_host_driver.c usbip_host_driver.h \
 		       usbip_common.c usbip_common.h vhci_driver.c vhci_driver.h \
 		       usbip_ux.c usbip_ux.h \
 		       sysfs_utils.c sysfs_utils.h
diff --git a/tools/usb/usbip/src/Makefile.am b/tools/usb/usbip/src/Makefile.am
index f5697c2..780bdb3 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,32 @@
 AM_CPPFLAGS = -I$(top_srcdir)/libsrc -DUSBIDS_FILE='"@USBIDS_DIR@/usb.ids"'
 AM_CFLAGS   = @EXTRA_CFLAGS@
-LDADD       = $(top_builddir)/libsrc/libusbip.la
+LDADD       = $(top_builddir)/libsrc/libusbiplib.la
 
 sbin_PROGRAMS := usbip usbipd usbipa
+lib_LTLIBRARIES := libusbip.la libusbipd.la libusbipa.la
 
-usbip_SOURCES := usbip.h utils.h usbip.c utils.c usbip_network.c \
+usbip_SOURCES := usbip.h utils.h usbip.c utils.c \
+		 usbip_network.c usbip_netconn.c\
 		 usbip_attach.c usbip_detach.c usbip_list.c \
 		 usbip_bind.c usbip_unbind.c usbip_port.c \
 		 usbip_connect.c usbip_disconnect.c
+usbip_CFLAGS := $(AM_CFLAGS)
 
 usbipd_SOURCES := usbip_network.h usbipd.c usbipd_dev.c usbip_network.c
+usbipd_CFLAGS := $(AM_CFLAGS)
 
 usbipa_SOURCES := usbip_network.h usbipd.c usbipd_app.c usbip_network.c
+usbipa_CFLAGS := $(AM_CFLAGS)
+
+libusbip_la_SOURCES := utils.h utils.c usbip_network.c \
+		 usbip_attach.c usbip_detach.c usbip_list.c \
+		 usbip_bind.c usbip_unbind.c usbip_port.c \
+		 usbip_connect.c usbip_disconnect.c
+libusbip_la_CFLAGS := $(AM_CFLAGS) -DAS_LIBRARY
+libusbip_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+libusbipd_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
+libusbipa_la_LDFLAGS := -version-info @LIBUSBIP_VERSION@
diff --git a/tools/usb/usbip/src/usbip.h b/tools/usb/usbip/src/usbip.h
index 0875d15..1d642cc 100644
--- a/tools/usb/usbip/src/usbip.h
+++ b/tools/usb/usbip/src/usbip.h
@@ -34,8 +34,15 @@ int usbip_port_show(int argc, char *argv[]);
 int usbip_connect(int argc, char *argv[]);
 int usbip_disconnect(int argc, char *argv[]);
 
+int usbip_attach_device(char *host, char *port, char *busid);
+int usbip_detach_port(char *port);
 int usbip_bind_device(char *busid);
 int usbip_unbind_device(char *busid);
+int usbip_list_imported_devices(void);
+int usbip_list_importable_devices(char *host, char *port);
+int usbip_list_devices(int parsable);
+int usbip_connect_device(char *host, char *port, char *busid);
+int usbip_disconnect_device(char *host, char *port, char *busid);
 
 void usbip_attach_usage(void);
 void usbip_detach_usage(void);
diff --git a/tools/usb/usbip/src/usbip_attach.c b/tools/usb/usbip/src/usbip_attach.c
index c67e3d2..aa09b5d 100644
--- a/tools/usb/usbip/src/usbip_attach.c
+++ b/tools/usb/usbip/src/usbip_attach.c
@@ -25,7 +25,9 @@
 #include <string.h>
 
 #include <fcntl.h>
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 #include <unistd.h>
 #include <errno.h>
 
@@ -35,6 +37,7 @@
 #include "usbip_ux.h"
 #include "usbip.h"
 
+#ifndef AS_LIBRARY
 static const char usbip_attach_usage_string[] =
 	"usbip attach <args>\n"
 	"    -r, --remote=<host>      The machine with exported USB devices\n"
@@ -44,6 +47,7 @@ void usbip_attach_usage(void)
 {
 	printf("usage: %s", usbip_attach_usage_string);
 }
+#endif
 
 static int import_device(usbip_sock_t *sock, struct usbip_usb_device *udev)
 {
@@ -128,14 +132,14 @@ static int query_import_device(usbip_sock_t *sock, char *busid)
 	return import_device(sock, &reply.udev);
 }
 
-static int attach_device(char *host, char *busid)
+int usbip_attach_device(char *host, char *port, char *busid)
 {
 	usbip_sock_t *sock;
 	usbip_ux_t *ux;
 	int rc;
 	int rhport;
 
-	sock = usbip_conn_ops.open(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, port);
 	if (!sock) {
 		err("tcp connect");
 		goto err_out;
@@ -153,7 +157,7 @@ static int attach_device(char *host, char *busid)
 		goto err_cleanup_ux;
 	}
 
-	rc = usbip_vhci_create_record(host, usbip_port_string, busid, rhport);
+	rc = usbip_vhci_create_record(host, port, busid, rhport);
 	if (rc < 0) {
 		err("record connection");
 		goto err_cleanup_ux;
@@ -175,6 +179,7 @@ err_out:
 	return -1;
 }
 
+#ifndef AS_LIBRARY
 int usbip_attach(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -208,7 +213,7 @@ int usbip_attach(int argc, char *argv[])
 	if (!host || !busid)
 		goto err_out;
 
-	ret = attach_device(host, busid);
+	ret = usbip_attach_device(host, usbip_port_string, busid);
 	goto out;
 
 err_out:
@@ -216,3 +221,4 @@ err_out:
 out:
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_bind.c b/tools/usb/usbip/src/usbip_bind.c
index 1c09338..3b58dbb 100644
--- a/tools/usb/usbip/src/usbip_bind.c
+++ b/tools/usb/usbip/src/usbip_bind.c
@@ -24,7 +24,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 
 #include "usbip_common.h"
 #include "utils.h"
@@ -37,6 +39,7 @@ enum unbind_status {
 	UNBIND_ST_FAILED
 };
 
+#ifndef AS_LIBRARY
 static const char usbip_bind_usage_string[] =
 	"usbip bind <args>\n"
 	"    -b, --busid=<busid>    Bind " USBIP_HOST_DRV_NAME ".ko to device "
@@ -46,6 +49,7 @@ void usbip_bind_usage(void)
 {
 	printf("usage: %s", usbip_bind_usage_string);
 }
+#endif
 
 /* call at unbound state */
 static int bind_usbip(char *busid)
@@ -183,6 +187,7 @@ int usbip_bind_device(char *busid)
 	return 0;
 }
 
+#ifndef AS_LIBRARY
 int usbip_bind(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -213,3 +218,4 @@ err_out:
 out:
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_connect.c b/tools/usb/usbip/src/usbip_connect.c
index 8dabd0b..f627c70 100644
--- a/tools/usb/usbip/src/usbip_connect.c
+++ b/tools/usb/usbip/src/usbip_connect.c
@@ -24,7 +24,9 @@
 #include <stdio.h>
 #include <string.h>
 
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 #include <unistd.h>
 
 #include "usbip_host_driver.h"
@@ -33,6 +35,7 @@
 #include "usbip_ux.h"
 #include "usbip.h"
 
+#ifndef AS_LIBRARY
 static const char usbip_connect_usage_string[] =
 	"usbip connect <args>\n"
 	"    -r, --remote=<host>    Address of a remote computer\n"
@@ -42,6 +45,7 @@ void usbip_connect_usage(void)
 {
 	printf("usage: %s", usbip_connect_usage_string);
 }
+#endif
 
 static int send_export_device(usbip_sock_t *sock, struct usbip_usb_device *udev)
 {
@@ -138,7 +142,7 @@ static int export_device(char *busid, usbip_sock_t *sock)
 	return 0;
 }
 
-static int connect_device(char *host, char *busid)
+int usbip_connect_device(char *host, char *port, char *busid)
 {
 	usbip_sock_t *sock;
 	usbip_ux_t *ux;
@@ -150,7 +154,7 @@ static int connect_device(char *host, char *busid)
 		goto err_out;
 	}
 
-	sock = usbip_conn_ops.open(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, port);
 	if (!sock) {
 		err("tcp connect");
 		goto err_unbind_device;
@@ -187,6 +191,7 @@ err_out:
 	return -1;
 }
 
+#ifndef AS_LIBRARY
 int usbip_connect(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -220,7 +225,7 @@ int usbip_connect(int argc, char *argv[])
 	if (!host || !busid)
 		goto err_out;
 
-	ret = connect_device(host, busid);
+	ret = usbip_connect_device(host, usbip_port_string, busid);
 	goto out;
 
 err_out:
@@ -228,3 +233,4 @@ err_out:
 out:
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_detach.c b/tools/usb/usbip/src/usbip_detach.c
index 21ab710..7784edf 100644
--- a/tools/usb/usbip/src/usbip_detach.c
+++ b/tools/usb/usbip/src/usbip_detach.c
@@ -24,7 +24,9 @@
 #include <stdlib.h>
 #include <string.h>
 
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 #include <unistd.h>
 
 #include "vhci_driver.h"
@@ -32,6 +34,7 @@
 #include "usbip_network.h"
 #include "usbip.h"
 
+#ifndef AS_LIBRARY
 static const char usbip_detach_usage_string[] =
 	"usbip detach <args>\n"
 	"    -p, --port=<port>    " USBIP_VHCI_DRV_NAME
@@ -41,15 +44,15 @@ void usbip_detach_usage(void)
 {
 	printf("usage: %s", usbip_detach_usage_string);
 }
+#endif
 
-static int detach_port(char *port)
+int usbip_detach_port(char *port)
 {
 	int ret;
 	uint8_t portnum;
+	unsigned int i, port_len = strlen(port);
 
-	unsigned int port_len = strlen(port);
-
-	for (unsigned int i = 0; i < port_len; i++)
+	for (i = 0; i < port_len; i++)
 		if (!isdigit(port[i])) {
 			err("invalid port %s", port);
 			return -1;
@@ -80,6 +83,7 @@ static int detach_port(char *port)
 	return ret;
 }
 
+#ifndef AS_LIBRARY
 int usbip_detach(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -97,7 +101,7 @@ int usbip_detach(int argc, char *argv[])
 
 		switch (opt) {
 		case 'p':
-			ret = detach_port(optarg);
+			ret = usbip_detach_port(optarg);
 			goto out;
 		default:
 			goto err_out;
@@ -109,3 +113,4 @@ err_out:
 out:
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_disconnect.c b/tools/usb/usbip/src/usbip_disconnect.c
index 1bda962..3f50018 100644
--- a/tools/usb/usbip/src/usbip_disconnect.c
+++ b/tools/usb/usbip/src/usbip_disconnect.c
@@ -24,7 +24,9 @@
 #include <stdio.h>
 #include <string.h>
 
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 #include <unistd.h>
 
 #include "usbip_host_driver.h"
@@ -33,6 +35,7 @@
 #include "usbip_ux.h"
 #include "usbip.h"
 
+#ifndef AS_LIBRARY
 static const char usbip_disconnect_usage_string[] =
 	"usbip disconnect <args>\n"
 	"    -r, --remote=<host>    Address of a remote computer\n"
@@ -42,6 +45,7 @@ void usbip_disconnect_usage(void)
 {
 	printf("usage: %s", usbip_disconnect_usage_string);
 }
+#endif
 
 static int send_unexport_device(usbip_sock_t *sock, struct usbip_usb_device *udev)
 {
@@ -131,12 +135,12 @@ static int unexport_device(char *busid, usbip_sock_t *sock)
 	return 0;
 }
 
-static int disconnect_device(char *host, char *busid)
+int usbip_disconnect_device(char *host, char *port, char *busid)
 {
 	usbip_sock_t *sock;
 	int rc;
 
-	sock = usbip_conn_ops.open(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, port);
 	if (!sock) {
 		err("tcp connect");
 		return -1;
@@ -162,6 +166,7 @@ static int disconnect_device(char *host, char *busid)
 	return 0;
 }
 
+#ifndef AS_LIBRARY
 int usbip_disconnect(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -195,7 +200,7 @@ int usbip_disconnect(int argc, char *argv[])
 	if (!host || !busid)
 		goto err_out;
 
-	ret = disconnect_device(host, busid);
+	ret = usbip_disconnect_device(host, usbip_port_string, busid);
 	goto out;
 
 err_out:
@@ -203,3 +208,4 @@ err_out:
 out:
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_list.c b/tools/usb/usbip/src/usbip_list.c
index 64f143a..a197bbe 100644
--- a/tools/usb/usbip/src/usbip_list.c
+++ b/tools/usb/usbip/src/usbip_list.c
@@ -21,19 +21,21 @@
 #include <libudev.h>
 
 #include <errno.h>
-#include <stdbool.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 #include <unistd.h>
 
 #include "usbip_common.h"
 #include "usbip_network.h"
 #include "usbip.h"
 
+#ifndef AS_LIBRARY
 static const char usbip_list_usage_string[] =
 	"usbip list <args>\n"
 	"    -p, --parsable         Parsable list format\n"
@@ -44,6 +46,7 @@ void usbip_list_usage(void)
 {
 	printf("usage: %s", usbip_list_usage_string);
 }
+#endif
 
 static int get_importable_devices(char *host, usbip_sock_t *sock)
 {
@@ -127,33 +130,42 @@ static int get_importable_devices(char *host, usbip_sock_t *sock)
 	return 0;
 }
 
-static int list_importable_devices(char *host)
+int usbip_list_importable_devices(char *host, char* port)
 {
 	int rc;
 	usbip_sock_t *sock;
 
-	sock = usbip_conn_ops.open(host, usbip_port_string);
+	if (usbip_names_init(USBIDS_FILE))
+		err("failed to open %s", USBIDS_FILE);
+
+	sock = usbip_conn_ops.open(host, port);
 	if (!sock) {
 		err("could not connect to %s:%s: %s", host,
-		    usbip_port_string, usbip_net_gai_strerror(sock->fd));
-		return -1;
+		    port, usbip_net_gai_strerror(sock->fd));
+		goto err_names_free;
 	}
-	dbg("connected to %s:%s", host, usbip_port_string);
+	dbg("connected to %s:%s", host, port);
 
 	rc = get_importable_devices(host, sock);
 	if (rc < 0) {
 		err("failed to get device list from %s", host);
-		usbip_conn_ops.close(sock);
-		return -1;
+		goto err_conn_close;
 	}
 
 	usbip_conn_ops.close(sock);
+	usbip_names_free();
 
 	return 0;
+
+err_conn_close:
+	usbip_conn_ops.close(sock);
+err_names_free:
+	usbip_names_free();
+	return -1;
 }
 
 static void print_device(const char *busid, const char *vendor,
-			 const char *product, bool parsable)
+			 const char *product, int parsable)
 {
 	if (parsable)
 		printf("busid=%s#usbid=%.4s:%.4s#", busid, vendor, product);
@@ -161,13 +173,13 @@ static void print_device(const char *busid, const char *vendor,
 		printf(" - busid %s (%.4s:%.4s)\n", busid, vendor, product);
 }
 
-static void print_product_name(char *product_name, bool parsable)
+static void print_product_name(char *product_name, int parsable)
 {
 	if (!parsable)
 		printf("   %s\n", product_name);
 }
 
-static int list_devices(bool parsable)
+int usbip_list_devices(int parsable)
 {
 	struct udev *udev;
 	struct udev_enumerate *enumerate;
@@ -182,6 +194,9 @@ static int list_devices(bool parsable)
 	char product_name[128];
 	int ret = -1;
 
+	if (usbip_names_init(USBIDS_FILE))
+		err("failed to open %s", USBIDS_FILE);
+
 	/* Create libudev context. */
 	udev = udev_new();
 
@@ -234,10 +249,12 @@ static int list_devices(bool parsable)
 err_out:
 	udev_enumerate_unref(enumerate);
 	udev_unref(udev);
+	usbip_names_free();
 
 	return ret;
 }
 
+#ifndef AS_LIBRARY
 int usbip_list(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -247,12 +264,11 @@ int usbip_list(int argc, char *argv[])
 		{ NULL,       0,                 NULL,  0  }
 	};
 
-	bool parsable = false;
+	int local = 0;
+	int remote = 0;
+	int parsable = 0;
 	int opt;
-	int ret = -1;
-
-	if (usbip_names_init(USBIDS_FILE))
-		err("failed to open %s", USBIDS_FILE);
+	char *host = NULL;
 
 	for (;;) {
 		opt = getopt_long(argc, argv, "pr:l", opts, NULL);
@@ -262,23 +278,27 @@ int usbip_list(int argc, char *argv[])
 
 		switch (opt) {
 		case 'p':
-			parsable = true;
+			parsable = 1;
 			break;
 		case 'r':
-			ret = list_importable_devices(optarg);
-			goto out;
+			remote = 1;
+			host = optarg;
+			break;
 		case 'l':
-			ret = list_devices(parsable);
-			goto out;
+			local = 1;
+			break;
 		default:
 			goto err_out;
 		}
 	}
+	if (remote && host != NULL) {
+		return usbip_list_importable_devices(host, usbip_port_string);
+	} else if (local) {
+		return usbip_list_devices(parsable);
+	}
 
 err_out:
 	usbip_list_usage();
-out:
-	usbip_names_free();
-
-	return ret;
+	return -1;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_netconn.c b/tools/usb/usbip/src/usbip_netconn.c
new file mode 100644
index 0000000..339df59
--- /dev/null
+++ b/tools/usb/usbip/src/usbip_netconn.c
@@ -0,0 +1,98 @@
+/*
+ * 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
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include <sys/socket.h>
+
+#include <string.h>
+
+#include <arpa/inet.h>
+#include <netdb.h>
+#include <netinet/tcp.h>
+#include <unistd.h>
+
+#include "usbip_common.h"
+#include "usbip_network.h"
+
+/*
+ * IPv6 Ready
+ */
+static usbip_sock_t *net_tcp_open(char *hostname, char *service)
+{
+	struct addrinfo hints, *res, *rp;
+	int sockfd;
+	usbip_sock_t *sock;
+	int ret;
+
+	memset(&hints, 0, sizeof(hints));
+	hints.ai_family = AF_UNSPEC;
+	hints.ai_socktype = SOCK_STREAM;
+
+	/* get all possible addresses */
+	ret = getaddrinfo(hostname, service, &hints, &res);
+	if (ret < 0) {
+		dbg("getaddrinfo: %s service %s: %s", hostname, service,
+		    usbip_net_gai_strerror(ret));
+		return NULL;
+	}
+
+	/* try the addresses */
+	for (rp = res; rp; rp = rp->ai_next) {
+		sockfd = socket(rp->ai_family, rp->ai_socktype,
+				rp->ai_protocol);
+		if (sockfd < 0)
+			continue;
+
+		/* should set TCP_NODELAY for usbip */
+		usbip_net_set_nodelay(sockfd);
+		/* TODO: write code for heartbeat */
+		usbip_net_set_keepalive(sockfd);
+
+		if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)
+			break;
+
+		close(sockfd);
+	}
+
+	freeaddrinfo(res);
+
+	if (!rp)
+		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;
+}
+
+static void net_tcp_close(usbip_sock_t *sock)
+{
+	close(sock->fd);
+	free(sock);
+}
+
+void usbip_net_tcp_conn_init()
+{
+	usbip_conn_init(net_tcp_open, net_tcp_close);
+}
+
diff --git a/tools/usb/usbip/src/usbip_network.c b/tools/usb/usbip/src/usbip_network.c
index 8906d92..beb34ae 100644
--- a/tools/usb/usbip/src/usbip_network.c
+++ b/tools/usb/usbip/src/usbip_network.c
@@ -265,73 +265,6 @@ int usbip_net_set_v6only(int sockfd)
 	return ret;
 }
 
-/*
- * IPv6 Ready
- */
-static usbip_sock_t *net_tcp_open(char *hostname, char *service)
-{
-	struct addrinfo hints, *res, *rp;
-	int sockfd;
-	usbip_sock_t *sock;
-	int ret;
-
-	memset(&hints, 0, sizeof(hints));
-	hints.ai_family = AF_UNSPEC;
-	hints.ai_socktype = SOCK_STREAM;
-
-	/* get all possible addresses */
-	ret = getaddrinfo(hostname, service, &hints, &res);
-	if (ret < 0) {
-		dbg("getaddrinfo: %s service %s: %s", hostname, service,
-		    usbip_net_gai_strerror(ret));
-		return NULL;
-	}
-
-	/* try the addresses */
-	for (rp = res; rp; rp = rp->ai_next) {
-		sockfd = socket(rp->ai_family, rp->ai_socktype,
-				rp->ai_protocol);
-		if (sockfd < 0)
-			continue;
-
-		/* should set TCP_NODELAY for usbip */
-		usbip_net_set_nodelay(sockfd);
-		/* TODO: write code for heartbeat */
-		usbip_net_set_keepalive(sockfd);
-
-		if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)
-			break;
-
-		close(sockfd);
-	}
-
-	freeaddrinfo(res);
-
-	if (!rp)
-		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;
-}
-
-static void net_tcp_close(usbip_sock_t *sock)
-{
-	close(sock->fd);
-	free(sock);
-}
-
-void usbip_net_tcp_conn_init(void)
-{
-	usbip_conn_init(net_tcp_open, net_tcp_close);
-}
-
 static const char *s_unknown_error = "?";
 
 const char *usbip_net_gai_strerror(int errcode)
diff --git a/tools/usb/usbip/src/usbip_port.c b/tools/usb/usbip/src/usbip_port.c
index acb49c71..b3a6ee1 100644
--- a/tools/usb/usbip/src/usbip_port.c
+++ b/tools/usb/usbip/src/usbip_port.c
@@ -16,13 +16,16 @@
 
 #include "vhci_driver.h"
 #include "usbip_common.h"
+#include "usbip.h"
 
+#ifndef AS_LIBRARY
 void usbip_port_usage(void)
 {
 	printf("usage: usbip port\n");
 }
+#endif
 
-static int list_imported_devices(void)
+int usbip_list_imported_devices(void)
 {
 	int i;
 	struct usbip_imported_device *idev;
@@ -59,14 +62,16 @@ err_names_free:
 	return -1;
 }
 
+#ifndef AS_LIBRARY
 int usbip_port_show(__attribute__((unused)) int argc,
 		    __attribute__((unused)) char *argv[])
 {
 	int ret;
 
-	ret = list_imported_devices();
+	ret = usbip_list_imported_devices();
 	if (ret < 0)
 		err("list imported devices");
 
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbip_unbind.c b/tools/usb/usbip/src/usbip_unbind.c
index cc1ff26..4edd458 100644
--- a/tools/usb/usbip/src/usbip_unbind.c
+++ b/tools/usb/usbip/src/usbip_unbind.c
@@ -23,13 +23,16 @@
 #include <stdio.h>
 #include <string.h>
 
+#ifndef AS_LIBRARY
 #include <getopt.h>
+#endif
 
 #include "usbip_common.h"
 #include "utils.h"
 #include "usbip.h"
 #include "sysfs_utils.h"
 
+#ifndef AS_LIBRARY
 static const char usbip_unbind_usage_string[] =
 	"usbip unbind <args>\n"
 	"    -b, --busid=<busid>    Unbind " USBIP_HOST_DRV_NAME ".ko from "
@@ -39,6 +42,7 @@ void usbip_unbind_usage(void)
 {
 	printf("usage: %s", usbip_unbind_usage_string);
 }
+#endif
 
 int usbip_unbind_device(char *busid)
 {
@@ -110,6 +114,7 @@ err_close_udev:
 	return ret;
 }
 
+#ifndef AS_LIBRARY
 int usbip_unbind(int argc, char *argv[])
 {
 	static const struct option opts[] = {
@@ -140,3 +145,4 @@ err_out:
 out:
 	return ret;
 }
+#endif
diff --git a/tools/usb/usbip/src/usbipd.c b/tools/usb/usbip/src/usbipd.c
index 9185b27..98fbf17 100644
--- a/tools/usb/usbip/src/usbipd.c
+++ b/tools/usb/usbip/src/usbipd.c
@@ -43,17 +43,13 @@
 
 #include "usbip_common.h"
 #include "usbip_network.h"
-#include "usbip_ux.h"
+#include "usbipd.h"
 #include "list.h"
 
-extern char *usbip_progname;
-
 #define MAXSOCKFD 20
 
 #define MAIN_LOOP_TIMEOUT 10
 
-extern char *usbip_default_pid_file;
-
 static const char usbip_version_string[] = PACKAGE_STRING;
 
 static const char usbipd_help_string[] =
@@ -138,8 +134,6 @@ static int do_accept(int listenfd, char *host, char *port)
 	return connfd;
 }
 
-extern int usbip_recv_pdu(usbip_sock_t *sock, char *host, char *port);
-
 int process_request(int listenfd)
 {
 	pid_t childpid;
@@ -258,7 +252,7 @@ static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
 static void signal_handler(int i)
 {
 	dbg("received '%s' signal", strsignal(i));
-	usbip_ux_interrupt_pgrp();
+	usbip_break_connections();
 }
 
 static void set_signal(void)
@@ -301,9 +295,6 @@ static void remove_pid_file(void)
 	}
 }
 
-extern int usbip_driver_open(void);
-extern void usbip_driver_close(void);
-
 static int do_standalone_mode(int daemonize, int ipv4, int ipv6)
 {
 	struct addrinfo *ai_head;
diff --git a/tools/usb/usbip/src/usbipd.h b/tools/usb/usbip/src/usbipd.h
new file mode 100644
index 0000000..d9ab8fa
--- /dev/null
+++ b/tools/usb/usbip/src/usbipd.h
@@ -0,0 +1,38 @@
+/*
+ * 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
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __USBIPD_H
+#define __USBIPD_H
+
+#include "usbip_common.h"
+#include "usbip_ux.h"
+
+#ifdef HAVE_CONFIG_H
+#include "../config.h"
+#endif
+
+extern char *usbip_progname;
+extern char *usbip_default_pid_file;
+
+int usbip_recv_pdu(usbip_sock_t *sock, char *host, char *port);
+inline void usbip_break_connections(void) { usbip_ux_interrupt_pgrp(); }
+int usbip_driver_open(void);
+void usbip_driver_close(void);
+
+#endif /* __USBIPD_H */
diff --git a/tools/usb/usbip/src/usbipd_app.c b/tools/usb/usbip/src/usbipd_app.c
index 53fdfc7..536987b 100644
--- a/tools/usb/usbip/src/usbipd_app.c
+++ b/tools/usb/usbip/src/usbipd_app.c
@@ -33,6 +33,7 @@
 #include "usbip_common.h"
 #include "usbip_network.h"
 #include "usbip_ux.h"
+#include "usbipd.h"
 
 char *usbip_progname = "usbipa";
 char *usbip_default_pid_file = "/var/run/usbipa";
diff --git a/tools/usb/usbip/src/usbipd_dev.c b/tools/usb/usbip/src/usbipd_dev.c
index 11988b4..0a50485 100644
--- a/tools/usb/usbip/src/usbipd_dev.c
+++ b/tools/usb/usbip/src/usbipd_dev.c
@@ -37,6 +37,7 @@
 #include "usbip_common.h"
 #include "usbip_network.h"
 #include "usbip_ux.h"
+#include "usbipd.h"
 #include "list.h"
 
 char *usbip_progname = "usbipd";
-- 
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



[Index of Archives]     [Linux Media]     [Linux Input]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Old Linux USB Devel Archive]

  Powered by Linux