From 1cdff5c9f7892f294a76a6478aad0dcc230338dc Mon Sep 17 00:00:00 2001 From: Hartmut Brandt Date: Wed, 13 Aug 2003 07:42:07 +0000 Subject: [PATCH] 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. --- usr.sbin/nghook/main.c | 25 +++++++++++++++++-------- usr.sbin/nghook/nghook.8 | 6 ++++++ 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/usr.sbin/nghook/main.c b/usr.sbin/nghook/main.c index c5fd2050728a..69ee581f84fb 100644 --- a/usr.sbin/nghook/main.c +++ b/usr.sbin/nghook/main.c @@ -61,6 +61,9 @@ static void WriteAscii(u_char * buf, int len); static void Usage(void); +static int outfd = STDOUT_FILENO; +static int infd = STDIN_FILENO; + /* * main() */ @@ -76,7 +79,7 @@ main(int ac, char *av[]) int ch; /* Parse flags */ - while ((ch = getopt(ac, av, "adln")) != EOF) { + while ((ch = getopt(ac, av, "adlnsS")) != EOF) { switch (ch) { case 'a': asciiFlag = 1; @@ -90,6 +93,12 @@ main(int ac, char *av[]) case 'n': noInput = 1; break; + case 's': + outfd = STDIN_FILENO; + break; + case 'S': + infd = STDOUT_FILENO; + break; case '?': default: Usage(); @@ -134,7 +143,7 @@ main(int ac, char *av[]) /* Setup bits */ FD_ZERO(&rfds); if (!noInput) - FD_SET(0, &rfds); + FD_SET(infd, &rfds); FD_SET(dsock, &rfds); /* Wait for something to happen */ @@ -156,7 +165,7 @@ main(int ac, char *av[]) /* Write packet to stdout */ if (asciiFlag) 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) { err(EX_OSERR, "write(stdout)"); } else { @@ -173,12 +182,12 @@ main(int ac, char *av[]) } /* Check data from stdin */ - if (FD_ISSET(0, &rfds)) { + if (FD_ISSET(infd, &rfds)) { char buf[BUF_SIZE]; int rl; /* 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)"); if (rl == 0) errx(EX_OSERR, "EOF(stdin)"); @@ -221,10 +230,10 @@ WriteAscii(u_char *buf, int len) sizeof(sbuf) - strlen(sbuf), " "); snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), "\n"); - (void) write(STDOUT_FILENO, sbuf, strlen(sbuf)); + (void) write(outfd, sbuf, strlen(sbuf)); } ch = '\n'; - write(1, &ch, 1); + write(outfd, &ch, 1); } /* @@ -233,6 +242,6 @@ WriteAscii(u_char *buf, int len) static void Usage(void) { - errx(EX_USAGE, "usage: nghook [-adln] path [hookname]"); + errx(EX_USAGE, "usage: nghook [-adlnsS] path [hookname]"); } diff --git a/usr.sbin/nghook/nghook.8 b/usr.sbin/nghook/nghook.8 index d786a3adf00a..2dc85af2d1da 100644 --- a/usr.sbin/nghook/nghook.8 +++ b/usr.sbin/nghook/nghook.8 @@ -43,10 +43,12 @@ node .Sh SYNOPSIS .Nm +.Op Fl S .Op Fl a .Op Fl d .Op Fl l .Op Fl n +.Op Fl s .Ar path .Op Ar hookname .Sh DESCRIPTION @@ -76,6 +78,8 @@ is detected on standard input. The options are as follows: .Pp .Bl -tag -width indent +.It Fl S +Use file descriptor 0 for output instead of the default 1. .It Fl a Output each packet read in human-readable decoded .Tn ASCII @@ -90,6 +94,8 @@ Don't attempt to read any data from standard input. The .Nm 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 .Sh BUGS Although all input is read in unbuffered mode,