1 /* Copyright 2006 Renzo Davoli
2 * from vde_plug Davoli Gardenghi
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
21 #include <libvdeplug/libvdeplug.h>
30 #include <linux/if_tun.h>
34 # define TAP_PREFIX "/dev/"
36 # if defined HAVE_SYSLIMITS_H
37 # include <syslimits.h>
38 # elif defined HAVE_SYS_SYSLIMITS_H
39 # include <sys/syslimits.h>
41 # error "No syslimits.h found"
49 static char *pidfile
= NULL
;
50 static char pidfile_path
[PATH_MAX
];
52 void printlog(int priority
, const char *format
, ...)
56 va_start (arg
, format
);
59 vsyslog(priority
,format
,arg
);
61 fprintf(stderr
,"%s: ",prog
);
62 vfprintf(stderr
,format
,arg
);
68 static void cleanup(void)
70 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
71 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
77 static void sig_handler(int sig
)
84 static void setsighandlers()
86 /* setting signal handlers.
87 * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
88 * ignores all the others signals which could cause termination. */
89 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
90 { SIGHUP
, "SIGHUP", 0 },
91 { SIGINT
, "SIGINT", 0 },
92 { SIGPIPE
, "SIGPIPE", 1 },
93 { SIGALRM
, "SIGALRM", 1 },
94 { SIGTERM
, "SIGTERM", 0 },
95 { SIGUSR1
, "SIGUSR1", 1 },
96 { SIGUSR2
, "SIGUSR2", 1 },
97 { SIGPROF
, "SIGPROF", 1 },
98 { SIGVTALRM
, "SIGVTALRM", 1 },
100 { SIGPOLL
, "SIGPOLL", 1 },
101 { SIGSTKFLT
, "SIGSTKFLT", 1 },
102 { SIGIO
, "SIGIO", 1 },
103 { SIGPWR
, "SIGPWR", 1 },
104 { SIGUNUSED
, "SIGUNUSED", 1 },
107 { SIGXCPU
, "SIGXCPU", 1 },
108 { SIGXFSZ
, "SIGXFSZ", 1 },
114 for(i
= 0; signals
[i
].sig
!= 0; i
++)
115 if(signal(signals
[i
].sig
,
116 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
117 perror("Setting handler");
120 struct pollfd pollv
[]={{0,POLLIN
|POLLHUP
},{0,POLLIN
|POLLHUP
},{0,POLLIN
|POLLHUP
}};
122 static void usage(void) {
123 fprintf(stderr
, "Usage: %s [OPTION]... tap_name\n\n", prog
);
124 fprintf(stderr
, " -p, --port=portnum Port number in the VDE switch\n"
125 " -g, --group=group Group for the socket\n"
126 " -m, --mode=mode Octal mode for the socket\n"
127 " -s, --sock=socket VDE switch control socket or dir\n"
128 " -d, --daemon Launch in background\n"
129 " -P, --pidfile=pidfile Create pidfile with our PID\n"
130 " -h, --help This help\n");
135 int open_tap(char *dev
)
140 if((fd
= open("/dev/net/tun", O_RDWR
)) < 0){
141 printlog(LOG_ERR
,"Failed to open /dev/net/tun %s",strerror(errno
));
144 memset(&ifr
, 0, sizeof(ifr
));
145 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
146 strncpy(ifr
.ifr_name
, dev
, sizeof(ifr
.ifr_name
) - 1);
147 /*printf("dev=\"%s\", ifr.ifr_name=\"%s\"\n", ifr.ifr_name, dev);*/
148 if(ioctl(fd
, TUNSETIFF
, (void *) &ifr
) < 0){
149 printlog(LOG_ERR
,"TUNSETIFF failed %s",strerror(errno
));
158 int open_tap(char *dev
)
161 int prefixlen
= strlen(TAP_PREFIX
);
164 fd
=open(dev
, O_RDWR
);
167 path
= malloc(strlen(dev
) + prefixlen
+ 1);
170 snprintf(path
, strlen(dev
) + prefixlen
+ 1, "%s%s", TAP_PREFIX
, dev
);
171 fd
=open(path
, O_RDWR
);
180 printlog(LOG_ERR
,"Failed to open tap device %s: %s", (*dev
== '/') ? dev
: path
, strerror(errno
));
187 unsigned char bufin
[BUFSIZE
];
189 static void save_pidfile()
191 if(pidfile
[0] != '/')
192 strncat(pidfile_path
, pidfile
, PATH_MAX
- strlen(pidfile_path
));
194 strcpy(pidfile_path
, pidfile
);
196 int fd
= open(pidfile_path
,
197 O_WRONLY
| O_CREAT
| O_EXCL
,
198 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
202 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
206 if((f
= fdopen(fd
, "w")) == NULL
) {
207 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
211 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
212 printlog(LOG_ERR
, "Error in writing pidfile");
219 int main(int argc
, char **argv
)
221 static char *sockname
=NULL
;
222 static char *tapname
=NULL
;
227 struct vde_open_args open_args
={.port
=0,.group
=NULL
,.mode
=0700};
231 int option_index
= 0;
233 static struct option long_options
[] = {
240 {"pidfile", 1, 0, 'P'},
243 c
= GETOPT_LONG (argc
, argv
, "hdP:p:s:m:g:",
244 long_options
, &option_index
);
250 open_args
.port
=atoi(optarg
);
251 if (open_args
.port
<= 0 || open_args
.port
> 255 )
252 usage(); //implies exit
256 usage(); //implies exit
260 sockname
=strdup(optarg
);
264 sscanf(optarg
,"%o",&(open_args
.mode
));
268 open_args
.group
=strdup(optarg
);
276 pidfile
=strdup(optarg
);
280 usage(); //implies exit
285 openlog(basename(prog
), LOG_PID
, 0);
287 syslog(LOG_INFO
,"VDE_PLUG2TAP started");
289 /* saves current path in pidfile_path, because otherwise with daemonize() we
291 if(getcwd(pidfile_path
, PATH_MAX
-1) == NULL
) {
292 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
295 strcat(pidfile_path
, "/");
296 if (daemonize
&& daemon(0, 1)) {
297 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
301 /* once here, we're sure we're the true process which will continue as a
302 * server: save PID file if needed */
303 if(pidfile
) save_pidfile();
306 tapname
=argv
[optind
];
310 tapfd
=open_tap(tapname
);
313 conn
=vde_open(sockname
,"vde_plug:",&open_args
);
318 pollv
[1].fd
=vde_datafd(conn
);
319 pollv
[2].fd
=vde_ctlfd(conn
);
322 result
=poll(pollv
,3,-1);
323 if ((pollv
[0].revents
| pollv
[1].revents
| pollv
[2].revents
) & POLLHUP
||
324 pollv
[2].revents
& POLLIN
)
326 if (pollv
[0].revents
& POLLIN
) {
327 nx
=read(tapfd
,bufin
,sizeof(bufin
));
328 /* if POLLIN but not data it means that the stream has been
329 * closed at the other end */
330 //fprintf(stderr,"%s: RECV %d %x %x \n",prog,nx,bufin[0],bufin[1]);
333 vde_send(conn
,bufin
,nx
,0);
335 if (pollv
[1].revents
& POLLIN
) {
336 nx
=vde_recv(conn
,bufin
,sizeof(bufin
),0);
339 write(tapfd
,bufin
,nx
);
340 //fprintf(stderr,"%s: SENT %d %x %x \n",prog,nx,bufin[0],bufin[1]);