Fix usbredirserver not listening for ipv6 connections (rhbz#957470)

- Fix a few (harmless) coverity warnings
This commit is contained in:
Hans de Goede 2013-05-13 11:35:27 +02:00
parent d5086897c8
commit c72c8b5d05
4 changed files with 211 additions and 2 deletions

View File

@ -0,0 +1,88 @@
From c7e8bfe7f88ea11b31ebe115a629fb1cc903f7bc Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Tue, 7 May 2013 15:29:09 +0200
Subject: [PATCH 1/3] usbredirserver: Allow connections from both ipv6 and ipv4
The while loop over the getaddrinfo result would bind to the ipv4 addr and
then stop, causing usbredirserver to not accept connections on ipv6.
Instead bind explicitly to ipv6 with in6addr_any, which accepts connections
from both.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
usbredirserver/usbredirserver.c | 42 +++++++++++++++++------------------------
1 file changed, 17 insertions(+), 25 deletions(-)
diff --git a/usbredirserver/usbredirserver.c b/usbredirserver/usbredirserver.c
index 7e063a8..c45a27c 100644
--- a/usbredirserver/usbredirserver.c
+++ b/usbredirserver/usbredirserver.c
@@ -196,9 +196,9 @@ int main(int argc, char *argv[])
int usbaddr = -1;
int usbvendor = -1;
int usbproduct = -1;
- struct addrinfo *r, *res, hints;
+ int on = 1;
+ struct sockaddr_in6 serveraddr;
struct sigaction act;
- char port_str[16];
libusb_device_handle *handle = NULL;
while ((o = getopt_long(argc, argv, "hp:v:", longopts, NULL)) != -1) {
@@ -271,37 +271,29 @@ int main(int argc, char *argv[])
libusb_set_debug(ctx, verbose);
- memset(&hints, 0, sizeof(hints));
- hints.ai_flags = AI_ADDRCONFIG | AI_NUMERICSERV | AI_PASSIVE;
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_protocol = IPPROTO_TCP;
-
- sprintf(port_str, "%d", port);
- if (getaddrinfo(NULL, port_str, &hints, &res) != 0) {
- perror("getaddrinfo");
+ server_fd = socket(AF_INET6, SOCK_STREAM, 0);
+ if (server_fd == -1) {
+ perror("Error creating ipv6 socket");
exit(1);
}
- for (r = res; r != NULL; r = r->ai_next) {
- server_fd = socket(r->ai_family, r->ai_socktype, r->ai_protocol);
- if (server_fd == -1)
- continue;
-
- if (bind(server_fd, r->ai_addr, r->ai_addrlen) == 0)
- break;
-
- close(server_fd);
+ if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) {
+ perror("Error setsockopt(SO_REUSEADDR) failed");
+ exit(1);
}
- freeaddrinfo(res);
-
- if (r == NULL) {
- fprintf(stderr, "Could not bind to port: %s\n", port_str);
+
+ memset(&serveraddr, 0, sizeof(serveraddr));
+ serveraddr.sin6_family = AF_INET6;
+ serveraddr.sin6_port = htons(port);
+ serveraddr.sin6_addr = in6addr_any;
+
+ if (bind(server_fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr))) {
+ fprintf(stderr, "Error binding port %d: %s\n", port, strerror(errno));
exit(1);
}
if (listen(server_fd, 1)) {
- perror("listen");
+ perror("Error listening");
exit(1);
}
--
1.8.2.1

View File

@ -0,0 +1,78 @@
From 65b99a49dc4d5d4cf16ba880d3377196e96f1bc5 Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Mon, 13 May 2013 09:59:32 +0200
Subject: [PATCH 2/3] usbredirserver/testclient: Error check fcntl calls
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
usbredirserver/usbredirserver.c | 14 +++++++++++---
usbredirtestclient/usbredirtestclient.c | 14 ++++++++++++--
2 files changed, 23 insertions(+), 5 deletions(-)
diff --git a/usbredirserver/usbredirserver.c b/usbredirserver/usbredirserver.c
index c45a27c..d2765c6 100644
--- a/usbredirserver/usbredirserver.c
+++ b/usbredirserver/usbredirserver.c
@@ -189,7 +189,7 @@ static void quit_handler(int sig)
int main(int argc, char *argv[])
{
- int o, server_fd = -1;
+ int o, flags, server_fd = -1;
char *endptr, *delim;
int port = 4000;
int usbbus = -1;
@@ -307,8 +307,16 @@ int main(int argc, char *argv[])
break;
}
- fcntl(client_fd, F_SETFL,
- (long)fcntl(client_fd, F_GETFL) | O_NONBLOCK);
+ flags = fcntl(client_fd, F_GETFL);
+ if (flags == -1) {
+ perror("fcntl F_GETFL");
+ break;
+ }
+ flags = fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
+ if (flags == -1) {
+ perror("fcntl F_SETFL O_NONBLOCK");
+ break;
+ }
/* Try to find the specified usb device */
if (usbvendor != -1) {
diff --git a/usbredirtestclient/usbredirtestclient.c b/usbredirtestclient/usbredirtestclient.c
index a9123da..42b16dc 100644
--- a/usbredirtestclient/usbredirtestclient.c
+++ b/usbredirtestclient/usbredirtestclient.c
@@ -189,7 +189,7 @@ static void quit_handler(int sig)
int main(int argc, char *argv[])
{
- int o;
+ int o, flags;
char *endptr, *server;
struct addrinfo *r, *res, hints;
struct sigaction act;
@@ -265,7 +265,17 @@ int main(int argc, char *argv[])
exit(1);
}
- fcntl(client_fd, F_SETFL, (long)fcntl(client_fd, F_GETFL) | O_NONBLOCK);
+ flags = fcntl(client_fd, F_GETFL);
+ if (flags == -1) {
+ perror("fcntl F_GETFL");
+ exit(1);
+ }
+ flags = fcntl(client_fd, F_SETFL, flags | O_NONBLOCK);
+ if (flags == -1) {
+ perror("fcntl F_SETFL O_NONBLOCK");
+ exit(1);
+ }
+
parser = usbredirparser_create();
if (!parser) {
exit(1);
--
1.8.2.1

View File

@ -0,0 +1,32 @@
From 3f85be6da588d17f272a4b3c9b34bbfb7510f1e7 Mon Sep 17 00:00:00 2001
From: Hans de Goede <hdegoede@redhat.com>
Date: Mon, 13 May 2013 10:30:30 +0200
Subject: [PATCH 3/3] usbredirhost: Fix coverity sign_extension warning
Coverity does not like uint8_t * int being casted to an uint64_t, change
the int to an unsigned int to make it happy.
Note that the int in question can never be > 255, so this is not a real issue.
Signed-off-by: Hans de Goede <hdegoede@redhat.com>
---
usbredirhost/usbredirhost.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/usbredirhost/usbredirhost.c b/usbredirhost/usbredirhost.c
index da3ef90..09745c2 100644
--- a/usbredirhost/usbredirhost.c
+++ b/usbredirhost/usbredirhost.c
@@ -1041,7 +1041,8 @@ static int usbredirhost_submit_stream_transfer_unlocked(
static int usbredirhost_start_stream_unlocked(struct usbredirhost *host,
uint8_t ep)
{
- int i, status, count = host->endpoint[EP2I(ep)].transfer_count;
+ unsigned int i, count = host->endpoint[EP2I(ep)].transfer_count;
+ int status;
/* For out endpoints 1/2 the transfers are a buffer for usb-guest data */
if (!(ep & LIBUSB_ENDPOINT_IN)) {
--
1.8.2.1

View File

@ -1,11 +1,15 @@
Name: usbredir
Version: 0.6
Release: 2%{?dist}
Release: 3%{?dist}
Summary: USB network redirection protocol libraries
Group: System Environment/Libraries
License: LGPLv2+
URL: http://spice-space.org/page/UsbRedir
Source0: http://spice-space.org/download/%{name}/%{name}-%{version}.tar.bz2
# Some patches from upstream git (drop at next rebase)
Patch1: 0001-usbredirserver-Allow-connections-from-both-ipv6-and-.patch
Patch2: 0002-usbredirserver-testclient-Error-check-fcntl-calls.patch
Patch3: 0003-usbredirhost-Fix-coverity-sign_extension-warning.patch
BuildRequires: libusb1-devel >= 1.0.9
%description
@ -45,6 +49,9 @@ A simple USB host TCP server, using libusbredirhost.
%prep
%setup -q
%patch1 -p1
%patch2 -p1
%patch3 -p1
%build
@ -53,7 +60,7 @@ make %{?_smp_mflags} V=1
%install
make install DESTDIR=$RPM_BUILD_ROOT
%make_install
rm $RPM_BUILD_ROOT%{_libdir}/libusbredir*.la
@ -78,6 +85,10 @@ rm $RPM_BUILD_ROOT%{_libdir}/libusbredir*.la
%changelog
* Mon May 13 2013 Hans de Goede <hdegoede@redhat.com> - 0.6-3
- Fix usbredirserver not listening for ipv6 connections (rhbz#957470)
- Fix a few (harmless) coverity warnings
* Fri Feb 15 2013 Fedora Release Engineering <rel-eng@lists.fedoraproject.org> - 0.6-2
- Rebuilt for https://fedoraproject.org/wiki/Fedora_19_Mass_Rebuild