1 /* Copyright 2006 Renzo Davoli
2 * from vde_plug Davoli Gardenghi
13 #include <sys/ioctl.h>
14 #include <sys/types.h>
25 #include <vdecommon.h>
26 #include <libvdeplug.h>
32 #include <linux/if_tun.h>
36 #include <sys/socket.h>
38 #include <net/if_tun.h>
41 #if defined(VDE_DARWIN) || defined(VDE_FREEBSD)
42 # define TAP_PREFIX "/dev/"
43 # if defined HAVE_SYSLIMITS_H
44 # include <syslimits.h>
45 # elif defined HAVE_SYS_SYSLIMITS_H
46 # include <sys/syslimits.h>
48 # error "No syslimits.h found"
56 static char *pidfile
= NULL
;
57 static char pidfile_path
[PATH_MAX
];
59 void printlog(int priority
, const char *format
, ...)
63 va_start (arg
, format
);
66 vsyslog(priority
,format
,arg
);
68 fprintf(stderr
,"%s: ",prog
);
69 vfprintf(stderr
,format
,arg
);
75 static void cleanup(void)
77 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
78 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
84 static void sig_handler(int sig
)
91 static void setsighandlers()
93 /* setting signal handlers.
94 * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
95 * ignores all the others signals which could cause termination. */
96 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
97 { SIGHUP
, "SIGHUP", 0 },
98 { SIGINT
, "SIGINT", 0 },
99 { SIGPIPE
, "SIGPIPE", 1 },
100 { SIGALRM
, "SIGALRM", 1 },
101 { SIGTERM
, "SIGTERM", 0 },
102 { SIGUSR1
, "SIGUSR1", 1 },
103 { SIGUSR2
, "SIGUSR2", 1 },
104 { SIGPROF
, "SIGPROF", 1 },
105 { SIGVTALRM
, "SIGVTALRM", 1 },
107 { SIGPOLL
, "SIGPOLL", 1 },
109 { SIGSTKFLT
, "SIGSTKFLT", 1 },
111 { SIGIO
, "SIGIO", 1 },
112 { SIGPWR
, "SIGPWR", 1 },
114 { SIGUNUSED
, "SIGUNUSED", 1 },
118 { SIGXCPU
, "SIGXCPU", 1 },
119 { SIGXFSZ
, "SIGXFSZ", 1 },
125 for(i
= 0; signals
[i
].sig
!= 0; i
++)
126 if(signal(signals
[i
].sig
,
127 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
128 perror("Setting handler");
131 struct pollfd pollv
[]={{0,POLLIN
|POLLHUP
},{0,POLLIN
|POLLHUP
},{0,POLLIN
|POLLHUP
}};
133 static void usage(void) {
134 fprintf(stderr
, "Usage: %s [OPTION]... tap_name\n\n", prog
);
135 fprintf(stderr
, " -p, --port=portnum Port number in the VDE switch\n"
136 " -g, --group=group Group for the socket\n"
137 " -m, --mode=mode Octal mode for the socket\n"
138 " -s, --sock=socket VDE switch control socket or dir\n"
139 " -d, --daemon Launch in background\n"
140 " -P, --pidfile=pidfile Create pidfile with our PID\n"
141 " -h, --help This help\n");
146 int open_tap(char *dev
)
151 if((fd
= open("/dev/net/tun", O_RDWR
)) < 0){
152 printlog(LOG_ERR
,"Failed to open /dev/net/tun %s",strerror(errno
));
155 memset(&ifr
, 0, sizeof(ifr
));
156 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
157 strncpy(ifr
.ifr_name
, dev
, sizeof(ifr
.ifr_name
) - 1);
158 /*printf("dev=\"%s\", ifr.ifr_name=\"%s\"\n", ifr.ifr_name, dev);*/
159 if(ioctl(fd
, TUNSETIFF
, (void *) &ifr
) < 0){
160 printlog(LOG_ERR
,"TUNSETIFF failed %s",strerror(errno
));
168 #if defined(VDE_DARWIN) || defined(VDE_FREEBSD)
169 int open_tap(char *dev
)
172 int prefixlen
= strlen(TAP_PREFIX
);
175 fd
=open(dev
, O_RDWR
);
178 path
= malloc(strlen(dev
) + prefixlen
+ 1);
181 snprintf(path
, strlen(dev
) + prefixlen
+ 1, "%s%s", TAP_PREFIX
, dev
);
182 fd
=open(path
, O_RDWR
);
191 printlog(LOG_ERR
,"Failed to open tap device %s: %s", (*dev
== '/') ? dev
: path
, strerror(errno
));
198 unsigned char bufin
[BUFSIZE
];
200 static void save_pidfile()
202 if(pidfile
[0] != '/')
203 strncat(pidfile_path
, pidfile
, PATH_MAX
- strlen(pidfile_path
));
205 strcpy(pidfile_path
, pidfile
);
207 int fd
= open(pidfile_path
,
208 O_WRONLY
| O_CREAT
| O_EXCL
,
209 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
213 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
217 if((f
= fdopen(fd
, "w")) == NULL
) {
218 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
222 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
223 printlog(LOG_ERR
, "Error in writing pidfile");
230 int main(int argc
, char **argv
)
232 static char *sockname
=NULL
;
233 static char *tapname
=NULL
;
238 struct vde_open_args open_args
={.port
=0,.group
=NULL
,.mode
=0700};
242 int option_index
= 0;
244 static struct option long_options
[] = {
251 {"pidfile", 1, 0, 'P'},
254 c
= GETOPT_LONG (argc
, argv
, "hdP:p:s:m:g:",
255 long_options
, &option_index
);
261 open_args
.port
=atoi(optarg
);
262 if (open_args
.port
<= 0)
263 usage(); //implies exit
267 usage(); //implies exit
271 sockname
=strdup(optarg
);
275 sscanf(optarg
,"%o",&(open_args
.mode
));
279 open_args
.group
=strdup(optarg
);
287 pidfile
=strdup(optarg
);
291 usage(); //implies exit
296 openlog(basename(prog
), LOG_PID
, 0);
298 syslog(LOG_INFO
,"VDE_PLUG2TAP started");
300 /* saves current path in pidfile_path, because otherwise with daemonize() we
302 if(getcwd(pidfile_path
, PATH_MAX
-1) == NULL
) {
303 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
306 strcat(pidfile_path
, "/");
307 if (daemonize
&& daemon(0, 0)) {
308 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
312 /* once here, we're sure we're the true process which will continue as a
313 * server: save PID file if needed */
314 if(pidfile
) save_pidfile();
317 tapname
=argv
[optind
];
319 usage(); // implies exit
324 tapfd
=open_tap(tapname
);
327 conn
=vde_open(sockname
,"vde_plug:",&open_args
);
332 pollv
[1].fd
=vde_datafd(conn
);
333 pollv
[2].fd
=vde_ctlfd(conn
);
336 result
=poll(pollv
,3,-1);
337 if ((pollv
[0].revents
| pollv
[1].revents
| pollv
[2].revents
) & POLLHUP
||
338 pollv
[2].revents
& POLLIN
)
340 if (pollv
[0].revents
& POLLIN
) {
341 nx
=read(tapfd
,bufin
,sizeof(bufin
));
342 /* if POLLIN but not data it means that the stream has been
343 * closed at the other end */
344 //fprintf(stderr,"%s: RECV %d %x %x \n",prog,nx,bufin[0],bufin[1]);
347 vde_send(conn
,bufin
,nx
,0);
349 if (pollv
[1].revents
& POLLIN
) {
350 nx
=vde_recv(conn
,bufin
,sizeof(bufin
),0);
353 write(tapfd
,bufin
,nx
);
354 //fprintf(stderr,"%s: SENT %d %x %x \n",prog,nx,bufin[0],bufin[1]);