4 * Copyright (c) 1996-1999 Whistle Communications, Inc.
7 * Subject to the following obligations and disclaimer of warranty, use and
8 * redistribution of this software, in source or object code forms, with or
9 * without modifications are expressly permitted by Whistle Communications;
10 * provided, however, that:
11 * 1. Any and all reproductions of the source or object code must include the
12 * copyright notice above and the following disclaimer of warranties; and
13 * 2. No rights are granted, in any manner or form, to use Whistle
14 * Communications, Inc. trademarks, including the mark "WHISTLE
15 * COMMUNICATIONS" on advertising, endorsements, or otherwise except as
16 * such appears in the above copyright notice or in the software.
18 * THIS SOFTWARE IS BEING PROVIDED BY WHISTLE COMMUNICATIONS "AS IS", AND
19 * TO THE MAXIMUM EXTENT PERMITTED BY LAW, WHISTLE COMMUNICATIONS MAKES NO
20 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
21 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
22 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
23 * WHISTLE COMMUNICATIONS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
24 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
25 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
26 * IN NO EVENT SHALL WHISTLE COMMUNICATIONS BE LIABLE FOR ANY DAMAGES
27 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
28 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
29 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
30 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 * THIS SOFTWARE, EVEN IF WHISTLE COMMUNICATIONS IS ADVISED OF THE POSSIBILITY
36 * $Whistle: main.c,v 1.9 1999/01/20 00:26:26 archie Exp $
39 #include <sys/cdefs.h>
40 __FBSDID("$FreeBSD$");
50 #include <stringlist.h>
52 #include <sys/types.h>
53 #include <sys/socket.h>
54 #include <sys/select.h>
58 #define DEFAULT_HOOKNAME "debug"
59 #define NG_SOCK_HOOK_NAME "hook"
61 #define BUF_SIZE (64 * 1024)
63 static void WriteAscii(u_char
* buf
, int len
);
64 static void Usage(void);
65 static void send_msgs(int, const char *);
67 static int outfd
= STDOUT_FILENO
;
68 static int infd
= STDIN_FILENO
;
70 static StringList
*msgs
;
76 main(int ac
, char *av
[])
78 struct ngm_connect ngc
;
79 const char *path
= NULL
;
80 const char *hook
= DEFAULT_HOOKNAME
;
88 if ((msgs
= sl_init()) == NULL
)
92 while ((ch
= getopt(ac
, av
, "aedlm:nsS")) != -1) {
98 NgSetDebug(NgSetDebug(-1) + 1);
110 if (sl_add(msgs
, optarg
) == -1)
114 outfd
= STDIN_FILENO
;
117 infd
= STDOUT_FILENO
;
128 if (asciiFlag
|| loopFlag
) {
129 fprintf(stderr
, "conflicting options\n");
153 if (NgMkSockNode(NULL
, &csock
, &dsock
) < 0)
154 errx(EX_OSERR
, "can't get sockets");
156 /* Connect socket node to specified node */
157 snprintf(ngc
.path
, sizeof(ngc
.path
), "%s", path
);
158 snprintf(ngc
.ourhook
, sizeof(ngc
.ourhook
), NG_SOCK_HOOK_NAME
);
159 snprintf(ngc
.peerhook
, sizeof(ngc
.peerhook
), "%s", hook
);
161 if (NgSendMsg(csock
, ".",
162 NGM_GENERIC_COOKIE
, NGM_CONNECT
, &ngc
, sizeof(ngc
)) < 0)
163 errx(EX_OSERR
, "can't connect to node");
166 /* move dsock to fd 0 and 1 */
170 (void)dup2(dsock
, 0);
171 (void)dup2(dsock
, 1);
173 send_msgs(csock
, path
);
175 /* try executing the program */
176 (void)execv(av
[0], av
);
177 err(EX_OSERR
, "%s", av
[0]);
180 send_msgs(csock
, path
);
182 /* Close standard input if not reading from it */
194 FD_SET(dsock
, &rfds
);
196 /* Wait for something to happen */
197 if (select(FD_SETSIZE
, &rfds
, NULL
, NULL
, NULL
) < 0)
198 err(EX_OSERR
, "select");
200 /* Check data from socket */
201 if (FD_ISSET(dsock
, &rfds
)) {
205 /* Read packet from socket */
206 if ((rl
= NgRecvData(dsock
,
207 buf
, sizeof(buf
), NULL
)) < 0)
208 err(EX_OSERR
, "read(hook)");
210 errx(EX_OSERR
, "read EOF from hook?!");
212 /* Write packet to stdout */
214 WriteAscii((u_char
*) buf
, rl
);
215 else if ((wl
= write(outfd
, buf
, rl
)) != rl
) {
217 err(EX_OSERR
, "write(stdout)");
220 "stdout: read %d, wrote %d",
226 if (NgSendData(dsock
, NG_SOCK_HOOK_NAME
, buf
, rl
) < 0)
227 err(EX_OSERR
, "write(hook)");
231 /* Check data from stdin */
232 if (FD_ISSET(infd
, &rfds
)) {
236 /* Read packet from stdin */
237 if ((rl
= read(infd
, buf
, sizeof(buf
))) < 0)
238 err(EX_OSERR
, "read(stdin)");
240 errx(EX_OSERR
, "EOF(stdin)");
242 /* Write packet to socket */
243 if (NgSendData(dsock
, NG_SOCK_HOOK_NAME
, buf
, rl
) < 0)
244 err(EX_OSERR
, "write(hook)");
250 * Dump data in hex and ASCII form
253 WriteAscii(u_char
*buf
, int len
)
258 for (count
= 0; count
< len
; count
+= 16) {
259 snprintf(sbuf
, sizeof(sbuf
), "%04x: ", count
);
260 for (k
= 0; k
< 16; k
++)
262 snprintf(sbuf
+ strlen(sbuf
),
263 sizeof(sbuf
) - strlen(sbuf
),
264 "%02x ", buf
[count
+ k
]);
266 snprintf(sbuf
+ strlen(sbuf
),
267 sizeof(sbuf
) - strlen(sbuf
), " ");
268 snprintf(sbuf
+ strlen(sbuf
), sizeof(sbuf
) - strlen(sbuf
), " ");
269 for (k
= 0; k
< 16; k
++)
270 if (count
+ k
< len
) {
271 ch
= isprint(buf
[count
+ k
]) ?
272 buf
[count
+ k
] : '.';
273 snprintf(sbuf
+ strlen(sbuf
),
274 sizeof(sbuf
) - strlen(sbuf
), "%c", ch
);
276 snprintf(sbuf
+ strlen(sbuf
),
277 sizeof(sbuf
) - strlen(sbuf
), " ");
278 snprintf(sbuf
+ strlen(sbuf
),
279 sizeof(sbuf
) - strlen(sbuf
), "\n");
280 (void) write(outfd
, sbuf
, strlen(sbuf
));
283 write(outfd
, &ch
, 1);
287 * Display usage and exit
292 fprintf(stderr
, "usage: nghook [-adlnsS] path [hookname]\n");
293 fprintf(stderr
, " or: nghook -e [-n] [-m msg]* path hookname prog "
299 * Send the messages to the node
302 send_msgs(int cs
, const char *path
)
306 for (i
= 0; i
< msgs
->sl_cur
; i
++)
307 if (NgSendAsciiMsg(cs
, path
, "%s", msgs
->sl_str
[i
]) == -1)
308 err(EX_OSERR
, "sending message '%s'", msgs
->sl_str
[i
]);