aboutsummaryrefslogtreecommitdiff
path: root/utils/ipaddr2list.py
blob: 5b2b10bf38b5aace04cddbdc8724d34d570c37ff (plain)
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
#!/usr/bin/env python3

import sys
import socket, struct

# This scripts is mainly used to create "ip -> protocols" lists.
# However it is also used to create "ip -> risk" lists
proto = "NDPI_PROTOCOL_XYX"
if len (sys.argv) < 2 :
    print("Usage: ipaddr2list.py <file> <protocol>")
    sys.exit (1)

if len (sys.argv) == 3:
    proto = sys.argv[2]



print("""/*
 *
 * This file is generated automatically and part of nDPI
 *
 * nDPI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * nDPI 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with nDPI.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

/* ****************************************************** */

""")

print("static ndpi_network "+proto.lower()+"_protocol_list[] = {")

with open(sys.argv[1]) as fp:
    for cnt, line in enumerate(fp):
        line = line.rstrip()

        if(line != ""):
            x = line.split("/")

            if(len(x) == 2):
                ipaddr = x[0]
                cidr   = x[1]
            else:
                ipaddr = line
                cidr = "32"

            if(ipaddr != ""):
                print(" { 0x"+socket.inet_aton(ipaddr).hex().upper()+" /* "+ipaddr+"/"+cidr+" */, "+cidr+", "+proto+" },")

print(" /* End */")
print(" { 0x0, 0, 0 }")
print("};")