Let's take a look at the Scapy code. How are you processing the structure?

Purpose

I have a little errand, for example, what does the Scapy code look like? What is Scapy? Scapy is a powerful Python-based interactive packet manipulation program and library. (Quote: https://pypi.org/project/scapy/)

** Python has no struct **, so I confirmed how to process structural data such as packets.

The target code is https://github.com/secdev/scapy

All citation codes are above.

What i found

Overview

In the code, there were the following two structure-related processes

Basically, the former is used.

struct module

↓ It was packed with the following feeling.

    psdhdr = struct.pack("!4s4sHH",
                         inet_pton(socket.AF_INET, u.src),
                         inet_pton(socket.AF_INET, u.dst),
                         proto,
                         ln)

Structure class

↓ There is the following code, but is it for Windows pcap support? ?? I didn't know how to use it.

class sockaddr_in(Structure):
    _fields_ = [("sin_family", SHORT),
                ("sin_port", USHORT),
                ("sin_addr", in_addr),
                ("sin_zero", 8 * CHAR)]   

Summary

I checked how the structural data of the packet is processed by the famous Scapy. The ** struct module ** was used. (There is a bit of a misnomer above. It may be appropriate to say that the struct module was used in the part where the data is stored.) In terms of how structures are handled in c language etc., I think it is correct to say that the parts other than the structuring are normally members of the class.

If you have any comments, please let us know. : candy:

Recommended Posts

Let's take a look at the Scapy code. How are you processing the structure?
Let's take a look at the Scapy code. Overload of special methods __div__, __getitem__ and so on.
Take a look at the Python built-in exception tree structure
Let's take a look at the feature map of YOLO v3
Take a look at the built-in exception tree structure in Python 3.8.2
Take a peek at the processing of LightGBM Tuner
Take a closer look at the Kaggle / Titanic tutorial
Challenge image classification by TensorFlow2 + Keras 2 ~ Let's take a closer look at the input data ~
[Go] Take a look at io.Writer
Take a look at Django's template.
Let's look at the scatter plot before data analysis
Take a look at profiling and dumping with Dataflow
Let's take a quick look at CornerNet, an object detector that does not use anchors.
Let's take a look at the infection tendency of the new coronavirus COVID-19 in each country and the medical response status (additional information).
A quick look at your profile within the django app
Until you look back at him like "You are a data scientist? 100 years faster" -Until you make Docker on Win 10 Home and start the SQL version of "Data Science 100 Knock (Structured Data Processing)"-