3 * Copyright (C) 2005-2007 Takahiro Hirofuchi
6 #include <sys/socket.h>
12 #include <netinet/tcp.h>
15 #include "usbip_common.h"
16 #include "usbip_network.h"
18 void pack_uint32_t(int pack, uint32_t *num)
30 void pack_uint16_t(int pack, uint16_t *num)
42 void pack_usb_device(int pack, struct usb_device *udev)
44 pack_uint32_t(pack, &udev->busnum);
45 pack_uint32_t(pack, &udev->devnum);
46 pack_uint32_t(pack, &udev->speed );
48 pack_uint16_t(pack, &udev->idVendor );
49 pack_uint16_t(pack, &udev->idProduct);
50 pack_uint16_t(pack, &udev->bcdDevice);
53 void pack_usb_interface(int pack __attribute__((unused)),
54 struct usb_interface *udev __attribute__((unused)))
56 /* uint8_t members need nothing */
60 static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
71 nbytes = send(sockfd, buff, bufflen, 0);
73 nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL);
78 buff = (void *) ((intptr_t) buff + nbytes);
82 } while (bufflen > 0);
88 ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen)
90 return usbip_xmit(sockfd, buff, bufflen, 0);
93 ssize_t usbip_send(int sockfd, void *buff, size_t bufflen)
95 return usbip_xmit(sockfd, buff, bufflen, 1);
98 int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)
101 struct op_common op_common;
103 memset(&op_common, 0, sizeof(op_common));
105 op_common.version = USBIP_VERSION;
106 op_common.code = code;
107 op_common.status = status;
109 PACK_OP_COMMON(1, &op_common);
111 ret = usbip_send(sockfd, (void *) &op_common, sizeof(op_common));
113 err("send op_common");
120 int usbip_recv_op_common(int sockfd, uint16_t *code)
123 struct op_common op_common;
125 memset(&op_common, 0, sizeof(op_common));
127 ret = usbip_recv(sockfd, (void *) &op_common, sizeof(op_common));
129 err("recv op_common, %d", ret);
133 PACK_OP_COMMON(0, &op_common);
135 if (op_common.version != USBIP_VERSION) {
136 err("version mismatch, %d %d", op_common.version, USBIP_VERSION);
144 if (op_common.code != *code) {
145 info("unexpected pdu %d for %d", op_common.code, *code);
150 if (op_common.status != ST_OK) {
151 info("request failed at peer, %d", op_common.status);
155 *code = op_common.code;
163 int usbip_set_reuseaddr(int sockfd)
168 ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
170 err("setsockopt SO_REUSEADDR");
175 int usbip_set_nodelay(int sockfd)
180 ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
182 err("setsockopt TCP_NODELAY");
187 int usbip_set_keepalive(int sockfd)
192 ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
194 err("setsockopt SO_KEEPALIVE");
202 int usbip_net_tcp_connect(char *hostname, char *port)
204 struct addrinfo hints, *res, *rp;
208 memset(&hints, 0, sizeof(hints));
209 hints.ai_family = AF_UNSPEC;
210 hints.ai_socktype = SOCK_STREAM;
212 /* get all possible addresses */
213 ret = getaddrinfo(hostname, port, &hints, &res);
215 dbg("getaddrinfo: %s port %s: %s", hostname, port,
220 /* try the addresses */
221 for (rp = res; rp; rp = rp->ai_next) {
222 sockfd = socket(rp->ai_family, rp->ai_socktype,
227 /* should set TCP_NODELAY for usbip */
228 usbip_set_nodelay(sockfd);
229 /* TODO: write code for heartbeat */
230 usbip_set_keepalive(sockfd);
232 if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)