Skip to content

Commit

Permalink
userspace: usbip_network: rename and cleanup function
Browse files Browse the repository at this point in the history
Rename tcp_connection to usbip_net_tcp_connection, which alludes to a
usbip network library that will eventually follow. The implementation
of this function has also been cleaned up.

Headers had to be adjusted due to the elimination of the old usbip.h.

Signed-off-by: matt mooney <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
mfmooney authored and george-hopkins committed Sep 14, 2017
1 parent fc82dae commit 39b94c8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 78 deletions.
4 changes: 4 additions & 0 deletions userspace/src/usbip_list.c
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,11 @@ static int show_exported_devices(char *host)
return -1;
}

#ifdef __linux__
close(sockfd);
#else
closesocket(sockfd);
#endif
return 0;
}

Expand Down
100 changes: 48 additions & 52 deletions userspace/src/usbip_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,22 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

#ifndef __linux__
#include "usbip_common.h"

#include <string.h>

#ifdef __linux__
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <netinet/tcp.h>
#include <unistd.h>
#endif

#ifndef EAI_SYSTEM
#define EAI_SYSTEM -11
#endif

#include "usbip_common.h"
#include "usbip_network.h"

void pack_uint32_t(int pack, uint32_t *num)
Expand All @@ -32,7 +45,7 @@ void pack_uint16_t(int pack, uint16_t *num)
*num = i;
}

void pack_usb_device(int pack, struct usb_device *udev)
void pack_usb_device(int pack, struct usbip_usb_device *udev)
{
pack_uint32_t(pack, &udev->busnum);
pack_uint32_t(pack, &udev->devnum);
Expand All @@ -43,7 +56,7 @@ void pack_usb_device(int pack, struct usb_device *udev)
pack_uint16_t(pack, &udev->bcdDevice);
}

void pack_usb_interface(int pack, struct usb_interface *udev)
void pack_usb_interface(int pack, struct usbip_usb_interface *udev)
{
UNUSED(pack);
UNUSED(udev);
Expand Down Expand Up @@ -110,15 +123,15 @@ int usbip_send_op_common(int sockfd, uint32_t code, uint32_t status)

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");
err("usbip_send has failed");
return -1;
}

Expand All @@ -130,11 +143,11 @@ int usbip_recv_op_common(int sockfd, uint16_t *code)
int ret;
struct op_common op_common;

memset(&op_common, '\0', 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);
err("usbip_recv has failed ret=%d", ret);
goto err;
}

Expand Down Expand Up @@ -204,70 +217,53 @@ int usbip_set_keepalive(int sockfd)
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 *port)
{
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, port, &hints, &res);
if (ret < 0) {
dbg("getaddrinfo: %s port %s: %s", hostname, port,
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));
/* 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;
}

dbg("trying %s port %s\n", hbuf, sbuf);

sockfd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
if (sockfd < 0) {
err("socket");
continue;
}

/* should set TCP_NODELAY for usbip */
usbip_set_nodelay(sockfd);
/* TODO: write code for heatbeat */
/* TODO: write code for heartbeat */
usbip_set_keepalive(sockfd);

err = connect(sockfd, res->ai_addr, res->ai_addrlen);
if (err < 0) {
if (connect(sockfd, rp->ai_addr, rp->ai_addrlen) == 0)
break;

#ifdef __linux__
close(sockfd);
close(sockfd);
#else
closesocket(sockfd);
closesocket(sockfd);
#endif
continue;
}

/* connected */
dbg("connected to %s:%s", hbuf, sbuf);
freeaddrinfo(res0);
return 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;
}
37 changes: 13 additions & 24 deletions userspace/src/usbip_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
* Copyright (C) 2005-2007 Takahiro Hirofuchi
*/

#ifndef _USBIP_NETWORK_H
#define _USBIP_NETWORK_H
#ifndef __USBIP_NETWORK_H
#define __USBIP_NETWORK_H

#include "usbip.h"
#ifdef HAVE_CONFIG_H
#include "../config.h"
#endif

#ifdef __GNUC__
#define PACKED __attribute__((__packed__))
Expand All @@ -16,15 +18,15 @@

#ifdef __linux__
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/tcp.h>
#include <sysfs/libsysfs.h>
#else
#include "usbip_common.h"
#endif

#include <stdint.h>

/* -------------------------------------------------- */
/* Define Protocol Format */
/* -------------------------------------------------- */

#define USBIP_PORT 3240
#define USBIP_PORT_STRING "3240"

/* ---------------------------------------------------------------------- */
/* Common header for all the kinds of PDUs. */
Expand All @@ -48,7 +50,6 @@ struct op_common {
pack_uint32_t(pack, &(op_common)->status );\
} while (0)


/* ---------------------------------------------------------------------- */
/* Dummy Code */
#define OP_UNSPEC 0x00
Expand Down Expand Up @@ -93,8 +94,6 @@ struct op_import_reply {
pack_usb_device(pack, &(reply)->udev);\
} while (0)



/* ---------------------------------------------------------------------- */
/* Export a USB device to a remote host. */
#define OP_EXPORT 0x06
Expand Down Expand Up @@ -138,8 +137,6 @@ struct op_unexport_reply {
#define PACK_OP_UNEXPORT_REPLY(pack, reply) do {\
} while (0)



/* ---------------------------------------------------------------------- */
/* Negotiate IPSec encryption key. (still not used) */
#define OP_CRYPKEY 0x04
Expand Down Expand Up @@ -184,11 +181,6 @@ struct op_devlist_reply_extra {
pack_uint32_t(pack, &(reply)->ndev);\
} while (0)


/* -------------------------------------------------- */
/* Declare Prototype Function */
/* -------------------------------------------------- */

void pack_uint32_t(int pack, uint32_t *num);
void pack_uint16_t(int pack, uint16_t *num);
void pack_usb_device(int pack, struct usb_device *udev);
Expand All @@ -202,14 +194,11 @@ int usbip_set_reuseaddr(int sockfd);
int usbip_set_nodelay(int sockfd);
int usbip_set_keepalive(int sockfd);

int tcp_connect(char *hostname, char *service);

#define USBIP_PORT 3240
#define USBIP_PORT_STRING "3240"
int usbip_net_tcp_connect(char *hostname, char *port);

#ifdef __GNUC__
#else
#pragma pack(pop)
#endif

#endif
#endif /* __USBIP_NETWORK_H */
4 changes: 2 additions & 2 deletions userspace/src/usbip_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,7 @@ int attach_device(char * host, char * busid)
HANDLE devfd=INVALID_HANDLE_VALUE;
struct usb_interface uinf;

sockfd = tcp_connect(host, USBIP_PORT_STRING);
sockfd = usbip_net_tcp_connect(host, USBIP_PORT_STRING);
if (INVALID_SOCKET == sockfd) {
err("tcp connect");
return 0;
Expand All @@ -895,7 +895,7 @@ int attach_device(char * host, char * busid)
return 0;
}
closesocket(sockfd);
sockfd = tcp_connect(host, USBIP_PORT_STRING);
sockfd = usbip_net_tcp_connect(host, USBIP_PORT_STRING);
if (INVALID_SOCKET == sockfd) {
err("tcp connect");
return 0;
Expand Down

0 comments on commit 39b94c8

Please sign in to comment.