1 /* Copyright 2006 Renzo Davoli
2 * from vde_plug Davoli Gardenghi
3 * Modified 2010 Renzo Davoli, vdestream added
4 * Licensed under the GPLv2
15 #include <sys/ioctl.h>
16 #include <sys/types.h>
27 #include <vdecommon.h>
28 #include <libvdeplug.h>
34 #include <linux/if_tun.h>
38 #include <sys/socket.h>
40 #include <net/if_tun.h>
43 #if defined(VDE_DARWIN) || defined(VDE_FREEBSD)
44 # define TAP_PREFIX "/dev/"
45 # if defined HAVE_SYSLIMITS_H
46 # include <syslimits.h>
47 # elif defined HAVE_SYS_SYSLIMITS_H
48 # include <sys/syslimits.h>
50 # error "No syslimits.h found"
59 static char *pidfile
= NULL
;
60 static char pidfile_path
[PATH_MAX
];
62 void printlog(int priority
, const char *format
, ...)
66 va_start (arg
, format
);
69 vsyslog(priority
,format
,arg
);
71 fprintf(stderr
,"%s: ",prog
);
72 vfprintf(stderr
,format
,arg
);
78 static void cleanup(void)
80 if((pidfile
!= NULL
) && unlink(pidfile_path
) < 0) {
81 printlog(LOG_WARNING
,"Couldn't remove pidfile '%s': %s", pidfile
, strerror(errno
));
84 if (vdestream
!= NULL
)
85 vdestream_close(vdestream
);
90 static void sig_handler(int sig
)
100 static void setsighandlers()
102 /* setting signal handlers.
103 * sets clean termination for SIGHUP, SIGINT and SIGTERM, and simply
104 * ignores all the others signals which could cause termination. */
105 struct { int sig
; const char *name
; int ignore
; } signals
[] = {
106 { SIGHUP
, "SIGHUP", 0 },
107 { SIGINT
, "SIGINT", 0 },
108 { SIGPIPE
, "SIGPIPE", 1 },
109 { SIGALRM
, "SIGALRM", 1 },
110 { SIGTERM
, "SIGTERM", 0 },
111 { SIGUSR1
, "SIGUSR1", 1 },
112 { SIGUSR2
, "SIGUSR2", 1 },
113 { SIGPROF
, "SIGPROF", 1 },
114 { SIGVTALRM
, "SIGVTALRM", 1 },
116 { SIGPOLL
, "SIGPOLL", 1 },
118 { SIGSTKFLT
, "SIGSTKFLT", 1 },
120 { SIGIO
, "SIGIO", 1 },
121 { SIGPWR
, "SIGPWR", 1 },
123 { SIGUNUSED
, "SIGUNUSED", 1 },
127 { SIGXCPU
, "SIGXCPU", 1 },
128 { SIGXFSZ
, "SIGXFSZ", 1 },
134 for(i
= 0; signals
[i
].sig
!= 0; i
++)
135 if(signal(signals
[i
].sig
,
136 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
137 perror("Setting handler");
140 static void usage(void) {
141 fprintf(stderr
, "Usage: %s [OPTION]... tap_name\n\n", prog
);
142 fprintf(stderr
, " -p, --port=portnum Port number in the VDE switch\n"
143 " -g, --group=group Group for the socket\n"
144 " -m, --mode=mode Octal mode for the socket\n"
145 " -s, --sock=socket VDE switch control socket or dir\n"
146 " -d, --daemon Launch in background\n"
147 " -P, --pidfile=pidfile Create pidfile with our PID\n"
148 " -h, --help This help\n");
153 int open_tap(char *dev
)
158 if((fd
= open("/dev/net/tun", O_RDWR
)) < 0){
159 printlog(LOG_ERR
,"Failed to open /dev/net/tun %s",strerror(errno
));
162 memset(&ifr
, 0, sizeof(ifr
));
163 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
164 strncpy(ifr
.ifr_name
, dev
, sizeof(ifr
.ifr_name
) - 1);
165 /*printf("dev=\"%s\", ifr.ifr_name=\"%s\"\n", ifr.ifr_name, dev);*/
166 if(ioctl(fd
, TUNSETIFF
, (void *) &ifr
) < 0){
167 printlog(LOG_ERR
,"TUNSETIFF failed %s",strerror(errno
));
175 #if defined(VDE_DARWIN) || defined(VDE_FREEBSD)
176 int open_tap(char *dev
)
179 int prefixlen
= strlen(TAP_PREFIX
);
182 fd
=open(dev
, O_RDWR
);
185 path
= malloc(strlen(dev
) + prefixlen
+ 1);
188 snprintf(path
, strlen(dev
) + prefixlen
+ 1, "%s%s", TAP_PREFIX
, dev
);
189 fd
=open(path
, O_RDWR
);
198 printlog(LOG_ERR
,"Failed to open tap device %s: %s", (*dev
== '/') ? dev
: path
, strerror(errno
));
205 unsigned char bufin
[BUFSIZE
];
207 static void save_pidfile()
209 if(pidfile
[0] != '/')
210 strncat(pidfile_path
, pidfile
, PATH_MAX
- strlen(pidfile_path
));
212 strcpy(pidfile_path
, pidfile
);
214 int fd
= open(pidfile_path
,
215 O_WRONLY
| O_CREAT
| O_EXCL
,
216 S_IRUSR
| S_IWUSR
| S_IRGRP
| S_IROTH
);
220 printlog(LOG_ERR
, "Error in pidfile creation: %s", strerror(errno
));
224 if((f
= fdopen(fd
, "w")) == NULL
) {
225 printlog(LOG_ERR
, "Error in FILE* construction: %s", strerror(errno
));
229 if(fprintf(f
, "%ld\n", (long int)getpid()) <= 0) {
230 printlog(LOG_ERR
, "Error in writing pidfile");
237 static ssize_t
vde_plug2tap_recv(void *opaque
, void *buf
, size_t count
)
240 return write(*tapfdp
,buf
,count
);
243 int main(int argc
, char **argv
)
245 static char *sockname
=NULL
;
246 static char *tapname
=NULL
;
250 struct vde_open_args open_args
={.port
=0,.group
=NULL
,.mode
=0700};
252 static struct pollfd pollv
[]={{0,POLLIN
|POLLHUP
},
259 int option_index
= 0;
261 static struct option long_options
[] = {
268 {"pidfile", 1, 0, 'P'},
271 c
= GETOPT_LONG (argc
, argv
, "hdP:p:s:m:g:",
272 long_options
, &option_index
);
278 open_args
.port
=atoi(optarg
);
279 if (open_args
.port
<= 0)
280 usage(); //implies exit
284 usage(); //implies exit
288 sockname
=strdup(optarg
);
292 sscanf(optarg
,"%o",(unsigned int *)&(open_args
.mode
));
296 open_args
.group
=strdup(optarg
);
304 pidfile
=strdup(optarg
);
308 usage(); //implies exit
313 openlog(basename(prog
), LOG_PID
, 0);
315 syslog(LOG_INFO
,"VDE_PLUG2TAP started");
317 /* saves current path in pidfile_path, because otherwise with daemonize() we
319 if(getcwd(pidfile_path
, PATH_MAX
-1) == NULL
) {
320 printlog(LOG_ERR
, "getcwd: %s", strerror(errno
));
323 strcat(pidfile_path
, "/");
324 if (daemonize
&& daemon(0, 0)) {
325 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
329 /* once here, we're sure we're the true process which will continue as a
330 * server: save PID file if needed */
331 if(pidfile
) save_pidfile();
334 tapname
=argv
[optind
];
336 usage(); // implies exit
341 tapfd
=open_tap(tapname
);
346 if (sockname
==NULL
|| strcmp(sockname
,"-") != 0) {
347 conn
=vde_open(sockname
,"vde_plug2tap:",&open_args
);
349 printlog(LOG_ERR
,"vde_open %s: %s",sockname
?sockname
:"DEF_SWITCH",strerror(errno
));
352 pollv
[1].fd
=vde_datafd(conn
);
353 pollv
[2].fd
=vde_ctlfd(conn
);
356 vdestream
=vdestream_open(&tapfd
,STDOUT_FILENO
,vde_plug2tap_recv
,NULL
);
357 if (vdestream
== NULL
)
359 pollv
[1].fd
=STDIN_FILENO
;
365 if ((pollv
[0].revents
| pollv
[1].revents
| pollv
[2].revents
) & POLLHUP
||
366 (npollv
> 2 && pollv
[2].revents
& POLLIN
))
368 if (pollv
[0].revents
& POLLIN
) {
369 nx
=read(tapfd
,bufin
,sizeof(bufin
));
370 /* if POLLIN but not data it means that the stream has been
371 * closed at the other end */
372 //fprintf(stderr,"%s: RECV %d %x %x \n",prog,nx,bufin[0],bufin[1]);
376 vde_send(conn
,bufin
,nx
,0);
378 vdestream_send(vdestream
, bufin
, nx
);
380 if (pollv
[1].revents
& POLLIN
) {
382 nx
=vde_recv(conn
,bufin
,sizeof(bufin
),0);
385 write(tapfd
,bufin
,nx
);
387 nx
=read(STDIN_FILENO
,bufin
,sizeof(bufin
));
390 vdestream_recv(vdestream
,bufin
,nx
);
392 //fprintf(stderr,"%s: SENT %d %x %x \n",prog,nx,bufin[0],bufin[1]);