Skip to content

Commit 84e92cd

Browse files
authored
Merge pull request #521 from okhowang/master
Fix malformed packet in stream based handler
2 parents 00bd5c6 + acdba02 commit 84e92cd

File tree

2 files changed

+15
-46
lines changed

2 files changed

+15
-46
lines changed

Diff for: server/tcp.go

+9-15
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package input
33
import (
44
"bufio"
55
"encoding/binary"
6+
"io"
67
"net"
78
"sync"
89
"sync/atomic"
@@ -58,26 +59,19 @@ func (h *HEPInput) serveTCP(addr string) {
5859
}
5960

6061
func (h *HEPInput) handleTCP(c net.Conn) {
62+
h.handleStream(c, "TCP")
63+
}
64+
65+
func (h *HEPInput) handleStream(c net.Conn, protocol string) {
6166
defer func() {
62-
logp.Info("closing TCP connection from %s", c.RemoteAddr())
67+
logp.Info("closing %s connection from %s", protocol, c.RemoteAddr())
6368
err := c.Close()
6469
if err != nil {
6570
logp.Err("%v", err)
6671
}
6772
}()
6873

6974
r := bufio.NewReader(c)
70-
readBytes := func(buffer []byte) (int, error) {
71-
n := uint(0)
72-
for n < uint(len(buffer)) {
73-
nn, err := r.Read(buffer[n:])
74-
n += uint(nn)
75-
if err != nil {
76-
return 0, err
77-
}
78-
}
79-
return int(n), nil
80-
}
8175
for {
8276
if atomic.LoadUint32(&h.stopped) == 1 {
8377
return
@@ -96,11 +90,11 @@ func (h *HEPInput) handleTCP(c net.Conn) {
9690
return
9791
}
9892
buf := h.buffer.Get().([]byte)
99-
n, err := readBytes(buf[:size])
100-
if err != nil || n > maxPktLen {
93+
n, err := io.ReadFull(r, buf[:size])
94+
if err != nil || n != int(size) {
10195
logp.Warn("%v, unusal packet size with %d bytes", err, n)
10296
atomic.AddUint64(&h.stats.ErrCount, 1)
103-
continue
97+
return
10498
}
10599
h.inputCh <- buf[:n]
106100
atomic.AddUint64(&h.stats.PktCount, 1)

Diff for: server/tls.go

+6-31
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,18 @@ package input
33
import (
44
"crypto/tls"
55
"net"
6+
"path/filepath"
67
"sync"
78
"sync/atomic"
89
"time"
9-
"path/filepath"
1010

11-
"github.com/sipcapture/heplify-server/config"
1211
"github.com/negbie/cert"
1312
"github.com/negbie/logp"
13+
"github.com/sipcapture/heplify-server/config"
1414
)
1515

16-
func parseTLSVersion(versionText string ) uint16 {
17-
switch(versionText){
16+
func parseTLSVersion(versionText string) uint16 {
17+
switch versionText {
1818
case "1.0":
1919
logp.Warn("TLS1.0 is not recommended. Use 1.2 or greater where possible")
2020
return tls.VersionTLS10
@@ -50,7 +50,7 @@ func (h *HEPInput) serveTLS(addr string) {
5050
cPath := config.Setting.TLSCertFolder
5151
minTLSVersion := parseTLSVersion(config.Setting.TLSMinVersion)
5252
// load any existing certs, otherwise generate a new one
53-
ca, err := cert.NewCertificateAuthority( filepath.Join(cPath, "heplify-server") )
53+
ca, err := cert.NewCertificateAuthority(filepath.Join(cPath, "heplify-server"))
5454
if err != nil {
5555
logp.Err("%v", err)
5656
return
@@ -88,30 +88,5 @@ func (h *HEPInput) serveTLS(addr string) {
8888
}
8989

9090
func (h *HEPInput) handleTLS(c net.Conn) {
91-
defer func() {
92-
logp.Info("closing TLS connection from %s", c.RemoteAddr())
93-
err := c.Close()
94-
if err != nil {
95-
logp.Err("%v", err)
96-
}
97-
}()
98-
99-
for {
100-
if atomic.LoadUint32(&h.stopped) == 1 {
101-
return
102-
}
103-
104-
buf := h.buffer.Get().([]byte)
105-
n, err := c.Read(buf)
106-
if err != nil {
107-
logp.Warn("%v from %s", err, c.RemoteAddr())
108-
return
109-
} else if n > maxPktLen {
110-
logp.Warn("received too big packet with %d bytes", n)
111-
atomic.AddUint64(&h.stats.ErrCount, 1)
112-
continue
113-
}
114-
h.inputCh <- buf[:n]
115-
atomic.AddUint64(&h.stats.PktCount, 1)
116-
}
91+
h.handleStream(c, "TLS")
11792
}

0 commit comments

Comments
 (0)