Repository: gojue/ecapture
File: user/event/event_openssl.go (Go)

Code before:
	Daddr       [16]byte `json:"daddr"`
	Comm        [16]byte `json:"Comm"`
	TimestampNs uint64   `json:"timestampNs"`
	Sock        uint64   `json:"sock"`
	Pid         uint32   `json:"pid"`
	Tid         uint32   `json:"tid"`
	Fd          uint32   `json:"fd"`
	Family      uint16   `json:"family"`
	Sport       uint16   `json:"sport"`
	Dport       uint16   `json:"dport"`
	IsDestroy   uint8    `json:"isDestroy"`
	Pad         [7]byte  `json:"-"`

	// NOTE: do not leave padding hole in this struct.
}
type ConnDataEvent struct {
	eventType EventType
	connDataEvent
	Tuple string `json:"tuple"`
}

func (ce *ConnDataEvent) Decode(payload []byte) (err error) {
	data := unsafe.Slice((*byte)(unsafe.Pointer(&ce.connDataEvent)), int(unsafe.Sizeof(ce.connDataEvent)))
	copy(data, payload)

        var saddr, daddr netip.Addr
        if ce.Family == 2 { //unix.AF_INET
            saddr = netip.AddrFrom4([4]byte(ce.Saddr[:4]))
            daddr = netip.AddrFrom4([4]byte(ce.Daddr[:4]))
        } else {
            saddr = netip.AddrFrom16(ce.Saddr)
            daddr = netip.AddrFrom16(ce.Daddr)
        }

        ce.Tuple = fmt.Sprintf("%s_%d-%s_%d", saddr, ce.Sport, daddr, ce.Dport)
        return nil
}

func (ce *ConnDataEvent) StringHex() string {
	s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, FD:%d, Tuple: %s", ce.Pid, commStr(ce.Comm[:]), ce.Tid, ce.Fd, ce.Tuple)
	return s
}

func (ce *ConnDataEvent) String() string {
	s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, FD:%d, Tuple: %s", ce.Pid, commStr(ce.Comm[:]), ce.Tid, ce.Fd, ce.Tuple)
	return s
}

func (ce *ConnDataEvent) Clone() IEventStruct {
	event := new(ConnDataEvent)
	event.eventType = EventTypeModuleData

Review comment (suggestion):
Pls 
```Go
        if ce.Family == unix.AF_INET {
```


Code after:
	Saddr       [16]byte `json:"saddr"`
	Daddr       [16]byte `json:"daddr"`
	Comm        [16]byte `json:"Comm"`
	TimestampNs uint64   `json:"timestampNs"`
	Sock        uint64   `json:"sock"`
	Pid         uint32   `json:"pid"`
	Tid         uint32   `json:"tid"`
	Fd          uint32   `json:"fd"`
	Family      uint16   `json:"family"`
	Sport       uint16   `json:"sport"`
	Dport       uint16   `json:"dport"`
	IsDestroy   uint8    `json:"isDestroy"`
	Pad         [7]byte  `json:"-"`

	// NOTE: do not leave padding hole in this struct.
}
type ConnDataEvent struct {
	eventType EventType
	connDataEvent
	Tuple string `json:"tuple"`
}

func (ce *ConnDataEvent) Decode(payload []byte) (err error) {
	data := unsafe.Slice((*byte)(unsafe.Pointer(&ce.connDataEvent)), int(unsafe.Sizeof(ce.connDataEvent)))
	copy(data, payload)

        if ce.Family == unix.AF_INET {
            saddr, daddr := netip.AddrFrom4([4]byte(ce.Saddr[:4])), netip.AddrFrom4([4]byte(ce.Saddr[:4]))
            ce.Tuple = fmt.Sprintf("%s:%d-%s:%d", saddr, ce.Sport, daddr, ce.Dport)
        } else {
            saddr, daddr := netip.AddrFrom16(ce.Saddr), netip.AddrFrom16(ce.Daddr)
            ce.Tuple = fmt.Sprintf("[%s]:%d-[%s]:%d", saddr, ce.Sport, daddr, ce.Dport)
        }

        return nil
}

func (ce *ConnDataEvent) StringHex() string {
	s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, FD:%d, Tuple: %s", ce.Pid, commStr(ce.Comm[:]), ce.Tid, ce.Fd, ce.Tuple)
	return s
}

func (ce *ConnDataEvent) String() string {
	s := fmt.Sprintf("PID:%d, Comm:%s, TID:%d, FD:%d, Tuple: %s", ce.Pid, commStr(ce.Comm[:]), ce.Tid, ce.Fd, ce.Tuple)
	return s
}

func (ce *ConnDataEvent) Clone() IEventStruct {
	event := new(ConnDataEvent)
	event.eventType = EventTypeModuleData
	return event