xref: /freebsd/usr.sbin/nghook/main.c (revision 1b6c76a2fe091c74f08427e6c870851025a9cf67)
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$
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 
61 static void	WriteAscii(u_char * buf, int len);
62 static void	Usage(void);
63 
64 /*
65  * main()
66  */
67 int
68 main(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	noInput = 0;
76 	int	ch;
77 
78 	/* Parse flags */
79 	while ((ch = getopt(ac, av, "adln")) != EOF) {
80 		switch (ch) {
81 		case 'a':
82 			asciiFlag = 1;
83 			break;
84 		case 'd':
85 			NgSetDebug(NgSetDebug(-1) + 1);
86 			break;
87 		case 'l':
88 			loopFlag = 1;
89 			break;
90 		case 'n':
91 			noInput = 1;
92 			break;
93 		case '?':
94 		default:
95 			Usage();
96 		}
97 	}
98 	ac -= optind;
99 	av += optind;
100 
101 	/* Get params */
102 	switch (ac) {
103 	case 2:
104 		hook = av[1];
105 		/* FALLTHROUGH */
106 	case 1:
107 		path = av[0];
108 		break;
109 	default:
110 		Usage();
111 	}
112 
113 	/* Get sockets */
114 	if (NgMkSockNode(NULL, &csock, &dsock) < 0)
115 		errx(EX_OSERR, "can't get sockets");
116 
117 	/* Connect socket node to specified node */
118 	snprintf(ngc.path, sizeof(ngc.path), "%s", path);
119 	snprintf(ngc.ourhook, sizeof(ngc.ourhook), NG_SOCK_HOOK_NAME);
120 	snprintf(ngc.peerhook, sizeof(ngc.peerhook), "%s", hook);
121 
122 	if (NgSendMsg(csock, ".",
123 	    NGM_GENERIC_COOKIE, NGM_CONNECT, &ngc, sizeof(ngc)) < 0)
124 		errx(EX_OSERR, "can't connect to node");
125 
126 	/* Close standard input if not reading from it */
127 	if (noInput)
128 		fclose(stdin);
129 
130 	/* Relay data */
131 	while (1) {
132 		fd_set  rfds;
133 
134 		/* Setup bits */
135 		FD_ZERO(&rfds);
136 		if (!noInput)
137 			FD_SET(0, &rfds);
138 		FD_SET(dsock, &rfds);
139 
140 		/* Wait for something to happen */
141 		if (select(FD_SETSIZE, &rfds, NULL, NULL, NULL) < 0)
142 			err(EX_OSERR, "select");
143 
144 		/* Check data from socket */
145 		if (FD_ISSET(dsock, &rfds)) {
146 			char    buf[BUF_SIZE];
147 			int     rl, wl;
148 
149 			/* Read packet from socket */
150 			if ((rl = NgRecvData(dsock,
151 			    buf, sizeof(buf), NULL)) < 0)
152 				err(EX_OSERR, "read(hook)");
153 			if (rl == 0)
154 				errx(EX_OSERR, "read EOF from hook?!");
155 
156 			/* Write packet to stdout */
157 			if (asciiFlag)
158 				WriteAscii((u_char *) buf, rl);
159 			else if ((wl = write(1, buf, rl)) != rl) {
160 				if (wl < 0) {
161 					err(EX_OSERR, "write(stdout)");
162 				} else {
163 					errx(EX_OSERR,
164 					    "stdout: read %d, wrote %d",
165 					    rl, wl);
166 				}
167 			}
168 			/* Loopback */
169 			if (loopFlag) {
170 				if (NgSendData(dsock, NG_SOCK_HOOK_NAME, buf, rl) < 0)
171 					err(EX_OSERR, "write(hook)");
172 			}
173 		}
174 
175 		/* Check data from stdin */
176 		if (FD_ISSET(0, &rfds)) {
177 			char    buf[BUF_SIZE];
178 			int     rl;
179 
180 			/* Read packet from stdin */
181 			if ((rl = read(0, buf, sizeof(buf))) < 0)
182 				err(EX_OSERR, "read(stdin)");
183 			if (rl == 0)
184 				errx(EX_OSERR, "EOF(stdin)");
185 
186 			/* Write packet to socket */
187 			if (NgSendData(dsock, NG_SOCK_HOOK_NAME, buf, rl) < 0)
188 				err(EX_OSERR, "write(hook)");
189 		}
190 	}
191 }
192 
193 /*
194  * Dump data in hex and ASCII form
195  */
196 static void
197 WriteAscii(u_char *buf, int len)
198 {
199 	char    ch, sbuf[100];
200 	int     k, count;
201 
202 	for (count = 0; count < len; count += 16) {
203 		snprintf(sbuf, sizeof(sbuf), "%04x:  ", count);
204 		for (k = 0; k < 16; k++)
205 			if (count + k < len)
206 				snprintf(sbuf + strlen(sbuf),
207 				    sizeof(sbuf) - strlen(sbuf),
208 				    "%02x ", buf[count + k]);
209 			else
210 				snprintf(sbuf + strlen(sbuf),
211 				    sizeof(sbuf) - strlen(sbuf), "   ");
212 		snprintf(sbuf + strlen(sbuf), sizeof(sbuf) - strlen(sbuf), " ");
213 		for (k = 0; k < 16; k++)
214 			if (count + k < len) {
215 				ch = isprint(buf[count + k]) ?
216 				    buf[count + k] : '.';
217 				snprintf(sbuf + strlen(sbuf),
218 				    sizeof(sbuf) - strlen(sbuf), "%c", ch);
219 			} else
220 				snprintf(sbuf + strlen(sbuf),
221 				    sizeof(sbuf) - strlen(sbuf), " ");
222 		snprintf(sbuf + strlen(sbuf),
223 		    sizeof(sbuf) - strlen(sbuf), "\n");
224 		(void) write(1, sbuf, strlen(sbuf));
225 	}
226 	ch = '\n';
227 	write(1, &ch, 1);
228 }
229 
230 /*
231  * Display usage and exit
232  */
233 static void
234 Usage(void)
235 {
236 	errx(EX_USAGE, "usage: nghook [-adln] path [hookname]");
237 }
238 
239