Merge rd-235 experimental version into trunk.
[vde.git] / vde-2 / src / vdetaplib / vdetap.c
blob03b53da82dee3e8b6efc976c6d28871ab51e0be3
1 /* Copyright 2004 Renzo Davoli
2 * Reseased under the GPLv2 */
4 #define _GNU_SOURCE
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <unistd.h>
8 #include <stdint.h>
9 #include <sys/select.h>
10 #include <sys/socket.h>
11 #include <sys/un.h>
12 #include <pwd.h>
14 #include <config.h>
15 #include <vde.h>
16 #include <vdecommon.h>
17 #include <libvdeplug.h>
19 #define SWITCH_MAGIC 0xfeedface
20 #define BUFSIZE 2048
21 #define MAXDESCR 128
23 VDECONN *conn;
25 static unsigned char bufin[BUFSIZE];
27 static struct pollfd pollv[]={{0,POLLIN|POLLHUP,0},{0,POLLIN|POLLHUP,0}};
29 int main(int argc,char *argv[])
31 int fd,fddata;
32 int nx;
33 register int i;
34 struct vde_open_args open_args={.port=0,.group=NULL,.mode=0700};
35 char *descr;
36 /*printf("argc = %d\n",argc);
37 for (i=0;i<argc;i++)
38 printf("argv %d -%s-\n",i,argv[i]);*/
39 if (argc < 4 && argv[0][0] != '-') {
40 fprintf(stderr,"vdetap must be activated by libvdetap e.g.\n"
41 " sh%% export LD_PRELOAD=%s/libvdetap.so\n"
42 " csh%% setenv LD_PRELOAD %s/libvdetap.so\n", LIBEXECDIR, LIBEXECDIR);
43 exit (-1);
46 fd = atoi(argv[1]);
47 for (i=3;i<FD_SETSIZE;i++)
48 if (i != fd)
49 close(i);
50 if((fddata = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0){
51 perror("socket");
52 exit(1);
54 if (argc == 8) {
55 open_args.port=atoi(argv[5]);
56 open_args.group=argv[6];
57 sscanf(argv[7],"%i",&open_args.mode);
58 //fprintf(stderr,"|%d|%s|%o|\n",open_args.port,open_args.group,open_args.mode);
59 asprintf(&descr,"tuntaplib %s/%s",argv[4],argv[3]);
60 } else
61 descr="tuntaplib";
62 conn=vde_open(argv[2],descr,&open_args);
63 pollv[0].fd=fd;
64 pollv[1].fd=vde_datafd(conn);
65 for(;;) {
66 poll(pollv,2,-1);
67 if (pollv[0].revents & POLLHUP || pollv[1].revents & POLLHUP)
68 break;
69 if (pollv[0].revents & POLLIN) {
70 nx=read(fd,bufin,sizeof(bufin));
71 /*fprintf(stderr,"RX from pgm %d\n",nx);*/
72 vde_send(conn,bufin,nx,0);
74 if (pollv[1].revents & POLLIN) {
75 nx=vde_recv(conn,bufin,BUFSIZE,0);
76 /*fprintf(stderr,"TX to pgm %d\n",nx);*/
77 write(fd,bufin,nx);
80 vde_close(conn);
81 return 0;