#include <sys/types.h>
#include <sys/socket.h>
#include <err.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <netgraph/ng_message.h>
#include <netgraph/ng_socket.h>
#include "ngctl.h"
#define BUF_SIZE 8192
static int WriteCmd(int ac, char **av);
const struct ngcmd write_cmd = {
WriteCmd,
"write hook < -f file | byte ... >",
"Send a data packet down the hook named by \"hook\".",
"The data may be contained in a file, or may be described directly"
" on the command line by supplying a sequence of bytes.",
{ "w" }
};
static int
WriteCmd(int ac, char **av)
{
u_int32_t sagbuf[64];
struct sockaddr_ng *sag = (struct sockaddr_ng *)sagbuf;
u_char buf[BUF_SIZE];
const char *hook;
size_t hooklen;
FILE *fp;
u_int len;
int byte;
int i;
if (ac < 3)
return (CMDRTN_USAGE);
hook = av[1];
_Static_assert(sizeof(sagbuf) >=
offsetof(struct sockaddr_ng, sg_data) + NG_HOOKSIZ,
"sagbuf is too small for NG_HOOKSIZ");
hooklen = strlcpy(sag->sg_data, hook, NG_HOOKSIZ);
if (hooklen >= NG_HOOKSIZ) {
warnx("hook name \"%s\" too long", hook);
return (CMDRTN_ERROR);
}
if (strcmp(av[2], "-f") == 0) {
if (ac != 4)
return (CMDRTN_USAGE);
if ((fp = fopen(av[3], "r")) == NULL) {
warn("can't read file \"%s\"", av[3]);
return (CMDRTN_ERROR);
}
if ((len = fread(buf, 1, sizeof(buf), fp)) == 0) {
if (ferror(fp))
warn("can't read file \"%s\"", av[3]);
else
warnx("file \"%s\" is empty", av[3]);
fclose(fp);
return (CMDRTN_ERROR);
}
fclose(fp);
} else {
for (i = 2, len = 0; i < ac && len < sizeof(buf); i++, len++) {
if (sscanf(av[i], "%i", &byte) != 1 ||
byte < -128 || byte > 255) {
warnx("invalid byte \"%s\"", av[i]);
return (CMDRTN_ERROR);
}
buf[len] = (u_char)byte;
}
if (len == 0)
return (CMDRTN_USAGE);
}
sag->sg_len = 3 + hooklen;
sag->sg_family = AF_NETGRAPH;
if (sendto(dsock, buf, len, 0, (struct sockaddr *)sag,
sag->sg_len) < 0) {
warn("writing to hook \"%s\"", hook);
return (CMDRTN_ERROR);
}
return (CMDRTN_OK);
}