-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathusbip_network.c
311 lines (245 loc) · 5.7 KB
/
usbip_network.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*
* $Id: usbip_network.c 40 2007-09-07 11:52:17Z hirofuchi $
*
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/
#define WINVER 0x0501
#include "usbip.h"
void pack_uint32_t(int pack, uint32_t *num)
{
uint32_t i;
if (pack)
i = htonl(*num);
else
i = ntohl(*num);
*num = i;
}
void pack_uint16_t(int pack, uint16_t *num)
{
uint16_t i;
if (pack)
i = htons(*num);
else
i = ntohs(*num);
*num = i;
}
void pack_usb_device(int pack, struct usb_device *udev)
{
pack_uint32_t(pack, &udev->busnum);
pack_uint32_t(pack, &udev->devnum);
pack_uint32_t(pack, &udev->speed );
pack_uint16_t(pack, &udev->idVendor );
pack_uint16_t(pack, &udev->idProduct);
pack_uint16_t(pack, &udev->bcdDevice);
}
void pack_usb_interface(int pack, struct usb_interface *udev)
{
/* uint8_t members need nothing */
}
void usbip_dump_buffer(unsigned char *buff, int bufflen)
{
int i,j;
char linebuf[80];
int pos=0;
for (i=0; i<bufflen; i+=16)
{
pos+=sprintf(linebuf+pos,"%8i: ",i);
for (j=i; j<i+16; j++)
{
if (j<bufflen)
pos+=sprintf(linebuf+pos,"%02X ",(int)(buff)[j]);
else
pos+=sprintf(linebuf+pos," ");
}
for (j=i; j<i+16; j++)
{
if (j<bufflen)
pos+=sprintf(linebuf+pos,"%c",(buff[j]>=32&&buff[j]<128)?((char*)buff)[j]:'.');
else
pos+=sprintf(linebuf+pos," ");
}
pos+=sprintf(linebuf+pos,"\n");
dbg_file("%s",linebuf);
// printk(KERN_DEBUG "%s",linebuf);
pos=0;
}
}
static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending)
{
ssize_t total = 0;
#ifdef DEBUG
void * orgbuf=buff;
#endif
if (!bufflen)
return 0;
// dbg_file("do %d: len:%d\n", sending, bufflen);
do {
ssize_t nbytes;
if (sending)
{
nbytes = send(sockfd, (char *)buff, bufflen, 0);
}
else
{
nbytes = recv(sockfd, (char *)buff, bufflen, 0);
dbg_file("Number of bytes received from socket synchronously: %d\n",nbytes);
}
if (nbytes <= 0)
return -1;
buff = (void *)((char *)buff + nbytes);
bufflen -= nbytes;
total += nbytes;
} while (bufflen > 0);
#ifdef DEBUG
usbip_dump_buffer(orgbuf,total);
#endif
// dbg_file("do %d: len:%d finish\n", sending, bufflen);
return total;
}
ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen)
{
return usbip_xmit(sockfd, buff, bufflen, 0);
}
ssize_t usbip_send(int sockfd, void *buff, size_t bufflen)
{
return usbip_xmit(sockfd, buff, bufflen, 1);
}
int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)
{
int ret;
struct op_common op_common;
memset(&op_common, 0, sizeof(op_common));
op_common.version = USBIP_VERSION;
op_common.code = code;
op_common.status = status;
PACK_OP_COMMON(1, &op_common);
ret = usbip_send(sockfd, (void *) &op_common, sizeof(op_common));
if (ret < 0) {
err("send op_common");
return -1;
}
return 0;
}
int usbip_recv_op_common(int sockfd, uint16_t *code)
{
int ret;
struct op_common op_common;
memset(&op_common, 0, sizeof(op_common));
ret = usbip_recv(sockfd, (void *) &op_common, sizeof(op_common));
if (ret < 0) {
err("recv op_common, %d", ret);
goto err;
}
PACK_OP_COMMON(0, &op_common);
if (op_common.version != USBIP_VERSION) {
err("version mismatch, %d %d", op_common.version, USBIP_VERSION);
goto err;
}
switch(*code) {
case OP_UNSPEC:
break;
default:
if (op_common.code != *code) {
info("unexpected pdu %d for %d", op_common.code, *code);
goto err;
}
}
if (op_common.status != ST_OK) {
info("request failed at peer, %d", op_common.status);
goto err;
}
*code = op_common.code;
return 0;
err:
return -1;
}
int usbip_set_reuseaddr(int sockfd)
{
const int val = 1;
int ret;
ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(const char *)&val, sizeof(val));
if (ret)
err("setsockopt SO_REUSEADDR");
return ret;
}
int usbip_set_nodelay(int sockfd)
{
const int val = 1;
int ret;
ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
(const char *)&val, sizeof(val));
if (ret)
err("setsockopt TCP_NODELAY");
return ret;
}
int usbip_set_keepalive(int sockfd)
{
const int val = 1;
int ret;
ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE,
(const char *)&val, sizeof(val));
if (ret)
err("setsockopt SO_KEEPALIVE");
return ret;
}
/* IPv6 Ready */
/*
* moved here from vhci_attach.c
*/
SOCKET tcp_connect(char *hostname, char *service)
{
struct addrinfo hints, *res, *res0;
SOCKET sockfd;
int ret;
memset(&hints, 0, sizeof(hints));
hints.ai_socktype = SOCK_STREAM;
/* get all possible addresses */
ret = getaddrinfo(hostname, service, &hints, &res0);
if (ret) {
err("%s %s: %s", hostname, service, gai_strerror(ret));
return INVALID_SOCKET;
}
/* try all the addresses */
for (res = res0; res; res = res->ai_next) {
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
ret = getnameinfo(res->ai_addr, res->ai_addrlen,
hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV);
if (ret) {
err("%s %s: %s", hostname, service, gai_strerror(ret));
continue;
}
dbg("trying %s port %s\n", hbuf, sbuf);
sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (INVALID_SOCKET == sockfd ) {
err("socket");
continue;
}
/* should set TCP_NODELAY for usbip */
usbip_set_nodelay(sockfd);
/* TODO: write code for heatbeat */
usbip_set_keepalive(sockfd);
ret = connect(sockfd, res->ai_addr, res->ai_addrlen);
if (ret) {
closesocket(sockfd);
continue;
}
/* connected */
dbg("connected to %s:%s", hbuf, sbuf);
freeaddrinfo(res0);
return sockfd;
}
dbg("%s:%s, %s", hostname, service, "no destination to connect to");
freeaddrinfo(res0);
return INVALID_SOCKET;
}
int init_winsock(void)
{
unsigned short version = 0x202; /* winsock 2.2 */
WSADATA data;
int ret;
ret = WSAStartup( version, &data);
if (ret)
return -1;
return 0;
}