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