summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHans de Goede <hdegoede@redhat.com>2013-05-07 15:29:09 +0200
committerHans de Goede <hdegoede@redhat.com>2013-05-07 15:29:09 +0200
commitc7e8bfe7f88ea11b31ebe115a629fb1cc903f7bc (patch)
treee59181b454071126c902188d637e9b54b4851910
parent7794d9ab044c76edf94d7b87f4000e3d7b8a3d57 (diff)
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>
-rw-r--r--usbredirserver/usbredirserver.c42
1 files 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);
}