2 * Copyright (C) 2011 matt mooney <mfm@muteddisk.com>
3 * 2005-2007 Takahiro Hirofuchi
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.
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.
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/>.
20 #include "../config.h"
29 #include <sys/types.h>
31 #include <arpa/inet.h>
32 #include <sys/socket.h>
33 #include <netinet/in.h>
43 #include "usbip_host_driver.h"
44 #include "usbip_common.h"
45 #include "usbip_network.h"
48 #define PROGNAME "usbipd"
51 #define MAIN_LOOP_TIMEOUT 10
53 static const char usbip_version_string[] = PACKAGE_STRING;
55 static const char usbipd_help_string[] =
56 "usage: usbipd [options] \n"
58 " Run as a daemon process. \n"
61 " Print debugging information. \n"
64 " Print this help. \n"
69 static void usbipd_help(void)
71 printf("%s\n", usbipd_help_string);
74 static int recv_request_import(int sockfd)
76 struct op_import_request req;
77 struct op_common reply;
78 struct usbip_exported_device *edev;
79 struct usbip_usb_device pdu_udev;
84 memset(&req, 0, sizeof(req));
85 memset(&reply, 0, sizeof(reply));
87 rc = usbip_net_recv(sockfd, &req, sizeof(req));
89 dbg("usbip_net_recv failed: import request");
92 PACK_OP_IMPORT_REQUEST(0, &req);
94 dlist_for_each_data(host_driver->edev_list, edev,
95 struct usbip_exported_device) {
96 if (!strncmp(req.busid, edev->udev.busid, SYSFS_BUS_ID_SIZE)) {
97 info("found requested device: %s", req.busid);
104 /* should set TCP_NODELAY for usbip */
105 usbip_net_set_nodelay(sockfd);
107 /* export device needs a TCP/IP socket descriptor */
108 rc = usbip_host_export_device(edev, sockfd);
112 info("requested device not found: %s", req.busid);
116 rc = usbip_net_send_op_common(sockfd, OP_REP_IMPORT,
117 (!error ? ST_OK : ST_NA));
119 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_IMPORT);
124 dbg("import request busid %s: failed", req.busid);
128 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
129 usbip_net_pack_usb_device(1, &pdu_udev);
131 rc = usbip_net_send(sockfd, &pdu_udev, sizeof(pdu_udev));
133 dbg("usbip_net_send failed: devinfo");
137 dbg("import request busid %s: complete", req.busid);
142 static int send_reply_devlist(int connfd)
144 struct usbip_exported_device *edev;
145 struct usbip_usb_device pdu_udev;
146 struct usbip_usb_interface pdu_uinf;
147 struct op_devlist_reply reply;
152 /* number of exported devices */
153 dlist_for_each_data(host_driver->edev_list, edev,
154 struct usbip_exported_device) {
157 info("exportable devices: %d", reply.ndev);
159 rc = usbip_net_send_op_common(connfd, OP_REP_DEVLIST, ST_OK);
161 dbg("usbip_net_send_op_common failed: %#0x", OP_REP_DEVLIST);
164 PACK_OP_DEVLIST_REPLY(1, &reply);
166 rc = usbip_net_send(connfd, &reply, sizeof(reply));
168 dbg("usbip_net_send failed: %#0x", OP_REP_DEVLIST);
172 dlist_for_each_data(host_driver->edev_list, edev,
173 struct usbip_exported_device) {
174 dump_usb_device(&edev->udev);
175 memcpy(&pdu_udev, &edev->udev, sizeof(pdu_udev));
176 usbip_net_pack_usb_device(1, &pdu_udev);
178 rc = usbip_net_send(connfd, &pdu_udev, sizeof(pdu_udev));
180 dbg("usbip_net_send failed: pdu_udev");
184 for (i = 0; i < edev->udev.bNumInterfaces; i++) {
185 dump_usb_interface(&edev->uinf[i]);
186 memcpy(&pdu_uinf, &edev->uinf[i], sizeof(pdu_uinf));
187 usbip_net_pack_usb_interface(1, &pdu_uinf);
189 rc = usbip_net_send(connfd, &pdu_uinf,
192 dbg("usbip_net_send failed: pdu_uinf");
201 static int recv_request_devlist(int connfd)
203 struct op_devlist_request req;
206 memset(&req, 0, sizeof(req));
208 rc = usbip_net_recv(connfd, &req, sizeof(req));
210 dbg("usbip_net_recv failed: devlist request");
214 rc = send_reply_devlist(connfd);
216 dbg("send_reply_devlist failed");
223 static int recv_pdu(int connfd)
225 uint16_t code = OP_UNSPEC;
228 ret = usbip_net_recv_op_common(connfd, &code);
230 dbg("could not receive opcode: %#0x", code);
234 ret = usbip_host_refresh_device_list();
236 dbg("could not refresh device list: %d", ret);
240 info("received request: %#0x(%d)", code, connfd);
243 ret = recv_request_devlist(connfd);
246 ret = recv_request_import(connfd);
251 err("received an unknown opcode: %#0x", code);
256 info("request %#0x(%d): complete", code, connfd);
258 info("request %#0x(%d): failed", code, connfd);
264 static int tcpd_auth(int connfd)
266 struct request_info request;
269 request_init(&request, RQ_DAEMON, PROGNAME, RQ_FILE, connfd, 0);
271 rc = hosts_access(&request);
279 static int do_accept(int listenfd)
282 struct sockaddr_storage ss;
283 socklen_t len = sizeof(ss);
284 char host[NI_MAXHOST], port[NI_MAXSERV];
287 memset(&ss, 0, sizeof(ss));
289 connfd = accept(listenfd, (struct sockaddr *) &ss, &len);
291 err("failed to accept connection");
295 rc = getnameinfo((struct sockaddr *) &ss, len, host, sizeof(host),
296 port, sizeof(port), NI_NUMERICHOST | NI_NUMERICSERV);
298 err("getnameinfo: %s", gai_strerror(rc));
301 rc = tcpd_auth(connfd);
303 info("denied access from %s", host);
308 info("connection from %s:%s", host, port);
313 int process_request(int listenfd)
318 connfd = do_accept(listenfd);
331 static void log_addrinfo(struct addrinfo *ai)
333 char hbuf[NI_MAXHOST];
334 char sbuf[NI_MAXSERV];
337 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
338 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
340 err("getnameinfo: %s", gai_strerror(rc));
342 info("listening on %s:%s", hbuf, sbuf);
345 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[])
348 int ret, nsockfd = 0;
350 for (ai = ai_head; ai && nsockfd < MAXSOCKFD; ai = ai->ai_next) {
351 sockfdlist[nsockfd] = socket(ai->ai_family, ai->ai_socktype,
353 if (sockfdlist[nsockfd] < 0)
356 usbip_net_set_reuseaddr(sockfdlist[nsockfd]);
357 usbip_net_set_nodelay(sockfdlist[nsockfd]);
359 if (sockfdlist[nsockfd] >= FD_SETSIZE) {
360 close(sockfdlist[nsockfd]);
361 sockfdlist[nsockfd] = -1;
365 ret = bind(sockfdlist[nsockfd], ai->ai_addr, ai->ai_addrlen);
367 close(sockfdlist[nsockfd]);
368 sockfdlist[nsockfd] = -1;
372 ret = listen(sockfdlist[nsockfd], SOMAXCONN);
374 close(sockfdlist[nsockfd]);
375 sockfdlist[nsockfd] = -1;
386 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
391 static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
393 struct addrinfo hints, *ai_head;
396 memset(&hints, 0, sizeof(hints));
397 hints.ai_family = ai_family;
398 hints.ai_socktype = SOCK_STREAM;
399 hints.ai_flags = AI_PASSIVE;
401 rc = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
403 err("failed to get a network address %s: %s", USBIP_PORT_STRING,
411 static void signal_handler(int i)
413 dbg("received '%s' signal", strsignal(i));
416 static void set_signal(void)
418 struct sigaction act;
420 memset(&act, 0, sizeof(act));
421 act.sa_handler = signal_handler;
422 sigemptyset(&act.sa_mask);
423 sigaction(SIGTERM, &act, NULL);
424 sigaction(SIGINT, &act, NULL);
425 act.sa_handler = SIG_IGN;
426 sigaction(SIGCLD, &act, NULL);
429 static int do_standalone_mode(int daemonize)
431 struct addrinfo *ai_head;
432 int sockfdlist[MAXSOCKFD];
436 struct timespec timeout;
439 if (usbip_names_init(USBIDS_FILE))
440 err("failed to open %s", USBIDS_FILE);
442 if (usbip_host_driver_open()) {
443 err("please load " USBIP_CORE_MOD_NAME ".ko and "
444 USBIP_HOST_DRV_NAME ".ko!");
449 if (daemon(0,0) < 0) {
450 err("daemonizing failed: %s", strerror(errno));
454 usbip_use_syslog = 1;
458 ai_head = do_getaddrinfo(NULL, PF_UNSPEC);
462 info("starting " PROGNAME " (%s)", usbip_version_string);
464 nsockfd = listen_all_addrinfo(ai_head, sockfdlist);
466 err("failed to open a listening socket");
469 fds = calloc(nsockfd, sizeof(struct pollfd));
470 for (i = 0; i < nsockfd; i++) {
471 fds[i].fd = sockfdlist[i];
472 fds[i].events = POLLIN;
474 timeout.tv_sec = MAIN_LOOP_TIMEOUT;
477 sigfillset(&sigmask);
478 sigdelset(&sigmask, SIGTERM);
479 sigdelset(&sigmask, SIGINT);
485 r = ppoll(fds, nsockfd, &timeout, &sigmask);
487 dbg("%s", strerror(errno));
490 for (i = 0; i < nsockfd; i++) {
491 if (fds[i].revents & POLLIN) {
492 dbg("read event on fd[%d]=%d",
494 process_request(sockfdlist[i]);
498 dbg("heartbeat timeout on ppoll()");
501 info("shutting down " PROGNAME);
503 freeaddrinfo(ai_head);
504 usbip_host_driver_close();
510 int main(int argc, char *argv[])
512 static const struct option longopts[] = {
513 { "daemon", no_argument, NULL, 'D' },
514 { "debug", no_argument, NULL, 'd' },
515 { "help", no_argument, NULL, 'h' },
516 { "version", no_argument, NULL, 'v' },
521 cmd_standalone_mode = 1,
529 usbip_use_stderr = 1;
530 usbip_use_syslog = 0;
533 err("not running as root?");
535 cmd = cmd_standalone_mode;
537 opt = getopt_long(argc, argv, "Ddhv", longopts, NULL);
563 case cmd_standalone_mode:
564 rc = do_standalone_mode(daemonize);
567 printf(PROGNAME " (%s)\n", usbip_version_string);
580 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);