[PATCH v2 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        |  19 +++-
 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     |  13 ++-
 tools/usb/usbip/src/usbip_disconnect.c |  12 ++-
 tools/usb/usbip/src/usbip_list.c       |  68 ++++++++-----
 tools/usb/usbip/src/usbip_netconn.c    | 133 +++++++++++++++++++++++++
 tools/usb/usbip/src/usbip_network.c    |  67 -------------
 tools/usb/usbip/src/usbip_port.c       |  12 ++-
 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, 306 insertions(+), 126 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..bb20cd1 100644
--- a/tools/usb/usbip/src/Makefile.am
+++ b/tools/usb/usbip/src/Makefile.am
@@ -1,14 +1,29 @@
 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
+
+libusbipd_la_SOURCES := usbipd_dev.c usbip_network.c
+
+libusbipa_la_SOURCES := usbipd_app.c usbip_network.c
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 e1c298e..49dffa3 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 err0;
@@ -153,7 +157,7 @@ static int attach_device(char *host, char *busid)
 		goto err2;
 	}
 
-	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 err2;
@@ -175,6 +179,7 @@ err0:
 	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 ae8104b..cf4ee82 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 err0;
 	}
 
-	sock = usbip_conn_ops.open(host, usbip_port_string);
+	sock = usbip_conn_ops.open(host, port);
 	if (!sock) {
 		err("tcp connect");
 		goto err1;
@@ -187,6 +191,7 @@ err0:
 	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 a2a187d..0f26101 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,13 +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;
 
-	for (unsigned int i = 0; i < strlen(port); i++)
+	for (i = 0; i < strlen(port); i++)
 		if (!isdigit(port[i])) {
 			err("invalid port %s", port);
 			return -1;
@@ -78,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[] = {
@@ -95,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;
@@ -107,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 9fb0bb9..2c8f4e8 100644
--- a/tools/usb/usbip/src/usbip_list.c
+++ b/tools/usb/usbip/src/usbip_list.c
@@ -21,13 +21,14 @@
 #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 <netdb.h>
 #include <unistd.h>
 
@@ -35,6 +36,7 @@
 #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"
@@ -45,6 +47,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)
 {
@@ -128,33 +131,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, gai_strerror(sock->fd));
-		return -1;
+		    port, gai_strerror(sock->fd));
+		goto err_out0;
 	}
-	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_out1;
 	}
 
 	usbip_conn_ops.close(sock);
+	usbip_names_free();
 
 	return 0;
+
+err_out1:
+	usbip_conn_ops.close(sock);
+err_out0:
+	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);
@@ -162,13 +174,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;
@@ -183,6 +195,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();
 
@@ -235,10 +250,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[] = {
@@ -248,12 +265,10 @@ 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);
 
 	for (;;) {
 		opt = getopt_long(argc, argv, "pr:l", opts, NULL);
@@ -263,23 +278,26 @@ 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;
+			break;
 		case 'l':
-			ret = list_devices(parsable);
-			goto out;
+			local = 1;
+			break;
 		default:
 			goto err_out;
 		}
 	}
+	if (remote) {
+		return usbip_list_importable_devices(optarg, 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..a315a15
--- /dev/null
+++ b/tools/usb/usbip/src/usbip_netconn.c
@@ -0,0 +1,133 @@
+/*
+ * 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"
+
+#ifdef USE_SSL
+#include "usbip_ssl.h"
+
+extern int usbip_use_ssl;
+SSL_CTX *usbip_ssl_context;
+#endif
+
+/*
+ * 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,
+		    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;
+	}
+#ifdef USE_SSL
+	if (usbip_use_ssl) {
+		SSL *ssl;
+		usbip_ssl_context = usbip_ssl_setup(0);
+		if (usbip_ssl_context == NULL) {
+			close(sockfd);
+			return NULL;
+		}
+		ssl = usbip_ssl_connect(usbip_ssl_context, sockfd);
+		if (ssl == NULL) {
+			usbip_ssl_teardown(usbip_ssl_context);
+			close(sockfd);
+			return NULL;
+		}
+		usbip_sock_init(sock, sockfd, ssl,
+				usbip_ssl_send, usbip_ssl_recv,
+				usbip_ssl_shutdown);
+	} else {
+		usbip_sock_init(sock, sockfd, NULL, NULL, NULL, NULL);
+	}
+#else
+	usbip_sock_init(sock, sockfd, NULL, NULL, NULL, NULL);
+#endif
+
+	return sock;
+}
+
+static void net_tcp_close(usbip_sock_t *sock)
+{
+#ifdef USE_SSL
+	if (usbip_use_ssl) {
+		usbip_ssl_close((SSL*)sock->arg);
+		usbip_ssl_teardown(usbip_ssl_context);
+	}
+#endif
+	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 200a071..dfddc5d 100644
--- a/tools/usb/usbip/src/usbip_network.c
+++ b/tools/usb/usbip/src/usbip_network.c
@@ -265,70 +265,3 @@ 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,
-		    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);
-}
-
diff --git a/tools/usb/usbip/src/usbip_port.c b/tools/usb/usbip/src/usbip_port.c
index 7ca120c..a2e7ca2 100644
--- a/tools/usb/usbip/src/usbip_port.c
+++ b/tools/usb/usbip/src/usbip_port.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
@@ -15,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;
@@ -49,14 +53,16 @@ static int list_imported_devices(void)
 
 }
 
+#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 5f7cd86..a96017b 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