Debian Codegraph @

rfc3164/rfc3164.go

raw symbols SCIP
1 package rfc3164
2
3 import (
4 "bytes"
5 "math"
6 "time"
7
8 "github.com/jeromer/syslogparser"
9 "github.com/jeromer/syslogparser/parsercommon"
10 )
11
12 const (
13 // according to https://tools.ietf.org/html/rfc3164#section-4.1
14 // "The total length of the packet MUST be 1024 bytes or less"
15 // However we will accept a bit more while protecting from exhaustion
16 MAX_PACKET_LEN = 2048
17 )
18
19 type Parser struct {
20 buff []byte
21 cursor int
22 l int
23 priority *parsercommon.Priority
24 version int
25 header *header
26 message *message
27 location *time.Location
28 hostname string
29 customTag string
30 customTimestampFormat string
31 }
32
33 type header struct {
34 timestamp time.Time
35 hostname string
36 }
37
38 type message struct {
39 tag string
40 content string
41 }
42
43 func NewParser(buff []byte) *Parser {
44 return &Parser{
45 buff: buff,
46 cursor: 0,
47 location: time.UTC,
48 l: int(
49 math.Min(
50 float64(len(buff)),
51 MAX_PACKET_LEN,
52 ),
53 ),
54 }
55 }
56
57 // Forces a priority for this parser. Priority will not be parsed.
58 func (p *Parser) WithPriority(pri *parsercommon.Priority) {
59 p.priority = pri
60 }
61
62 // Forces a location. UTC will be used otherwise.
63 func (p *Parser) WithLocation(l *time.Location) {
64 p.location = l
65 }
66
67 // Forces a hostname. Hostname will not be parsed
68 func (p *Parser) WithHostname(h string) {
69 p.hostname = h
70 }
71
72 // Forces a tag. Tag will not be parsed
73 func (p *Parser) WithTag(t string) {
74 p.customTag = t
75 }
76
77 // Forces a given time format.
78 // Refer to pkg/time layouts for more informations
79 // By default the following formats will be tried in order:
80 // Jan 02 15:04:05
81 // Jan 2 15:04:05
82 // The timezone MUST be specified using WithLocation() and
83 // not using WithTimestampFormat
84 func (p *Parser) WithTimestampFormat(s string) {
85 p.customTimestampFormat = s
86 }
87
88 // DEPRECATED. Use WithLocation() instead
89 func (p *Parser) Location(location *time.Location) {
90 p.WithLocation(location)
91 }
92
93 // DEPRECATED. Use WithHostname() instead
94 func (p *Parser) Hostname(hostname string) {
95 p.WithHostname(hostname)
96 }
97
98 func (p *Parser) Parse() error {
99 p.version = parsercommon.NO_VERSION
100
101 pri, err := p.parsePriority()
102 if err != nil {
103 return err
104 }
105
106 p.priority = pri
107
108 hdr, err := p.parseHeader()
109 if err != nil {
110 return err
111 }
112
113 p.header = hdr
114
115 if p.buff[p.cursor] == ' ' {
116 p.cursor++
117 }
118
119 msg, err := p.parsemessage()
120 if err != parsercommon.ErrEOL {
121 return err
122 }
123
124 p.message = msg
125
126 return nil
127 }
128
129 func (p *Parser) Dump() syslogparser.LogParts {
130 return syslogparser.LogParts{
131 "timestamp": p.header.timestamp,
132 "hostname": p.header.hostname,
133 "tag": p.message.tag,
134 "content": p.message.content,
135 "priority": p.priority.P,
136 "facility": p.priority.F.Value,
137 "severity": p.priority.S.Value,
138 }
139 }
140
141 func (p *Parser) parsePriority() (*parsercommon.Priority, error) {
142 if p.priority != nil {
143 return p.priority, nil
144 }
145
146 return parsercommon.ParsePriority(
147 p.buff, &p.cursor, p.l,
148 )
149 }
150
151 // HEADER: TIMESTAMP + HOSTNAME (or IP)
152 // https://tools.ietf.org/html/rfc3164#section-4.1.2
153 func (p *Parser) parseHeader() (*header, error) {
154 var err error
155
156 if p.buff[p.cursor] == ' ' {
157 p.cursor++
158 }
159
160 ts, err := p.parseTimestamp()
161 if err != nil {
162 return nil, err
163 }
164
165 h, err := p.parseHostname()
166 if err != nil {
167 return nil, err
168 }
169
170 hdr := &header{
171 timestamp: ts,
172 hostname: h,
173 }
174
175 return hdr, nil
176 }
177
178 // MSG: TAG + CONTENT
179 // https://tools.ietf.org/html/rfc3164#section-4.1.3
180 func (p *Parser) parsemessage() (*message, error) {
181 var err error
182
183 tag, err := p.parseTag()
184 if err != nil {
185 return nil, err
186 }
187
188 content, err := p.parseContent()
189 if err != parsercommon.ErrEOL {
190 return nil, err
191 }
192
193 msg := &message{
194 tag: tag,
195 content: content,
196 }
197
198 return msg, err
199 }
200
201 // https://tools.ietf.org/html/rfc3164#section-4.1.2
202 func (p *Parser) parseTimestamp() (time.Time, error) {
203 var ts time.Time
204 var err error
205 var tsFmtLen int
206 var sub []byte
207
208 tsFmts := []string{
209 "Jan 02 15:04:05",
210 "Jan 2 15:04:05",
211 }
212
213 if p.customTimestampFormat != "" {
214 tsFmts = []string{
215 p.customTimestampFormat,
216 }
217 }
218
219 found := false
220 for _, tsFmt := range tsFmts {
221 tsFmtLen = len(tsFmt)
222
223 if p.cursor+tsFmtLen > p.l {
224 continue
225 }
226
227 sub = p.buff[p.cursor : tsFmtLen+p.cursor]
228 ts, err = time.ParseInLocation(
229 tsFmt, string(sub), p.location,
230 )
231
232 if err == nil {
233 found = true
234 break
235 }
236 }
237
238 if !found {
239 p.cursor = tsFmtLen
240
241 // XXX : If the timestamp is invalid we try to push the cursor one byte
242 // XXX : further, in case it is a space
243 if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
244 p.cursor++
245 }
246
247 return ts, parsercommon.ErrTimestampUnknownFormat
248 }
249
250 fixTimestampIfNeeded(&ts)
251
252 p.cursor += tsFmtLen
253
254 if (p.cursor < p.l) && (p.buff[p.cursor] == ' ') {
255 p.cursor++
256 }
257
258 return ts, nil
259 }
260
261 func (p *Parser) parseHostname() (string, error) {
262 if p.hostname != "" {
263 return p.hostname, nil
264 }
265
266 return parsercommon.ParseHostname(
267 p.buff, &p.cursor, p.l,
268 )
269 }
270
271 // http://tools.ietf.org/html/rfc3164#section-4.1.3
272 func (p *Parser) parseTag() (string, error) {
273 if p.customTag != "" {
274 return p.customTag, nil
275 }
276
277 var b byte
278 var tag []byte
279 var err error
280 var enough bool
281
282 previous := p.cursor
283
284 // "The TAG is a string of ABNF alphanumeric characters that MUST NOT exceed 32 characters."
285 to := int(
286 math.Min(
287 float64(p.l),
288 float64(p.cursor+32),
289 ),
290 )
291
292 for p.cursor < to {
293 b = p.buff[p.cursor]
294
295 if b == ' ' {
296 p.cursor++
297 break
298 }
299
300 if b == '[' || b == ']' || b == ':' || enough {
301 enough = true
302 p.cursor++
303 continue
304 }
305
306 tag = append(tag, b)
307 p.cursor++
308 }
309
310 if len(tag) == 0 {
311 p.cursor = previous
312 }
313
314 return string(tag), err
315 }
316
317 func (p *Parser) parseContent() (string, error) {
318 if p.cursor > p.l {
319 return "", parsercommon.ErrEOL
320 }
321
322 content := bytes.Trim(
323 p.buff[p.cursor:p.l], " ",
324 )
325
326 p.cursor += len(content)
327
328 return string(content), parsercommon.ErrEOL
329 }
330
331 func fixTimestampIfNeeded(ts *time.Time) {
332 now := time.Now()
333 y := ts.Year()
334
335 if ts.Year() == 0 {
336 y = now.Year()
337 }
338
339 newTs := time.Date(
340 y, ts.Month(), ts.Day(),
341 ts.Hour(), ts.Minute(), ts.Second(), ts.Nanosecond(),
342 ts.Location(),
343 )
344
345 *ts = newTs
346 }