xref: /src/usr.sbin/ngctl/write.c (revision 585190dff436eeea3be97300e36c82559028d3dd)
1 /*
2  * write.c
3  *
4  * Copyright (c) 2002 Archie L. Cobbs
5  * All rights reserved.
6  *
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 Archie L. Cobbs;
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
13  *
14  * THIS SOFTWARE IS BEING PROVIDED BY ARCHIE L. COBBS AS IS", AND TO
15  * THE MAXIMUM EXTENT PERMITTED BY LAW, ARCHIE L. COBBS MAKES NO
16  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
17  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
19  * ARCHIE L. COBBS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
20  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
21  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
22  * IN NO EVENT SHALL ARCHIE L. COBBS BE LIABLE FOR ANY DAMAGES
23  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
24  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
25  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
26  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29  * THIS SOFTWARE, EVEN IF ARCHIE L. COBBS IS ADVISED OF THE POSSIBILITY
30  * OF SUCH DAMAGE.
31  */
32 
33 #include <sys/types.h>
34 #include <sys/socket.h>
35 
36 #include <err.h>
37 #include <stddef.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 
42 #include <netgraph/ng_message.h>
43 #include <netgraph/ng_socket.h>
44 
45 #include "ngctl.h"
46 
47 #define BUF_SIZE	8192
48 
49 static int WriteCmd(int ac, char **av);
50 
51 const struct ngcmd write_cmd = {
52 	WriteCmd,
53 	"write hook < -f file | byte ... >",
54 	"Send a data packet down the hook named by \"hook\".",
55 	"The data may be contained in a file, or may be described directly"
56 	" on the command line by supplying a sequence of bytes.",
57 	{ "w" }
58 };
59 
60 static int
WriteCmd(int ac,char ** av)61 WriteCmd(int ac, char **av)
62 {
63 	u_int32_t sagbuf[64];
64 	struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf;
65 	u_char buf[BUF_SIZE];
66 	const char *hook;
67 	size_t hooklen;
68 	FILE *fp;
69 	u_int len;
70 	int byte;
71 	int i;
72 
73 	/* Get arguments */
74 	if (ac < 3)
75 		return (CMDRTN_USAGE);
76 	hook = av[1];
77 	_Static_assert(sizeof(sagbuf) >=
78 	    offsetof(struct sockaddr_ng, sg_data) + NG_HOOKSIZ,
79 	    "sagbuf is too small for NG_HOOKSIZ");
80 	hooklen = strlcpy(sag->sg_data, hook, NG_HOOKSIZ);
81 	if (hooklen >= NG_HOOKSIZ) {
82 		warnx("hook name \"%s\" too long", hook);
83 		return (CMDRTN_ERROR);
84 	}
85 
86 	/* Get data */
87 	if (strcmp(av[2], "-f") == 0) {
88 		if (ac != 4)
89 			return (CMDRTN_USAGE);
90 		if ((fp = fopen(av[3], "r")) == NULL) {
91 			warn("can't read file \"%s\"", av[3]);
92 			return (CMDRTN_ERROR);
93 		}
94 		if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) {
95 			if (ferror(fp))
96 				warn("can't read file \"%s\"", av[3]);
97 			else
98 				warnx("file \"%s\" is empty", av[3]);
99 			fclose(fp);
100 			return (CMDRTN_ERROR);
101 		}
102 		fclose(fp);
103 	} else {
104 		for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) {
105 			if (sscanf(av[i], "%i", &byte) != 1 ||
106 			    byte < -128 || byte > 255) {
107 				warnx("invalid byte \"%s\"", av[i]);
108 				return (CMDRTN_ERROR);
109 			}
110 			buf[len] = (u_char)byte;
111 		}
112 		if (len == 0)
113 			return (CMDRTN_USAGE);
114 	}
115 
116 	/* Send data */
117 	sag->sg_len = 3 + hooklen;
118 	sag->sg_family = AF_NETGRAPH;
119 	if (sendto(dsock, buf, len, 0, (struct sockaddr *)sag,
120 	    sag->sg_len) < 0) {
121 		warn("writing to hook \"%s\"", hook);
122 		return (CMDRTN_ERROR);
123 	}
124 
125 	/* Done */
126 	return (CMDRTN_OK);
127 }
128