Revert "TODO epan/dissectors/asn1/kerberos/packet-kerberos-template.c new GSS flags"
[wireshark-sm.git] / tools / generate_udp_pcap.py
blob0d6a6d621e0483ea2a4e18cced816b62a5259fc1
1 #!/usr/bin/env python3
2 '''
3 Convert a CBOR diagnostic notation file into a UDP payload
4 for the encoded cbor.
5 This allows straightforward test and debugging of simple pcap files.
7 Copyright 2021-2024 Brian Sipos <brian.sipos@gmail.com>
9 SPDX-License-Identifier: LGPL-2.1-or-later
10 '''
12 from argparse import ArgumentParser
13 from io import BytesIO
14 import scapy
15 from scapy.layers.l2 import Ether
16 from scapy.layers.inet import IP, UDP
17 from scapy.packet import Raw
18 from scapy.utils import wrpcap
19 from scapy.volatile import RandNum
20 from subprocess import check_output
21 import sys
24 def main():
25 parser = ArgumentParser()
26 parser.add_argument('--infile', default='-',
27 help='The diagnostic text input file, or "-" for stdin')
28 parser.add_argument('--sport', type=int,
29 help='The source port (default is random)')
30 parser.add_argument('--dport', type=int,
31 help='The destination port (default is random)')
32 parser.add_argument('--outfile', default='-',
33 help='The PCAP output file, or "-" for stdout')
34 parser.add_argument('--intype', default='cbordiag',
35 choices=['cbordiag', 'raw'],
36 help='The input data type.')
37 args = parser.parse_args()
39 # First get the CBOR data itself
40 infile_name = args.infile.strip()
41 if infile_name != '-':
42 infile = open(infile_name, 'rb')
43 else:
44 infile = sys.stdin.buffer
46 if args.intype == 'raw':
47 cbordata = infile.read()
48 elif args.intype == 'cbordiag':
49 cbordata = check_output('diag2cbor.rb', stdin=infile)
51 # Write the request directly into pcap
52 outfile_name = args.outfile.strip()
53 if outfile_name != '-':
54 outfile = open(outfile_name, 'wb')
55 else:
56 outfile = sys.stdout.buffer
58 sport = args.sport or RandNum(49152, 65535)
59 dport = args.dport or RandNum(49152, 65535)
61 pkt = Ether()/IP()/UDP(sport=sport, dport=dport)/Raw(cbordata)
62 wrpcap(outfile, pkt)
64 if __name__ == '__main__':
65 sys.exit(main())