All pastes #769961 Raw Edit

UDP forwarding with slirpvde

public diff v1 · immutable
·published 2007-11-11 20:13 UTC
rendered paste body
diff -x CVS -x .svn -x 'Makefile*' -Nu /home/matteo/src/virtual-square-svn/vde_svn/slirpvde/misc.c ./misc.c--- /home/matteo/src/virtual-square-svn/vde_svn/slirpvde/misc.c 2007-11-11 19:24:10.000000000 +0100+++ ./misc.c    2007-11-11 04:41:24.000000000 +0100@@ -78,6 +78,20 @@                lprint("Redirection failed: %s\r\n", strerror(errno)); } +void redir_udp(inaddr, port, lport)+       u_int32_t inaddr;+       int port;+       int lport;+{+       struct socket *so;++       if ((so=udp_listen(htons(lport), inaddr, htons(port), 0)) != NULL) {+               lprint("Redirecting UDP port %d to %s:%d\r\n",+                      ntohs(so->so_fport), inet_ntoa(so->so_laddr), port);+       } else+               lprint("Redirection failed: %s\r\n", strerror(errno));+}+ #ifndef HAVE_INET_ATON int inet_aton(cp, ia)diff -x CVS -x .svn -x 'Makefile*' -Nu /home/matteo/src/virtual-square-svn/vde_svn/slirpvde/misc.h ./misc.h--- /home/matteo/src/virtual-square-svn/vde_svn/slirpvde/misc.h 2007-11-11 19:24:10.000000000 +0100+++ ./misc.h    2007-11-11 03:46:36.000000000 +0100@@ -72,6 +72,7 @@ int show_x _P((void)); void redir_x _P((u_int32_t, int, int, int)); void redir_tcp _P((u_int32_t, int, int));+void redir_udp _P((u_int32_t, int, int));  void getouraddr _P((void)); inline  void slirp_insque  _P((void *, void *));diff -x CVS -x .svn -x 'Makefile*' -Nu /home/matteo/src/virtual-square-svn/vde_svn/slirpvde/slirpvde.c ./slirpvde.c--- /home/matteo/src/virtual-square-svn/vde_svn/slirpvde/slirpvde.c     2007-11-11 19:24:10.000000000 +0100+++ ./slirpvde.c        2007-11-11 19:35:17.000000000 +0100@@ -214,6 +214,13 @@        struct redirtcp *next; }; +struct redirudp {+       u_int32_t inaddr;+       int port;+       int lport;+       struct redirudp *next;+};+ static struct redirtcp *parse_redir_tcp(struct redirtcp *head, char *buff) {        u_int32_t inaddr=0;@@ -259,6 +266,51 @@        } } +static struct redirudp *parse_redir_udp(struct redirudp *head, char *buff)+{+       u_int32_t inaddr=0; // OK+       int port=0; // OK+       int lport=0; // OK+       char *ipaddrstr=NULL; // OK+       char *portstr=NULL; // OK+       struct redirudp *new; ++       if ((ipaddrstr = strchr(buff, ':'))) {+               *ipaddrstr++ = 0;+               if (*ipaddrstr == 0) {+                       fprintf(stderr,"redir UDP syntax error\n");+                       return head;+               }+       }+       if ((portstr = strchr(ipaddrstr, ':'))) {+               *portstr++ = 0;+               if (*portstr == 0) {+                       fprintf(stderr,"redir UDP syntax error\n");+                       return head;+               }+       }++       sscanf(buff,"%d",&lport);+       sscanf(portstr,"%d",&port);+       if (ipaddrstr) +               inaddr = inet_addr(ipaddrstr);++       if (!inaddr) {+               fprintf(stderr,"UDP redirection error: an IP address must be specified\r\n");+               return head;+       }++       if ((new=malloc(sizeof(struct redirudp)))==NULL)+               return head;+       else {+               new->inaddr=inaddr;+               new->port=port;+               new->lport=lport;+               new->next=head;+               return new;+       }+}+ static struct redirx *parse_redir_x(struct redirx *head, char *buff) {        char *ptr=NULL;@@ -328,6 +380,15 @@        } } +static void do_redir_udp(struct redirudp *head)+{+       if (head) {+               do_redir_udp(head->next);+               redir_udp(head->inaddr,head->port,head->lport);+               free(head);+       }+}+ static void do_redir_x(struct redirx *head) {        if (head) {@@ -344,8 +405,8 @@                        "\t [-L host_port:guest_addr:guest_port] [-X guest_addr[:display[.screen]]] \n"                        "\t [-x portno:unix_socket_path]\n"                        " %s [-s vdesock] [-D] [-d] [-n netaddr]\n"-                       "\t [-L host_port:guest_addr:guest_port] [-X guest_addr[:display[.screen]]] \n" -                       "\t [-x portno:unix_socket_path]\n"+                       "\t [-T host_tcp_port:guest_addr:guest_tcp_port] [-U host_udp_port:guest_addr:guest_udp_port]\n"+                       "\t [-X guest_addr[:display[.screen]]] [-x portno:unix_socket_path]\n"                        ,name,name);        exit(-1); }@@ -373,14 +434,15 @@   fd_set rs,ws,xs;   int opt,longindx;   char *netw=NULL;-       int daemonize=0;-       struct redirtcp *rtcp=NULL;-       struct redirx *rx=NULL;-       struct vde_open_args open_args={.port=0,.group=NULL,.mode=0700};+  int daemonize=0;+  struct redirtcp *rtcp=NULL;+  struct redirudp *rudp=NULL;+  struct redirx *rx=NULL;+  struct vde_open_args open_args={.port=0,.group=NULL,.mode=0700};    prog=basename(argv[0]); -  while ((opt=GETOPT_LONG(argc,argv,"s:n:p:g:m:L:X:x:dD",slirpvdeopts,&longindx)) > 0) {+  while ((opt=GETOPT_LONG(argc,argv,"s:n:p:g:m:T:U:X:x:dD",slirpvdeopts,&longindx)) > 0) {                switch (opt) {                        case 's' : sockname=optarg;                                                                 break;@@ -398,8 +460,10 @@                                                                 break;                        case 'P' : open_args.port=atoi(optarg);                                                                 break;-                       case 'L': rtcp=parse_redir_tcp(rtcp,optarg);+                       case 'T': rtcp=parse_redir_tcp(rtcp,optarg);                                                                 break;+                       case 'U': rudp=parse_redir_udp(rudp, optarg);+                                                                break;                        case 'X': rx=parse_redir_x(rx,optarg);                                                                 break;                        case 'x': parse_redir_locx(optarg);@@ -431,6 +495,7 @@        slirp_init(netw);         do_redir_tcp(rtcp);+       do_redir_udp(rudp);        do_redir_x(rx);         for(;;) {