2 * VDETELWEB: VDE telnet and WEB interface
6 * Copyright 2005 Renzo Davoli University of Bologna - Italy
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/select.h>
35 #include <sys/utsname.h>
37 #include <netinet/in.h>
38 #include <arpa/inet.h>
42 #include "vdetelweb.h"
56 struct pollfd pfd
[MAXFD
];
57 typedef int (*intfun
)();
61 #define ROOTCONFFILE "/etc/vde/vdetelwebrc"
63 void printlog(int priority
, const char *format
, ...)
67 va_start (arg
, format
);
70 vsyslog(priority
,format
,arg
);
72 fprintf(stderr
,"%s: ",progname
);
73 vfprintf(stderr
,format
,arg
);
80 static void cleanup(void)
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 { SIGPOLL
, "SIGPOLL", 1 },
105 { SIGPROF
, "SIGPROF", 1 },
106 { SIGVTALRM
, "SIGVTALRM", 1 },
107 { SIGSTKFLT
, "SIGSTKFLT", 1 },
108 { SIGIO
, "SIGIO", 1 },
109 { SIGPWR
, "SIGPWR", 1 },
110 { SIGUNUSED
, "SIGUNUSED", 1 },
115 for(i
= 0; signals
[i
].sig
!= 0; i
++)
116 if(signal(signals
[i
].sig
,
117 signals
[i
].ignore
? SIG_IGN
: sig_handler
) < 0)
118 perror("Setting handler");
121 static void usage(char *progname
) {
122 fprintf (stderr
,"Usage: %s [-w] [-t] [-d] [-n nodename] mgmt_socket\n"
123 " %s [--web] [--telnet] [--daemon] [--nodename nodename] mgmt_socket\n",progname
,progname
);
127 void setprompt(char *ctrl
,char *nodename
)
130 if (nodename
==NULL
) {
133 snprintf(buf
,BUFSIZE
,"VDE2@%s[%s]: ",un
.nodename
,ctrl
);
135 snprintf(buf
,BUFSIZE
,"VDE2@%s[%s]: ",nodename
,ctrl
);
139 int openvdem(char *mgmt
,char *progname
, struct netif
**nif
,char *nodename
)
141 struct sockaddr_un sun
;
143 char buf
[BUFSIZE
+1],*line2
,*ctrl
;
144 sun
.sun_family
=PF_UNIX
;
145 snprintf(sun
.sun_path
,UNIX_PATH_MAX
,"%s",mgmt
);
146 fd
=socket(PF_UNIX
,SOCK_STREAM
,0);
147 if (connect(fd
,(struct sockaddr
*)(&sun
),sizeof(sun
)) < 0) {
148 printlog(LOG_ERR
,"mgmt connect %s",strerror(errno
));
151 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
152 printlog(LOG_ERR
,"banner %s",strerror(errno
));
156 if ((ctrl
=rindex(buf
,'\n')) != NULL
)
159 write(fd
,"ds/showinfo\n",13);
160 if ((n
=read(fd
,buf
,BUFSIZE
))<=0) {
161 printlog(LOG_ERR
,"read ctl socket %s",strerror(errno
));
165 if ((line2
=index(buf
,'\n')) == NULL
) {
166 printlog(LOG_ERR
,"read ctl socket parse error 1");
170 if (strncmp(line2
,"ctl dir ",8) != 0) {
171 printlog(LOG_ERR
,"read ctl socket parse error");
174 for(ctrl
=line2
+8;*ctrl
!='\n' && ctrl
<buf
+n
;ctrl
++)
178 setprompt(ctrl
,nodename
);
180 *nif
=lwip_vdeif_add(ctrl
);
182 printlog(LOG_ERR
,"cannot connect to the switch");
190 static void bitno2mask(unsigned char *addr
,int bitno
,int len
)
193 for(i
=0;i
<len
;i
++,bitno
-= 8) {
199 addr
[i
]=256 - (1<<(8-bitno
));
203 static void sockaddr2ip_6addr(struct ip_addr
*ipaddrp
,unsigned char *addr
)
206 (addr
[0]<<8)|addr
[1],
207 (addr
[2]<<8)|addr
[3],
208 (addr
[4]<<8)|addr
[5],
209 (addr
[6]<<8)|addr
[7],
210 (addr
[8]<<8)|addr
[9],
211 (addr
[10]<<8)|addr
[11],
212 (addr
[12]<<8)|addr
[13],
213 (addr
[14]<<8)|addr
[15]);
216 static void readip(char *arg
,struct netif
*nif
,int af
)
218 char *bit
=rindex(arg
,'/');
220 printlog(LOG_ERR
,"ip addresses must include the netmask i.e. addr/maskbits");
222 int bitno
=atoi(bit
+1);
224 struct addrinfo
*res
,hint
;
225 struct ip_addr ipaddr
,netmask
;
227 memset(&hint
,0,sizeof(hint
));
229 if (err
=getaddrinfo(arg
,NULL
,&hint
,&res
))
230 printlog(LOG_ERR
,"ip address %s error %s",arg
,gai_strerror(err
));
232 switch(res
->ai_family
) {
234 struct sockaddr_in
*in
=(struct sockaddr_in
*)res
->ai_addr
;
235 int addrh
=ntohl(in
->sin_addr
.s_addr
);
236 unsigned char i
,addr
[4];
237 for (i
=0;i
<4;i
++,addrh
>>=8)
239 IP64_ADDR(&ipaddr
, addr
[0],addr
[1],addr
[2],addr
[3]);
240 bitno2mask(addr
,bitno
,4);
241 IP64_MASKADDR(&netmask
, addr
[0],addr
[1],addr
[2],addr
[3]);
242 lwip_add_addr(nif
,&ipaddr
,&netmask
);
246 struct sockaddr_in6
*in
=(struct sockaddr_in6
*)res
->ai_addr
;
248 unsigned char *addr
=in
->sin6_addr
.s6_addr
;
249 sockaddr2ip_6addr(&ipaddr
,addr
);
250 bitno2mask(addr
,bitno
,16);
251 sockaddr2ip_6addr(&netmask
,addr
);
252 lwip_add_addr(nif
,&ipaddr
,&netmask
);
256 printlog(LOG_ERR
,"unsupported Address Family: %s",arg
);
263 static void readdefroute(char *arg
,struct netif
*nif
,int af
)
265 struct addrinfo
*res
,hint
;
266 struct ip_addr ipaddr
,netmask
;
268 memset(&hint
,0,sizeof(hint
));
270 if (err
=getaddrinfo(arg
,NULL
,&hint
,&res
))
271 printlog(LOG_ERR
,"ip address %s error %s",arg
,gai_strerror(err
));
273 switch(res
->ai_family
) {
275 struct sockaddr_in
*in
=(struct sockaddr_in
*)res
->ai_addr
;
276 int addrh
=ntohl(in
->sin_addr
.s_addr
);
277 unsigned char i
,addr
[4];
278 for (i
=0;i
<4;i
++,addrh
>>=8)
280 IP64_ADDR(&ipaddr
, addr
[0],addr
[1],addr
[2],addr
[3]);
281 lwip_add_route(IP_ADDR_ANY
,IP_ADDR_ANY
,&ipaddr
,nif
,0);
285 struct sockaddr_in6
*in
=(struct sockaddr_in6
*)res
->ai_addr
;
287 sockaddr2ip_6addr(&ipaddr
,in
->sin6_addr
.s6_addr
);
288 lwip_add_route(IP_ADDR_ANY
,IP_ADDR_ANY
,&ipaddr
,nif
,0);
292 printlog(LOG_ERR
,"unsupported Address Family: %s",arg
);
298 static void readpassword(char *arg
,int unused
)
308 {"ip4",readip
,PF_INET
},
309 {"ip6",readip
,PF_INET6
},
311 {"defroute4",readdefroute
,PF_INET
},
312 {"defroute6",readdefroute
,PF_INET6
},
313 {"defroute",readdefroute
,0},
314 {"password",readpassword
,0},
317 int readconffile(char *path
,struct netif
*nif
)
320 char buf
[BUFSIZE
],*s
;
321 if (path
== NULL
&& geteuid() == 0)
325 if((f
=fopen(path
,"r"))==NULL
)
327 while (fgets(buf
,BUFSIZE
,f
) != NULL
) {
328 if ((s
=rindex(buf
,'\n')) != NULL
)
330 for(s
=buf
;*s
== ' ' || *s
== '\t';s
++)
334 for (scf
=cft
;scf
->tag
!= NULL
;scf
++)
335 if(strncmp(s
,scf
->tag
,strlen(scf
->tag
)) == 0){
337 for(;*s
== ' ' || *s
== '\t';s
++)
340 for(;*s
== ' ' || *s
== '\t';s
++)
342 scf
->f(s
,nif
,scf
->arg
);
345 if (scf
->tag
== NULL
) {
346 printlog(LOG_ERR
,"rc file syntax error: %s",buf
);
353 int addpfd(int fd
,intfun cb
)
357 pfd
[npfd
].events
=POLLIN
|POLLHUP
;
368 for (i
=fn
;i
<npfd
-1;i
++) {
371 status
[i
]=status
[i
+1];
376 int pfdsearch(int fd
)
379 for (i
=0;i
<npfd
&& pfd
[i
].fd
!=fd
;i
++)
384 int setfds(fd_set
*rds
, fd_set
*exc
)
389 for (i
=0;i
<npfd
;i
++) {
390 FD_SET(pfd
[i
].fd
,rds
);
391 FD_SET(pfd
[i
].fd
,exc
);
392 if (pfd
[i
].fd
>max
) max
=pfd
[i
].fd
;
397 int main(int argc
, char *argv
[])
407 int option_index
= 0;
409 static struct option long_options
[] = {
410 {"daemon", 0, 0, 'd'},
412 {"telnet", 0, 0, 't'},
416 {"nodename",1,0,'n'},
419 c
= getopt_long_only (argc
, argv
, "hdwtM:f:n:",
420 long_options
, &option_index
);
429 conffile
=strdup(optarg
);
432 nodename
=strdup(optarg
);
444 usage(argv
[0]); //implies exit
448 if (optind
< argc
&& mgmt
==NULL
)
452 printlog(LOG_ERR
,"mgmt_socket not defined");
455 if (telnet
==0 && web
==0) {
456 printlog(LOG_ERR
,"at least one service option (-t -w) must be specified");
460 vdefd
=openvdem(mgmt
,argv
[0],&nif
,nodename
);
461 if (readconffile(conffile
,nif
) < 0) {
462 printlog(LOG_ERR
,"configuration file not found");
465 if (daemonize
&& daemon(0, 1)) {
466 printlog(LOG_ERR
,"daemon: %s",strerror(errno
));
478 int max
=setfds(&rds
,&exc
);
479 m
=lwip_select(max
,&rds
,NULL
,&exc
,NULL
);
480 for(i
=0; m
>0 && i
<max
; i
++) {
481 if (FD_ISSET(i
,&rds
) || FD_ISSET(i
,&exc
)) {