From 6549e50d7239f2ef2ed8863f6ab9cc5bffda52b7 Mon Sep 17 00:00:00 2001 From: rd235 Date: Wed, 4 Aug 2010 14:42:59 +0000 Subject: [PATCH] stream mgmt to libvdeplug_dyn and vde_plug2tap added git-svn-id: https://vde.svn.sourceforge.net/svnroot/vde/trunk@439 d37a7db1-d92d-0410-89df-f68f52f87b57 --- vde-2/include/libvdeplug_dyn.h | 27 +++++++++++++++++- vde-2/man/vde_plug2tap.1 | 18 +++++++++++- vde-2/src/vde_plug2tap.c | 64 ++++++++++++++++++++++++++++++++---------- 3 files changed, 92 insertions(+), 17 deletions(-) diff --git a/vde-2/include/libvdeplug_dyn.h b/vde-2/include/libvdeplug_dyn.h index ed7ef7e..23c0292 100644 --- a/vde-2/include/libvdeplug_dyn.h +++ b/vde-2/include/libvdeplug_dyn.h @@ -58,9 +58,11 @@ #define LIBVDEPLUG_INTERFACE_VERSION 1 struct vdeconn; - typedef struct vdeconn VDECONN; +struct vdestream; +typedef struct vdestream VDESTREAM; + /* Open a VDE connection. * vde_open_options: * port: connect to a specific port of the switch (0=any) @@ -89,12 +91,27 @@ struct vdepluglib { int (* vde_datafd)(VDECONN *conn); int (* vde_ctlfd)(VDECONN *conn); int (* vde_close)(VDECONN *conn); + VDESTREAM * (* vdestream_open)(void *opaque, int fdout, + ssize_t (* frecv)(void *opaque, void *buf, size_t count), + void (* ferr)(void *opaque, int type, char *format, ...) + ); + ssize_t (* vdestream_send)(VDESTREAM *vdestream, const void *buf, size_t len); + void (* vdestream_recv)(VDESTREAM *vdestream, unsigned char *buf, size_t len); + void (* vdestream_close)(VDESTREAM *vdestream); }; typedef VDECONN * (* VDE_OPEN_REAL_T)(const char *vde_switch,char *descr,int interface_version, struct vde_open_args *open_args); typedef size_t (* VDE_RECV_T)(VDECONN *conn,void *buf,size_t len,int flags); typedef size_t (* VDE_SEND_T)(VDECONN *conn,const void *buf,size_t len,int flags); typedef int (* VDE_INT_FUN)(VDECONN *conn); +typedef VDESTREAM * (* VDESTREAM_OPEN_T)(void *opaque, int fdout, + ssize_t (* frecv)(void *opaque, void *buf, size_t count), + void (* ferr)(void *opaque, int type, char *format, ...) + ); +typedef ssize_t (* VDESTREAM_SEND_T)(VDESTREAM *vdestream, const void *buf, size_t len); +typedef void (* VDESTREAM_RECV_T)(VDESTREAM *vdestream, unsigned char *buf, size_t len); +typedef void (* VDESTREAM_CLOSE_T)(VDESTREAM *vdestream); + #define libvdeplug_dynopen(x) ({ \ (x).dl_handle=dlopen("libvdeplug.so",RTLD_NOW); \ if ((x).dl_handle) { \ @@ -104,11 +121,19 @@ typedef int (* VDE_INT_FUN)(VDECONN *conn); (x).vde_datafd=(VDE_INT_FUN) dlsym((x).dl_handle,"vde_datafd"); \ (x).vde_ctlfd=(VDE_INT_FUN) dlsym((x).dl_handle,"vde_ctlfd"); \ (x).vde_close=(VDE_INT_FUN) dlsym((x).dl_handle,"vde_close"); \ + (x).vdestream_open=(VDESTREAM_OPEN_T) dlsym((x).dl_handle,"vdestream_open"); \ + (x).vdestream_send=(VDESTREAM_SEND_T) dlsym((x).dl_handle,"vdestream_send"); \ + (x).vdestream_recv=(VDESTREAM_RECV_T) dlsym((x).dl_handle,"vdestream_recv"); \ + (x).vdestream_close=(VDESTREAM_CLOSE_T) dlsym((x).dl_handle,"vdestream_close"); \ } else { \ (x).vde_open_real=NULL; \ (x).vde_send= NULL; \ (x).vde_recv= NULL; \ (x).vde_datafd= (x).vde_ctlfd= (x).vde_close= NULL; \ + (x).vdestream_open= NULL; \ + (x).vdestream_send= NULL; \ + (x).vdestream_recv= NULL; \ + (x).vdestream_close= NULL; \ }\ }) diff --git a/vde-2/man/vde_plug2tap.1 b/vde-2/man/vde_plug2tap.1 index 0c1057f..360094a 100644 --- a/vde-2/man/vde_plug2tap.1 +++ b/vde-2/man/vde_plug2tap.1 @@ -11,6 +11,13 @@ is a plug to be connected into a VDE switch. All the data that is catched by the plug is sent to the tap interface .I tap_name and everything from that interface is injected in the switch. +.br +Example: +.in +2 +.B vde_plug2tap tap4 +.in -2 +.br +connects the default switch (/var/run/vde.ctl) to the tap4 interface. .SH OPTIONS .TP @@ -35,7 +42,16 @@ octal chmod like permissions for the comm sockets .TP \fB\-s\fP, \fB\-\-sock\fP=\fIsocket\fP specify the UNIX socket to be used by local programs for joining the VDE. -The default value is "/tmp/vde.ctl". +The default value is "/var/run/vde.ctl". +When '-' is used in place of the VDE switch directory, \fBvde_plug2tap\fP +works as a plug (see vde_plug(1)). +The command: +.br +.in +2 +.B dpipe vde_plug = ssh remote.machine.org vde_plug2tap -s - tapx +.in -2 +.br +connects the default local switch to a remote tapx interface. .TP \fB\-d\fP, \fB\-\-daemon\fP start vde_plug2tap as a background process diff --git a/vde-2/src/vde_plug2tap.c b/vde-2/src/vde_plug2tap.c index 96d5f2d..b5f561f 100644 --- a/vde-2/src/vde_plug2tap.c +++ b/vde-2/src/vde_plug2tap.c @@ -1,5 +1,7 @@ /* Copyright 2006 Renzo Davoli * from vde_plug Davoli Gardenghi + * Modified 2010 Renzo Davoli, vdestream added + * Licensed under the GPLv2 */ #include @@ -50,6 +52,7 @@ #endif VDECONN *conn; +VDESTREAM *vdestream; char *prog; int logok; @@ -78,7 +81,10 @@ static void cleanup(void) printlog(LOG_WARNING,"Couldn't remove pidfile '%s': %s", pidfile, strerror(errno)); } - vde_close(conn); + if (vdestream != NULL) + vdestream_close(vdestream); + if (conn != NULL) + vde_close(conn); } static void sig_handler(int sig) @@ -128,8 +134,6 @@ static void setsighandlers() perror("Setting handler"); } -struct pollfd pollv[]={{0,POLLIN|POLLHUP},{0,POLLIN|POLLHUP},{0,POLLIN|POLLHUP}}; - static void usage(void) { fprintf(stderr, "Usage: %s [OPTION]... tap_name\n\n", prog); fprintf(stderr, " -p, --port=portnum Port number in the VDE switch\n" @@ -227,6 +231,12 @@ static void save_pidfile() fclose(f); } +static ssize_t vde_plug2tap_recv(void *opaque, void *buf, size_t count) +{ + int *tapfdp=opaque; + return write(*tapfdp,buf,count); +} + int main(int argc, char **argv) { static char *sockname=NULL; @@ -237,6 +247,11 @@ int main(int argc, char **argv) register ssize_t nx; struct vde_open_args open_args={.port=0,.group=NULL,.mode=0700}; int c; + static struct pollfd pollv[]={{0,POLLIN|POLLHUP}, + {0,POLLIN|POLLHUP}, + {0,POLLIN|POLLHUP}}; + int npollv; + prog=argv[0]; while (1) { int option_index = 0; @@ -324,18 +339,27 @@ int main(int argc, char **argv) tapfd=open_tap(tapname); if(tapfd<0) exit(1); - conn=vde_open(sockname,"vde_plug:",&open_args); - if (conn == NULL) - exit(1); - pollv[0].fd=tapfd; - pollv[1].fd=vde_datafd(conn); - pollv[2].fd=vde_ctlfd(conn); + + if (sockname==NULL || strcmp(sockname,"-") != 0) { + conn=vde_open(sockname,"vde_plug:",&open_args); + if (conn == NULL) + exit(1); + pollv[1].fd=vde_datafd(conn); + pollv[2].fd=vde_ctlfd(conn); + npollv=3; + } else { + vdestream=vdestream_open(&tapfd,STDOUT_FILENO,vde_plug2tap_recv,NULL); + if (vdestream == NULL) + exit(1); + pollv[1].fd=STDIN_FILENO; + npollv=2; + } for(;;) { result=poll(pollv,3,-1); if ((pollv[0].revents | pollv[1].revents | pollv[2].revents) & POLLHUP || - pollv[2].revents & POLLIN) + (npollv > 2 && pollv[2].revents & POLLIN)) break; if (pollv[0].revents & POLLIN) { nx=read(tapfd,bufin,sizeof(bufin)); @@ -344,13 +368,23 @@ int main(int argc, char **argv) //fprintf(stderr,"%s: RECV %d %x %x \n",prog,nx,bufin[0],bufin[1]); if (nx<=0) break; - vde_send(conn,bufin,nx,0); + if (conn != NULL) + vde_send(conn,bufin,nx,0); + else + vdestream_send(vdestream, bufin, nx); } if (pollv[1].revents & POLLIN) { - nx=vde_recv(conn,bufin,sizeof(bufin),0); - if (nx<=0) - break; - write(tapfd,bufin,nx); + if (conn != NULL) { + nx=vde_recv(conn,bufin,sizeof(bufin),0); + if (nx<=0) + break; + write(tapfd,bufin,nx); + } else { + nx=read(STDIN_FILENO,bufin,sizeof(bufin)); + if (nx<=0) + break; + vdestream_recv(vdestream,bufin,nx); + } //fprintf(stderr,"%s: SENT %d %x %x \n",prog,nx,bufin[0],bufin[1]); } -- 2.11.4.GIT