Skip to content

Commit

Permalink
Ip packet differences in wireshark and pingo. Difference in datagram …
Browse files Browse the repository at this point in the history
…lengths.
  • Loading branch information
gokhan-uysal committed Jul 8, 2024
1 parent afaf820 commit c8d9ddf
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 24 deletions.
37 changes: 22 additions & 15 deletions cmd/pingo/main.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
package main

import (
"encoding/hex"
"fmt"
"log"
"syscall"

"github.com/code-brew-lab/pingo/internal/ip"
"github.com/code-brew-lab/pingo/internal/netcore"
)

func main() {
h, err := ip.NewHeaderBuilder().Build()
if err != nil {
log.Fatalln(err)
}

req := h.Marshal()

fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_IP)
fd, err := syscall.Socket(syscall.AF_INET, syscall.SOCK_RAW, syscall.IPPROTO_ICMP)
if err != nil {
fmt.Println(err)
return
}

syscall.Sendto(fd, req, 0, &syscall.SockaddrInet4{
Port: 0,
Addr: [4]byte{127, 0, 0, 1},
})

for {
buf := make([]byte, 1024)
numRead, err := syscall.Read(fd, buf)
if err != nil {
fmt.Println(err)
}

header, i, err := ip.ParseHeader(buf[:numRead])
packet := netcore.NewPacket(buf[:numRead])

fmt.Printf("Raw Packet: %s\n", hex.EncodeToString(packet.Raw()))

header, i, err := ip.ParseHeader(packet.Raw())
if err != nil {
fmt.Println(err)
continue
Expand All @@ -43,3 +36,17 @@ func main() {
fmt.Println(i)
}
}

/*
Wireshark:
Packet_1 [RESP]: 45000054000000003801c12cd8ef2678c0a8016d0000f2c767470000668bcac80001899808090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
Packet_2 [RESP]: 45000054000000003801c12cd8ef2678c0a8016d0000dffa67470001668bcac900019c6308090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
*/

/*
Pingo:
Packet_1 [RESP]: 45004000000000003801c12cd8ef2678c0a8016d0000f2c767470000668bcac80001899808090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
Packet_2 [RESP]: 45004000000000003801c12cd8ef2678c0a8016d0000dffa67470001668bcac900019c6308090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f3031323334353637
*/
Binary file removed icmp_capture.pcapng
Binary file not shown.
14 changes: 5 additions & 9 deletions internal/ip/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ package ip

import (
"encoding/binary"
"encoding/hex"
"errors"
"fmt"

"github.com/code-brew-lab/pingo/internal/checksum"
"github.com/code-brew-lab/pingo/internal/netcore/checksum"
)

type (
Expand Down Expand Up @@ -46,8 +45,8 @@ func ParseHeader(h []byte) (*Header, int, error) {
}

headerLen := h[0] & 0x0F
fmt.Println(hex.EncodeToString(h[:headerLen*headerMultiplier]))
if !checksum.Verify(h[:headerLen*headerMultiplier]) {
totalLen := headerLen * headerMultiplier
if !checksum.Verify(h[:totalLen]) {
return nil, 0, errors.New("ip.ParseHeader: Checksum verification failed")
}

Expand All @@ -70,9 +69,9 @@ func ParseHeader(h []byte) (*Header, int, error) {
copy(dstIP, h[16:20])

var options []byte
if headerLen > 5 {
if totalLen > minHeaderLen {
options = make([]byte, (headerLen-5)*headerMultiplier)
copy(options, h[20:headerLen*headerMultiplier])
copy(options, h[minHeaderLen:totalLen])
}

return &Header{
Expand Down Expand Up @@ -180,6 +179,3 @@ func (h *Header) Marshal() []byte {

return buff
}

// Wireshark => Req: 45000054df2100004001da0ac0a8016dd8ef2678, Resp: 45000054000000003801c12cd8ef2678c0a8016d
// Pingo => 45004000000000003801c12cd8ef2678c0a8016d
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ var (
input: []byte{0x45, 0x00, 0x00, 0x28, 0x04, 0x59, 0x00, 0x00, 0x40, 0x06, 0x00, 0x00, 0xc0, 0xa8, 0x01, 0x6d, 0x0d, 0x45, 0x44, 0x40},
expected: 25309,
},
{
input: []byte{0x45, 0x00, 0x05, 0x58, 0xe8, 0x82, 0x00, 0x00, 0x40, 0x01, 0x00, 0x00, 0xc0, 0xa8, 0x01, 0x6d, 0xc0, 0xa8, 0x01, 0x01},
expected: 2404,
},
{
input: []byte{0x45, 0x00, 0x00, 0x54, 0xca, 0xf3, 0x00, 0x00, 0x38, 0x01, 0x08, 0xab, 0x68, 0x10, 0x00, 0x00, 0xc0, 0xa8, 0x01, 0x6d},
expected: 34021,
},
}

verifyCases = []VerifyTestCase{
Expand All @@ -40,6 +48,22 @@ var (
input: []byte{0x45, 0x00, 0x05, 0x58, 0x4e, 0xa0, 0x00, 0x00, 0x40, 0x01, 0x43, 0x2a, 0xc0, 0xa8, 0x01, 0x6d, 0x11, 0xfd, 0x0f, 0xc9},
isValid: true,
},
{
input: []byte{0x45, 0x00, 0x05, 0x58, 0xe8, 0x82, 0x00, 0x00, 0x40, 0x01, 0x09, 0x64, 0xc0, 0xa8, 0x01, 0x6d, 0xc0, 0xa8, 0x01, 0x01},
isValid: true,
},
{
input: []byte{0x45, 0x00, 0x00, 0x54, 0xca, 0xf3, 0x00, 0x00, 0x38, 0x01, 0x08, 0xab, 0x68, 0x10, 0x84, 0xe5, 0xc0, 0xa8, 0x01, 0x6d},
isValid: true,
},
{
input: []byte{0x45, 0xdf, 0x05, 0x58, 0x4e, 0xa0, 0x00, 0x00, 0x40, 0x01, 0x43, 0x2a, 0xc0, 0xa8, 0x01, 0x6d, 0x11, 0xfd, 0x0f, 0xc9},
isValid: false,
},
{
input: []byte{0x45, 0x00, 0x00, 0x28, 0x04, 0x59, 0x00, 0x00, 0x40, 0x06, 0x60, 0xdd, 0xc0, 0xa8, 0x01, 0x6d, 0x0d, 0x45, 0x44, 0x40},
isValid: false,
},
}
)

Expand Down
File renamed without changes.
25 changes: 25 additions & 0 deletions internal/netcore/packet.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package netcore

import (
"time"
)

type Packet struct {
timestamp time.Time
raw []byte
}

func NewPacket(raw []byte) *Packet {
return &Packet{
timestamp: time.Now(),
raw: raw,
}
}

func (p *Packet) Timestamp() time.Time {
return p.timestamp
}

func (p *Packet) Raw() []byte {
return p.raw
}

0 comments on commit c8d9ddf

Please sign in to comment.