Implement two command line options that allow one to change the

file descriptors that are used for input and output. That allows
one, for example, to use nghook to bi-directionally pipe the
input and output into/from another non-netgraph-aware program.
This commit is contained in:
Hartmut Brandt 2003-08-13 07:42:07 +00:00
parent de244df7c9
commit 1cdff5c9f7
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=118858
2 changed files with 23 additions and 8 deletions

View File

@ -61,6 +61,9 @@
static void WriteAscii(u_char * buf, int len); static void WriteAscii(u_char * buf, int len);
static void Usage(void); static void Usage(void);
static int outfd = STDOUT_FILENO;
static int infd = STDIN_FILENO;
/* /*
* main() * main()
*/ */
@ -76,7 +79,7 @@ main(int ac, char *av[])
int ch; int ch;
/* Parse flags */ /* Parse flags */
while ((ch = getopt(ac, av, "adln")) != EOF) { while ((ch = getopt(ac, av, "adlnsS")) != EOF) {
switch (ch) { switch (ch) {
case 'a': case 'a':
asciiFlag = 1; asciiFlag = 1;
@ -90,6 +93,12 @@ main(int ac, char *av[])
case 'n': case 'n':
noInput = 1; noInput = 1;
break; break;
case 's':
outfd = STDIN_FILENO;
break;
case 'S':
infd = STDOUT_FILENO;
break;
case '?': case '?':
default: default:
Usage(); Usage();
@ -134,7 +143,7 @@ main(int ac, char *av[])
/* Setup bits */ /* Setup bits */
FD_ZERO(&rfds); FD_ZERO(&rfds);
if (!noInput) if (!noInput)
FD_SET(0, &rfds); FD_SET(infd, &rfds);
FD_SET(dsock, &rfds); FD_SET(dsock, &rfds);
/* Wait for something to happen */ /* Wait for something to happen */
@ -156,7 +165,7 @@ main(int ac, char *av[])
/* Write packet to stdout */ /* Write packet to stdout */
if (asciiFlag) if (asciiFlag)
WriteAscii((u_char *) buf, rl); WriteAscii((u_char *) buf, rl);
else if ((wl = write(STDOUT_FILENO, buf, rl)) != rl) { else if ((wl = write(outfd, buf, rl)) != rl) {
if (wl < 0) { if (wl < 0) {
err(EX_OSERR, "write(stdout)"); err(EX_OSERR, "write(stdout)");
} else { } else {
@ -173,12 +182,12 @@ main(int ac, char *av[])
} }
/* Check data from stdin */ /* Check data from stdin */
if (FD_ISSET(0, &rfds)) { if (FD_ISSET(infd, &rfds)) {
char buf[BUF_SIZE]; char buf[BUF_SIZE];
int rl; int rl;
/* Read packet from stdin */ /* Read packet from stdin */
if ((rl = read(0, buf, sizeof(buf))) < 0) if ((rl = read(infd, buf, sizeof(buf))) < 0)
err(EX_OSERR, "read(stdin)"); err(EX_OSERR, "read(stdin)");
if (rl == 0) if (rl == 0)
errx(EX_OSERR, "EOF(stdin)"); errx(EX_OSERR, "EOF(stdin)");
@ -221,10 +230,10 @@ WriteAscii(u_char *buf, int len)
sizeof(sbuf) - strlen(sbuf), " "); sizeof(sbuf) - strlen(sbuf), " ");
snprintf(sbuf + strlen(sbuf), snprintf(sbuf + strlen(sbuf),
sizeof(sbuf) - strlen(sbuf), "\n"); sizeof(sbuf) - strlen(sbuf), "\n");
(void) write(STDOUT_FILENO, sbuf, strlen(sbuf)); (void) write(outfd, sbuf, strlen(sbuf));
} }
ch = '\n'; ch = '\n';
write(1, &ch, 1); write(outfd, &ch, 1);
} }
/* /*
@ -233,6 +242,6 @@ WriteAscii(u_char *buf, int len)
static void static void
Usage(void) Usage(void)
{ {
errx(EX_USAGE, "usage: nghook [-adln] path [hookname]"); errx(EX_USAGE, "usage: nghook [-adlnsS] path [hookname]");
} }

View File

@ -43,10 +43,12 @@
node node
.Sh SYNOPSIS .Sh SYNOPSIS
.Nm .Nm
.Op Fl S
.Op Fl a .Op Fl a
.Op Fl d .Op Fl d
.Op Fl l .Op Fl l
.Op Fl n .Op Fl n
.Op Fl s
.Ar path .Ar path
.Op Ar hookname .Op Ar hookname
.Sh DESCRIPTION .Sh DESCRIPTION
@ -76,6 +78,8 @@ is detected on standard input.
The options are as follows: The options are as follows:
.Pp .Pp
.Bl -tag -width indent .Bl -tag -width indent
.It Fl S
Use file descriptor 0 for output instead of the default 1.
.It Fl a .It Fl a
Output each packet read in human-readable decoded Output each packet read in human-readable decoded
.Tn ASCII .Tn ASCII
@ -90,6 +94,8 @@ Don't attempt to read any data from standard input.
The The
.Nm .Nm
utility will continue reading from the node until stopped by a signal. utility will continue reading from the node until stopped by a signal.
.It Fl s
Use file descriptor 1 for input instead of the default 0.
.El .El
.Sh BUGS .Sh BUGS
Although all input is read in unbuffered mode, Although all input is read in unbuffered mode,