usbip_list.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
  3. * 2005-2007 Takahiro Hirofuchi
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <sys/types.h>
  19. #include <libudev.h>
  20. #include <errno.h>
  21. #include <stdbool.h>
  22. #include <stdint.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <getopt.h>
  27. #include <netdb.h>
  28. #include <unistd.h>
  29. #include "usbip_common.h"
  30. #include "usbip_network.h"
  31. #include "usbip.h"
  32. static const char usbip_list_usage_string[] =
  33. "usbip list [-p|--parsable] <args>\n"
  34. " -p, --parsable Parsable list format\n"
  35. " -r, --remote=<host> List the exportable USB devices on <host>\n"
  36. " -l, --local List the local USB devices\n";
  37. void usbip_list_usage(void)
  38. {
  39. printf("usage: %s", usbip_list_usage_string);
  40. }
  41. static int get_exported_devices(char *host, int sockfd)
  42. {
  43. char product_name[100];
  44. char class_name[100];
  45. struct op_devlist_reply reply;
  46. uint16_t code = OP_REP_DEVLIST;
  47. struct usbip_usb_device udev;
  48. struct usbip_usb_interface uintf;
  49. unsigned int i;
  50. int rc, j;
  51. rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0);
  52. if (rc < 0) {
  53. dbg("usbip_net_send_op_common failed");
  54. return -1;
  55. }
  56. rc = usbip_net_recv_op_common(sockfd, &code);
  57. if (rc < 0) {
  58. dbg("usbip_net_recv_op_common failed");
  59. return -1;
  60. }
  61. memset(&reply, 0, sizeof(reply));
  62. rc = usbip_net_recv(sockfd, &reply, sizeof(reply));
  63. if (rc < 0) {
  64. dbg("usbip_net_recv_op_devlist failed");
  65. return -1;
  66. }
  67. PACK_OP_DEVLIST_REPLY(0, &reply);
  68. dbg("exportable devices: %d\n", reply.ndev);
  69. if (reply.ndev == 0) {
  70. info("no exportable devices found on %s", host);
  71. return 0;
  72. }
  73. printf("Exportable USB devices\n");
  74. printf("======================\n");
  75. printf(" - %s\n", host);
  76. for (i = 0; i < reply.ndev; i++) {
  77. memset(&udev, 0, sizeof(udev));
  78. rc = usbip_net_recv(sockfd, &udev, sizeof(udev));
  79. if (rc < 0) {
  80. dbg("usbip_net_recv failed: usbip_usb_device[%d]", i);
  81. return -1;
  82. }
  83. usbip_net_pack_usb_device(0, &udev);
  84. usbip_names_get_product(product_name, sizeof(product_name),
  85. udev.idVendor, udev.idProduct);
  86. usbip_names_get_class(class_name, sizeof(class_name),
  87. udev.bDeviceClass, udev.bDeviceSubClass,
  88. udev.bDeviceProtocol);
  89. printf("%11s: %s\n", udev.busid, product_name);
  90. printf("%11s: %s\n", "", udev.path);
  91. printf("%11s: %s\n", "", class_name);
  92. for (j = 0; j < udev.bNumInterfaces; j++) {
  93. rc = usbip_net_recv(sockfd, &uintf, sizeof(uintf));
  94. if (rc < 0) {
  95. err("usbip_net_recv failed: usbip_usb_intf[%d]",
  96. j);
  97. return -1;
  98. }
  99. usbip_net_pack_usb_interface(0, &uintf);
  100. usbip_names_get_class(class_name, sizeof(class_name),
  101. uintf.bInterfaceClass,
  102. uintf.bInterfaceSubClass,
  103. uintf.bInterfaceProtocol);
  104. printf("%11s: %2d - %s\n", "", j, class_name);
  105. }
  106. printf("\n");
  107. }
  108. return 0;
  109. }
  110. static int list_exported_devices(char *host)
  111. {
  112. int rc;
  113. int sockfd;
  114. sockfd = usbip_net_tcp_connect(host, usbip_port_string);
  115. if (sockfd < 0) {
  116. err("could not connect to %s:%s: %s", host,
  117. usbip_port_string, gai_strerror(sockfd));
  118. return -1;
  119. }
  120. dbg("connected to %s:%s", host, usbip_port_string);
  121. rc = get_exported_devices(host, sockfd);
  122. if (rc < 0) {
  123. err("failed to get device list from %s", host);
  124. return -1;
  125. }
  126. close(sockfd);
  127. return 0;
  128. }
  129. static void print_device(const char *busid, const char *vendor,
  130. const char *product, bool parsable)
  131. {
  132. if (parsable)
  133. printf("busid=%s#usbid=%.4s:%.4s#", busid, vendor, product);
  134. else
  135. printf(" - busid %s (%.4s:%.4s)\n", busid, vendor, product);
  136. }
  137. static void print_product_name(char *product_name, bool parsable)
  138. {
  139. if (!parsable)
  140. printf(" %s\n", product_name);
  141. }
  142. static int list_devices(bool parsable)
  143. {
  144. struct udev *udev;
  145. struct udev_enumerate *enumerate;
  146. struct udev_list_entry *devices, *dev_list_entry;
  147. struct udev_device *dev;
  148. const char *path;
  149. const char *idVendor;
  150. const char *idProduct;
  151. const char *bConfValue;
  152. const char *bNumIntfs;
  153. const char *busid;
  154. char product_name[128];
  155. int ret = -1;
  156. /* Create libudev context. */
  157. udev = udev_new();
  158. /* Create libudev device enumeration. */
  159. enumerate = udev_enumerate_new(udev);
  160. /* Take only USB devices that are not hubs and do not have
  161. * the bInterfaceNumber attribute, i.e. are not interfaces.
  162. */
  163. udev_enumerate_add_match_subsystem(enumerate, "usb");
  164. udev_enumerate_add_nomatch_sysattr(enumerate, "bDeviceClass", "09");
  165. udev_enumerate_add_nomatch_sysattr(enumerate, "bInterfaceNumber", NULL);
  166. udev_enumerate_scan_devices(enumerate);
  167. devices = udev_enumerate_get_list_entry(enumerate);
  168. /* Show information about each device. */
  169. udev_list_entry_foreach(dev_list_entry, devices) {
  170. path = udev_list_entry_get_name(dev_list_entry);
  171. dev = udev_device_new_from_syspath(udev, path);
  172. /* Get device information. */
  173. idVendor = udev_device_get_sysattr_value(dev, "idVendor");
  174. idProduct = udev_device_get_sysattr_value(dev, "idProduct");
  175. bConfValue = udev_device_get_sysattr_value(dev, "bConfigurationValue");
  176. bNumIntfs = udev_device_get_sysattr_value(dev, "bNumInterfaces");
  177. busid = udev_device_get_sysname(dev);
  178. if (!idVendor || !idProduct || !bConfValue || !bNumIntfs) {
  179. err("problem getting device attributes: %s",
  180. strerror(errno));
  181. goto err_out;
  182. }
  183. /* Get product name. */
  184. usbip_names_get_product(product_name, sizeof(product_name),
  185. strtol(idVendor, NULL, 16),
  186. strtol(idProduct, NULL, 16));
  187. /* Print information. */
  188. print_device(busid, idVendor, idProduct, parsable);
  189. print_product_name(product_name, parsable);
  190. printf("\n");
  191. udev_device_unref(dev);
  192. }
  193. ret = 0;
  194. err_out:
  195. udev_enumerate_unref(enumerate);
  196. udev_unref(udev);
  197. return ret;
  198. }
  199. int usbip_list(int argc, char *argv[])
  200. {
  201. static const struct option opts[] = {
  202. { "parsable", no_argument, NULL, 'p' },
  203. { "remote", required_argument, NULL, 'r' },
  204. { "local", no_argument, NULL, 'l' },
  205. { NULL, 0, NULL, 0 }
  206. };
  207. bool parsable = false;
  208. int opt;
  209. int ret = -1;
  210. if (usbip_names_init(USBIDS_FILE))
  211. err("failed to open %s", USBIDS_FILE);
  212. for (;;) {
  213. opt = getopt_long(argc, argv, "pr:l", opts, NULL);
  214. if (opt == -1)
  215. break;
  216. switch (opt) {
  217. case 'p':
  218. parsable = true;
  219. break;
  220. case 'r':
  221. ret = list_exported_devices(optarg);
  222. goto out;
  223. case 'l':
  224. ret = list_devices(parsable);
  225. goto out;
  226. default:
  227. goto err_out;
  228. }
  229. }
  230. err_out:
  231. usbip_list_usage();
  232. out:
  233. usbip_names_free();
  234. return ret;
  235. }