3 * Copyright (C) 2005-2007 Takahiro Hirofuchi
14 #include <sys/types.h>
16 #include <arpa/inet.h>
17 #include <sys/socket.h>
18 #include <netinet/in.h>
29 #include "usbip_network.h"
33 static const char version
[] = PACKAGE_STRING
;
36 static int send_reply_devlist(int sockfd
)
39 struct usbip_exported_device
*edev
;
40 struct op_devlist_reply reply
;
45 /* how many devices are exported ? */
46 dlist_for_each_data(stub_driver
->edev_list
, edev
, struct usbip_exported_device
) {
50 dbg("%d devices are exported", reply
.ndev
);
52 ret
= usbip_send_op_common(sockfd
, OP_REP_DEVLIST
, ST_OK
);
54 err("send op_common");
58 PACK_OP_DEVLIST_REPLY(1, &reply
);
60 ret
= usbip_send(sockfd
, (void *) &reply
, sizeof(reply
));
62 err("send op_devlist_reply");
66 dlist_for_each_data(stub_driver
->edev_list
, edev
, struct usbip_exported_device
) {
67 struct usb_device pdu_udev
;
69 dump_usb_device(&edev
->udev
);
70 memcpy(&pdu_udev
, &edev
->udev
, sizeof(pdu_udev
));
71 pack_usb_device(1, &pdu_udev
);
73 ret
= usbip_send(sockfd
, (void *) &pdu_udev
, sizeof(pdu_udev
));
79 for (int i
=0; i
< edev
->udev
.bNumInterfaces
; i
++) {
80 struct usb_interface pdu_uinf
;
82 dump_usb_interface(&edev
->uinf
[i
]);
83 memcpy(&pdu_uinf
, &edev
->uinf
[i
], sizeof(pdu_uinf
));
84 pack_usb_interface(1, &pdu_uinf
);
86 ret
= usbip_send(sockfd
, (void *) &pdu_uinf
, sizeof(pdu_uinf
));
98 static int recv_request_devlist(int sockfd
)
101 struct op_devlist_request req
;
103 bzero(&req
, sizeof(req
));
105 ret
= usbip_recv(sockfd
, (void *) &req
, sizeof(req
));
107 err("recv devlist request");
111 ret
= send_reply_devlist(sockfd
);
113 err("send devlist reply");
121 static int recv_request_import(int sockfd
)
124 struct op_import_request req
;
125 struct op_common reply
;
126 struct usbip_exported_device
*edev
;
130 bzero(&req
, sizeof(req
));
131 bzero(&reply
, sizeof(reply
));
133 ret
= usbip_recv(sockfd
, (void *) &req
, sizeof(req
));
135 err("recv import request");
139 PACK_OP_IMPORT_REQUEST(0, &req
);
141 dlist_for_each_data(stub_driver
->edev_list
, edev
, struct usbip_exported_device
) {
142 if (!strncmp(req
.busid
, edev
->udev
.busid
, SYSFS_BUS_ID_SIZE
)) {
143 dbg("found requested device %s", req
.busid
);
150 /* should set TCP_NODELAY for usbip */
151 usbip_set_nodelay(sockfd
);
153 /* export_device needs a TCP/IP socket descriptor */
154 ret
= usbip_stub_export_device(edev
, sockfd
);
158 info("not found requested device %s", req
.busid
);
163 ret
= usbip_send_op_common(sockfd
, OP_REP_IMPORT
, (!error
? ST_OK
: ST_NA
));
165 err("send import reply");
170 struct usb_device pdu_udev
;
172 memcpy(&pdu_udev
, &edev
->udev
, sizeof(pdu_udev
));
173 pack_usb_device(1, &pdu_udev
);
175 ret
= usbip_send(sockfd
, (void *) &pdu_udev
, sizeof(pdu_udev
));
187 static int recv_pdu(int sockfd
)
190 uint16_t code
= OP_UNSPEC
;
193 ret
= usbip_recv_op_common(sockfd
, &code
);
195 err("recv op_common, %d", ret
);
200 ret
= usbip_stub_refresh_device_list();
206 ret
= recv_request_devlist(sockfd
);
210 ret
= recv_request_import(sockfd
);
217 err("unknown op_code, %d", code
);
228 static void log_addrinfo(struct addrinfo
*ai
)
231 char hbuf
[NI_MAXHOST
];
232 char sbuf
[NI_MAXSERV
];
234 ret
= getnameinfo(ai
->ai_addr
, ai
->ai_addrlen
, hbuf
, sizeof(hbuf
),
235 sbuf
, sizeof(sbuf
), NI_NUMERICHOST
| NI_NUMERICSERV
);
237 err("getnameinfo, %s", gai_strerror(ret
));
239 info("listen at [%s]:%s", hbuf
, sbuf
);
242 static struct addrinfo
*my_getaddrinfo(char *host
, int ai_family
)
245 struct addrinfo hints
, *ai_head
;
247 bzero(&hints
, sizeof(hints
));
249 hints
.ai_family
= ai_family
;
250 hints
.ai_socktype
= SOCK_STREAM
;
251 hints
.ai_flags
= AI_PASSIVE
;
253 ret
= getaddrinfo(host
, USBIP_PORT_STRING
, &hints
, &ai_head
);
255 err("%s: %s", USBIP_PORT_STRING
, gai_strerror(ret
));
263 static int listen_all_addrinfo(struct addrinfo
*ai_head
, int lsock
[])
266 int n
= 0; /* number of sockets */
268 for (ai
= ai_head
; ai
&& n
< MAXSOCK
; ai
= ai
->ai_next
) {
271 lsock
[n
] = socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
275 usbip_set_reuseaddr(lsock
[n
]);
276 usbip_set_nodelay(lsock
[n
]);
278 if (lsock
[n
] >= FD_SETSIZE
) {
284 ret
= bind(lsock
[n
], ai
->ai_addr
, ai
->ai_addrlen
);
291 ret
= listen(lsock
[n
], SOMAXCONN
);
300 /* next if succeed */
305 err("no socket to listen to");
309 dbg("listen %d address%s", n
, (n
==1)?"":"es");
315 static int tcpd_auth(int csock
)
318 struct request_info request
;
320 request_init(&request
, RQ_DAEMON
, "usbipd", RQ_FILE
, csock
, 0);
324 ret
= hosts_access(&request
);
332 static int my_accept(int lsock
)
335 struct sockaddr_storage ss
;
336 socklen_t len
= sizeof(ss
);
337 char host
[NI_MAXHOST
], port
[NI_MAXSERV
];
340 bzero(&ss
, sizeof(ss
));
342 csock
= accept(lsock
, (struct sockaddr
*) &ss
, &len
);
348 ret
= getnameinfo((struct sockaddr
*) &ss
, len
,
349 host
, sizeof(host
), port
, sizeof(port
),
350 (NI_NUMERICHOST
| NI_NUMERICSERV
));
352 err("getnameinfo, %s", gai_strerror(ret
));
355 ret
= tcpd_auth(csock
);
357 info("deny access from %s", host
);
363 info("connected from %s:%s", host
, port
);
369 GMainLoop
*main_loop
;
371 static void signal_handler(int i
)
373 dbg("signal catched, code %d", i
);
376 g_main_loop_quit(main_loop
);
379 static void set_signal(void)
381 struct sigaction act
;
383 bzero(&act
, sizeof(act
));
384 act
.sa_handler
= signal_handler
;
385 sigemptyset(&act
.sa_mask
);
386 sigaction(SIGTERM
, &act
, NULL
);
387 sigaction(SIGINT
, &act
, NULL
);
391 gboolean
process_comming_request(GIOChannel
*gio
, GIOCondition condition
,
392 gpointer data
__attribute__((unused
)))
396 if (condition
& (G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
))
397 g_error("unknown condition");
400 if (condition
& G_IO_IN
) {
404 lsock
= g_io_channel_unix_get_fd(gio
);
406 csock
= my_accept(lsock
);
410 ret
= recv_pdu(csock
);
412 err("process recieved pdu");
421 static void do_standalone_mode(gboolean daemonize
)
425 struct addrinfo
*ai_head
;
430 ret
= usbip_names_init(USBIDS_FILE
);
434 ret
= usbip_stub_driver_open();
436 g_error("driver open failed");
440 g_error("daemonizing failed: %s", g_strerror(errno
));
442 usbip_use_syslog
= 1;
447 ai_head
= my_getaddrinfo(NULL
, PF_UNSPEC
);
451 n
= listen_all_addrinfo(ai_head
, lsock
);
453 g_error("no socket to listen to");
455 for (int i
= 0; i
< n
; i
++) {
458 gio
= g_io_channel_unix_new(lsock
[i
]);
459 g_io_add_watch(gio
, (G_IO_IN
| G_IO_ERR
| G_IO_HUP
| G_IO_NVAL
),
460 process_comming_request
, NULL
);
464 info("usbipd start (%s)", version
);
467 main_loop
= g_main_loop_new(FALSE
, FALSE
);
468 g_main_loop_run(main_loop
);
472 freeaddrinfo(ai_head
);
474 usbip_stub_driver_close();
480 static const char help_message
[] = "\
481 Usage: usbipd [options] \n\
483 Run as a daemon process. \n\
486 Print debugging information. \n\
492 Print this help. \n";
494 static void show_help(void)
496 printf("%s", help_message
);
499 static const struct option longopts
[] = {
500 {"daemon", no_argument
, NULL
, 'D'},
501 {"debug", no_argument
, NULL
, 'd'},
502 {"version", no_argument
, NULL
, 'v'},
503 {"help", no_argument
, NULL
, 'h'},
507 int main(int argc
, char *argv
[])
509 gboolean daemonize
= FALSE
;
512 cmd_standalone_mode
= 1,
515 } cmd
= cmd_standalone_mode
;
518 usbip_use_stderr
= 1;
519 usbip_use_syslog
= 0;
522 g_warning("running non-root?");
528 c
= getopt_long(argc
, argv
, "vhdD", longopts
, &index
);
555 case cmd_standalone_mode
:
556 do_standalone_mode(daemonize
);
559 printf("%s\n", version
);