Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix packet loss in heavy traffic #151

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion output_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func (o *HTTPOutput) sendRequest(client *http.Client, data []byte) {
request, err := ParseRequest(data)

if err != nil {
log.Println("Cannot parse request", string(data), err)
log.Println("Cannot parse request", string(data[0:32]), err)
return
}

Expand Down
13 changes: 5 additions & 8 deletions raw_socket_listener/listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ func (t *Listener) readRAWSocket() {

defer conn.Close()

buf := make([]byte, 4096*2)

for {
buf := make([]byte, 4096*2)

// Note: ReadFrom receive messages without IP header
n, addr, err := conn.ReadFrom(buf)

Expand All @@ -80,17 +80,14 @@ func (t *Listener) readRAWSocket() {
}

if n > 0 {
t.parsePacket(addr, buf[:n])
go t.parsePacket(addr, buf[:n])
}
}
}

func (t *Listener) parsePacket(addr net.Addr, buf []byte) {
if t.isIncomingDataPacket(buf) {
new_buf := make([]byte, len(buf))
copy(new_buf, buf)

t.c_packets <- ParseTCPPacket(addr, new_buf)
t.c_packets <- ParseTCPPacket(addr, buf)
}
}

Expand Down Expand Up @@ -122,7 +119,7 @@ func (t *Listener) processTCPPacket(packet *TCPPacket) {
defer func() { recover() }()

var message *TCPMessage
m_id := packet.Addr.String() + strconv.Itoa(int(packet.Ack))
m_id := packet.Addr.String() + strconv.Itoa(int(packet.SrcPort)) + strconv.Itoa(int(packet.Ack))

message, ok := t.messages[m_id]

Expand Down
8 changes: 6 additions & 2 deletions raw_socket_listener/tcp_packet.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,25 @@ type TCPPacket struct {

func ParseTCPPacket(addr net.Addr, b []byte) (p *TCPPacket) {
p = &TCPPacket{Data: b}
p.ParseBasic()
p.Parse()
p.Addr = addr

return p
}

// Parse TCP Packet, inspired by: https://github.com/miekg/pcap/blob/master/packet.go
func (t *TCPPacket) Parse() {
t.ParseBasic()
t.SrcPort = binary.BigEndian.Uint16(t.Data[0:2])
t.DestPort = binary.BigEndian.Uint16(t.Data[2:4])
t.Seq = binary.BigEndian.Uint32(t.Data[4:8])
t.Ack = binary.BigEndian.Uint32(t.Data[8:12])
t.DataOffset = (t.Data[12] & 0xF0) >> 4
t.Flags = binary.BigEndian.Uint16(t.Data[12:14]) & 0x1FF
t.Window = binary.BigEndian.Uint16(t.Data[14:16])
t.Checksum = binary.BigEndian.Uint16(t.Data[16:18])
t.Urgent = binary.BigEndian.Uint16(t.Data[18:20])

t.Data = t.Data[t.DataOffset*4:]
}

// ParseBasic set of fields
Expand Down