@@ -2,21 +2,25 @@ package syslog
22
33import (
44 "bufio"
5+ "bytes"
56 "errors"
67 "net"
8+ "strconv"
79 "sync"
810
11+ "time"
12+
913 "github.com/jeromer/syslogparser"
1014 "github.com/jeromer/syslogparser/rfc3164"
1115 "github.com/jeromer/syslogparser/rfc5424"
12- "time"
1316)
1417
1518type Format int
1619
1720const (
1821 RFC3164 Format = 1 + iota // RFC3164: http://www.ietf.org/rfc/rfc3164.txt
1922 RFC5424 // RFC5424: http://www.ietf.org/rfc/rfc5424.txt
23+ RFC6587 // RFC6587: http://www.ietf.org/rfc/rfc6587.txt
2024)
2125
2226type Server struct {
@@ -37,7 +41,7 @@ func NewServer() *Server {
3741 return server
3842}
3943
40- //Sets the syslog format (RFC3164 or RFC5424)
44+ //Sets the syslog format (RFC3164 or RFC5424 or RFC6587 )
4145func (self * Server ) SetFormat (format Format ) {
4246 self .format = format
4347}
@@ -154,8 +158,33 @@ type ScanCloser struct {
154158 closer TimeoutCloser
155159}
156160
161+ func rfc6587ScannerSplit (data []byte , atEOF bool ) (advance int , token []byte , err error ) {
162+ if atEOF && len (data ) == 0 {
163+ return 0 , nil , nil
164+ }
165+
166+ if i := bytes .IndexByte (data , ' ' ); i > 0 {
167+ pLength := data [0 :i ]
168+ length , err := strconv .Atoi (string (pLength ))
169+ if err != nil {
170+ return 0 , nil , err
171+ }
172+ if len (data ) >= length + i + 1 {
173+ //Return the frame with the length removed
174+ return length + i + 1 , data [i + 1 : length + i + 1 ], nil
175+ }
176+ }
177+
178+ // Request more data
179+ return 0 , nil , nil
180+ }
181+
157182func (self * Server ) goScanConnection (connection net.Conn , needClose bool ) {
158183 scanner := bufio .NewScanner (connection )
184+ switch self .format {
185+ case RFC6587 :
186+ scanner .Split (rfc6587ScannerSplit )
187+ }
159188
160189 var scanCloser * ScanCloser
161190 if needClose {
@@ -170,10 +199,12 @@ func (self *Server) goScanConnection(connection net.Conn, needClose bool) {
170199
171200func (self * Server ) scan (scanCloser * ScanCloser ) {
172201 if scanCloser .closer == nil {
202+ // UDP
173203 for scanCloser .Scan () {
174204 self .parser ([]byte (scanCloser .Text ()))
175205 }
176206 } else {
207+ // TCP
177208 loop:
178209 for {
179210 select {
@@ -202,7 +233,7 @@ func (self *Server) parser(line []byte) {
202233 switch self .format {
203234 case RFC3164 :
204235 parser = self .getParserRFC3164 (line )
205- case RFC5424 :
236+ case RFC5424 , RFC6587 :
206237 parser = self .getParserRFC5424 (line )
207238 }
208239
0 commit comments