Re: [PATCH] usbredirserver: add support for bind specific address

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi

----- Original Message -----
> From: Chen Hanxiao <chenhanxiao@xxxxxxxxx>
> 
> We bind our listen address to in6addr_any, which may be
> unsecure with multi network cards that belong to
> internal or external networks.
> 
> This patch introduces option -4 and -6 to bind a specific
> address.
> 
> Signed-off-by: Chen Hanxiao <chenhanxiao@xxxxxxxxx>
> ---
>  usbredirserver/usbredirserver.1 |  3 +-
>  usbredirserver/usbredirserver.c | 64
>  ++++++++++++++++++++++++++++++++++-------
>  2 files changed, 55 insertions(+), 12 deletions(-)
> 
> diff --git a/usbredirserver/usbredirserver.1
> b/usbredirserver/usbredirserver.1
> index e857190..8bbce07 100644
> --- a/usbredirserver/usbredirserver.1
> +++ b/usbredirserver/usbredirserver.1
> @@ -3,7 +3,8 @@
>  usbredirserver \- exporting an USB device for use from another (virtual)
>  machine
>  .SH SYNOPSIS
>  .B usbredirserver
> -[\fI-p|--port <port>\fR] [\fI-v|--verbose <0-5>\fR]
> \fI<busnum-devnum|vendorid:prodid>\fR
> +[\fI-p|--port <port>\fR] [\fI-v|--verbose <0-5>\fR] [\fI-4 <ipv4_addr|I-6
> <ipv6_addr>]
> +\fI<busnum-devnum|vendorid:prodid>\fR
>  .SH DESCRIPTION
>  usbredirserver is a small standalone server for exporting an USB device for
>  use from another (virtual) machine through the usbredir protocol.
> diff --git a/usbredirserver/usbredirserver.c
> b/usbredirserver/usbredirserver.c
> index 13965dc..b2fdb70 100644
> --- a/usbredirserver/usbredirserver.c
> +++ b/usbredirserver/usbredirserver.c
> @@ -36,6 +36,7 @@
>  #include <sys/time.h>
>  #include <netdb.h>
>  #include <netinet/in.h>
> +#include <arpa/inet.h>
>  #include "usbredirhost.h"
>  
>  
> @@ -49,6 +50,8 @@ static struct usbredirhost *host;
>  static const struct option longopts[] = {
>      { "port", required_argument, NULL, 'p' },
>      { "verbose", required_argument, NULL, 'v' },
> +    { "ipv4", required_argument, NULL, '4' },
> +    { "ipv6", required_argument, NULL, '6' },
>      { "help", no_argument, NULL, 'h' },
>      { NULL, 0, NULL, 0 }
>  };
> @@ -93,7 +96,9 @@ static int usbredirserver_write(void *priv, uint8_t *data,
> int count)
>  static void usage(int exit_code, char *argv0)
>  {
>      fprintf(exit_code? stderr:stdout,
> -        "Usage: %s [-p|--port <port>] [-v|--verbose <0-5>]
> <busnum-devnum|vendorid:prodid>\n",
> +        "Usage: %s [-p|--port <port>] [-v|--verbose <0-5>] "
> +        "[[-4|--ipv4 ipaddr]|[-6|--ipv6 ipaddr]] "
> +        "<busnum-devnum|vendorid:prodid>\n",
>          argv0);
>      exit(exit_code);
>  }
> @@ -198,11 +203,13 @@ int main(int argc, char *argv[])
>      int usbvendor  = -1;
>      int usbproduct = -1;
>      int on = 1;
> +    char *ipv4_addr = NULL, *ipv6_addr = NULL;
> +    struct sockaddr_in serveraddr4;
>      struct sockaddr_in6 serveraddr;
>      struct sigaction act;
>      libusb_device_handle *handle = NULL;
>  
> -    while ((o = getopt_long(argc, argv, "hp:v:", longopts, NULL)) != -1) {
> +    while ((o = getopt_long(argc, argv, "hp:v:4:6:", longopts, NULL)) != -1)
> {
>          switch (o) {
>          case 'p':
>              port = strtol(optarg, &endptr, 10);
> @@ -218,6 +225,12 @@ int main(int argc, char *argv[])
>                  usage(1, argv[0]);
>              }
>              break;
> +        case '4':
> +            ipv4_addr = optarg;
> +            break;
> +        case '6':
> +            ipv6_addr = optarg;
> +            break;
>          case '?':
>          case 'h':
>              usage(o == '?', argv[0]);
> @@ -272,9 +285,13 @@ int main(int argc, char *argv[])
>  
>      libusb_set_debug(ctx, verbose);
>  
> -    server_fd = socket(AF_INET6, SOCK_STREAM, 0);
> +    if (ipv4_addr) {
> +        server_fd = socket(AF_INET, SOCK_STREAM, 0);
> +    } else {
> +        server_fd = socket(AF_INET6, SOCK_STREAM, 0);
> +    }
>      if (server_fd == -1) {
> -        perror("Error creating ipv6 socket");
> +        perror("Error creating ip socket");
>          exit(1);
>      }
>  
> @@ -283,14 +300,39 @@ int main(int argc, char *argv[])
>          exit(1);
>      }
>  
> -    memset(&serveraddr, 0, sizeof(serveraddr));
> -    serveraddr.sin6_family = AF_INET6;
> -    serveraddr.sin6_port   = htons(port);
> -    serveraddr.sin6_addr   = in6addr_any;
> +    if (ipv4_addr) {
> +        memset(&serveraddr, 0, sizeof(serveraddr));
> +        serveraddr4.sin_family = AF_INET;
> +        serveraddr4.sin_port   = htons(port);
> +        if ((inet_pton(AF_INET, ipv4_addr, &serveraddr4.sin_addr)) != 1) {
> +            perror("Error convert ipv4 address");
> +            exit(1);
> +        }
>  
> -    if (bind(server_fd, (struct sockaddr *)&serveraddr, sizeof(serveraddr)))
> {
> -        fprintf(stderr, "Error binding port %d: %s\n", port,
> strerror(errno));
> -        exit(1);
> +        if (bind(server_fd, (struct sockaddr *)&serveraddr4,
> +                 sizeof(serveraddr4))) {
> +            fprintf(stderr, "Error binding port %d: %s\n", port,
> strerror(errno));

Why not perror() like the rest of the code?

> +            exit(1);
> +        }
> +    } else {
> +        memset(&serveraddr, 0, sizeof(serveraddr));
> +        serveraddr.sin6_family = AF_INET6;
> +        serveraddr.sin6_port   = htons(port);
> +        if (ipv6_addr) {
> +            if ((inet_pton(AF_INET6, ipv4_addr, &serveraddr.sin6_addr)) !=
> 1) {
> +                perror("Error convert ipv4 address");
> +                exit(1);
> +            }
> +        } else {
> +            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));

same here

> +            exit(1);
> +        }
>      }
>  
>      if (listen(server_fd, 1)) {
> --
> 2.13.6

Look good otherwise,

> 
> _______________________________________________
> Spice-devel mailing list
> Spice-devel@xxxxxxxxxxxxxxxxxxxxx
> https://lists.freedesktop.org/mailman/listinfo/spice-devel
> 
_______________________________________________
Spice-devel mailing list
Spice-devel@xxxxxxxxxxxxxxxxxxxxx
https://lists.freedesktop.org/mailman/listinfo/spice-devel




[Index of Archives]     [Linux ARM Kernel]     [Linux ARM]     [Linux Omap]     [Fedora ARM]     [IETF Annouce]     [Security]     [Bugtraq]     [Linux]     [Linux OMAP]     [Linux MIPS]     [ECOS]     [Asterisk Internet PBX]     [Linux API]     [Monitors]