FreeBSD Bugzilla – Attachment 5614 Details for
Bug 13309
Fixes to nos-tun(8)
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
Remember
[x]
|
Forgot Password
Login:
[x]
[patch]
file.diff
file.diff (text/plain), 5.97 KB, created by
Nick Johnson
on 1999-08-21 23:40:01 UTC
(
hide
)
Description:
file.diff
Filename:
MIME Type:
Creator:
Nick Johnson
Created:
1999-08-21 23:40:01 UTC
Size:
5.97 KB
patch
obsolete
>--- nos-tun.c Tue Jul 27 13:48:38 1999 >+++ nos-tun.c Tue Jul 27 12:27:21 1999 >@@ -55,6 +55,13 @@ > * > */ > >+/* >+ * 19990727 - Nick Johnson <freebsd@spatula.net> >+ * Added -r and -w flags, cleaned up the code a bit, >+ * made writes more robust (return values were not being >+ * checked) >+ */ >+ > #ifndef lint > static const char rcsid[] = > "$Id: nos-tun.c,v 1.5 1999/03/29 13:58:27 phk Exp $"; >@@ -186,7 +193,7 @@ > return(1); > } > >-void Finish(int signum) >+void Cleanup() > { > int s; > >@@ -227,8 +234,44 @@ > close(s); > closing_tun: > close(tun); >+} >+ >+void Finish() >+{ >+ Cleanup(); > closelog(); >- exit(signum); >+} >+ >+void sighandle(int signum) >+{ >+ Finish(); >+ exit(signum); >+} >+ >+int Setup (char *devname, struct sockaddr *t_laddr, char *to_point, >+ struct sockaddr *whereto, char *target, struct sockaddr_in *to, >+ int protnum) >+{ >+ if(tun_open(devname, t_laddr, to_point)) { >+ closelog(); >+ return(3); >+ } >+ >+ to = (struct sockaddr_in *)&whereto; >+ if(Set_address(target, to)) >+ return(4); >+ >+ if ((net = socket(AF_INET, SOCK_RAW, protnum)) < 0) { >+ syslog(LOG_ERR,"can't open socket - %m"); >+ return(5); >+ } >+ >+ if (connect(net,whereto,sizeof(struct sockaddr_in)) < 0 ) { >+ syslog(LOG_ERR,"can't connect to target - %m"); >+ close(net); >+ return(6); >+ } >+ return(0); > } > > int main (int argc, char **argv) >@@ -252,8 +295,15 @@ > fd_set rfds, wfds, efds; /* File descriptors for select() */ > int nfds; /* Return from select() */ > >+ ssize_t woff, wsize; /* offset & Return values for write */ >+ int reset = 0; /* try to re-setup when writes fail */ >+ int sleeplen = 60; /* how long to sleep before retrying */ >+ int retries = 0; /* how many times to try to set up */ >+ int werror = 0; /* write error flag */ >+ int retcnt = 0; /* retry counter */ >+ int ret; /* generic return value */ > >- while ((c = getopt(argc, argv, "d:s:t:p:")) != -1) { >+ while ((c = getopt(argc, argv, "d:s:t:p:r:w:")) != -1) { > switch (c) { > case 'd': > to_point = optarg; >@@ -267,6 +317,17 @@ > case 'p': > protocol = optarg; > break; >+ case 'r': >+ reset = 1; >+ if (optarg[0] != ':') >+ retries = atoi(optarg); >+ break; >+ case 'w': >+ if (optarg[0] != ':') >+ sleeplen = atoi(optarg); >+ else >+ usage(); >+ break; > } > } > argc -= optind; >@@ -292,35 +353,47 @@ > 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 */ >- (void)signal(SIGHUP,Finish); >- (void)signal(SIGINT,Finish); >- (void)signal(SIGTERM,Finish); >+ (void)signal(SIGHUP,sighandle); >+ (void)signal(SIGINT,sighandle); >+ (void)signal(SIGTERM,sighandle); >+ >+ /* Set up the tunnel */ >+ if ((ret=Setup(devname, &t_laddr, to_point, &whereto, target, to, protnum))) { >+ Finish(); >+ exit(ret); >+ } > > for (;;) { >+ fortop: >+ if (werror) { >+ if (!reset) { >+ syslog(LOG_ERR,"write error, exiting"); >+ Finish(); >+ exit(8); >+ } >+ /* We got a write error and we're trying to maintain the tunnel */ >+ /* Clean out the old and try to set it up again */ >+ do { >+ Cleanup(); >+ syslog(LOG_NOTICE,"waiting to retry tunnel setup"); >+ sleep(sleeplen); >+ retcnt++; >+ } while((ret = Setup(devname, &t_laddr, to_point, &whereto, target, >+ to, protnum)) >+ && (retcnt < retries) >+ && (werror < retries)); >+ if (ret) { >+ syslog(LOG_ERR,"unable to re-establish tunnel"); >+ Finish(); >+ exit(8); >+ } else { >+ retcnt = 0; /* werror does not get reset until we successfully write */ >+ } >+ } > /* Set file descriptors for select() */ > FD_ZERO(&rfds); FD_ZERO(&wfds); FD_ZERO(&efds); > FD_SET(tun,&rfds); FD_SET(net,&rfds); >@@ -329,12 +402,14 @@ > if(nfds < 0) { > syslog(LOG_ERR,"interrupted select"); > close(net); >- Finish(7); >+ Finish(); >+ exit(7); > } > if(nfds == 0) { /* Impossible ? */ > syslog(LOG_ERR,"timeout in select"); > close(net); >- Finish(8); >+ Finish(); >+ exit(8); > } > > >@@ -346,7 +421,17 @@ > /* ... skip encapsulation headers ... */ > ipoff = (ip->ip_hl << 2); > /* ... and write to tun-device */ >- write(tun,buf+ipoff,len-ipoff); >+ woff = 0; >+ while (woff < (len-ipoff)) { >+ if((wsize = write(tun, buf+ipoff+woff, len-ipoff-woff))<1) { >+ syslog(LOG_ERR, "can't send - %m"); >+ werror++; >+ goto fortop; >+ } else { >+ werror = 0; >+ } >+ woff += wsize; >+ } > } > } > >@@ -354,8 +439,16 @@ > /* 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"); >+ woff = 0; >+ while (woff < len) { >+ if((wsize = send(net, buf+woff, len-woff,0)) < 1) { >+ syslog(LOG_ERR,"can't send - %m"); >+ werror++; >+ goto fortop; >+ } else { >+ werror = 0; >+ } >+ woff += wsize; > } > } > } >@@ -365,7 +458,7 @@ > usage() > { > 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> [-r [retry count] -w <retry delay>] <target_addr>\n"); > exit(1); > }
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
Attachments on
bug 13309
: 5614 |
5615