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/>.
19 #include <sys/socket.h>
23 #include <arpa/inet.h>
25 #include <netinet/tcp.h>
32 #include "usbip_common.h"
33 #include "usbip_network.h"
35 int usbip_port = 3240;
36 char *usbip_port_string = "3240";
38 void usbip_setup_port_number(char *arg)
40 dbg("parsing port arg '%s'", arg);
42 unsigned long int port = strtoul(arg, &end, 10);
45 err("port: could not parse '%s' as a decimal integer", arg);
50 err("port: garbage at end of '%s'", arg);
54 if (port > UINT16_MAX) {
55 err("port: %s too high (max=%d)",
61 usbip_port_string = arg;
62 info("using port %d (\"%s\")", usbip_port, usbip_port_string);
65 uint32_t usbip_net_pack_uint32_t(int pack, uint32_t num)
77 uint16_t usbip_net_pack_uint16_t(int pack, uint16_t num)
89 void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev)
91 udev->busnum = usbip_net_pack_uint32_t(pack, udev->busnum);
92 udev->devnum = usbip_net_pack_uint32_t(pack, udev->devnum);
93 udev->speed = usbip_net_pack_uint32_t(pack, udev->speed);
95 udev->idVendor = usbip_net_pack_uint16_t(pack, udev->idVendor);
96 udev->idProduct = usbip_net_pack_uint16_t(pack, udev->idProduct);
97 udev->bcdDevice = usbip_net_pack_uint16_t(pack, udev->bcdDevice);
100 void usbip_net_pack_usb_interface(int pack __attribute__((unused)),
101 struct usbip_usb_interface *udev
102 __attribute__((unused)))
104 /* uint8_t members need nothing */
107 static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen,
118 nbytes = send(sockfd, buff, bufflen, 0);
120 nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL);
125 buff = (void *)((intptr_t) buff + nbytes);
129 } while (bufflen > 0);
134 ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen)
136 return usbip_net_xmit(sockfd, buff, bufflen, 0);
139 ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen)
141 return usbip_net_xmit(sockfd, buff, bufflen, 1);
144 static inline void usbip_net_pack_op_common(int pack,
145 struct op_common *op_common)
147 op_common->version = usbip_net_pack_uint16_t(pack, op_common->version);
148 op_common->code = usbip_net_pack_uint16_t(pack, op_common->code);
149 op_common->status = usbip_net_pack_uint32_t(pack, op_common->status);
152 int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status)
154 struct op_common op_common;
157 memset(&op_common, 0, sizeof(op_common));
159 op_common.version = USBIP_VERSION;
160 op_common.code = code;
161 op_common.status = status;
163 usbip_net_pack_op_common(1, &op_common);
165 rc = usbip_net_send(sockfd, &op_common, sizeof(op_common));
167 dbg("usbip_net_send failed: %d", rc);
174 int usbip_net_recv_op_common(int sockfd, uint16_t *code)
176 struct op_common op_common;
179 memset(&op_common, 0, sizeof(op_common));
181 rc = usbip_net_recv(sockfd, &op_common, sizeof(op_common));
183 dbg("usbip_net_recv failed: %d", rc);
187 usbip_net_pack_op_common(0, &op_common);
189 if (op_common.version != USBIP_VERSION) {
190 dbg("version mismatch: %d %d", op_common.version,
199 if (op_common.code != *code) {
200 dbg("unexpected pdu %#0x for %#0x", op_common.code,
206 if (op_common.status != ST_OK) {
207 dbg("request failed at peer: %d", op_common.status);
211 *code = op_common.code;
218 int usbip_net_set_reuseaddr(int sockfd)
223 ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
225 dbg("setsockopt: SO_REUSEADDR");
230 int usbip_net_set_nodelay(int sockfd)
235 ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
237 dbg("setsockopt: TCP_NODELAY");
242 int usbip_net_set_keepalive(int sockfd)
247 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
249 dbg("setsockopt: SO_KEEPALIVE");
254 int usbip_net_set_v6only(int sockfd)
259 ret = setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &val, sizeof(val));
261 dbg("setsockopt: IPV6_V6ONLY");
269 int usbip_net_tcp_connect(char *hostname, char *service)
271 struct addrinfo hints, *res, *rp;
275 memset(&hints, 0, sizeof(hints));
276 hints.ai_family = AF_UNSPEC;
277 hints.ai_socktype = SOCK_STREAM;
279 /* get all possible addresses */
280 ret = getaddrinfo(hostname, service, &hints, &res);
282 dbg("getaddrinfo: %s service %s: %s", hostname, service,
287 /* try the addresses */
288 for (rp = res; rp; rp = rp->ai_next) {
289 sockfd = socket(rp->ai_family, rp->ai_socktype,
294 /* should set TCP_NODELAY for usbip */
295 usbip_net_set_nodelay(sockfd);
296 /* TODO: write code for heartbeat */
297 usbip_net_set_keepalive(sockfd);
299 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)