5 * Copyright (c) 2002 Archie L. Cobbs
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 Archie L. Cobbs;
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
15 * THIS SOFTWARE IS BEING PROVIDED BY ARCHIE L. COBBS AS IS", AND TO
16 * THE MAXIMUM EXTENT PERMITTED BY LAW, ARCHIE L. COBBS MAKES NO
17 * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
18 * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
20 * ARCHIE L. COBBS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
21 * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
22 * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
23 * IN NO EVENT SHALL ARCHIE L. COBBS BE LIABLE FOR ANY DAMAGES
24 * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
25 * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26 * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ARCHIE L. COBBS IS ADVISED OF THE POSSIBILITY
34 #include <sys/types.h>
35 #include <sys/socket.h>
42 #include <netgraph/ng_socket.h>
48 static int WriteCmd(int ac, char **av);
50 const struct ngcmd write_cmd = {
52 "write hook < -f file | byte ... >",
53 "Send a data packet down the hook named by \"hook\".",
54 "The data may be contained in a file, or may be described directly"
55 " on the command line by supplying a sequence of bytes.",
60 WriteCmd(int ac, char **av)
63 struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf;
73 return (CMDRTN_USAGE);
77 if (strcmp(av[2], "-f") == 0) {
79 return (CMDRTN_USAGE);
80 if ((fp = fopen(av[3], "r")) == NULL) {
81 warn("can't read file \"%s\"", av[3]);
82 return (CMDRTN_ERROR);
84 if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) {
86 warn("can't read file \"%s\"", av[3]);
88 warnx("file \"%s\" is empty", av[3]);
90 return (CMDRTN_ERROR);
94 for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) {
95 if (sscanf(av[i], "%i", &byte) != 1
96 || (byte < -128 || byte > 255)) {
97 warnx("invalid byte \"%s\"", av[i]);
98 return (CMDRTN_ERROR);
100 buf[len] = (u_char)byte;
103 return (CMDRTN_USAGE);
107 sag->sg_len = 3 + strlen(hook);
108 sag->sg_family = AF_NETGRAPH;
109 strlcpy(sag->sg_data, hook, sizeof(sagbuf) - 2);
110 if (sendto(dsock, buf, len,
111 0, (struct sockaddr *)sag, sag->sg_len) == -1) {
112 warn("writing to hook \"%s\"", hook);
113 return (CMDRTN_ERROR);