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