1 #include <arpa/inet.h>
2 #include <errno.h>
3 #include <fcntl.h>
4 #include <log/log.h>
5 #include <netinet/in.h>
6 #include <stdio.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <sys/socket.h>
10 #include <unistd.h>
11
12 #include "UsbIpUtils.h"
13
main(int argc,char * argv[])14 int main(int argc, char *argv[]) {
15 if (argc != 4) {
16 ALOGE("usage: 'usbip_service <ip_addr> <port> <dev_id>'\n");
17 return -1;
18 }
19 ALOGD("IP: %s\nPort: %s\nDevId: %s\n", argv[1], argv[2], argv[3]);
20
21 usbip_conn_info info;
22 if (!get_usbip_connection(argv[1], argv[2], argv[3], &info)) {
23 ALOGE("Couldn't retrieve socket connection\n");
24 return -1;
25 }
26
27 // Get free port.
28 FILE *file = fopen("/sys/devices/platform/vhci_hcd.0/status", "r");
29 if (file == NULL) {
30 ALOGE("Couldn't open sysfs status file: %s\n", strerror(errno));
31 close(info.sock_fd);
32 return -1;
33 }
34 int port_num = get_free_vhci_port(file, info.speed);
35
36 // Pass socket to the kernel driver.
37 int fd =
38 openat(AT_FDCWD, "/sys/devices/platform/vhci_hcd.0/attach", O_WRONLY);
39 if (fd == -1) {
40 ALOGE("Couldn't open sysfs attach file: %s\n", strerror(errno));
41 close(info.sock_fd);
42 return -1;
43 }
44 if (dprintf(fd, "%d %d %d %d", port_num, info.sock_fd, info.dev_id,
45 info.speed) < 0) {
46 ALOGE("Failed to attach socket to VHCI.\n");
47 close(info.sock_fd);
48 close(fd);
49 return -1;
50 }
51
52 close(info.sock_fd);
53 close(fd);
54 return 0;
55 }
56