forked from ilikenwf/pg2ipset
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg2ipset.c
91 lines (78 loc) · 2.62 KB
/
pg2ipset.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
83
84
85
86
87
88
89
90
91
/*
pg2ipset.c - Convert PeerGuardian lists to IPSet scripts.
Copyright (C) 2009-2010, [email protected]
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
int main(int argc, char* argv[]) {
FILE* ifp;
FILE* ofp;
const char* rulename;
char* line = NULL;
size_t linelen = 0;
char* fromaddr;
char* toaddr;
unsigned int linecount = 0;
char* tok;
if (argc == 2 && (!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help"))) {
fprintf(stderr, "Usage: %s [<input> [<output> [<set name>]]]\n", argv[0]);
fprintf(stderr, "Input should be a PeerGuardian .p2p file, blank or '-' reads from stdin.\n");
fprintf(stderr, "Output is suitable for usage by 'ipset restore', blank or '-' prints to stdout.\n");
fprintf(stderr, "Set name is 'IPFILTER' if not specified.\n");
fprintf(stderr, "Example: curl http://www.example.com/guarding.p2p | %s | ipset restore\n", argv[0]);
return 0;
}
if (argc < 2 || !strcmp(argv[1], "-")) {
ifp = stdin;
} else {
ifp = fopen(argv[1], "r");
}
if (!ifp) { perror("Could not open input file"); return -errno; }
if (argc < 3 || !strcmp(argv[2], "-")) {
ofp = stdout;
} else {
ofp = fopen(argv[2], "w");
}
if (!ofp) { perror("Could not open output file"); return -errno; }
if (argc < 4) {
rulename = "IPFILTER";
} else {
rulename = argv[3];
}
while (getline(&line, &linelen, ifp) > 0) {
linecount++;
tok = line;
fromaddr = strrchr(tok, ':');
if (!fromaddr) {
fprintf(stderr, "Line %u: Failed parsing 'from' address.\n", linecount);
continue;
}
*fromaddr++ = 0;
toaddr = strchr(fromaddr, '-');
if (!toaddr) {
fprintf(stderr, "Line %u: Failed parsing 'to' address.\n", linecount);
continue;
}
*toaddr++ = 0;
fprintf(ofp, "add -exist %s %s-%s\n", rulename, fromaddr, toaddr);
}
fprintf(ofp, "COMMIT\n");
// fprintf(stderr, "Converted %u rules.\n", linecount);
return 0;
}
// EOF