Skip to content

Commit

Permalink
Creating icmp stream.
Browse files Browse the repository at this point in the history
  • Loading branch information
gokhan-uysal committed Jul 12, 2024
1 parent bc0a318 commit 95f04a7
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 44 deletions.
56 changes: 19 additions & 37 deletions cmd/pingo/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package main

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

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

func main() {
Expand All @@ -16,40 +16,22 @@ func main() {
}
defer syscall.Close(fd)

icmp, err := netcore.NewICMP(netcore.ControlKindEchoRequest, 1)
if err != nil {
fmt.Println(err)
return
}

addr := &syscall.SockaddrInet4{
Port: 0,
Addr: [4]byte{216, 239, 38, 120},
}

err = syscall.Sendto(fd, icmp.Marshal(), 0, addr)
if err != nil {
fmt.Println(err)
return
}

for {
buff := make([]byte, 1024)
numRead, err := syscall.Read(fd, buff)
if err != nil {
fmt.Println(err)
continue
doneChan := make(chan bool)
go func() {
dataChan, errChan := pingo.Read(doneChan, fd)

for {
select {
case d := <-dataChan:
ip := d.IP()
icmp := d.ICMP()
fmt.Printf("%s -> %s\n", ip.SourceIP(), ip.DestinationIP())
fmt.Printf("Kind: %s, Status: %s\n", icmp.Kind(), icmp.Code().String(icmp.Kind()))
case e := <-errChan:
fmt.Println(e)
}
}
fmt.Printf("Raw Datagram:%s\n", hex.EncodeToString(buff[:numRead]))
d, err := netcore.ParseDatagram(buff[:numRead], netcore.ProtocolICMP)
if err != nil {
fmt.Println(err)
continue
}
ip := d.IP()
icmp := d.ICMP()
fmt.Printf("%s -> %s\n", ip.SourceIP(), ip.DestinationIP())
fmt.Printf("Kind: %s, Status: %s\n", icmp.Kind(), icmp.Code().String(icmp.Kind()))

}
}()
time.Sleep(time.Second * 5)
doneChan <- true
}
9 changes: 2 additions & 7 deletions pkg/netcore/icmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package netcore

import (
"encoding/binary"
"errors"
"fmt"
"math/rand"

Expand Down Expand Up @@ -53,19 +52,15 @@ func ParseICMP(b []byte) (*ICMP, int, error) {
}, len(b), nil
}

func NewICMP(kind ControlKind, seq uint16) (*ICMP, error) {
if seq < 0 {
return nil, errors.New("netcore.NewICMP: Sequence number should be greater than 0")
}

func NewICMP(kind ControlKind, seq uint16) *ICMP {
icmp := &ICMP{
kind: kind,
code: 0,
id: uint16(rand.Uint32()),
seq: seq,
}

return icmp, nil
return icmp
}

func (icmp *ICMP) Marshal() []byte {
Expand Down
44 changes: 44 additions & 0 deletions pkg/pingo/socket.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package pingo

import (
"syscall"

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

func Read(doneChan <-chan bool, fd int) (<-chan *netcore.Datagram, <-chan error) {
dataChan := make(chan *netcore.Datagram, 10)
errChan := make(chan error, 10)

go func(dataChan chan<- *netcore.Datagram, errChan chan<- error, fd int) {
defer close(dataChan)
defer close(errChan)

for {
select {
case <-doneChan:
return
default:
read(dataChan, errChan, fd)
}
}
}(dataChan, errChan, fd)

return dataChan, errChan
}

func read(dataChan chan<- *netcore.Datagram, errChan chan<- error, fd int) {
buff := make([]byte, 1024)
numRead, err := syscall.Read(fd, buff)
if err != nil {
errChan <- err
return
}
d, err := netcore.ParseDatagram(buff[:numRead], netcore.ProtocolICMP)
if err != nil {
errChan <- err
return
}

dataChan <- d
}

0 comments on commit 95f04a7

Please sign in to comment.