Skip to content

Commit

Permalink
Merge pull request #21 from abligh/fix-rfc6587-parsing-on-udp
Browse files Browse the repository at this point in the history
Fix RFC6587 parsing on UDP.
  • Loading branch information
mcuadros committed Jul 16, 2015
2 parents a6e53ed + 31402d4 commit 97e2282
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
8 changes: 7 additions & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,13 @@ func (s *Server) goParseDatagrams() {
if !ok {
return
}
s.parser(msg.message, msg.client)
if sf := s.format.GetSplitFunc(); sf != nil {
if _, token, err := sf(msg.message, true); err == nil {
s.parser(token, msg.client)
}
} else {
s.parser(msg.message, msg.client)
}
}
}
}()
Expand Down
41 changes: 39 additions & 2 deletions server_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package syslog

import (
"fmt"
"io"
"net"
"testing"
Expand All @@ -17,6 +18,7 @@ type ServerSuite struct {

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

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

go func(server *Server) {
time.Sleep(100 * time.Microsecond)
time.Sleep(100 * time.Millisecond)

serverAddr, _ := net.ResolveUDPAddr("udp", "localhost:5141")
con, _ := net.DialUDP("udp", nil, serverAddr)
con.Write([]byte(exampleSyslog))
time.Sleep(100 * time.Microsecond)
time.Sleep(100 * time.Millisecond)

server.Kill()
}(server)
Expand Down Expand Up @@ -146,3 +148,38 @@ func (s *ServerSuite) TestTcpTimeout(c *C) {
c.Check(handler.LastMessageLength, Equals, int64(0))
c.Check(handler.LastError, IsNil)
}

func (s *ServerSuite) TestUDP3164(c *C) {
handler := new(HandlerMock)
server := NewServer()
server.SetFormat(RFC3164)
server.SetHandler(handler)
server.SetTimeout(10)
server.goParseDatagrams()
server.datagramChannel <- DatagramMessage{[]byte(exampleSyslog), "0.0.0.0"}
close(server.datagramChannel)
server.Wait()
c.Check(handler.LastLogParts["hostname"], Equals, "hostname")
c.Check(handler.LastLogParts["tag"], Equals, "tag")
c.Check(handler.LastLogParts["content"], Equals, "content")
c.Check(handler.LastMessageLength, Equals, int64(len(exampleSyslog)))
c.Check(handler.LastError, IsNil)
}

func (s *ServerSuite) TestUDP6587(c *C) {
handler := new(HandlerMock)
server := NewServer()
server.SetFormat(RFC6587)
server.SetHandler(handler)
server.SetTimeout(10)
server.goParseDatagrams()
framedSyslog := []byte(fmt.Sprintf("%d %s", len(exampleRFC5424Syslog), exampleRFC5424Syslog))
server.datagramChannel <- DatagramMessage{[]byte(framedSyslog), "0.0.0.0"}
close(server.datagramChannel)
server.Wait()
c.Check(handler.LastLogParts["hostname"], Equals, "mymachine.example.com")
c.Check(handler.LastLogParts["facility"], Equals, 4)
c.Check(handler.LastLogParts["message"], Equals, "'su root' failed for lonvick on /dev/pts/8")
c.Check(handler.LastMessageLength, Equals, int64(len(exampleRFC5424Syslog)))
c.Check(handler.LastError, IsNil)
}

0 comments on commit 97e2282

Please sign in to comment.