Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 1 | /* |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 2 | * Copyright (C) 2011 matt mooney <mfm@muteddisk.com> |
| 3 | * 2005-2007 Takahiro Hirofuchi |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 4 | * |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 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. |
| 9 | * |
| 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. |
| 14 | * |
| 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/>. |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 17 | */ |
| 18 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 19 | #include <sys/socket.h> |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 20 | |
| 21 | #include <string.h> |
| 22 | |
matt mooney | 35dd0c2 | 2011-05-27 01:44:14 -0700 | [diff] [blame] | 23 | #include <arpa/inet.h> |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 24 | #include <netdb.h> |
| 25 | #include <netinet/tcp.h> |
| 26 | #include <unistd.h> |
| 27 | |
| 28 | #include "usbip_common.h" |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 29 | #include "usbip_network.h" |
| 30 | |
| 31 | void pack_uint32_t(int pack, uint32_t *num) |
| 32 | { |
| 33 | uint32_t i; |
| 34 | |
| 35 | if (pack) |
| 36 | i = htonl(*num); |
| 37 | else |
| 38 | i = ntohl(*num); |
| 39 | |
| 40 | *num = i; |
| 41 | } |
| 42 | |
| 43 | void pack_uint16_t(int pack, uint16_t *num) |
| 44 | { |
| 45 | uint16_t i; |
| 46 | |
| 47 | if (pack) |
| 48 | i = htons(*num); |
| 49 | else |
| 50 | i = ntohs(*num); |
| 51 | |
| 52 | *num = i; |
| 53 | } |
| 54 | |
matt mooney | 35dd0c2 | 2011-05-27 01:44:14 -0700 | [diff] [blame] | 55 | void pack_usb_device(int pack, struct usbip_usb_device *udev) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 56 | { |
| 57 | pack_uint32_t(pack, &udev->busnum); |
| 58 | pack_uint32_t(pack, &udev->devnum); |
| 59 | pack_uint32_t(pack, &udev->speed ); |
| 60 | |
| 61 | pack_uint16_t(pack, &udev->idVendor ); |
| 62 | pack_uint16_t(pack, &udev->idProduct); |
| 63 | pack_uint16_t(pack, &udev->bcdDevice); |
| 64 | } |
| 65 | |
matt mooney | 968c659 | 2011-05-14 03:55:17 -0700 | [diff] [blame] | 66 | void pack_usb_interface(int pack __attribute__((unused)), |
matt mooney | 35dd0c2 | 2011-05-27 01:44:14 -0700 | [diff] [blame] | 67 | struct usbip_usb_interface *udev __attribute__((unused))) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 68 | { |
| 69 | /* uint8_t members need nothing */ |
| 70 | } |
| 71 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 72 | static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending) |
| 73 | { |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 74 | ssize_t nbytes; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 75 | ssize_t total = 0; |
| 76 | |
| 77 | if (!bufflen) |
| 78 | return 0; |
| 79 | |
| 80 | do { |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 81 | if (sending) |
| 82 | nbytes = send(sockfd, buff, bufflen, 0); |
| 83 | else |
| 84 | nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL); |
| 85 | |
| 86 | if (nbytes <= 0) |
| 87 | return -1; |
| 88 | |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 89 | buff = (void *)((intptr_t) buff + nbytes); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 90 | bufflen -= nbytes; |
| 91 | total += nbytes; |
| 92 | |
| 93 | } while (bufflen > 0); |
| 94 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 95 | return total; |
| 96 | } |
| 97 | |
| 98 | ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen) |
| 99 | { |
| 100 | return usbip_xmit(sockfd, buff, bufflen, 0); |
| 101 | } |
| 102 | |
| 103 | ssize_t usbip_send(int sockfd, void *buff, size_t bufflen) |
| 104 | { |
| 105 | return usbip_xmit(sockfd, buff, bufflen, 1); |
| 106 | } |
| 107 | |
| 108 | int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status) |
| 109 | { |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 110 | struct op_common op_common; |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 111 | int rc; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 112 | |
matt mooney | 950a4cd | 2011-05-27 01:44:10 -0700 | [diff] [blame] | 113 | memset(&op_common, 0, sizeof(op_common)); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 114 | |
matt mooney | 35dd0c2 | 2011-05-27 01:44:14 -0700 | [diff] [blame] | 115 | op_common.version = USBIP_VERSION; |
| 116 | op_common.code = code; |
| 117 | op_common.status = status; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 118 | |
| 119 | PACK_OP_COMMON(1, &op_common); |
| 120 | |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 121 | rc = usbip_send(sockfd, &op_common, sizeof(op_common)); |
| 122 | if (rc < 0) { |
| 123 | dbg("usbip_send failed: %d", rc); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 124 | return -1; |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | int usbip_recv_op_common(int sockfd, uint16_t *code) |
| 131 | { |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 132 | struct op_common op_common; |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 133 | int rc; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 134 | |
matt mooney | 950a4cd | 2011-05-27 01:44:10 -0700 | [diff] [blame] | 135 | memset(&op_common, 0, sizeof(op_common)); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 136 | |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 137 | rc = usbip_recv(sockfd, &op_common, sizeof(op_common)); |
| 138 | if (rc < 0) { |
| 139 | dbg("usbip_recv failed: %d", rc); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 140 | goto err; |
| 141 | } |
| 142 | |
| 143 | PACK_OP_COMMON(0, &op_common); |
| 144 | |
| 145 | if (op_common.version != USBIP_VERSION) { |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 146 | dbg("version mismatch: %d %d", op_common.version, |
| 147 | USBIP_VERSION); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 148 | goto err; |
| 149 | } |
| 150 | |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 151 | switch (*code) { |
| 152 | case OP_UNSPEC: |
| 153 | break; |
| 154 | default: |
| 155 | if (op_common.code != *code) { |
| 156 | dbg("unexpected pdu %#0x for %#0x", op_common.code, |
| 157 | *code); |
| 158 | goto err; |
| 159 | } |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 160 | } |
| 161 | |
| 162 | if (op_common.status != ST_OK) { |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 163 | dbg("request failed at peer: %d", op_common.status); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 164 | goto err; |
| 165 | } |
| 166 | |
| 167 | *code = op_common.code; |
| 168 | |
| 169 | return 0; |
| 170 | err: |
| 171 | return -1; |
| 172 | } |
| 173 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 174 | int usbip_set_reuseaddr(int sockfd) |
| 175 | { |
| 176 | const int val = 1; |
| 177 | int ret; |
| 178 | |
| 179 | ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); |
| 180 | if (ret < 0) |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 181 | dbg("setsockopt: SO_REUSEADDR"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 182 | |
| 183 | return ret; |
| 184 | } |
| 185 | |
| 186 | int usbip_set_nodelay(int sockfd) |
| 187 | { |
| 188 | const int val = 1; |
| 189 | int ret; |
| 190 | |
| 191 | ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); |
| 192 | if (ret < 0) |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 193 | dbg("setsockopt: TCP_NODELAY"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 194 | |
| 195 | return ret; |
| 196 | } |
| 197 | |
| 198 | int usbip_set_keepalive(int sockfd) |
| 199 | { |
| 200 | const int val = 1; |
| 201 | int ret; |
| 202 | |
| 203 | ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)); |
| 204 | if (ret < 0) |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 205 | dbg("setsockopt: SO_KEEPALIVE"); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 210 | /* |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 211 | * IPv6 Ready |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 212 | */ |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 213 | int usbip_net_tcp_connect(char *hostname, char *service) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 214 | { |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 215 | struct addrinfo hints, *res, *rp; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 216 | int sockfd; |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 217 | int ret; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 218 | |
| 219 | memset(&hints, 0, sizeof(hints)); |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 220 | hints.ai_family = AF_UNSPEC; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 221 | hints.ai_socktype = SOCK_STREAM; |
| 222 | |
| 223 | /* get all possible addresses */ |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 224 | ret = getaddrinfo(hostname, service, &hints, &res); |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 225 | if (ret < 0) { |
matt mooney | fb5ca81 | 2011-06-19 22:44:49 -0700 | [diff] [blame^] | 226 | dbg("getaddrinfo: %s service %s: %s", hostname, service, |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 227 | gai_strerror(ret)); |
| 228 | return ret; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 229 | } |
| 230 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 231 | /* try the addresses */ |
| 232 | for (rp = res; rp; rp = rp->ai_next) { |
| 233 | sockfd = socket(rp->ai_family, rp->ai_socktype, |
| 234 | rp->ai_protocol); |
| 235 | if (sockfd < 0) |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 236 | continue; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 237 | |
| 238 | /* should set TCP_NODELAY for usbip */ |
| 239 | usbip_set_nodelay(sockfd); |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 240 | /* TODO: write code for heartbeat */ |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 241 | usbip_set_keepalive(sockfd); |
| 242 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 243 | if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0) |
| 244 | break; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 245 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 246 | close(sockfd); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 247 | } |
| 248 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 249 | if (!rp) |
| 250 | return EAI_SYSTEM; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 251 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 252 | freeaddrinfo(res); |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 253 | |
matt mooney | 1e35d87 | 2011-05-26 06:17:13 -0700 | [diff] [blame] | 254 | return sockfd; |
Takahiro Hirofuchi | 0945b4f | 2011-05-14 03:55:07 -0700 | [diff] [blame] | 255 | } |