// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2011 matt mooney <[email protected]>
* 2005-2007 Takahiro Hirofuchi
* Copyright (C) 2015-2016 Samsung Electronics
* Igor Kotrasinski <[email protected]>
* Krzysztof Opasiak <[email protected]>
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#define _GNU_SOURCE
#include <errno.h>
#include <unistd.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#ifdef HAVE_LIBWRAP
#include <tcpd.h>
#endif
#include <getopt.h>
#include <signal.h>
#include <poll.h>
#include "usbip_host_driver.h"
#include "usbip_host_common.h"
#include "usbip_device_driver.h"
#include "usbip_common.h"
#include "usbip_network.h"
#include "list.h"
#undef PROGNAME
#define PROGNAME "usbipd"
#define MAXSOCKFD 20
#define PACKAGE_STRING "2.0"
#define MAIN_LOOP_TIMEOUT 10
#define DEFAULT_PID_FILE "/var/run/" PROGNAME ".pid"
static const char usbip_version_string[] = PACKAGE_STRING;
static const char usbipd_help_string[] =
"usage: usbipd [options]\n"
"\n"
" -4, --ipv4\n"
" Bind to IPv4. Default is both.\n"
"\n"
" -6, --ipv6\n"
" Bind to IPv6. Default is both.\n"
"\n"
" -e, --device\n"
" Run in device mode.\n"
" Rather than drive an attached device, create\n"
" a virtual UDC to bind gadgets to.\n"
"\n"
" -D, --daemon\n"
" Run as a daemon process.\n"
"\n"
" -d, --debug\n"
" Print debugging information.\n"
"\n"
" -PFILE, --pid FILE\n"
" Write process id to FILE.\n"
" If no FILE specified, use " DEFAULT_PID_FILE "\n"
"\n"
" -tPORT, --tcp-port PORT\n"
" Listen on TCP/IP port PORT.\n"
"\n"
" -h, --help\n"
" Print this help.\n"
"\n"
" -v, --version\n"
" Show version.\n";
static struct usbip_host_driver *driver;
static void usbipd_help(void)
{
printf("%s\n", usbipd_help_string);
}
static int recv_request_import(int sockfd)
{
struct op_import_request req;
struct usbip_exported_device *edev;
struct usbip_usb_device pdu_udev;
struct list_head *i;
int found = 0;
int status = ST_OK;
int rc;
memset(&req, 0, sizeof(req));
rc = usbip_net_recv(sockfd, &req, sizeof(req));
if (rc < 0) {
dbg("usbip_net_recv failed: import request");
return -1;
}
PACK_OP_IMPORT_REQUEST(0, &req);
list_for_each(i, &driver->edev_list) {
edev = list_entry(i, struct usbip_exported_device, node);
if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
info("found requested device: %s", req.busid);
found = 1;
break;
}
}
if (found) {
/* should set TCP_NODELAY for usbip */
usbip_net_set_nodelay(sockfd);
/* export device needs a TCP/IP socket descriptor */
status = usbip_export_device(edev, sockfd);
if (status < 0)
status = ST_NA;
} else {
info("requested device not found: %s", req.busid);
status = ST_NODEV;
}
rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT, status);
if (rc < 0) {
dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
return -1;
}
if (status) {
dbg("import request busid %s: failed", req.busid);
return -1;
}
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));
if (rc < 0) {
dbg("usbip_net_send failed: devinfo");
return -1;
}
dbg("import request busid %s: complete", req.busid);
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;
struct list_head *j;
int rc, i;
/*
* Exclude devices that are already exported to a client from
* the exportable device list to avoid:
* - import requests for devices that are exported only to
* fail the request.
* - revealing devices that are imported by a client to
* another client.
*/
reply.ndev = 0;
/* number of exported devices */
list_for_each(j, &driver->edev_list) {
edev = list_entry(j, struct usbip_exported_device, node);
if (edev->status != SDEV_ST_USED)
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;
}
list_for_each(j, &driver->edev_list) {
edev = list_entry(j, struct usbip_exported_device, node);
if (edev->status == SDEV_ST_USED)
continue;
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) {
err("usbip_net_send failed: pdu_uinf");
return -1;
}
}
}
return 0;
}
static int recv_request_devlist(int connfd)
{
struct op_devlist_request req;
int rc;
memset(&req, 0, sizeof(req));
rc = usbip_net_recv(connfd, &req, sizeof(req));
if (rc < 0) {
dbg("usbip_net_recv failed: devlist request");
return -1;
}
rc = send_reply_devlist(connfd);
if (rc < 0) {
dbg("send_reply_devlist failed");
return -1;
}
return 0;
}
static int recv_pdu(int connfd)
{
uint16_t code = OP_UNSPEC;
int ret;
int status;
ret = usbip_net_recv_op_common(connfd, &code, &status);
if (ret < 0) {
dbg("could not receive opcode: %#0x", code);
return -1;
}
ret = usbip_refresh_device_list(driver);
if (ret < 0) {
dbg("could not refresh device list: %d", ret);
return -1;
}
info("received request: %#0x(%d)", code, connfd);
switch (code) {
case OP_REQ_DEVLIST:
ret = recv_request_devlist(connfd);
break;
case OP_REQ_IMPORT:
ret = recv_request_import(connfd);
break;
case OP_REQ_DEVINFO:
case OP_REQ_CRYPKEY:
default:
err("received an unknown opcode: %#0x", code);
ret = -1;
}
if (ret == 0)
info("request %#0x(%d): complete", code, connfd);
else
info("request %#0x(%d): failed", code, connfd);
return ret;
}
#ifdef HAVE_LIBWRAP
static int tcpd_auth(int connfd)
{
struct request_info request;
int rc;
request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
fromhost(&request);
rc = hosts_access(&request);
if (rc == 0)
return -1;
return 0;
}
#endif
static int do_accept(int listenfd)
{
int connfd;
struct sockaddr_storage ss;
socklen_t len = sizeof(ss);
char host[NI_MAXHOST], port[NI_MAXSERV];
int rc;
memset(&ss, 0, sizeof(ss));
connfd = accept(listenfd, (struct sockaddr *)&ss, &len);
if (connfd < 0) {
err("failed to accept connection");
return -1;
}
rc = getnameinfo((struct sockaddr *)&ss, len, host, sizeof(host),
port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
if (rc)
err("getnameinfo: %s", gai_strerror(rc));
#ifdef HAVE_LIBWRAP
rc = tcpd_auth(connfd);
if (rc < 0) {
info("denied access from %s", host);
close(connfd);
return -1;
}
#endif
info("connection from %s:%s", host, port);
return connfd;
}
int process_request(int listenfd)
{
pid_t childpid;
int connfd;
connfd = do_accept(listenfd);
if (connfd < 0)
return -1;
childpid = fork();
if (childpid == 0) {
close(listenfd);
recv_pdu(connfd);
exit(0);
}
close(connfd);
return 0;
}
static void addrinfo_to_text(struct addrinfo *ai, char buf[],
const size_t buf_size)
{
char hbuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
int rc;
buf[0] = '\0';
rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
sbuf, sizeof(sb
没有合适的资源?快使用搜索试试~ 我知道了~
usbip linux编译
共32个文件
c:18个
h:12个
makefile:1个
需积分: 0 1 下载量 58 浏览量
2023-10-31
12:15:51
上传
评论
收藏 89KB RAR 举报
温馨提示
usbip linux编译
资源推荐
资源详情
资源评论
收起资源包目录
usbip.rar (32个子文件)
usbip
usbip_list.c 9KB
names.c 10KB
usbip_host_driver.c 1KB
makefile 3KB
usbip_common.c 7KB
usbip_host_common.c 6KB
list.h 4KB
usbip_device_driver.c 4KB
usbip_host_driver.h 561B
usbip_network.c 6KB
sysfs_utils.c 1018B
usbip_unbind.c 3KB
usbip_attach.c 5KB
usbip_log.h 176B
usbip_bind.c 5KB
usbip_port.c 1KB
usbip_detach.c 2KB
usbip.h 693B
usbipd.c 14KB
utils.c 1KB
gxp_usbip 158KB
vhci_driver.c 10KB
usbip_network.h 5KB
usbip_device_driver.h 610B
usbip_log.c 686B
names.h 798B
usbip.c 4KB
usbip_common.h 4KB
vhci_driver.h 1KB
utils.h 264B
sysfs_utils.h 208B
usbip_host_common.h 3KB
共 32 条
- 1
资源评论
wang5525369
- 粉丝: 7
- 资源: 10
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功