static int send_reply_devlist(int sockfd) { int ret; struct usbip_exported_device *edev; struct op_devlist_reply reply; reply.ndev = 0; /* how many devices are exported ? */ dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) { reply.ndev += 1; } dbg("%d devices are exported", reply.ndev); ret = usbip_send_op_common(sockfd, OP_REP_DEVLIST, ST_OK); if (ret < 0) { err("send op_common"); return ret; } PACK_OP_DEVLIST_REPLY(1, &reply); ret = usbip_send(sockfd, (void *) &reply, sizeof(reply)); if (ret < 0) { err("send op_devlist_reply"); return ret; } dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) { struct usb_device pdu_udev; dump_usb_device(&edev->udev); memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev)); pack_usb_device(1, &pdu_udev); ret = usbip_send(sockfd, (void *) &pdu_udev, sizeof(pdu_udev)); if (ret < 0) { err("send pdu_udev"); return ret; } for (int i=0; i < edev->udev.bNumInterfaces; i++) { struct usb_interface pdu_uinf; dump_usb_interface(&edev->uinf[i]); memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf)); pack_usb_interface(1, &pdu_uinf); ret = usbip_send(sockfd, (void *) &pdu_uinf, sizeof(pdu_uinf)); if (ret < 0) { err("send pdu_uinf"); return ret; } } } return 0; }
static int send_reply_devlist(int connfd) { struct usbip_exported_device *edev; struct usbip_usb_device pdu_udev; struct usbip_usb_interface pdu_uinf; struct op_devlist_reply reply; int i; int rc; reply.ndev = 0; /* number of exported devices */ dlist_for_each_data(host_driver->edev_list, edev, struct usbip_exported_device) { reply.ndev += 1; } info("exportable devices: %d", reply.ndev); rc = usbip_net_send_op_common(connfd, 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)); if (rc < 0) { dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST); return -1; } dlist_for_each_data(host_driver->edev_list, edev, struct usbip_exported_device) { dump_usb_device(&edev->udev); 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)); if (rc < 0) { dbg("usbip_net_send failed: pdu_udev"); return -1; } for (i = 0; i < edev->udev.bNumInterfaces; i++) { dump_usb_interface(&edev->uinf[i]); memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf)); usbip_net_pack_usb_interface(1, &pdu_uinf); rc = usbip_net_send(connfd, &pdu_uinf, sizeof(pdu_uinf)); if (rc < 0) { dbg("usbip_net_send failed: pdu_uinf"); return -1; } } } return 0; }
static int attach_exported_devices(char *host, int sockfd) { int ret; struct op_devlist_reply rep; uint16_t code = OP_REP_DEVLIST; bzero(&rep, sizeof(rep)); ret = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0); if(ret < 0) { err("send op_common"); return -1; } ret = usbip_recv_op_common(sockfd, &code); if(ret < 0) { err("recv op_common"); return -1; } ret = usbip_recv(sockfd, (void *) &rep, sizeof(rep)); if(ret < 0) { err("recv op_devlist"); return -1; } PACK_OP_DEVLIST_REPLY(0, &rep); dbg("exportable %d devices", rep.ndev); for(unsigned int i=0; i < rep.ndev; i++) { char product_name[100]; char class_name[100]; struct usb_device udev; bzero(&udev, sizeof(udev)); ret = usbip_recv(sockfd, (void *) &udev, sizeof(udev)); if(ret < 0) { err("recv usb_device[%d]", i); return -1; } pack_usb_device(0, &udev); usbip_names_get_product(product_name, sizeof(product_name), udev.idVendor, udev.idProduct); usbip_names_get_class(class_name, sizeof(class_name), udev.bDeviceClass, udev.bDeviceSubClass, udev.bDeviceProtocol); dbg("Attaching usb port %s from host %s on usbip, with deviceid: %s", udev.busid, host, product_name); attach_device(host, udev.busid); } return rep.ndev; }
static int get_exported_devices(char *host, int sockfd) { char product_name[100]; char class_name[100]; struct op_devlist_reply reply; uint16_t code = OP_REP_DEVLIST; struct usbip_usb_device udev; struct usbip_usb_interface uintf; unsigned int i; int j, rc; rc = usbip_net_send_op_common(sockfd, OP_REQ_DEVLIST, 0); if (rc < 0) { dbg("usbip_net_send_op_common failed"); return -1; } rc = usbip_net_recv_op_common(sockfd, &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)); if (rc < 0) { dbg("usbip_net_recv_op_devlist failed"); return -1; } PACK_OP_DEVLIST_REPLY(0, &reply); dbg("exportable devices: %d\n", reply.ndev); if (reply.ndev == 0) { info("no exportable devices found on %s", host); return 0; } printf("Exportable USB devices\n"); printf("======================\n"); printf(" - %s\n", host); for (i = 0; i < reply.ndev; i++) { memset(&udev, 0, sizeof(udev)); rc = usbip_net_recv(sockfd, &udev, sizeof(udev)); if (rc < 0) { dbg("usbip_net_recv failed: usbip_usb_device[%d]", i); return -1; } usbip_net_pack_usb_device(0, &udev); usbip_names_get_product(product_name, sizeof(product_name), udev.idVendor, udev.idProduct); usbip_names_get_class(class_name, sizeof(class_name), udev.bDeviceClass, udev.bDeviceSubClass, udev.bDeviceProtocol); printf("%11s: %s\n", udev.busid, product_name); printf("%11s: %s\n", "", udev.path); printf("%11s: %s\n", "", class_name); for (j = 0; j < udev.bNumInterfaces; j++) { rc = usbip_net_recv(sockfd, &uintf, sizeof(uintf)); if (rc < 0) { dbg("usbip_net_recv failed: usbip_usb_intf[%d]", j); return -1; } usbip_net_pack_usb_interface(0, &uintf); usbip_names_get_class(class_name, sizeof(class_name), uintf.bInterfaceClass, uintf.bInterfaceSubClass, uintf.bInterfaceProtocol); printf("%11s: %2d - %s\n", "", j, class_name); } printf("\n"); } return 0; }
static int query_exported_devices(int sockfd) { int ret; struct op_devlist_reply rep; uint16_t code = OP_REP_DEVLIST; bzero(&rep, sizeof(rep)); ret = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0); if (ret < 0) { err("send op_common"); return -1; } ret = usbip_recv_op_common(sockfd, &code); if (ret < 0) { err("recv op_common"); return -1; } ret = usbip_recv(sockfd, (void *) &rep, sizeof(rep)); if (ret < 0) { err("recv op_devlist"); return -1; } PACK_OP_DEVLIST_REPLY(0, &rep); dbg("exportable %d devices", rep.ndev); for (unsigned int i=0; i < rep.ndev; i++) { char product_name[100]; char class_name[100]; struct usb_device udev; bzero(&udev, sizeof(udev)); ret = usbip_recv(sockfd, (void *) &udev, sizeof(udev)); if (ret < 0) { err("recv usb_device[%d]", i); return -1; } pack_usb_device(0, &udev); usbip_names_get_product(product_name, sizeof(product_name), udev.idVendor, udev.idProduct); usbip_names_get_class(class_name, sizeof(class_name), udev.bDeviceClass, udev.bDeviceSubClass, udev.bDeviceProtocol); info("%8s: %s", udev.busid, product_name); info("%8s: %s", " ", udev.path); info("%8s: %s", " ", class_name); for (int j=0; j < udev.bNumInterfaces; j++) { struct usb_interface uinf; ret = usbip_recv(sockfd, (void *) &uinf, sizeof(uinf)); if (ret < 0) { err("recv usb_interface[%d]", j); return -1; } pack_usb_interface(0, &uinf); usbip_names_get_class(class_name, sizeof(class_name), uinf.bInterfaceClass, uinf.bInterfaceSubClass, uinf.bInterfaceProtocol); info("%8s: %2d - %s", " ", j, class_name); } info(" "); } return rep.ndev; }
static int query_interface0(SOCKET sockfd, char * busid, struct usb_interface * uinf0) { int ret; struct op_devlist_reply rep; uint16_t code = OP_REP_DEVLIST; uint32_t i,j; char product_name[100]; char class_name[100]; struct usb_device udev; struct usb_interface uinf; int found=0; memset(&rep, 0, sizeof(rep)); ret = usbip_send_op_common(sockfd, OP_REQ_DEVLIST, 0); if (ret < 0) { err("send op_common"); return -1; } ret = usbip_recv_op_common(sockfd, &code); if (ret < 0) { err("recv op_common"); return -1; } ret = usbip_recv(sockfd, (void *) &rep, sizeof(rep)); if (ret < 0) { err("recv op_devlist"); return -1; } PACK_OP_DEVLIST_REPLY(0, &rep); dbg("exportable %d devices", rep.ndev); for (i=0; i < rep.ndev; i++) { memset(&udev, 0, sizeof(udev)); ret = usbip_recv(sockfd, (void *) &udev, sizeof(udev)); if (ret < 0) { err("recv usb_device[%d]", i); return -1; } pack_usb_device(0, &udev); usbip_names_get_product(product_name, sizeof(product_name), udev.idVendor, udev.idProduct); usbip_names_get_class(class_name, sizeof(class_name), udev.bDeviceClass, udev.bDeviceSubClass, udev.bDeviceProtocol); dbg("%8s: %s", udev.busid, product_name); dbg("%8s: %s", " ", udev.path); dbg("%8s: %s", " ", class_name); for (j=0; j < udev.bNumInterfaces; j++) { ret = usbip_recv(sockfd, (void *) &uinf, sizeof(uinf)); if (ret < 0) { err("recv usb_interface[%d]", j); return -1; } pack_usb_interface(0, &uinf); if(!strcmp(udev.busid, busid)&&j==0){ memcpy(uinf0, &uinf, sizeof(uinf)); found=1; } usbip_names_get_class(class_name, sizeof(class_name), uinf.bInterfaceClass, uinf.bInterfaceSubClass, uinf.bInterfaceProtocol); dbg("%8s: %2d - %s", " ", j, class_name); } dbg(" "); } if(found) return 0; return -1; }