Skip to content

jimdiroffii/ip-protocol-from-scratch

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

24 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

IP Protocol from Scratch

This is an effort to fully understand IPv4 and IPv6 by building up the protocols from scratch and using them.

There are different "versions" split into different branches. My first iteration for IPv4 and ICMP is in v1, the second in v2, and my current version is v3. Each version is a progression of understanding and additional features. Each one has been rewritten from scratch, taking advantages of the lessons learned from the previous version.

Version 1

The first version if the IP Header and ICMP Header/Payload was built in C. The structure of the program, including comments, is extremely verbose. Usage of assert is heavy, and overvalidates the data structures as they are built up. This code was built for purely educational purposes to practice the bit manipulation and other techniques required to construct a custom IP packet and send it out "over the wire."

Compile:

gcc -Wall -Wextra ip_stack_v1.c -o ip_stack_v1; sudo ./ip_stack_v1

Output:

Test 1 Passed: ipv4_header offsets are correctly positioned.
Test 2 Passed: ipv4_header is exactly 20 bytes.
Test 3 Passed: version_ihl packed correctly (0x45)
Test 4 Passed: Type of Service and Total Length are correct.
Test 5 Passed: Exactly 4 contiguous bytes have been populated.
Test 6 Passed: Identification, Flags and Fragment Offset are correct.
Test 7 Passed: Exactly 8 contiguous bytes have been populated.
Test 8 Passed: TTL, Protocol, and Checksum correctly set
Test 9 Passed: Exactly 12 contiguous bytes have been populated.
Test 10 Passed: SRC and DST correctly set to 127.0.0.1
Test 11 Passed: Exactly 20 contiguous bytes have been populated.
Header complete.
Test 12 Passed: Checksum validation with dummy data.
Test 13 Passed: Checksum validation with constructed packet header.

Raw IPv4 Header (Wire Format):
45 00 00 14 
BE EF 00 00 
40 01 BD F7 
7F 00 00 01 
7F 00 00 01 

Raw IPv4 Header (Wire Format) | Packet 2:
45 00 00 14 
BE EF 00 00 
40 01 BD F7 
7F 00 00 01 
7F 00 00 01 

Test 14 Passed: ICMP header set and offset is exactly 20 bytes.
New IPv4 Header Checksum: F3BD
Test 15 Passed: Total length updated and checksum recalculated.
Test 16 Passed: ICMP Echo specific fields mapped precisely at offset 24.
New IPv4 Header Checksum: EFBD
Test 17 Passed: Total length updated and checksum recalculated.
Test 18 Passed: ICMP Checksum calculated as E5CA
Test 19 Passed: Checksum algorithm now handles odd-byte payloads.
Test 20 Passed: Payload attached. IP Total Length: 33. Checksums updated.

Raw IPv4 + ICMP Datagram (Wire Format):
45 00 00 21 
BE EF 00 00 
40 01 BD EA 
7F 00 00 01 
7F 00 00 01 
08 00 02 39 
12 34 00 01 
48 45 4C 4C 
4F 
Phase 4: Raw socket successfully created with IP_HDRINCL (fd: 3).
Test 21 Passed: Kernel routing structure (sockaddr_in) populated.
Test 22 Passed: Successfully injected 33 bytes onto the wire.
Use `sudo tcpdump -vv -i lo -n icmp` to see the results.
Listening for Echo Reply...
Test 25 Passed: Ignored our own request and caught the true Echo Reply!
Test 26 Passed: Echo Reply Identifier and Sequence match our Request.
Test 27 Passed: Payload successfully extracted and verified as 'HELLO'.

Version 2

This version refactored much of the code from V1, while including some more dynamic elements. Now, any IP can be sent a customized ICMP echo request without recompiling. Adjustable payloads, multiple requests, cli arguments, reply latency, and sequence numbers are now supported.

> sudo ./ip_stack_v2 -s 192.168.50.128 -d 192.168.50.230 -p "TESTING" -c 3
** ICMP Echo Sender **
Source Address: 192.168.50.128
Destination Address: 192.168.50.230
Payload: TESTING (Len: 7)
Number of packets: 3

Bytes sent in packet number 1: 35
Bytes received: 35, time=0.266 ms
Bytes sent in packet number 2: 35
Bytes received: 35, time=0.166 ms
Bytes sent in packet number 3: 35
Bytes received: 35, time=0.190 ms

About

This is an effort to fully understand IPv4 and IPv6 by building up the protocols from scratch and using them with ICMP.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages