diff options
Diffstat (limited to 'drivers/staging/usbip/userspace/src/usbip_network.c')
-rw-r--r-- | drivers/staging/usbip/userspace/src/usbip_network.c | 198 |
1 files changed, 102 insertions, 96 deletions
diff --git a/drivers/staging/usbip/userspace/src/usbip_network.c b/drivers/staging/usbip/userspace/src/usbip_network.c index 01be3c7211d..1a84dd37e12 100644 --- a/drivers/staging/usbip/userspace/src/usbip_network.c +++ b/drivers/staging/usbip/userspace/src/usbip_network.c @@ -1,11 +1,34 @@ /* + * Copyright (C) 2011 matt mooney <mfm@muteddisk.com> + * 2005-2007 Takahiro Hirofuchi * - * Copyright (C) 2005-2007 Takahiro Hirofuchi + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. */ +#include <sys/socket.h> + +#include <string.h> + +#include <arpa/inet.h> +#include <netdb.h> +#include <netinet/tcp.h> +#include <unistd.h> + +#include "usbip_common.h" #include "usbip_network.h" -void pack_uint32_t(int pack, uint32_t *num) +void usbip_net_pack_uint32_t(int pack, uint32_t *num) { uint32_t i; @@ -17,7 +40,7 @@ void pack_uint32_t(int pack, uint32_t *num) *num = i; } -void pack_uint16_t(int pack, uint16_t *num) +void usbip_net_pack_uint16_t(int pack, uint16_t *num) { uint16_t i; @@ -29,34 +52,34 @@ void pack_uint16_t(int pack, uint16_t *num) *num = i; } -void pack_usb_device(int pack, struct usb_device *udev) +void usbip_net_pack_usb_device(int pack, struct usbip_usb_device *udev) { - pack_uint32_t(pack, &udev->busnum); - pack_uint32_t(pack, &udev->devnum); - pack_uint32_t(pack, &udev->speed ); + usbip_net_pack_uint32_t(pack, &udev->busnum); + usbip_net_pack_uint32_t(pack, &udev->devnum); + usbip_net_pack_uint32_t(pack, &udev->speed ); - pack_uint16_t(pack, &udev->idVendor ); - pack_uint16_t(pack, &udev->idProduct); - pack_uint16_t(pack, &udev->bcdDevice); + usbip_net_pack_uint16_t(pack, &udev->idVendor); + usbip_net_pack_uint16_t(pack, &udev->idProduct); + usbip_net_pack_uint16_t(pack, &udev->bcdDevice); } -void pack_usb_interface(int pack __attribute__((unused)), - struct usb_interface *udev __attribute__((unused))) +void usbip_net_pack_usb_interface(int pack __attribute__((unused)), + struct usbip_usb_interface *udev + __attribute__((unused))) { /* uint8_t members need nothing */ } - -static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending) +static ssize_t usbip_net_xmit(int sockfd, void *buff, size_t bufflen, + int sending) { + ssize_t nbytes; ssize_t total = 0; if (!bufflen) return 0; do { - ssize_t nbytes; - if (sending) nbytes = send(sockfd, buff, bufflen, 0); else @@ -65,80 +88,81 @@ static ssize_t usbip_xmit(int sockfd, void *buff, size_t bufflen, int sending) if (nbytes <= 0) return -1; - buff = (void *) ((intptr_t) buff + nbytes); + buff = (void *)((intptr_t) buff + nbytes); bufflen -= nbytes; total += nbytes; } while (bufflen > 0); - return total; } -ssize_t usbip_recv(int sockfd, void *buff, size_t bufflen) +ssize_t usbip_net_recv(int sockfd, void *buff, size_t bufflen) { - return usbip_xmit(sockfd, buff, bufflen, 0); + return usbip_net_xmit(sockfd, buff, bufflen, 0); } -ssize_t usbip_send(int sockfd, void *buff, size_t bufflen) +ssize_t usbip_net_send(int sockfd, void *buff, size_t bufflen) { - return usbip_xmit(sockfd, buff, bufflen, 1); + return usbip_net_xmit(sockfd, buff, bufflen, 1); } -int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status) +int usbip_net_send_op_common(int sockfd, uint32_t code, uint32_t status) { - int ret; struct op_common op_common; + int rc; - bzero(&op_common, sizeof(op_common)); + memset(&op_common, 0, sizeof(op_common)); - op_common.version = USBIP_VERSION; - op_common.code = code; - op_common.status = status; + 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"); + rc = usbip_net_send(sockfd, &op_common, sizeof(op_common)); + if (rc < 0) { + dbg("usbip_net_send failed: %d", rc); return -1; } return 0; } -int usbip_recv_op_common(int sockfd, uint16_t *code) +int usbip_net_recv_op_common(int sockfd, uint16_t *code) { - int ret; struct op_common op_common; + int rc; - bzero(&op_common, sizeof(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); + rc = usbip_net_recv(sockfd, &op_common, sizeof(op_common)); + if (rc < 0) { + dbg("usbip_net_recv failed: %d", rc); goto err; } PACK_OP_COMMON(0, &op_common); if (op_common.version != USBIP_VERSION) { - err("version mismatch, %d %d", op_common.version, USBIP_VERSION); + dbg("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; - } + switch (*code) { + case OP_UNSPEC: + break; + default: + if (op_common.code != *code) { + dbg("unexpected pdu %#0x for %#0x", op_common.code, + *code); + goto err; + } } if (op_common.status != ST_OK) { - info("request failed at peer, %d", op_common.status); + dbg("request failed at peer: %d", op_common.status); goto err; } @@ -149,103 +173,85 @@ err: return -1; } - -int usbip_set_reuseaddr(int sockfd) +int usbip_net_set_reuseaddr(int sockfd) { const int val = 1; int ret; ret = setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val)); if (ret < 0) - err("setsockopt SO_REUSEADDR"); + dbg("setsockopt: SO_REUSEADDR"); return ret; } -int usbip_set_nodelay(int sockfd) +int usbip_net_set_nodelay(int sockfd) { const int val = 1; int ret; ret = setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof(val)); if (ret < 0) - err("setsockopt TCP_NODELAY"); + dbg("setsockopt: TCP_NODELAY"); return ret; } -int usbip_set_keepalive(int sockfd) +int usbip_net_set_keepalive(int sockfd) { const int val = 1; int ret; ret = setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, &val, sizeof(val)); if (ret < 0) - err("setsockopt SO_KEEPALIVE"); + dbg("setsockopt: SO_KEEPALIVE"); return ret; } -/* IPv6 Ready */ /* - * moved here from vhci_attach.c + * IPv6 Ready */ -int tcp_connect(char *hostname, char *service) +int usbip_net_tcp_connect(char *hostname, char *service) { - struct addrinfo hints, *res, *res0; + struct addrinfo hints, *res, *rp; int sockfd; - int err; - + int ret; memset(&hints, 0, sizeof(hints)); + hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; /* get all possible addresses */ - err = getaddrinfo(hostname, service, &hints, &res0); - if (err) { - err("%s %s: %s", hostname, service, gai_strerror(err)); - return -1; + ret = getaddrinfo(hostname, service, &hints, &res); + if (ret < 0) { + dbg("getaddrinfo: %s service %s: %s", hostname, service, + gai_strerror(ret)); + return ret; } - /* try all the addresses */ - for (res = res0; res; res = res->ai_next) { - char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV]; - - err = getnameinfo(res->ai_addr, res->ai_addrlen, - hbuf, sizeof(hbuf), sbuf, sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV); - if (err) { - err("%s %s: %s", hostname, service, gai_strerror(err)); - continue; - } - - dbg("trying %s port %s\n", hbuf, sbuf); - - sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol); - if (sockfd < 0) { - err("socket"); + /* try the addresses */ + for (rp = res; rp; rp = rp->ai_next) { + sockfd = socket(rp->ai_family, rp->ai_socktype, + rp->ai_protocol); + if (sockfd < 0) continue; - } /* should set TCP_NODELAY for usbip */ - usbip_set_nodelay(sockfd); - /* TODO: write code for heatbeat */ - usbip_set_keepalive(sockfd); + usbip_net_set_nodelay(sockfd); + /* TODO: write code for heartbeat */ + usbip_net_set_keepalive(sockfd); - err = connect(sockfd, res->ai_addr, res->ai_addrlen); - if (err < 0) { - close(sockfd); - continue; - } + if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0) + break; - /* connected */ - dbg("connected to %s:%s", hbuf, sbuf); - freeaddrinfo(res0); - return sockfd; + close(sockfd); } + if (!rp) + return EAI_SYSTEM; - dbg("%s:%s, %s", hostname, service, "no destination to connect to"); - freeaddrinfo(res0); + freeaddrinfo(res); - return -1; + return sockfd; } |