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"
28 #include <sys/types.h>
30 #include <arpa/inet.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
43 #include "usbip_host_driver.h"
44 #include "usbip_common.h"
45 #include "usbip_network.h"
48 #define PROGNAME "usbipd"
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 gboolean process_request(GIOChannel *gio, GIOCondition condition,
314 gpointer unused_data)
321 if (condition & (G_IO_ERR | G_IO_HUP | G_IO_NVAL)) {
322 err("unknown condition");
326 if (condition & G_IO_IN) {
327 listenfd = g_io_channel_unix_get_fd(gio);
328 connfd = do_accept(listenfd);
339 static void log_addrinfo(struct addrinfo *ai)
341 char hbuf[NI_MAXHOST];
342 char sbuf[NI_MAXSERV];
345 rc = getnameinfo(ai->ai_addr, ai->ai_addrlen, hbuf, sizeof(hbuf),
346 sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
348 err("getnameinfo: %s", gai_strerror(rc));
350 info("listening on %s:%s", hbuf, sbuf);
353 static int listen_all_addrinfo(struct addrinfo *ai_head, int sockfdlist[])
356 int ret, nsockfd = 0;
358 for (ai = ai_head; ai && nsockfd < MAXSOCKFD; ai = ai->ai_next) {
359 sockfdlist[nsockfd] = socket(ai->ai_family, ai->ai_socktype,
361 if (sockfdlist[nsockfd] < 0)
364 usbip_net_set_reuseaddr(sockfdlist[nsockfd]);
365 usbip_net_set_nodelay(sockfdlist[nsockfd]);
367 if (sockfdlist[nsockfd] >= FD_SETSIZE) {
368 close(sockfdlist[nsockfd]);
369 sockfdlist[nsockfd] = -1;
373 ret = bind(sockfdlist[nsockfd], ai->ai_addr, ai->ai_addrlen);
375 close(sockfdlist[nsockfd]);
376 sockfdlist[nsockfd] = -1;
380 ret = listen(sockfdlist[nsockfd], SOMAXCONN);
382 close(sockfdlist[nsockfd]);
383 sockfdlist[nsockfd] = -1;
394 dbg("listening on %d address%s", nsockfd, (nsockfd == 1) ? "" : "es");
399 static struct addrinfo *do_getaddrinfo(char *host, int ai_family)
401 struct addrinfo hints, *ai_head;
404 memset(&hints, 0, sizeof(hints));
405 hints.ai_family = ai_family;
406 hints.ai_socktype = SOCK_STREAM;
407 hints.ai_flags = AI_PASSIVE;
409 rc = getaddrinfo(host, USBIP_PORT_STRING, &hints, &ai_head);
411 err("failed to get a network address %s: %s", USBIP_PORT_STRING,
419 static void signal_handler(int i)
421 dbg("received signal: code %d", i);
424 g_main_loop_quit(main_loop);
427 static void set_signal(void)
429 struct sigaction act;
431 memset(&act, 0, sizeof(act));
432 act.sa_handler = signal_handler;
433 sigemptyset(&act.sa_mask);
434 sigaction(SIGTERM, &act, NULL);
435 sigaction(SIGINT, &act, NULL);
438 static int do_standalone_mode(gboolean daemonize)
440 struct addrinfo *ai_head;
441 int sockfdlist[MAXSOCKFD];
445 if (usbip_names_init(USBIDS_FILE))
446 err("failed to open %s", USBIDS_FILE);
448 if (usbip_host_driver_open()) {
449 err("please load " USBIP_CORE_MOD_NAME ".ko and "
450 USBIP_HOST_DRV_NAME ".ko!");
455 if (daemon(0,0) < 0) {
456 err("daemonizing failed: %s", strerror(errno));
460 usbip_use_syslog = 1;
464 ai_head = do_getaddrinfo(NULL, PF_UNSPEC);
468 info("starting " PROGNAME " (%s)", usbip_version_string);
470 nsockfd = listen_all_addrinfo(ai_head, sockfdlist);
472 err("failed to open a listening socket");
476 for (i = 0; i < nsockfd; i++) {
479 gio = g_io_channel_unix_new(sockfdlist[i]);
480 g_io_add_watch(gio, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
481 process_request, NULL);
484 main_loop = g_main_loop_new(FALSE, FALSE);
485 g_main_loop_run(main_loop);
487 info("shutting down " PROGNAME);
489 freeaddrinfo(ai_head);
490 usbip_host_driver_close();
496 int main(int argc, char *argv[])
498 static const struct option longopts[] = {
499 { "daemon", no_argument, NULL, 'D' },
500 { "debug", no_argument, NULL, 'd' },
501 { "help", no_argument, NULL, 'h' },
502 { "version", no_argument, NULL, 'v' },
507 cmd_standalone_mode = 1,
512 gboolean daemonize = FALSE;
515 usbip_use_stderr = 1;
516 usbip_use_syslog = 0;
519 err("not running as root?");
521 cmd = cmd_standalone_mode;
523 opt = getopt_long(argc, argv, "Ddhv", longopts, NULL);
549 case cmd_standalone_mode:
550 rc = do_standalone_mode(daemonize);
553 printf(PROGNAME " (%s)\n", usbip_version_string);
566 return (rc > -1 ? EXIT_SUCCESS : EXIT_FAILURE);