main.c revision 63436
1
2/*
3 * main.c
4 *
5 * Copyright (c) 1996-1999 Whistle Communications, Inc.
6 * All rights reserved.
7 *
8 * Subject to the following obligations and disclaimer of warranty, use and
9 * redistribution of this software, in source or object code forms, with or
10 * without modifications are expressly permitted by Whistle Communications;
11 * provided, however, that:
12 * 1. Any and all reproductions of the source or object code must include the
13 *    copyright notice above and the following disclaimer of warranties; and
14 * 2. No rights are granted, in any manner or form, to use Whistle
15 *    Communications, Inc. trademarks, including the mark "WHISTLE
16 *    COMMUNICATIONS" on advertising, endorsements, or otherwise except as
17 *    such appears in the above copyright notice or in the software.
18 *
19 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
20 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
21 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
22 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
23 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
24 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
25 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
26 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
27 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
28 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
29 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
30 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
31 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
34 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
35 * OF SUCH DAMAGE.
36 *
37 * $FreeBSD: head/usr.sbin/nghook/main.c 63436 2000-07-18 16:52:27Z phk $
38 * $Whistle: main.c,v 1.9 1999/01/20 00:26:26 archie Exp $
39 */
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <string.h>
44#include <ctype.h>
45#include <unistd.h>
46#include <sysexits.h>
47#include <errno.h>
48#include <err.h>
49
50#include <sys/types.h>
51#include <sys/socket.h>
52#include <sys/select.h>
53
54#include <netgraph.h>
55
56#define DEFAULT_HOOKNAME	"debug"
57#define NG_SOCK_HOOK_NAME	"hook"
58
59#define BUF_SIZE		(64 * 1024)
60
61static void	WriteAscii(u_char * buf, int len);
62static void	Usage(void);
63
64/*
65 * main()
66 */
67int
68main(int ac, char *av[])
69{
70	struct ngm_connect ngc;
71	char   *path = NULL, *hook = DEFAULT_HOOKNAME;
72	int     csock, dsock;
73	int     asciiFlag = 0;
74	int     loopFlag = 0;
75	int	ch;
76
77	/* Parse flags */
78	while ((ch = getopt(ac, av, "adl")) != EOF) {
79		switch (ch) {
80		case 'a':
81			asciiFlag = 1;
82			break;
83		case 'd':
84			NgSetDebug(NgSetDebug(-1) + 1);
85			break;
86		case 'l':
87			loopFlag = 1;
88			break;
89		case '?':
90		default:
91			Usage();
92		}
93	}
94	ac -= optind;
95	av += optind;
96
97	/* Get params */
98	switch (ac) {
99	case 2:
100		hook = av[1];
101		/* FALLTHROUGH */
102	case 1:
103		path = av[0];
104		break;
105	default:
106		Usage();
107	}
108
109	/* Get sockets */
110	if (NgMkSockNode(NULL, &csock, &dsock) < 0)
111		errx(EX_OSERR, "can't get sockets");
112
113	/* Connect socket node to specified node */
114	snprintf(ngc.path, sizeof(ngc.path), "%s", path);
115	snprintf(ngc.ourhook, sizeof(ngc.ourhook), NG_SOCK_HOOK_NAME);
116	snprintf(ngc.peerhook, sizeof(ngc.peerhook), "%s", hook);
117
118	if (NgSendMsg(csock, ".",
119	    NGM_GENERIC_COOKIE, NGM_CONNECT, &ngc, sizeof(ngc)) < 0)
120		errx(EX_OSERR, "can't connect to node");
121
122	/* Relay data */
123	while (1) {
124		fd_set  rfds;
125
126		/* Setup bits */
127		FD_ZERO(&rfds);
128		FD_SET(0, &rfds);
129		FD_SET(dsock, &rfds);
130
131		/* Wait for something to happen */
132		if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0)
133			err(EX_OSERR, "select");
134
135		/* Check data from socket */
136		if (FD_ISSET(dsock, &rfds)) {
137			char    buf[BUF_SIZE];
138			int     rl, wl;
139
140			/* Read packet from socket */
141			if ((rl = NgRecvData(dsock,
142			    buf, sizeof(buf), NULL)) < 0)
143				err(EX_OSERR, "read(hook)");
144			if (rl == 0)
145				errx(EX_OSERR, "read EOF from hook?!");
146
147			/* Write packet to stdout */
148			if (asciiFlag)
149				WriteAscii((u_char *) buf, rl);
150			else if ((wl = write(1, buf, rl)) != rl) {
151				if (wl < 0) {
152					err(EX_OSERR, "write(stdout)");
153				} else {
154					errx(EX_OSERR,
155					    "stdout: read %d, wrote %d",
156					    rl, wl);
157				}
158			}
159			/* Loopback */
160			if (loopFlag) {
161				if (NgSendData(dsock, NG_SOCK_HOOK_NAME, buf, rl) < 0)
162					err(EX_OSERR, "write(hook)");
163			}
164		}
165
166		/* Check data from stdin */
167		if (FD_ISSET(0, &rfds)) {
168			char    buf[BUF_SIZE];
169			int     rl;
170
171			/* Read packet from stdin */
172			if ((rl = read(0, buf, sizeof(buf))) < 0)
173				err(EX_OSERR, "read(stdin)");
174			if (rl == 0)
175				errx(EX_OSERR, "EOF(stdin)");
176
177			/* Write packet to socket */
178			if (NgSendData(dsock, NG_SOCK_HOOK_NAME, buf, rl) < 0)
179				err(EX_OSERR, "write(hook)");
180		}
181	}
182}
183
184/*
185 * Dump data in hex and ASCII form
186 */
187static void
188WriteAscii(u_char *buf, int len)
189{
190	char    ch, sbuf[100];
191	int     k, count;
192
193	for (count = 0; count < len; count += 16) {
194		snprintf(sbuf, sizeof(sbuf), "%04x:  ", count);
195		for (k = 0; k < 16; k++)
196			if (count + k < len)
197				snprintf(sbuf + strlen(sbuf),
198				    sizeof(sbuf) - strlen(sbuf),
199				    "%02x ", buf[count + k]);
200			else
201				snprintf(sbuf + strlen(sbuf),
202				    sizeof(sbuf) - strlen(sbuf), "   ");
203		snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), " ");
204		for (k = 0; k < 16; k++)
205			if (count + k < len) {
206				ch = isprint(buf[count + k]) ?
207				    buf[count + k] : '.';
208				snprintf(sbuf + strlen(sbuf),
209				    sizeof(sbuf) - strlen(sbuf), "%c", ch);
210			} else
211				snprintf(sbuf + strlen(sbuf),
212				    sizeof(sbuf) - strlen(sbuf), " ");
213		snprintf(sbuf + strlen(sbuf),
214		    sizeof(sbuf) - strlen(sbuf), "\n");
215		(void) write(1, sbuf, strlen(sbuf));
216	}
217	ch = '\n';
218	write(1, &ch, 1);
219}
220
221/*
222 * Display usage and exit
223 */
224static void
225Usage(void)
226{
227	errx(EX_USAGE, "usage: nghook [-da] path [hookname]");
228}
229
230