DragonFly submit List (threaded) for 2005-03
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
[
Date Index][
Thread Index]
[PATCH]sbin/nos-tun WARNS=6, style
WARNS=6, style clean.
a brief comment at the beginning of the patch further states what the
patch does.
please review it.
1) bump up warn level to 6
2) reorganize "include" according to style(9)
3) change function name upper case initail letter to lower case
4) clean up global variable white space
5) move function name to a seperate line, where function is defined
6) remove obvious comments, fix some comments
7) rename local-varible/function-parameter to avoid name collision
8) make certain function parameters const when function operation does not change them
9) add white space between "if" and "(". add white space between function calls parameter list.
10) remove unnecessary "{" "}" after "if"
11) change "usage" string according to style and man page
12) 2 white space indent -> 1 tab indent
Index: Makefile
===================================================================
RCS file: /opt/df_cvs/src/sbin/nos-tun/Makefile,v
retrieving revision 1.2
diff -u -r1.2 Makefile
--- Makefile 17 Jun 2003 04:27:34 -0000 1.2
+++ Makefile 18 Mar 2005 13:12:59 -0000
@@ -1,8 +1,8 @@
# $FreeBSD: src/sbin/nos-tun/Makefile,v 1.1.6.1 2001/04/25 10:58:45 ru Exp $
# $DragonFly: src/sbin/nos-tun/Makefile,v 1.2 2003/06/17 04:27:34 dillon Exp $
+WARNS?= 6
PROG= nos-tun
MAN= nos-tun.8
.include <bsd.prog.mk>
-
Index: nos-tun.c
===================================================================
RCS file: /opt/df_cvs/src/sbin/nos-tun/nos-tun.c,v
retrieving revision 1.5
diff -u -r1.5 nos-tun.c
--- nos-tun.c 18 Dec 2004 21:43:39 -0000 1.5
+++ nos-tun.c 18 Mar 2005 13:12:59 -0000
@@ -54,315 +54,300 @@
* Mar. 23 1999 by Isao SEKI <iseki@xxxxxxxxxx>
* I added a new flag for ip protocol number.
* We are using 4 as protocol number in ampr.org.
- *
*/
-#include <fcntl.h>
-#include <netdb.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <syslog.h>
+#include <sys/types.h>
#include <sys/signal.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
+
#include <netinet/in.h>
#include <netinet/in_systm.h>
#include <netinet/ip.h>
#include <net/if.h>
#include <arpa/inet.h>
+
+#include <fcntl.h>
+#include <netdb.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <syslog.h>
#include <unistd.h>
-/* Tunnel interface configuration stuff */
-static struct ifaliasreq ifra;
-static struct ifreq ifrq;
+static struct ifaliasreq ifra;
+static struct ifreq ifrq;
+
+static int net; /* socket descriptor */
+static int tun; /* tunnel descriptor */
-/* Global descriptors */
-int net; /* socket descriptor */
-int tun; /* tunnel descriptor */
+static void usage(void);
-static void usage(void);
-int Set_address(char *addr, struct sockaddr_in *sin)
+static int
+set_address(const char *addr, struct sockaddr_in *in)
{
- struct hostent *hp;
+ struct hostent *hp;
- bzero((char *)sin, sizeof(struct sockaddr));
- sin->sin_family = AF_INET;
- if((sin->sin_addr.s_addr = inet_addr(addr)) == (u_long)-1) {
- hp = gethostbyname(addr);
- if (!hp) {
- syslog(LOG_ERR,"unknown host %s", addr);
- return 1;
- }
- sin->sin_family = hp->h_addrtype;
- bcopy(hp->h_addr, (caddr_t)&sin->sin_addr, hp->h_length);
- }
- return 0;
+ bzero(in, sizeof(struct sockaddr));
+ in->sin_family = AF_INET;
+ if ((in->sin_addr.s_addr = inet_addr(addr)) == INADDR_NONE) {
+ hp = gethostbyname(addr);
+ if (!hp) {
+ syslog(LOG_ERR, "unknown host %s", addr);
+ return 1;
+ }
+ in->sin_family = hp->h_addrtype;
+ bcopy(hp->h_addr, &in->sin_addr, hp->h_length);
+ }
+ return 0;
}
-int tun_open(char *devname, struct sockaddr *ouraddr, char *theiraddr)
+static int
+tun_open(const char *dev_name, const struct sockaddr *ouraddr,
+ const char *theiraddr)
{
- int s;
- struct sockaddr_in *sin;
+ int s;
+ struct sockaddr_in *in;
+
+ /* Open tun device */
+ tun = open(dev_name, O_RDWR);
+ if (tun < 0) {
+ syslog(LOG_ERR, "can't open %s - %m", dev_name);
+ return 1;
+ }
+
+ /* Name the interface */
+ bzero(&ifra, sizeof(ifra));
+ bzero(&ifrq, sizeof(ifrq));
+ strncpy(ifra.ifra_name, dev_name + 5, IFNAMSIZ);
+ strncpy(ifrq.ifr_name, dev_name + 5, IFNAMSIZ);
+
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0) {
+ syslog(LOG_ERR, "can't open socket - %m");
+ goto tunc_return;
+ }
+
+ /*
+ * Delete (previous) addresses for interface
+ *
+ * XXX
+ * On FreeBSD this ioctl returns error
+ * when tunN have no addresses, so - log and ignore it.
+ */
+ if (ioctl(s, SIOCDIFADDR, &ifra) < 0)
+ syslog(LOG_ERR, "SIOCDIFADDR - %m");
+
+ /* Set interface address */
+ in = (struct sockaddr_in *)&ifra.ifra_addr;
+ bcopy(ouraddr, in, sizeof(struct sockaddr_in));
+ in->sin_len = sizeof(*in);
+
+ /* Set destination address */
+ in = (struct sockaddr_in *)&ifra.ifra_broadaddr;
+ if (set_address(theiraddr, in)) {
+ syslog(LOG_ERR, "bad destination address: %s", theiraddr);
+ goto stunc_return;
+ }
+ in->sin_len = sizeof(*in);
+
+ if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
+ syslog(LOG_ERR, "can't set interface address - %m");
+ goto stunc_return;
+ }
+
+ /* Bring up the interface */
+ if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
+ syslog(LOG_ERR, "can't get interface flags - %m");
+ goto stunc_return;
+ }
+ ifrq.ifr_flags |= IFF_UP;
+ if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
+ close(s);
+ return 0;
+ }
+ syslog(LOG_ERR, "can't set interface UP - %m");
- /* Open tun device */
- tun = open (devname, O_RDWR);
- if (tun < 0) {
- syslog(LOG_ERR,"can't open %s - %m",devname);
- return(1);
- }
-
- /*
- * At first, name the interface.
- */
- bzero((char *)&ifra, sizeof(ifra));
- bzero((char *)&ifrq, sizeof(ifrq));
-
- strncpy(ifrq.ifr_name, devname+5, IFNAMSIZ);
- strncpy(ifra.ifra_name, devname+5, IFNAMSIZ);
-
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0) {
- syslog(LOG_ERR,"can't open socket - %m");
- goto tunc_return;
- }
-
- /*
- * Delete (previous) addresses for interface
- *
- * !!!!
- * On FreeBSD this ioctl returns error
- * when tunN have no addresses, so - log and ignore it.
- *
- */
- if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
- syslog(LOG_ERR,"SIOCDIFADDR - %m");
- }
-
- /*
- * Set interface address
- */
- sin = (struct sockaddr_in *)&(ifra.ifra_addr);
- bcopy(ouraddr, sin, sizeof(struct sockaddr_in));
- sin->sin_len = sizeof(*sin);
-
- /*
- * Set destination address
- */
- sin = (struct sockaddr_in *)&(ifra.ifra_broadaddr);
- if(Set_address(theiraddr,sin)) {
- syslog(LOG_ERR,"bad destination address: %s",theiraddr);
- goto stunc_return;
- }
- sin->sin_len = sizeof(*sin);
-
- if (ioctl(s, SIOCAIFADDR, &ifra) < 0) {
- syslog(LOG_ERR,"can't set interface address - %m");
- goto stunc_return;
- }
-
- /*
- * Now, bring up the interface.
- */
- if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
- syslog(LOG_ERR,"can't get interface flags - %m");
- goto stunc_return;
- }
-
- ifrq.ifr_flags |= IFF_UP;
- if (!(ioctl(s, SIOCSIFFLAGS, &ifrq) < 0)) {
- close(s);
- return(0);
- }
- syslog(LOG_ERR,"can't set interface UP - %m");
stunc_return:
- close(s);
+ close(s);
tunc_return:
- close(tun);
- return(1);
+ close(tun);
+ return 1;
}
-void Finish(int signum)
+static void
+finish(int signum)
{
- int s;
+ int s;
- syslog(LOG_INFO,"exiting");
+ syslog(LOG_INFO, "exiting");
- close(net);
+ close(net);
+
+ s = socket(AF_INET, SOCK_DGRAM, 0);
+ if (s < 0) {
+ syslog(LOG_ERR, "can't open socket - %m");
+ goto closing_tun;
+ }
+
+ /* Shut down interface */
+ if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
+ syslog(LOG_ERR, "can't get interface flags - %m");
+ goto closing_fds;
+ }
+ ifrq.ifr_flags &= ~(IFF_UP | IFF_RUNNING);
+ if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
+ syslog(LOG_ERR,"can't set interface DOWN - %m");
+ goto closing_fds;
+ }
+
+ /* Delete addresses for interface */
+ bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
+ bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
+ bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
+ if (ioctl(s, SIOCDIFADDR, &ifra) < 0)
+ syslog(LOG_ERR, "can't delete interface's addresses - %m");
- s = socket(AF_INET, SOCK_DGRAM, 0);
- if (s < 0) {
- syslog(LOG_ERR,"can't open socket - %m");
- goto closing_tun;
- }
-
- /*
- * Shut down interface.
- */
- if (ioctl(s, SIOCGIFFLAGS, &ifrq) < 0) {
- syslog(LOG_ERR,"can't get interface flags - %m");
- goto closing_fds;
- }
-
- ifrq.ifr_flags &= ~(IFF_UP|IFF_RUNNING);
- if (ioctl(s, SIOCSIFFLAGS, &ifrq) < 0) {
- syslog(LOG_ERR,"can't set interface DOWN - %m");
- goto closing_fds;
- }
-
- /*
- * Delete addresses for interface
- */
- bzero(&ifra.ifra_addr, sizeof(ifra.ifra_addr));
- bzero(&ifra.ifra_broadaddr, sizeof(ifra.ifra_addr));
- bzero(&ifra.ifra_mask, sizeof(ifra.ifra_addr));
- if (ioctl(s, SIOCDIFADDR, &ifra) < 0) {
- syslog(LOG_ERR,"can't delete interface's addresses - %m");
- }
closing_fds:
- close(s);
+ close(s);
closing_tun:
- close(tun);
- closelog();
- exit(signum);
+ close(tun);
+ closelog();
+ exit(signum);
}
-int main (int argc, char **argv)
+int
+main(int argc, char **argv)
{
- int c, len, ipoff;
+ int c, len, ipoff;
- char *devname = NULL;
- char *point_to = NULL;
- char *to_point = NULL;
- char *target;
- char *protocol = NULL;
- int protnum;
-
- struct sockaddr t_laddr; /* Source address of tunnel */
- struct sockaddr whereto; /* Destination of tunnel */
- struct sockaddr_in *to;
-
- char buf[0x2000]; /* Packets buffer */
- struct ip *ip = (struct ip *)buf;
-
- fd_set rfds, wfds, efds; /* File descriptors for select() */
- int nfds; /* Return from select() */
-
-
- while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
- switch (c) {
- case 'd':
- to_point = optarg;
- break;
- case 's':
- point_to = optarg;
- break;
- case 't':
- devname = optarg;
- break;
- case 'p':
- protocol = optarg;
- break;
- }
- }
- argc -= optind;
- argv += optind;
-
- if (argc != 1 || (devname == NULL) ||
- (point_to == NULL) || (to_point == NULL)) {
- usage();
- }
-
- if(protocol == NULL)
- protnum = 94;
- else
- protnum = atoi(protocol);
-
- target = *argv;
-
- /* Establish logging through 'syslog' */
- openlog("nos-tun", LOG_PID, LOG_DAEMON);
-
- if(Set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
- closelog();
- exit(2);
- }
-
- if(tun_open(devname, &t_laddr, to_point)) {
- closelog();
- exit(3);
- }
-
- to = (struct sockaddr_in *)&whereto;
- if(Set_address(target, to))
- Finish(4);
-
- if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
- syslog(LOG_ERR,"can't open socket - %m");
- Finish(5);
- }
-
- if (connect(net,&whereto,sizeof(struct sockaddr_in)) < 0 ) {
- syslog(LOG_ERR,"can't connect to target - %m");
- close(net);
- Finish(6);
- }
-
- /* Demonize it */
- daemon(0,0);
-
- /* Install signal handlers */
- signal(SIGHUP,Finish);
- signal(SIGINT,Finish);
- signal(SIGTERM,Finish);
-
- for (;;) {
- /* Set file descriptors for select() */
- FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds);
- FD_SET(tun,&rfds); FD_SET(net,&rfds);
-
- nfds = select(net+10,&rfds,&wfds,&efds,NULL);
- if(nfds < 0) {
- syslog(LOG_ERR,"interrupted select");
- close(net);
- Finish(7);
- }
- if(nfds == 0) { /* Impossible ? */
- syslog(LOG_ERR,"timeout in select");
- close(net);
- Finish(8);
- }
-
-
- if(FD_ISSET(net,&rfds)) {
- /* Read from socket ... */
- len = read(net, buf, sizeof(buf));
- /* Check if this is "our" packet */
- if((ip->ip_src).s_addr == (to->sin_addr).s_addr) {
- /* ... skip encapsulation headers ... */
- ipoff = (ip->ip_hl << 2);
- /* ... and write to tun-device */
- write(tun,buf+ipoff,len-ipoff);
- }
- }
-
- if(FD_ISSET(tun,&rfds)) {
- /* Read from tun ... */
- len = read(tun, buf, sizeof(buf));
- /* ... and send to network */
- if(send(net, buf, len,0) <= 0) {
- syslog(LOG_ERR,"can't send - %m");
- }
- }
- }
+ char *dev_name = NULL;
+ char *point_to = NULL;
+ char *to_point = NULL;
+ char *target;
+ char *protocol = NULL;
+ int protnum;
+
+ struct sockaddr t_laddr; /* Source address of tunnel */
+ struct sockaddr whereto; /* Destination of tunnel */
+ struct sockaddr_in *to;
+
+ char buf[0x2000]; /* Packets buffer */
+ struct ip *ip = (struct ip *)buf;
+
+ fd_set rfds, wfds, efds;
+ int nfds;
+
+
+ while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) {
+ switch (c) {
+ case 'd':
+ to_point = optarg;
+ break;
+ case 's':
+ point_to = optarg;
+ break;
+ case 't':
+ dev_name = optarg;
+ break;
+ case 'p':
+ protocol = optarg;
+ break;
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc != 1 || dev_name == NULL ||
+ point_to == NULL || to_point == NULL)
+ usage();
+
+ if (protocol == NULL)
+ protnum = 94;
+ else
+ protnum = atoi(protocol);
+
+ target = *argv;
+
+ openlog("nos-tun", LOG_PID, LOG_DAEMON);
+
+ if (set_address(point_to, (struct sockaddr_in *)&t_laddr)) {
+ closelog();
+ exit(2);
+ }
+
+ if(tun_open(dev_name, &t_laddr, to_point)) {
+ closelog();
+ exit(3);
+ }
+
+ to = (struct sockaddr_in *)&whereto;
+ if(set_address(target, to))
+ finish(4);
+
+ if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) {
+ syslog(LOG_ERR, "can't open socket - %m");
+ finish(5);
+ }
+
+ if (connect(net, &whereto, sizeof(struct sockaddr_in)) < 0 ) {
+ syslog(LOG_ERR, "can't connect to target - %m");
+ close(net);
+ finish(6);
+ }
+
+ daemon(0, 0);
+
+ signal(SIGHUP, finish);
+ signal(SIGINT, finish);
+ signal(SIGTERM, finish);
+
+ for (;;) {
+ FD_ZERO(&rfds);
+ FD_ZERO(&wfds);
+ FD_ZERO(&efds);
+ FD_SET(tun, &rfds);
+ FD_SET(net, &rfds);
+
+ nfds = select(net + 10, &rfds, &wfds, &efds, NULL);
+ if (nfds < 0) {
+ syslog(LOG_ERR,"interrupted select");
+ close(net);
+ finish(7);
+ }
+ if (nfds == 0) { /* XXX Impossible ? */
+ syslog(LOG_ERR, "timeout in select");
+ close(net);
+ finish(8);
+ }
+
+ if (FD_ISSET(net,&rfds)) {
+ /* Read from network ... */
+ len = read(net, buf, sizeof(buf));
+ /* Check if this is "our" packet */
+ if (ip->ip_src.s_addr == to->sin_addr.s_addr) {
+ /* ... skip encapsulation headers ... */
+ ipoff = (ip->ip_hl << 2);
+ /* ... and write to tun-device */
+ write(tun, buf+ipoff, len-ipoff);
+ }
+ }
+
+ if(FD_ISSET(tun,&rfds)) {
+ /* Read from tun ... */
+ len = read(tun, buf, sizeof(buf));
+ /* ... and send to network */
+ if(send(net, buf, len,0) <= 0)
+ syslog(LOG_ERR,"can't send - %m");
+ }
+ }
}
static void
usage(void)
{
fprintf(stderr,
-"usage: nos-tun -t <tun_name> -s <source_addr> -d <dest_addr> -p <protocol_number> <target_addr>\n");
+"usage: nos-tun -t tun_name -s source_addr -d dest_addr [-p protocol_number] target_addr\n");
exit(1);
}
-
[
Date Prev][
Date Next]
[
Thread Prev][
Thread Next]
[
Date Index][
Thread Index]