3
* Copyright (C) 2005-2007 Takahiro Hirofuchi
6
#include "usbip_network.h"
8
void pack_uint32_t(int pack, uint32_t *num)
20
void pack_uint16_t(int pack, uint16_t *num)
32
void pack_usb_device(int pack, struct usb_device *udev)
34
pack_uint32_t(pack, &udev->busnum);
35
pack_uint32_t(pack, &udev->devnum);
36
pack_uint32_t(pack, &udev->speed );
38
pack_uint16_t(pack, &udev->idVendor );
39
pack_uint16_t(pack, &udev->idProduct);
40
pack_uint16_t(pack, &udev->bcdDevice);
43
void pack_usb_interface(int pack __attribute__((unused)),
44
struct usb_interface *udev __attribute__((unused)))
46
/* uint8_t members need nothing */
50
static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
61
nbytes = send(sockfd, buff, bufflen, 0);
63
nbytes = recv(sockfd, buff, bufflen, MSG_WAITALL);
68
buff = (void *) ((intptr_t) buff + nbytes);
72
} while (bufflen > 0);
78
ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen)
80
return usbip_xmit(sockfd, buff, bufflen, 0);
83
ssize_t usbip_send(int sockfd, void *buff, size_t bufflen)
85
return usbip_xmit(sockfd, buff, bufflen, 1);
88
int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)
91
struct op_common op_common;
93
bzero(&op_common, sizeof(op_common));
95
op_common.version = USBIP_VERSION;
96
op_common.code = code;
97
op_common.status = status;
99
PACK_OP_COMMON(1, &op_common);
101
ret = usbip_send(sockfd, (void *) &op_common, sizeof(op_common));
103
err("send op_common");
110
int usbip_recv_op_common(int sockfd, uint16_t *code)
113
struct op_common op_common;
115
bzero(&op_common, sizeof(op_common));
117
ret = usbip_recv(sockfd, (void *) &op_common, sizeof(op_common));
119
err("recv op_common, %d", ret);
123
PACK_OP_COMMON(0, &op_common);
125
if (op_common.version != USBIP_VERSION) {
126
err("version mismatch, %d %d", op_common.version, USBIP_VERSION);
134
if (op_common.code != *code) {
135
info("unexpected pdu %d for %d", op_common.code, *code);
140
if (op_common.status != ST_OK) {
141
info("request failed at peer, %d", op_common.status);
145
*code = op_common.code;
153
int usbip_set_reuseaddr(int sockfd)
158
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val));
160
err("setsockopt SO_REUSEADDR");
165
int usbip_set_nodelay(int sockfd)
170
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val));
172
err("setsockopt TCP_NODELAY");
177
int usbip_set_keepalive(int sockfd)
182
ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val));
184
err("setsockopt SO_KEEPALIVE");
191
* moved here from vhci_attach.c
193
int tcp_connect(char *hostname, char *service)
195
struct addrinfo hints, *res, *res0;
200
memset(&hints, 0, sizeof(hints));
201
hints.ai_socktype = SOCK_STREAM;
203
/* get all possible addresses */
204
err = getaddrinfo(hostname, service, &hints, &res0);
206
err("%s %s: %s", hostname, service, gai_strerror(err));
210
/* try all the addresses */
211
for (res = res0; res; res = res->ai_next) {
212
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
214
err = getnameinfo(res->ai_addr, res->ai_addrlen,
215
hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
217
err("%s %s: %s", hostname, service, gai_strerror(err));
221
dbg("trying %s port %s\n", hbuf, sbuf);
223
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
229
/* should set TCP_NODELAY for usbip */
230
usbip_set_nodelay(sockfd);
231
/* TODO: write code for heatbeat */
232
usbip_set_keepalive(sockfd);
234
err = connect(sockfd, res->ai_addr, res->ai_addrlen);
241
dbg("connected to %s:%s", hbuf, sbuf);
247
dbg("%s:%s, %s", hostname, service, "no destination to connect to");