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>
28 #include "usbip_common.h"
29 #include "usbip_network.h"
31 void pack_uint32_t(int pack
, uint32_t *num
)
43 void pack_uint16_t(int pack
, uint16_t *num
)
55 void pack_usb_device(int pack
, struct usbip_usb_device
*udev
)
57 pack_uint32_t(pack
, &udev
->busnum
);
58 pack_uint32_t(pack
, &udev
->devnum
);
59 pack_uint32_t(pack
, &udev
->speed
);
61 pack_uint16_t(pack
, &udev
->idVendor
);
62 pack_uint16_t(pack
, &udev
->idProduct
);
63 pack_uint16_t(pack
, &udev
->bcdDevice
);
66 void pack_usb_interface(int pack
__attribute__((unused
)),
67 struct usbip_usb_interface
*udev
__attribute__((unused
)))
69 /* uint8_t members need nothing */
72 static ssize_t
usbip_xmit(int sockfd
, void *buff
, size_t bufflen
, int sending
)
82 nbytes
= send(sockfd
, buff
, bufflen
, 0);
84 nbytes
= recv(sockfd
, buff
, bufflen
, MSG_WAITALL
);
89 buff
= (void *)((intptr_t) buff
+ nbytes
);
93 } while (bufflen
> 0);
98 ssize_t
usbip_recv(int sockfd
, void *buff
, size_t bufflen
)
100 return usbip_xmit(sockfd
, buff
, bufflen
, 0);
103 ssize_t
usbip_send(int sockfd
, void *buff
, size_t bufflen
)
105 return usbip_xmit(sockfd
, buff
, bufflen
, 1);
108 int usbip_send_op_common(int sockfd
, uint32_t code
, uint32_t status
)
110 struct op_common op_common
;
113 memset(&op_common
, 0, sizeof(op_common
));
115 op_common
.version
= USBIP_VERSION
;
116 op_common
.code
= code
;
117 op_common
.status
= status
;
119 PACK_OP_COMMON(1, &op_common
);
121 rc
= usbip_send(sockfd
, &op_common
, sizeof(op_common
));
123 dbg("usbip_send failed: %d", rc
);
130 int usbip_recv_op_common(int sockfd
, uint16_t *code
)
132 struct op_common op_common
;
135 memset(&op_common
, 0, sizeof(op_common
));
137 rc
= usbip_recv(sockfd
, &op_common
, sizeof(op_common
));
139 dbg("usbip_recv failed: %d", rc
);
143 PACK_OP_COMMON(0, &op_common
);
145 if (op_common
.version
!= USBIP_VERSION
) {
146 dbg("version mismatch: %d %d", op_common
.version
,
155 if (op_common
.code
!= *code
) {
156 dbg("unexpected pdu %#0x for %#0x", op_common
.code
,
162 if (op_common
.status
!= ST_OK
) {
163 dbg("request failed at peer: %d", op_common
.status
);
167 *code
= op_common
.code
;
174 int usbip_set_reuseaddr(int sockfd
)
179 ret
= setsockopt(sockfd
, SOL_SOCKET
, SO_REUSEADDR
, &val
, sizeof(val
));
181 dbg("setsockopt: SO_REUSEADDR");
186 int usbip_set_nodelay(int sockfd
)
191 ret
= setsockopt(sockfd
, IPPROTO_TCP
, TCP_NODELAY
, &val
, sizeof(val
));
193 dbg("setsockopt: TCP_NODELAY");
198 int usbip_set_keepalive(int sockfd
)
203 ret
= setsockopt(sockfd
, SOL_SOCKET
, SO_KEEPALIVE
, &val
, sizeof(val
));
205 dbg("setsockopt: SO_KEEPALIVE");
213 int usbip_net_tcp_connect(char *hostname
, char *service
)
215 struct addrinfo hints
, *res
, *rp
;
219 memset(&hints
, 0, sizeof(hints
));
220 hints
.ai_family
= AF_UNSPEC
;
221 hints
.ai_socktype
= SOCK_STREAM
;
223 /* get all possible addresses */
224 ret
= getaddrinfo(hostname
, service
, &hints
, &res
);
226 dbg("getaddrinfo: %s service %s: %s", hostname
, service
,
231 /* try the addresses */
232 for (rp
= res
; rp
; rp
= rp
->ai_next
) {
233 sockfd
= socket(rp
->ai_family
, rp
->ai_socktype
,
238 /* should set TCP_NODELAY for usbip */
239 usbip_set_nodelay(sockfd
);
240 /* TODO: write code for heartbeat */
241 usbip_set_keepalive(sockfd
);
243 if (connect(sockfd
, rp
->ai_addr
, rp
->ai_addrlen
) == 0)