-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathstdpf.c
82 lines (69 loc) · 1.61 KB
/
stdpf.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/ip.h>
#include <netinet/tcp.h>
#include <netinet/in_systm.h>
#include <netinet/ip_var.h>
#include <arpa/inet.h>
#include <net/pfvar.h>
#include <err.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "stdpf.h"
int pfdev = -1;
int timeout = 0;
TAILQ_HEAD( pftimeout_head, pftimeout) timeouts;
struct pftimeout {
TAILQ_ENTRY(pftimeout) queue;
struct in_addr ip;
uint8_t mask;
time_t timeout;
char table[PF_TABLE_NAME_SIZE];
};
/*
* Borrowed from pftabled (was static in pftabled.c)
*/
void add(char *tname, struct in_addr *ip, uint8_t mask) {
struct pfioc_table io;
struct pfr_table table;
struct pfr_addr addr;
struct pftimeout *t;
bzero(&io, sizeof io);
bzero(&table, sizeof(table));
bzero(&addr, sizeof(addr));
strncpy(table.pfrt_name, tname, sizeof(table.pfrt_name));
bcopy(ip, &addr.pfra_ip4addr, 4);
addr.pfra_af = AF_INET;
addr.pfra_net = mask;
io.pfrio_table = table;
io.pfrio_buffer = &addr;
io.pfrio_esize = sizeof(addr);
io.pfrio_size = 1;
if (ioctl(pfdev, DIOCRADDADDRS, &io))
err(1, "ioctl");
if (timeout) {
if ((t = malloc(sizeof(struct pftimeout))) == NULL)
err(1, "malloc");
t->ip = *ip;
t->mask = mask;
t->timeout = time(NULL) + timeout;
strncpy(t->table, tname, sizeof(t->table));
TAILQ_INSERT_HEAD(&timeouts, t, queue);
}
}
void ets_pf_open() {
// Open
pfdev = open(PFDEV, O_RDWR);
if (pfdev == -1)
err(1, "open " PFDEV);
}
void ets_pf_close() {
// and close..
close(pfdev);
}