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