Skip to content

Commit

Permalink
fix ip address show
Browse files Browse the repository at this point in the history
  • Loading branch information
chaolihf committed Mar 8, 2024
1 parent ebb71e9 commit 076f1f4
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions pkg/ebpf/uretprobe.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,12 +333,30 @@ func main() {
log.Printf("parsing socket ringbuf event: %s", err)
continue
}
log.Printf("http request from %d:%d to %d:%d , content:%s\n",
socketEvent.SrcAddr, socketEvent.Ports>>2, socketEvent.DstAddr, (socketEvent.Ports&0xff00)>>2,
portBytes := networkToHostOrder(socketEvent.Ports)
log.Printf("http request from %s:%d to %s:%d , content:%s\n",
uint32ToIPString(networkToHostOrder(socketEvent.SrcAddr)),
int(portBytes[0])*256+int(portBytes[1]),
uint32ToIPString(networkToHostOrder(socketEvent.DstAddr)),
int(portBytes[2])*256+int(portBytes[3]),
unix.ByteSliceToString(socketEvent.Payload[:]))
}
}()
time.Sleep(1000000 * time.Second)
time.Sleep(30 * time.Second)
}

func networkToHostOrder(ip uint32) [4]byte {
ipBytes := make([]byte, 4)
binary.BigEndian.PutUint32(ipBytes, ip)
for i, j := 0, len(ipBytes)-1; i < j; i, j = i+1, j-1 {
ipBytes[i], ipBytes[j] = ipBytes[j], ipBytes[i]
}
return [4]byte(ipBytes)
}

func uint32ToIPString(ipBytes [4]byte) string {
netIP := net.IPv4(ipBytes[0], ipBytes[1], ipBytes[2], ipBytes[3])
return netIP.String()
}

func openRawSock(index int) (int, error) {
Expand Down

0 comments on commit 076f1f4

Please sign in to comment.