user506710
user506710

Reputation:

Creating a IPv4 packet header in python

For my larger project i need to start with creating a IPv4 network packet ( for simulation )

for the same I need to write a function that creates a header out of the passed data which has the source ip , dest ip and all the necessary fields

def convertIpv4( data ):
     pass

For this I need you to guide me in the right direction.

Firstly I need to store the IP in 32 bits so for that if I have a string "192.168.2.1" what is the most efficient way to convert it into bytes and that too a size of 32 ??

Also if I create first a normal class with stuff like version = 4 and sourceip and dest ip then is there a way to convert it directly into a byte array with the position of objects just like the following header IPHeader

Please tell how should i proceed....

Upvotes: 0

Views: 7301

Answers (2)

Rumple Stiltskin
Rumple Stiltskin

Reputation: 10395

Scapy can create IP headers easily.
Here they have an example on creating IP header.

Upvotes: 1

ThiefMaster
ThiefMaster

Reputation: 318488

Have a look at the PyIP module.

Upvotes: 0

Related Questions