All pastes #151630 Raw Edit

Someone

public c v1 · immutable
#151630 ·published 2006-08-28 06:57 UTC
rendered paste body
#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <unistd.h>#include <sys/types.h>#include <sys/socket.h>#include <net/bpf.h>#include <netgraph.h>#include <netgraph/ng_socket.h>#include <netgraph/ng_bpf.h>/* * Connects node1:hook1 to node2:hook2 * cfd is the command socket. */int ng_connect (int *cfd, char *node1, char *hook1, char *node2, char *hook2){        struct ngm_connect ngc;        char nodename[50];        snprintf(ngc.path, sizeof(ngc.path), "%s:", node2);        snprintf(ngc.ourhook, sizeof(ngc.ourhook), "%s", hook1);        snprintf(ngc.peerhook, sizeof(ngc.peerhook), "%s", hook2);        snprintf (nodename, sizeof (nodename), "%s:", node1);        if (NgSendMsg(*cfd, nodename, NGM_GENERIC_COOKIE,                      NGM_CONNECT, &ngc, sizeof(ngc)) < 0) {                return (-1);        }        return (0);}int main (){        int     dfd, cfd;        char    socket_name[] = "test_socket";        struct ngm_mkpeer mkp;        char path[50];        /* bpf */        struct bpf_program fp;        char filter_exp[] = "tcp and dst not 192.168.3.1";        struct ng_bpf_hookprog bpf_hookprog;        struct bpf_insn *insn;        if (NgMkSockNode(socket_name, &cfd, &dfd) < 0) {                perror ("NgMkSockNode");                return (-1);        }        snprintf (mkp.type, sizeof(mkp.type), "%s", NG_BPF_NODE_TYPE);        snprintf (mkp.ourhook, sizeof(mkp.ourhook), "%s", "mixed");        snprintf (mkp.peerhook, sizeof(mkp.peerhook), "%s", "mixed");        snprintf (path, sizeof (path), "%s:", ".");        if (NgSendMsg(cfd, path, NGM_GENERIC_COOKIE,                      NGM_MKPEER, &mkp, sizeof(mkp)) < 0) {                perror ("create_bpf");                return (-1);        }        snprintf (path, sizeof (path), ".:%s", "mixed");        NgNameNode(cfd, path, "bpf");        ng_connect (&cfd, "bpf", "in1", "test_socket", "in1");        ng_connect (&cfd, "bpf", "in2", "test_socket", "in2");        ng_connect (&cfd, "bpf", "out", "test_socket", "out");        bzero (&bpf_hookprog, sizeof(bpf_hookprog));        strlcpy (bpf_hookprog.thisHook, "in1", sizeof (bpf_hookprog.thisHook));        strlcpy (bpf_hookprog.ifNotMatch, "mixed", sizeof (bpf_hookprog.ifNotMatch));        if (NgSendMsg(cfd, "bpf:", NGM_GENERIC_COOKIE,                      NGM_BPF_SET_PROGRAM, &bpf_hookprog, sizeof(bpf_hookprog)) < 0) {                perror ("Setup Hooks");                return (-1);        }        sleep(25);        return (0);}