static PyObject *vdeplug_ctlfd(PyObject *self, PyObject *args) { VDECONN *conn; unsigned long vde_magic = 0; if (!PyArg_ParseTuple(args, "k", &vde_magic)) goto failure; conn = (VDECONN *) vde_magic; if (!conn) goto failure; return Py_BuildValue("i", vde_ctlfd(conn)); failure: return PyErr_SetFromErrno(PyExc_RuntimeError); }
int main(int argc, char **argv) { static char *sockname=NULL; static char *tapname=NULL; int daemonize=0; int tapfd; 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; static struct option long_options[] = { {"sock", 1, 0, 's'}, {"port", 1, 0, 'p'}, {"help",0,0,'h'}, {"mod",1,0,'m'}, {"group",1,0,'g'}, {"daemon",0,0,'d'}, {"pidfile", 1, 0, 'P'}, {0, 0, 0, 0} }; c = GETOPT_LONG (argc, argv, "hdP:p:s:m:g:", long_options, &option_index); if (c == -1) break; switch (c) { case 'p': open_args.port=atoi(optarg); if (open_args.port <= 0) usage(); //implies exit break; case 'h': usage(); //implies exit break; case 's': sockname=strdup(optarg); break; case 'm': sscanf(optarg,"%o",(unsigned int *)&(open_args.mode)); break; case 'g': open_args.group=strdup(optarg); break; case 'd': daemonize=1; break; case 'P': pidfile=strdup(optarg); break; default: usage(); //implies exit } } if (daemonize) { openlog(basename(prog), LOG_PID, 0); logok=1; syslog(LOG_INFO,"VDE_PLUG2TAP started"); } /* saves current path in pidfile_path, because otherwise with daemonize() we * forget it */ if(getcwd(pidfile_path, PATH_MAX-1) == NULL) { printlog(LOG_ERR, "getcwd: %s", strerror(errno)); exit(1); } strcat(pidfile_path, "/"); if (daemonize && daemon(0, 0)) { printlog(LOG_ERR,"daemon: %s",strerror(errno)); exit(1); } /* once here, we're sure we're the true process which will continue as a * server: save PID file if needed */ if(pidfile) save_pidfile(); if (optind < argc) tapname=argv[optind]; else usage(); // implies exit atexit(cleanup); setsighandlers(); tapfd=open_tap(tapname); if(tapfd<0) exit(1); pollv[0].fd=tapfd; if (sockname==NULL || strcmp(sockname,"-") != 0) { conn=vde_open(sockname,"vde_plug2tap:",&open_args); if (conn == NULL) { printlog(LOG_ERR,"vde_open %s: %s",sockname?sockname:"DEF_SWITCH",strerror(errno)); 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(;;) { poll(pollv,3,-1); if ((pollv[0].revents | pollv[1].revents | pollv[2].revents) & POLLHUP || (npollv > 2 && pollv[2].revents & POLLIN)) break; if (pollv[0].revents & POLLIN) { nx=read(tapfd,bufin,sizeof(bufin)); /* if POLLIN but not data it means that the stream has been * closed at the other end */ //fprintf(stderr,"%s: RECV %d %x %x \n",prog,nx,bufin[0],bufin[1]); if (nx<=0) break; if (conn != NULL) vde_send(conn,bufin,nx,0); else vdestream_send(vdestream, bufin, nx); } if (pollv[1].revents & POLLIN) { 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]); } } return(0); }
int main(int argc, char **argv) { static char *sockname=NULL; register ssize_t nx; struct vde_open_args open_args={.port=0,.group=NULL,.mode=0700}; uname(&me); //get the login name callerpwd=getpwuid(getuid()); if (argv[0][0] == '-') netusage(); //implies exit /* option parsing */ { int c; while (1) { int option_index = 0; static struct option long_options[] = { {"sock", 1, 0, 's'}, {"vdesock", 1, 0, 's'}, {"unix", 1, 0, 's'}, {"port", 1, 0, 'p'}, {"help",0,0,'h'}, {"mod",1,0,'m'}, {"group",1,0,'g'}, {0, 0, 0, 0} }; c = GETOPT_LONG (argc, argv, "hc:p:s:m:g:l", long_options, &option_index); if (c == -1) break; switch (c) { case 'c': if (strcmp(optarg,"vde_plug")==0) { #ifdef DO_SYSLOG write_syslog_entry("START"); atexit(write_syslog_close); #ifdef VDE_IP_LOG vde_ip_log=1; #endif #endif } else netusage(); //implies exit break; case 'p': open_args.port=atoi(optarg); if (open_args.port <= 0) usage(argv[0]); //implies exit break; case 'h': usage(argv[0]); //implies exit break; case 's': sockname=strdup(optarg); break; case 'm': sscanf(optarg,"%o",(unsigned int *)&(open_args.mode)); break; case 'g': open_args.group=strdup(optarg); break; case 'l': #ifdef VDE_IP_LOG write_syslog_entry("START"); atexit(write_syslog_close); vde_ip_log=1; break; #endif default: usage(argv[0]); //implies exit } } if (optind < argc && sockname==NULL) sockname=argv[optind]; } atexit(cleanup); setsighandlers(); conn=vde_open(sockname,"vde_plug:",&open_args); if (conn == NULL) { fprintf(stderr,"vde_open %s: %s\n",sockname?sockname:"DEF_SWITCH",strerror(errno)); exit(1); } vdestream=vdestream_open(conn,STDOUT_FILENO,vdeplug_recv,vdeplug_err); pollv[1].fd=vde_datafd(conn); pollv[2].fd=vde_ctlfd(conn); for(;;) { poll(pollv,3,-1); if ((pollv[0].revents | pollv[1].revents | pollv[2].revents) & POLLHUP || pollv[2].revents & POLLIN) break; if (pollv[0].revents & POLLIN) { nx=read(STDIN_FILENO,bufin,sizeof(bufin)); /* if POLLIN but not data it means that the stream has been * closed at the other end */ /*fprintf(stderr,"%s: RECV %d %x %x \n",myname,nx,bufin[0],bufin[1]);*/ if (nx==0) break; vdestream_recv(vdestream, bufin, nx); } if (pollv[1].revents & POLLIN) { nx=vde_recv(conn,bufin,BUFSIZE-2,0); if (nx<0) perror("vde_plug: recvfrom "); else { vdestream_send(vdestream, bufin, nx); /*fprintf(stderr,"%s: SENT %d %x %x \n",myname,nx,bufin[0],bufin[1]);*/ } } } return(0); }