Skip to content

Commit 97e2282

Browse files
committed
Merge pull request #21 from abligh/fix-rfc6587-parsing-on-udp
Fix RFC6587 parsing on UDP.
2 parents a6e53ed + 31402d4 commit 97e2282

File tree

2 files changed

+46
-3
lines changed

2 files changed

+46
-3
lines changed

server.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,13 @@ func (s *Server) goParseDatagrams() {
299299
if !ok {
300300
return
301301
}
302-
s.parser(msg.message, msg.client)
302+
if sf := s.format.GetSplitFunc(); sf != nil {
303+
if _, token, err := sf(msg.message, true); err == nil {
304+
s.parser(token, msg.client)
305+
}
306+
} else {
307+
s.parser(msg.message, msg.client)
308+
}
303309
}
304310
}
305311
}()

server_test.go

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package syslog
22

33
import (
4+
"fmt"
45
"io"
56
"net"
67
"testing"
@@ -17,6 +18,7 @@ type ServerSuite struct {
1718

1819
var _ = Suite(&ServerSuite{})
1920
var exampleSyslog = "<31>Dec 26 05:08:46 hostname tag[296]: content"
21+
var exampleRFC5424Syslog = "<34>1 2003-10-11T22:14:15.003Z mymachine.example.com su - ID47 - 'su root' failed for lonvick on /dev/pts/8"
2022

2123
func (s *ServerSuite) TestTailFile(c *C) {
2224
handler := new(HandlerMock)
@@ -27,12 +29,12 @@ func (s *ServerSuite) TestTailFile(c *C) {
2729
server.ListenTCP("0.0.0.0:5141")
2830

2931
go func(server *Server) {
30-
time.Sleep(100 * time.Microsecond)
32+
time.Sleep(100 * time.Millisecond)
3133

3234
serverAddr, _ := net.ResolveUDPAddr("udp", "localhost:5141")
3335
con, _ := net.DialUDP("udp", nil, serverAddr)
3436
con.Write([]byte(exampleSyslog))
35-
time.Sleep(100 * time.Microsecond)
37+
time.Sleep(100 * time.Millisecond)
3638

3739
server.Kill()
3840
}(server)
@@ -146,3 +148,38 @@ func (s *ServerSuite) TestTcpTimeout(c *C) {
146148
c.Check(handler.LastMessageLength, Equals, int64(0))
147149
c.Check(handler.LastError, IsNil)
148150
}
151+
152+
func (s *ServerSuite) TestUDP3164(c *C) {
153+
handler := new(HandlerMock)
154+
server := NewServer()
155+
server.SetFormat(RFC3164)
156+
server.SetHandler(handler)
157+
server.SetTimeout(10)
158+
server.goParseDatagrams()
159+
server.datagramChannel <- DatagramMessage{[]byte(exampleSyslog), "0.0.0.0"}
160+
close(server.datagramChannel)
161+
server.Wait()
162+
c.Check(handler.LastLogParts["hostname"], Equals, "hostname")
163+
c.Check(handler.LastLogParts["tag"], Equals, "tag")
164+
c.Check(handler.LastLogParts["content"], Equals, "content")
165+
c.Check(handler.LastMessageLength, Equals, int64(len(exampleSyslog)))
166+
c.Check(handler.LastError, IsNil)
167+
}
168+
169+
func (s *ServerSuite) TestUDP6587(c *C) {
170+
handler := new(HandlerMock)
171+
server := NewServer()
172+
server.SetFormat(RFC6587)
173+
server.SetHandler(handler)
174+
server.SetTimeout(10)
175+
server.goParseDatagrams()
176+
framedSyslog := []byte(fmt.Sprintf("%d %s", len(exampleRFC5424Syslog), exampleRFC5424Syslog))
177+
server.datagramChannel <- DatagramMessage{[]byte(framedSyslog), "0.0.0.0"}
178+
close(server.datagramChannel)
179+
server.Wait()
180+
c.Check(handler.LastLogParts["hostname"], Equals, "mymachine.example.com")
181+
c.Check(handler.LastLogParts["facility"], Equals, 4)
182+
c.Check(handler.LastLogParts["message"], Equals, "'su root' failed for lonvick on /dev/pts/8")
183+
c.Check(handler.LastMessageLength, Equals, int64(len(exampleRFC5424Syslog)))
184+
c.Check(handler.LastError, IsNil)
185+
}

0 commit comments

Comments
 (0)