xref: /freebsd/usr.sbin/ngctl/write.c (revision b3e7694832e81d7a904a10f525f8797b753bf0d3)
1be44fce4SArchie Cobbs 
2be44fce4SArchie Cobbs /*
3be44fce4SArchie Cobbs  * write.c
4be44fce4SArchie Cobbs  *
5be44fce4SArchie Cobbs  * Copyright (c) 2002 Archie L. Cobbs
6be44fce4SArchie Cobbs  * All rights reserved.
7be44fce4SArchie Cobbs  *
8be44fce4SArchie Cobbs  * Subject to the following obligations and disclaimer of warranty, use and
9be44fce4SArchie Cobbs  * redistribution of this software, in source or object code forms, with or
10be44fce4SArchie Cobbs  * without modifications are expressly permitted by Archie L. Cobbs;
11be44fce4SArchie Cobbs  * provided, however, that:
12be44fce4SArchie Cobbs  * 1. Any and all reproductions of the source or object code must include the
13be44fce4SArchie Cobbs  *    copyright notice above and the following disclaimer of warranties
14be44fce4SArchie Cobbs  *
15be44fce4SArchie Cobbs  * THIS SOFTWARE IS BEING PROVIDED BY ARCHIE L. COBBS AS IS", AND TO
16be44fce4SArchie Cobbs  * THE MAXIMUM EXTENT PERMITTED BY LAW, ARCHIE L. COBBS MAKES NO
17be44fce4SArchie Cobbs  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING THIS SOFTWARE,
18be44fce4SArchie Cobbs  * INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED WARRANTIES OF
19be44fce4SArchie Cobbs  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, OR NON-INFRINGEMENT.
20be44fce4SArchie Cobbs  * ARCHIE L. COBBS DOES NOT WARRANT, GUARANTEE, OR MAKE ANY
21be44fce4SArchie Cobbs  * REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS OF THE USE OF THIS
22be44fce4SArchie Cobbs  * SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY, RELIABILITY OR OTHERWISE.
23be44fce4SArchie Cobbs  * IN NO EVENT SHALL ARCHIE L. COBBS BE LIABLE FOR ANY DAMAGES
24be44fce4SArchie Cobbs  * RESULTING FROM OR ARISING OUT OF ANY USE OF THIS SOFTWARE, INCLUDING
25be44fce4SArchie Cobbs  * WITHOUT LIMITATION, ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26be44fce4SArchie Cobbs  * PUNITIVE, OR CONSEQUENTIAL DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR
27be44fce4SArchie Cobbs  * SERVICES, LOSS OF USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY
28be44fce4SArchie Cobbs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29be44fce4SArchie Cobbs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30be44fce4SArchie Cobbs  * THIS SOFTWARE, EVEN IF ARCHIE L. COBBS IS ADVISED OF THE POSSIBILITY
31be44fce4SArchie Cobbs  * OF SUCH DAMAGE.
32be44fce4SArchie Cobbs  */
33be44fce4SArchie Cobbs 
3478cdd8edSGleb Smirnoff #include <sys/types.h>
3578cdd8edSGleb Smirnoff #include <sys/socket.h>
3678cdd8edSGleb Smirnoff 
3778cdd8edSGleb Smirnoff #include <err.h>
3878cdd8edSGleb Smirnoff #include <stdio.h>
3978cdd8edSGleb Smirnoff #include <string.h>
4078cdd8edSGleb Smirnoff #include <unistd.h>
4178cdd8edSGleb Smirnoff 
4278cdd8edSGleb Smirnoff #include <netgraph/ng_socket.h>
4378cdd8edSGleb Smirnoff 
44be44fce4SArchie Cobbs #include "ngctl.h"
45be44fce4SArchie Cobbs 
46be44fce4SArchie Cobbs #define BUF_SIZE	8192
47be44fce4SArchie Cobbs 
48be44fce4SArchie Cobbs static int WriteCmd(int ac, char **av);
49be44fce4SArchie Cobbs 
50be44fce4SArchie Cobbs const struct ngcmd write_cmd = {
51be44fce4SArchie Cobbs 	WriteCmd,
52be44fce4SArchie Cobbs 	"write hook < -f file | byte ... >",
53be44fce4SArchie Cobbs 	"Send a data packet down the hook named by \"hook\".",
54be44fce4SArchie Cobbs 	"The data may be contained in a file, or may be described directly"
55be44fce4SArchie Cobbs 	" on the command line by supplying a sequence of bytes.",
56be44fce4SArchie Cobbs 	{ "w" }
57be44fce4SArchie Cobbs };
58be44fce4SArchie Cobbs 
59be44fce4SArchie Cobbs static int
WriteCmd(int ac,char ** av)60be44fce4SArchie Cobbs WriteCmd(int ac, char **av)
61be44fce4SArchie Cobbs {
62be44fce4SArchie Cobbs 	u_int32_t sagbuf[64];
63be44fce4SArchie Cobbs 	struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf;
64be44fce4SArchie Cobbs 	u_char buf[BUF_SIZE];
65be44fce4SArchie Cobbs 	const char *hook;
66be44fce4SArchie Cobbs 	FILE *fp;
67be44fce4SArchie Cobbs 	u_int len;
68be44fce4SArchie Cobbs 	int byte;
69be44fce4SArchie Cobbs 	int i;
70be44fce4SArchie Cobbs 
71be44fce4SArchie Cobbs 	/* Get arguments */
72be44fce4SArchie Cobbs 	if (ac < 3)
73be44fce4SArchie Cobbs 		return (CMDRTN_USAGE);
74be44fce4SArchie Cobbs 	hook = av[1];
75be44fce4SArchie Cobbs 
76be44fce4SArchie Cobbs 	/* Get data */
77be44fce4SArchie Cobbs 	if (strcmp(av[2], "-f") == 0) {
78be44fce4SArchie Cobbs 		if (ac != 4)
79be44fce4SArchie Cobbs 			return (CMDRTN_USAGE);
80be44fce4SArchie Cobbs 		if ((fp = fopen(av[3], "r")) == NULL) {
81be44fce4SArchie Cobbs 			warn("can't read file \"%s\"", av[3]);
82be44fce4SArchie Cobbs 			return (CMDRTN_ERROR);
83be44fce4SArchie Cobbs 		}
84be44fce4SArchie Cobbs 		if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) {
85be44fce4SArchie Cobbs 			if (ferror(fp))
86be44fce4SArchie Cobbs 				warn("can't read file \"%s\"", av[3]);
87be44fce4SArchie Cobbs 			else
88be44fce4SArchie Cobbs 				warnx("file \"%s\" is empty", av[3]);
89be44fce4SArchie Cobbs 			fclose(fp);
90be44fce4SArchie Cobbs 			return (CMDRTN_ERROR);
91be44fce4SArchie Cobbs 		}
92be44fce4SArchie Cobbs 		fclose(fp);
93be44fce4SArchie Cobbs 	} else {
94be44fce4SArchie Cobbs 		for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) {
95be44fce4SArchie Cobbs 			if (sscanf(av[i], "%i", &byte) != 1
96be44fce4SArchie Cobbs 			    || (byte < -128 || byte > 255)) {
97be44fce4SArchie Cobbs 				warnx("invalid byte \"%s\"", av[i]);
98be44fce4SArchie Cobbs 				return (CMDRTN_ERROR);
99be44fce4SArchie Cobbs 			}
100be44fce4SArchie Cobbs 			buf[len] = (u_char)byte;
101be44fce4SArchie Cobbs 		}
102be44fce4SArchie Cobbs 		if (len == 0)
103be44fce4SArchie Cobbs 			return (CMDRTN_USAGE);
104be44fce4SArchie Cobbs 	}
105be44fce4SArchie Cobbs 
106be44fce4SArchie Cobbs 	/* Send data */
107be44fce4SArchie Cobbs 	sag->sg_len = 3 + strlen(hook);
108be44fce4SArchie Cobbs 	sag->sg_family = AF_NETGRAPH;
109be44fce4SArchie Cobbs 	strlcpy(sag->sg_data, hook, sizeof(sagbuf) - 2);
110be44fce4SArchie Cobbs 	if (sendto(dsock, buf, len,
111be44fce4SArchie Cobbs 	    0, (struct sockaddr *)sag, sag->sg_len) == -1) {
112be44fce4SArchie Cobbs 		warn("writing to hook \"%s\"", hook);
113be44fce4SArchie Cobbs 		return (CMDRTN_ERROR);
114be44fce4SArchie Cobbs 	}
115be44fce4SArchie Cobbs 
116be44fce4SArchie Cobbs 	/* Done */
117be44fce4SArchie Cobbs 	return (CMDRTN_OK);
118be44fce4SArchie Cobbs }
119be44fce4SArchie Cobbs 
120