Revision: 404
          http://vde.svn.sourceforge.net/vde/?rev=404&view=rev
Author:   rd235
Date:     2010-04-25 16:00:29 +0000 (Sun, 25 Apr 2010)

Log Message:
-----------
stream encoding of vde links moved from vde_plug to libvdeplug

Modified Paths:
--------------
    trunk/vde-2/include/libvdeplug.h
    trunk/vde-2/src/lib/libvdeplug.c
    trunk/vde-2/src/vde_plug.c

Modified: trunk/vde-2/include/libvdeplug.h
===================================================================
--- trunk/vde-2/include/libvdeplug.h    2010-04-04 08:57:30 UTC (rev 403)
+++ trunk/vde-2/include/libvdeplug.h    2010-04-25 16:00:29 UTC (rev 404)
@@ -62,4 +62,24 @@
 
 int vde_close(VDECONN *conn);
 
+/* vdestream */
+
+struct vdestream;
+
+typedef struct vdestream VDESTREAM;
+
+#define PACKET_LENGTH_ERROR 1
+
+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);
+
 #endif

Modified: trunk/vde-2/src/lib/libvdeplug.c
===================================================================
--- trunk/vde-2/src/lib/libvdeplug.c    2010-04-04 08:57:30 UTC (rev 403)
+++ trunk/vde-2/src/lib/libvdeplug.c    2010-04-25 16:00:29 UTC (rev 404)
@@ -67,6 +67,10 @@
 #define IPN_SO_DESCR 1
 #endif
 
+#ifndef MIN
+#define MIN(X,Y) (((X)<(Y))?(X):(Y))
+#endif
+
 /* Fallback names for the control socket, NULL-terminated array of absolute
  * filenames. */
 char *fallback_sockname[] = {
@@ -440,3 +444,101 @@
                return -1;
        }
 }
+
+/* vdestream */
+
+#define MAXPACKET 1521
+
+struct vdestream {
+       void *opaque;
+       int fdout;
+       ssize_t (*frecv)(void *opaque, void *buf, size_t count);
+       void (*ferr)(void *opaque, int type, char *format, ...);
+       char fragment[MAXPACKET];
+       char *fragp;
+       unsigned int rnx,remaining;
+};
+
+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, ...)
+               )
+{
+       VDESTREAM *vdestream;
+       if ((vdestream=calloc(1,sizeof(struct vdestream)))==NULL) {
+               errno=ENOMEM;
+               return NULL;
+       } else {
+               vdestream->opaque=opaque;
+               vdestream->fdout=fdout;
+               vdestream->frecv=frecv;
+               vdestream->ferr=ferr;
+               return vdestream;
+       }
+}
+
+ssize_t vdestream_send(VDESTREAM *vdestream, const void *buf, size_t len)
+{
+       if (len <= MAXPACKET) {
+               unsigned char header[2];
+               struct iovec iov[2]={{header,2},{(void *)buf,len}};
+               header[0]=len >> 8;
+               header[1]=len & 0xff;
+               return writev(vdestream->fdout,iov,2);
+       } else
+               return 0;
+}
+
+void vdestream_recv(VDESTREAM *vdestream, unsigned char *buf, size_t len)
+{
+       //fprintf(stderr,"%s: splitpacket rnx=%d remaining=%d 
size=%d\n",myname,rnx,vdestream->remaining,len);
+       if (len==0) return;
+       if (vdestream->rnx>0) {
+               register int amount=MIN(vdestream->remaining,len);
+               //fprintf(stderr,"%s: fragment amount %d\n",myname,amount);
+               memcpy(vdestream->fragp,buf,amount);
+               vdestream->remaining-=amount;
+               vdestream->fragp+=amount;
+               buf+=amount;
+               len-=amount;
+               if (vdestream->remaining==0) {
+                       //fprintf(stderr,"%s: delivered defrag 
%d\n",myname,vdestream->rnx);
+                       
vdestream->frecv(vdestream->opaque,vdestream->fragment,vdestream->rnx);
+                       vdestream->rnx=0;
+               }
+       }
+       while (len > 0) {
+               vdestream->rnx=(buf[0]<<8)+buf[1];
+               len-=2;
+               //fprintf(stderr,"%s %d: packet %d size %d %x 
%x\n",myname,getpid(),vdestream->rnx,len,buf[0],buf[1]);
+               buf+=2;
+               if (vdestream->rnx > MAXPACKET) {
+                       if (vdestream->ferr != NULL)
+                               
vdestream->ferr(vdestream->opaque,PACKET_LENGTH_ERROR,
+                                               "size %d expected size 
%d",len,vdestream->rnx);
+                       vdestream->rnx=0;
+                       return;
+               }
+               if (vdestream->rnx > len) {
+                       //fprintf(stderr,"%s: begin defrag 
%d\n",myname,vdestream->rnx);
+                       vdestream->fragp=vdestream->fragment;
+                       memcpy(vdestream->fragp,buf,len);
+                       vdestream->remaining=vdestream->rnx-len;
+                       vdestream->fragp+=len;
+                       len=0;
+               } else {
+                       //fprintf(stderr,"%s: deliver 
%d\n",myname,vdestream->rnx);
+                       vdestream->frecv(vdestream->opaque,buf,vdestream->rnx);
+                       buf+=vdestream->rnx;
+                       len-=vdestream->rnx;
+                       vdestream->rnx=0;
+               }
+       }
+}
+
+void vdestream_close(VDESTREAM *vdestream)
+{
+       free(vdestream);
+}
+

Modified: trunk/vde-2/src/vde_plug.c
===================================================================
--- trunk/vde-2/src/vde_plug.c  2010-04-04 08:57:30 UTC (rev 403)
+++ trunk/vde-2/src/vde_plug.c  2010-04-25 16:00:29 UTC (rev 404)
@@ -20,6 +20,7 @@
 #include <sys/stat.h>
 #include <pwd.h>
 #include <grp.h>
+#include <stdarg.h>
 
 #include <config.h>
 #include <vde.h>
@@ -39,10 +40,11 @@
 #define MIN(X,Y) (((X)<(Y))?(X):(Y))
 #endif
 
-#define BUFSIZE 2048
+#define BUFSIZE 4096
 #define ETH_ALEN 6
 
 VDECONN *conn;
+VDESTREAM *vdestream;
 
 struct utsname me;
 #define myname me.nodename
@@ -184,69 +186,32 @@
 }
 #endif
 
-unsigned char bufin[BUFSIZE];
-
-void splitpacket(const unsigned char *buf,int size,VDECONN *conn)
+void vdeplug_err(void *opaque, int type, char *format,...)
 {
-       static char fragment[BUFSIZE];
-       static char *fragp;
-       static unsigned int rnx,remaining;
+       va_list args;
 
-       //fprintf(stderr,"%s: splitpacket rnx=%d remaining=%d 
size=%d\n",myname,rnx,remaining,size);
-       if (size==0) return;
-       if (rnx>0) {
-               register int amount=MIN(remaining,size);
-               //fprintf(stderr,"%s: fragment amount %d\n",myname,amount);
-               memcpy(fragp,buf,amount);
-               remaining-=amount;
-               fragp+=amount;
-               buf+=amount;
-               size-=amount;
-               if (remaining==0) {
-                       //fprintf(stderr,"%s: delivered defrag 
%d\n",myname,rnx);
-                       //send(fd,fragment,rnx,0);
-#ifdef VDE_IP_LOG
-                       if (vde_ip_log)
-                               vde_ip_check(buf,rnx);
-#endif
-                       vde_send(conn,fragment,rnx,0);
-                       rnx=0;
-               }
+       if (isatty(STDERR_FILENO)) {
+               fprintf(stderr, "%s: Packet length error",myname);
+               va_start(args, format);
+               vfprintf(stderr, format, args);
+               va_end(args);
+               fprintf(stderr,"\n");
        }
-       while (size > 0) {
-               rnx=(buf[0]<<8)+buf[1];
-               size-=2;
-               //fprintf(stderr,"%s %d: packet %d size %d %x 
%x\n",myname,getpid(),rnx,size,buf[0],buf[1]);
-               buf+=2;
-               if (rnx>1521) {
-                       fprintf(stderr,"%s: Packet length error size %d rnx 
%d\n",myname,size,rnx);
-                       rnx=0;
-                       return;
-               }
-               if (rnx > size) {
-                       //fprintf(stderr,"%s: begin defrag %d\n",myname,rnx);
-                       fragp=fragment;
-                       memcpy(fragp,buf,size);
-                       remaining=rnx-size;
-                       fragp+=size;
-                       size=0;
-               } else {
-                       //fprintf(stderr,"%s: deliver %d\n",myname,rnx);
-                       //send(fd,buf,rnx,0);
+}
+
+ssize_t vdeplug_recv(void *opaque, void *buf, size_t count)
+{
+       VDECONN *conn=opaque;
 #ifdef VDE_IP_LOG
-                       if (vde_ip_log)
-                               vde_ip_check(buf,rnx);
+       if (vde_ip_log)
+               vde_ip_check(buf,count);
 #endif
-                       vde_send(conn,(char *)buf,rnx,0);
-                       buf+=rnx;
-                       size-=rnx;
-                       rnx=0;
-               }
-       }
+       return vde_send(conn,(char *)buf,count,0);
 }
 
 static void cleanup(void)
 {
+       vdestream_close(vdestream);
   vde_close(conn);
 }
 
@@ -313,6 +278,8 @@
        exit(-1);
 }
 
+unsigned char bufin[BUFSIZE];
+
 int main(int argc, char **argv)
 {
        static char *sockname=NULL;
@@ -407,6 +374,8 @@
        if (conn == NULL)
                exit(1);
 
+       vdestream=vdestream_open(conn,STDOUT_FILENO,vdeplug_recv,vdeplug_err);
+
        pollv[1].fd=vde_datafd(conn);
        pollv[2].fd=vde_ctlfd(conn);
 
@@ -422,17 +391,15 @@
                        /*fprintf(stderr,"%s: RECV %d %x %x 
\n",myname,nx,bufin[0],bufin[1]);*/
                        if (nx==0)
                                break;
-                       splitpacket(bufin,nx,conn);
+                       vdestream_recv(vdestream, bufin, nx);
                }
                if (pollv[1].revents & POLLIN) {
-                       nx=vde_recv(conn,(char *)(bufin+2),BUFSIZE-2,0);
+                       nx=vde_recv(conn,bufin,BUFSIZE-2,0);
                        if (nx<0)
                                perror("vde_plug: recvfrom ");
                        else
                        {
-                               bufin[0]=nx >> 8;
-                               bufin[1]=nx & 0xff;
-                               write(STDOUT_FILENO,bufin,nx+2);
+                               vdestream_send(vdestream, bufin, nx);
                                /*fprintf(stderr,"%s: SENT %d %x %x 
\n",myname,nx,bufin[0],bufin[1]);*/
                        }
                }


This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.

------------------------------------------------------------------------------
_______________________________________________
vde-users mailing list
vde-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/vde-users

Reply via email to