Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make gen_dns working with python3 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

.PHONY: all
all: linux_tools/bpf_dbg linux_tools/bpf_asm
python -c "import pcappy"
python3 -c "import pcappy"


linux_tools/bpf_dbg: linux_tools/*.[ch]
Expand All @@ -11,4 +11,4 @@ linux_tools/bpf_asm: linux_tools/*.[ch]
make -C linux_tools bpf_asm

test:
python -m unittest discover tests/
python3 -m unittest discover tests/
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ To run these scripts you will need:

- Installed dependencies:

$ sudo apt-get install python-setuptools libpcap-dev \
$ sudo apt-get install python3-setuptools libpcap-dev \
libreadline-dev binutils-dev bison flex
$ sudo easy_install pcappy
$ pip install git+https://github.com/allfro/pcappy

- Build the binary tools in `linux_tools` directory:

Expand Down
58 changes: 34 additions & 24 deletions bpfgen
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env python
#!/usr/bin/env python3

from __future__ import print_function
import argparse
import os
import stat
Expand All @@ -12,7 +13,7 @@ import bpftools
def main():
parser = argparse.ArgumentParser(
formatter_class=argparse.RawDescriptionHelpFormatter,
description=r'''
description=r"""

This tool creates a Berkeley Packet Filter (BPF) bytecode that will
match packets based on given criteria. Right now we support the
Expand Down Expand Up @@ -43,35 +44,44 @@ BPF generators, for example:
%(prog)s -s -n suffix -- 010203
%(prog)s -s -n -o 14 suffix -- 010203
%(prog)s -s -n -o 14 -6 suffix -- 010203
''')

parser.add_argument('-6', '--inet6', action='store_true',
help='generate script for IPv6')
parser.add_argument('-n', '--negate', action='store_true',
help='negate the logic')
parser.add_argument('-o', '--offset', type=int, default=0,
help='offset of an L3 header')
parser.add_argument('-s', '--assembly', action='store_true',
help='print readable assembly, not numeric bytecode')
parser.add_argument('type', nargs=1, choices=bpftools.generator_names,
help='BPF generator type')
parser.add_argument('parameters', nargs='*',
help='parameters passed to the BPF generator')
""",
)

parser.add_argument(
"-6", "--inet6", action="store_true", help="generate script for IPv6"
)
parser.add_argument("-n", "--negate", action="store_true", help="negate the logic")
parser.add_argument(
"-o", "--offset", type=int, default=0, help="offset of an L3 header"
)
parser.add_argument(
"-s",
"--assembly",
action="store_true",
help="print readable assembly, not numeric bytecode",
)
parser.add_argument(
"type", nargs=1, choices=bpftools.generator_names, help="BPF generator type"
)
parser.add_argument(
"parameters", nargs="*", help="parameters passed to the BPF generator"
)

args = parser.parse_args()

if len(args.type) != 1:
parser.print_help()
sys.exit(-1)

name, ret = bpftools.gen(args.type[0],
args.parameters,
assembly=args.assembly,
l3_off=args.offset,
ipversion=4 if not args.inet6 else 6,
negate=args.negate,
)
print ret
name, ret = bpftools.gen(
args.type[0],
args.parameters,
assembly=args.assembly,
l3_off=args.offset,
ipversion=4 if not args.inet6 else 6,
negate=args.negate,
)
print(ret)


if __name__ == "__main__":
Expand Down
30 changes: 19 additions & 11 deletions bpftools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import StringIO as stringio
#!/usr/bin/env python3

from __future__ import print_function
from future import standard_library

standard_library.install_aliases()
import io as stringio
import os
import sys

Expand All @@ -10,29 +16,31 @@
from . import gen_tcpdump

name_to_gen = {
'dns': gen_dns.gen,
'dns_validate': gen_dns_validate.gen,
'p0f': gen_p0f.gen,
'suffix': gen_suffix.gen,
'tcpdump': gen_tcpdump.gen,
}
"dns": gen_dns.gen,
"dns_validate": gen_dns_validate.gen,
"p0f": gen_p0f.gen,
"suffix": gen_suffix.gen,
"tcpdump": gen_tcpdump.gen,
}

generator_names = name_to_gen.keys()
generator_names = list(name_to_gen.keys())


def gen(typename, params, **kwargs):
gentype = name_to_gen[typename]

assembly = kwargs.get('assembly', False)
del kwargs['assembly']
assembly = kwargs.get("assembly", False)
del kwargs["assembly"]

sys.stdout, saved_stdout = stringio.StringIO(), sys.stdout

def new_exit(s):
sys.stdout.seek(0)
data = sys.stdout.read()
sys.stdout = saved_stdout
print data
print(data)
os._exit(s)

sys.exit, saved_exit = new_exit, sys.exit

name = gentype(params, **kwargs)
Expand Down
Loading