Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 | /*
*
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif
#include <unistd.h>
#include <netdb.h>
#include <strings.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
#define _GNU_SOURCE
#include <getopt.h>
#include <signal.h>
#include "usbip.h"
#include "usbip_network.h"
#include <glib.h>
static const char version[] = PACKAGE_STRING;
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 recv_request_devlist(int sockfd)
{
int ret;
struct op_devlist_request req;
bzero(&req, sizeof(req));
ret = usbip_recv(sockfd, (void *) &req, sizeof(req));
if (ret < 0) {
err("recv devlist request");
return -1;
}
ret = send_reply_devlist(sockfd);
if (ret < 0) {
err("send devlist reply");
return -1;
}
return 0;
}
static int recv_request_import(int sockfd)
{
int ret;
struct op_import_request req;
struct op_common reply;
struct usbip_exported_device *edev;
int found = 0;
int error = 0;
bzero(&req, sizeof(req));
bzero(&reply, sizeof(reply));
ret = usbip_recv(sockfd, (void *) &req, sizeof(req));
if (ret < 0) {
err("recv import request");
return -1;
}
PACK_OP_IMPORT_REQUEST(0, &req);
dlist_for_each_data(stub_driver->edev_list, edev, struct usbip_exported_device) {
if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
dbg("found requested device %s", req.busid);
found = 1;
break;
}
}
if (found) {
/* should set TCP_NODELAY for usbip */
usbip_set_nodelay(sockfd);
/* export_device needs a TCP/IP socket descriptor */
ret = usbip_stub_export_device(edev, sockfd);
if (ret < 0)
error = 1;
} else {
info("not found requested device %s", req.busid);
error = 1;
}
ret = usbip_send_op_common(sockfd, OP_REP_IMPORT, (!error ? ST_OK : ST_NA));
if (ret < 0) {
err("send import reply");
return -1;
}
if (!error) {
struct usb_device pdu_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 devinfo");
return -1;
}
}
return 0;
}
static int recv_pdu(int sockfd)
{
int ret;
uint16_t code = OP_UNSPEC;
ret = usbip_recv_op_common(sockfd, &code);
if (ret < 0) {
err("recv op_common, %d", ret);
return ret;
}
ret = usbip_stub_refresh_device_list();
if (ret < 0)
return -1;
switch(code) {
case OP_REQ_DEVLIST:
ret = recv_request_devlist(sockfd);
break;
case OP_REQ_IMPORT:
ret = recv_request_import(sockfd);
break;
case OP_REQ_DEVINFO:
case OP_REQ_CRYPKEY:
default:
err("unknown op_code, %d", code);
ret = -1;
}
return ret;
}
static void log_addrinfo(struct addrinfo *ai)
{
int ret;
char hbuf[NI_MAXHOST];
char sbuf[NI_MAXSERV];
ret = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
if (ret)
err("getnameinfo, %s", gai_strerror(ret));
info("listen at [%s]:%s", hbuf, sbuf);
}
static struct addrinfo *my_getaddrinfo(char *host, int ai_family)
{
int ret;
struct addrinfo hints, *ai_head;
bzero(&hints, sizeof(hints));
hints.ai_family = ai_family;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
ret = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
if (ret) {
err("%s: %s", USBIP_PORT_STRING, gai_strerror(ret));
return NULL;
}
return ai_head;
}
#define MAXSOCK 20
static int listen_all_addrinfo(struct addrinfo *ai_head, int lsock[])
{
struct addrinfo *ai;
int n = 0; /* number of sockets */
for (ai = ai_head; ai && n < MAXSOCK; ai = ai->ai_next) {
int ret;
lsock[n] = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
if (lsock[n] < 0)
continue;
usbip_set_reuseaddr(lsock[n]);
usbip_set_nodelay(lsock[n]);
if (lsock[n] >= FD_SETSIZE) {
close(lsock[n]);
lsock[n] = -1;
continue;
}
ret = bind(lsock[n], ai->ai_addr, ai->ai_addrlen);
if (ret < 0) {
close(lsock[n]);
lsock[n] = -1;
continue;
}
ret = listen(lsock[n], SOMAXCONN);
if (ret < 0) {
close(lsock[n]);
lsock[n] = -1;
continue;
}
log_addrinfo(ai);
/* next if succeed */
n++;
}
if (n == 0) {
err("no socket to listen to");
return -1;
}
dbg("listen %d address%s", n, (n==1)?"":"es");
return n;
}
#ifdef HAVE_LIBWRAP
static int tcpd_auth(int csock)
{
int ret;
struct request_info request;
request_init(&request, RQ_DAEMON, "usbipd", RQ_FILE, csock, 0);
fromhost(&request);
ret = hosts_access(&request);
if (!ret)
return -1;
return 0;
}
#endif
static int my_accept(int lsock)
{
int csock;
struct sockaddr_storage ss;
socklen_t len = sizeof(ss);
char host[NI_MAXHOST], port[NI_MAXSERV];
int ret;
bzero(&ss, sizeof(ss));
csock = accept(lsock, (struct sockaddr *) &ss, &len);
if (csock < 0) {
err("accept");
return -1;
}
ret = getnameinfo((struct sockaddr *) &ss, len,
host, sizeof(host), port, sizeof(port),
(NI_NUMERICHOST | NI_NUMERICSERV));
if (ret)
err("getnameinfo, %s", gai_strerror(ret));
#ifdef HAVE_LIBWRAP
ret = tcpd_auth(csock);
if (ret < 0) {
info("deny access from %s", host);
close(csock);
return -1;
}
#endif
info("connected from %s:%s", host, port);
return csock;
}
GMainLoop *main_loop;
static void signal_handler(int i)
{
dbg("signal catched, code %d", i);
if (main_loop)
g_main_loop_quit(main_loop);
}
static void set_signal(void)
{
struct sigaction act;
bzero(&act, sizeof(act));
act.sa_handler = signal_handler;
sigemptyset(&act.sa_mask);
sigaction(SIGTERM, &act, NULL);
sigaction(SIGINT, &act, NULL);
}
gboolean process_comming_request(GIOChannel *gio, GIOCondition condition,
gpointer data __attribute__((unused)))
{
int ret;
if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL))
g_error("unknown condition");
if (condition & G_IO_IN) {
int lsock;
int csock;
lsock = g_io_channel_unix_get_fd(gio);
csock = my_accept(lsock);
if (csock < 0)
return TRUE;
ret = recv_pdu(csock);
if (ret < 0)
err("process recieved pdu");
close(csock);
}
return TRUE;
}
static void do_standalone_mode(gboolean daemonize)
{
int ret;
int lsock[MAXSOCK];
struct addrinfo *ai_head;
int n;
ret = usbip_names_init(USBIDS_FILE);
if (ret)
err("open usb.ids");
ret = usbip_stub_driver_open();
if (ret < 0)
g_error("driver open failed");
if (daemonize) {
if (daemon(0,0) < 0)
g_error("daemonizing failed: %s", g_strerror(errno));
usbip_use_syslog = 1;
}
set_signal();
ai_head = my_getaddrinfo(NULL, PF_UNSPEC);
if (!ai_head)
return;
n = listen_all_addrinfo(ai_head, lsock);
if (n <= 0)
g_error("no socket to listen to");
for (int i = 0; i < n; i++) {
GIOChannel *gio;
gio = g_io_channel_unix_new(lsock[i]);
g_io_add_watch(gio, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
process_comming_request, NULL);
}
info("usbipd start (%s)", version);
main_loop = g_main_loop_new(FALSE, FALSE);
g_main_loop_run(main_loop);
info("shutdown");
freeaddrinfo(ai_head);
usbip_names_free();
usbip_stub_driver_close();
return;
}
static const char help_message[] = "\
Usage: usbipd [options] \n\
-D, --daemon \n\
Run as a daemon process. \n\
\n\
-d, --debug \n\
Print debugging information. \n\
\n\
-v, --version \n\
Show version. \n\
\n\
-h, --help \n\
Print this help. \n";
static void show_help(void)
{
printf("%s", help_message);
}
static const struct option longopts[] = {
{"daemon", no_argument, NULL, 'D'},
{"debug", no_argument, NULL, 'd'},
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, NULL, 0}
};
int main(int argc, char *argv[])
{
gboolean daemonize = FALSE;
enum {
cmd_standalone_mode = 1,
cmd_help,
cmd_version
} cmd = cmd_standalone_mode;
usbip_use_stderr = 1;
usbip_use_syslog = 0;
if (geteuid() != 0)
g_warning("running non-root?");
for (;;) {
int c;
int index = 0;
c = getopt_long(argc, argv, "vhdD", longopts, &index);
if (c == -1)
break;
switch (c) {
case 'd':
usbip_use_debug = 1;
continue;
case 'v':
cmd = cmd_version;
break;
case 'h':
cmd = cmd_help;
break;
case 'D':
daemonize = TRUE;
break;
case '?':
show_help();
exit(EXIT_FAILURE);
default:
err("getopt");
}
}
switch (cmd) {
case cmd_standalone_mode:
do_standalone_mode(daemonize);
break;
case cmd_version:
printf("%s\n", version);
break;
case cmd_help:
show_help();
break;
default:
info("unknown cmd");
show_help();
}
return 0;
}
|