Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / usr.sbin / ngctl / write.c
1
2 /*
3  * write.c
4  *
5  * Copyright (c) 2002 Archie L. Cobbs
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 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
14  * 
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
31  * OF SUCH DAMAGE.
32  *
33  * $FreeBSD: src/usr.sbin/ngctl/write.c,v 1.1.2.1 2002/02/01 18:17:43 archie Exp $
34  * $DragonFly: src/usr.sbin/ngctl/write.c,v 1.2 2003/06/17 04:29:57 dillon Exp $
35  */
36
37 #include "ngctl.h"
38
39 #define BUF_SIZE        8192
40
41 static int WriteCmd(int ac, char **av);
42
43 const struct ngcmd write_cmd = {
44         WriteCmd,
45         "write hook < -f file | byte ... >",
46         "Send a data packet down the hook named by \"hook\".",
47         "The data may be contained in a file, or may be described directly"
48         " on the command line by supplying a sequence of bytes.",
49         { "w" }
50 };
51
52 static int
53 WriteCmd(int ac, char **av)
54 {
55         u_int32_t sagbuf[64];
56         struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf;
57         u_char buf[BUF_SIZE];
58         const char *hook;
59         FILE *fp;
60         u_int len;
61         int byte;
62         int i;
63
64         /* Get arguments */
65         if (ac < 3)
66                 return(CMDRTN_USAGE);
67         hook = av[1];
68
69         /* Get data */
70         if (strcmp(av[2], "-f") == 0) {
71                 if (ac != 4)
72                         return(CMDRTN_USAGE);
73                 if ((fp = fopen(av[3], "r")) == NULL) {
74                         warn("can't read file \"%s\"", av[3]);
75                         return(CMDRTN_ERROR);
76                 }
77                 if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) {
78                         if (ferror(fp))
79                                 warn("can't read file \"%s\"", av[3]);
80                         else
81                                 warnx("file \"%s\" is empty", av[3]);
82                         fclose(fp);
83                         return(CMDRTN_ERROR);
84                 }
85                 fclose(fp);
86         } else {
87                 for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) {
88                         if (sscanf(av[i], "%i", &byte) != 1
89                             || (byte < -128 || byte > 255)) {
90                                 warnx("invalid byte \"%s\"", av[i]);
91                                 return(CMDRTN_ERROR);
92                         }
93                         buf[len] = (u_char)byte;
94                 }
95                 if (len == 0)
96                         return(CMDRTN_USAGE);
97         }
98
99         /* Send data */
100         sag->sg_len = 3 + strlen(hook);
101         sag->sg_family = AF_NETGRAPH;
102         strlcpy(sag->sg_data, hook, sizeof(sagbuf) - 2);
103         if (sendto(dsock, buf, len,
104             0, (struct sockaddr *)sag, sag->sg_len) == -1) {
105                 warn("writing to hook \"%s\"", hook);
106                 return(CMDRTN_ERROR);
107         }
108
109         /* Done */
110         return(CMDRTN_OK);
111 }
112