3 Convert a CBOR diagnostic notation file into an HTTP request
5 This allows straightforward test and debugging of simple pcap files.
7 Copyright 2021 Brian Sipos <brian.sipos@gmail.com>
9 SPDX-License-Identifier: LGPL-2.1-or-later
12 from argparse
import ArgumentParser
13 from io
import BytesIO
15 from scapy
.layers
.l2
import Ether
16 from scapy
.layers
.inet
import IP
, TCP
17 from scapy
.layers
.http
import HTTP
, HTTPRequest
18 from scapy
.packet
import Raw
19 from scapy
.utils
import wrpcap
20 from subprocess
import check_output
25 parser
= ArgumentParser()
26 parser
.add_argument('--content-type', default
='application/cbor',
27 help='The request content-type header')
28 parser
.add_argument('--infile', default
='-',
29 help='The diagnostic text input file, or "-" for stdin')
30 parser
.add_argument('--outfile', default
='-',
31 help='The PCAP output file, or "-" for stdout')
32 parser
.add_argument('--intype', default
='cbordiag',
33 choices
=['cbordiag', 'raw'],
34 help='The input data type.')
35 args
= parser
.parse_args()
37 # First get the CBOR data itself
38 infile_name
= args
.infile
.strip()
39 if infile_name
!= '-':
40 infile
= open(infile_name
, 'rb')
42 infile
= sys
.stdin
.buffer
44 if args
.intype
== 'raw':
45 cbordata
= infile
.read()
46 elif args
.intype
== 'cbordiag':
47 cbordata
= check_output('diag2cbor.rb', stdin
=infile
)
49 # Now synthesize an HTTP request with that body
54 Content_Type
=args
.content_type
,
55 Content_Length
=str(len(cbordata
)),
58 # Write the request directly into pcap
59 outfile_name
= args
.outfile
.strip()
60 if outfile_name
!= '-':
61 outfile
= open(outfile_name
, 'wb')
63 outfile
= sys
.stdout
.buffer
65 pkt
= Ether()/IP()/TCP()/HTTP()/req
68 if __name__
== '__main__':