Parallelize in_ifaddrhead operation
[dragonfly.git] / usr.sbin / ngctl / write.c
blob539f21faf01f6e0dd8a03c89c304dc05b22082db
1 /*
2 * write.c
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
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.
32 * $FreeBSD: src/usr.sbin/ngctl/write.c,v 1.1.2.1 2002/02/01 18:17:43 archie Exp $
33 * $DragonFly: src/usr.sbin/ngctl/write.c,v 1.4 2005/03/16 05:19:11 joerg Exp $
36 #include "ngctl.h"
38 #define BUF_SIZE 8192
40 static int WriteCmd(int ac, const char **av);
42 const struct ngcmd write_cmd = {
43 WriteCmd,
44 "write hook < -f file | byte ... >",
45 "Send a data packet down the hook named by \"hook\".",
46 "The data may be contained in a file, or may be described directly"
47 " on the command line by supplying a sequence of bytes.",
48 { "w" }
51 static int
52 WriteCmd(int ac, const char **av)
54 uint32_t sagbuf[64];
55 struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf;
56 u_char buf[BUF_SIZE];
57 const char *hook;
58 FILE *fp;
59 u_int len;
60 int byte;
61 int i;
63 /* Get arguments */
64 if (ac < 3)
65 return(CMDRTN_USAGE);
66 hook = av[1];
68 /* Get data */
69 if (strcmp(av[2], "-f") == 0) {
70 if (ac != 4)
71 return(CMDRTN_USAGE);
72 if ((fp = fopen(av[3], "r")) == NULL) {
73 warn("can't read file \"%s\"", av[3]);
74 return(CMDRTN_ERROR);
76 if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) {
77 if (ferror(fp))
78 warn("can't read file \"%s\"", av[3]);
79 else
80 warnx("file \"%s\" is empty", av[3]);
81 fclose(fp);
82 return(CMDRTN_ERROR);
84 fclose(fp);
85 } else {
86 for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) {
87 if (sscanf(av[i], "%i", &byte) != 1
88 || (byte < -128 || byte > 255)) {
89 warnx("invalid byte \"%s\"", av[i]);
90 return(CMDRTN_ERROR);
92 buf[len] = (u_char)byte;
94 if (len == 0)
95 return(CMDRTN_USAGE);
98 /* Send data */
99 sag->sg_len = 3 + strlen(hook);
100 sag->sg_family = AF_NETGRAPH;
101 strlcpy(sag->sg_data, hook, sizeof(sagbuf) - 2);
102 if (sendto(dsock, buf, len,
103 0, (struct sockaddr *)sag, sag->sg_len) == -1) {
104 warn("writing to hook \"%s\"", hook);
105 return(CMDRTN_ERROR);
108 /* Done */
109 return(CMDRTN_OK);