forked from influxdata/go-syslog
-
Notifications
You must be signed in to change notification settings - Fork 1
/
machine.go
13710 lines (13401 loc) · 222 KB
/
machine.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package rfc5424
import (
"fmt"
"time"
"github.com/observiq/go-syslog/v3"
"github.com/observiq/go-syslog/v3/common"
)
// ColumnPositionTemplate is the template used to communicate the column where errors occur.
var ColumnPositionTemplate = " [col %d]"
const (
// ErrPrival represents an error in the priority value (PRIVAL) inside the PRI part of the RFC5424 syslog message.
ErrPrival = "expecting a priority value in the range 1-191 or equal to 0"
// ErrPri represents an error in the PRI part of the RFC5424 syslog message.
ErrPri = "expecting a priority value within angle brackets"
// ErrVersion represents an error in the VERSION part of the RFC5424 syslog message.
ErrVersion = "expecting a version value in the range 1-999"
// ErrTimestamp represents an error in the TIMESTAMP part of the RFC5424 syslog message.
ErrTimestamp = "expecting a RFC3339MICRO timestamp or a nil value"
// ErrHostname represents an error in the HOSTNAME part of the RFC5424 syslog message.
ErrHostname = "expecting an hostname (from 1 to max 255 US-ASCII characters) or a nil value"
// ErrAppname represents an error in the APP-NAME part of the RFC5424 syslog message.
ErrAppname = "expecting an app-name (from 1 to max 48 US-ASCII characters) or a nil value"
// ErrProcID represents an error in the PROCID part of the RFC5424 syslog message.
ErrProcID = "expecting a procid (from 1 to max 128 US-ASCII characters) or a nil value"
// ErrMsgID represents an error in the MSGID part of the RFC5424 syslog message.
ErrMsgID = "expecting a msgid (from 1 to max 32 US-ASCII characters) or a nil value"
// ErrStructuredData represents an error in the STRUCTURED DATA part of the RFC5424 syslog message.
ErrStructuredData = "expecting a structured data section containing one or more elements (`[id( key=\"value\")*]+`) or a nil value"
// ErrSdID represents an error regarding the ID of a STRUCTURED DATA element of the RFC5424 syslog message.
ErrSdID = "expecting a structured data element id (from 1 to max 32 US-ASCII characters; except `=`, ` `, `]`, and `\"`"
// ErrSdIDDuplicated represents an error occurring when two STRUCTURED DATA elementes have the same ID in a RFC5424 syslog message.
ErrSdIDDuplicated = "duplicate structured data element id"
// ErrSdParam represents an error regarding a STRUCTURED DATA PARAM of the RFC5424 syslog message.
ErrSdParam = "expecting a structured data parameter (`key=\"value\"`, both part from 1 to max 32 US-ASCII characters; key cannot contain `=`, ` `, `]`, and `\"`, while value cannot contain `]`, backslash, and `\"` unless escaped)"
// ErrMsg represents an error in the MESSAGE part of the RFC5424 syslog message.
ErrMsg = "expecting a free-form optional message in UTF-8 (starting with or without BOM)"
// ErrMsgNotCompliant represents an error in the MESSAGE part of the RFC5424 syslog message if WithCompliatMsg option is on.
ErrMsgNotCompliant = ErrMsg + " or a free-form optional message in any encoding (starting without BOM)"
// ErrEscape represents the error for a RFC5424 syslog message occurring when a STRUCTURED DATA PARAM value contains '"', '\', or ']' not escaped.
ErrEscape = "expecting chars `]`, `\"`, and `\\` to be escaped within param value"
// ErrParse represents a general parsing error for a RFC5424 syslog message.
ErrParse = "parsing error"
)
// RFC3339MICRO represents the timestamp format that RFC5424 mandates.
const RFC3339MICRO = "2006-01-02T15:04:05.999999Z07:00"
const start int = 1
const firstFinal int = 667
const enMsgAny int = 671
const enMsgCompliant int = 673
const enFail int = 678
const enMain int = 1
type machine struct {
data []byte
cs int
p, pe, eof int
pb int
err error
currentelem string
currentparam string
msgat int
backslashat []int
bestEffort bool
compliantMsg bool
}
// NewMachine creates a new FSM able to parse RFC5424 syslog messages.
func NewMachine(options ...syslog.MachineOption) syslog.Machine {
m := &machine{}
for _, opt := range options {
opt(m)
}
return m
}
// WithBestEffort enables best effort mode.
func (m *machine) WithBestEffort() {
m.bestEffort = true
}
// HasBestEffort tells whether the receiving machine has best effort mode on or off.
func (m *machine) HasBestEffort() bool {
return m.bestEffort
}
// Err returns the error that occurred on the last call to Parse.
//
// If the result is nil, then the line was parsed successfully.
func (m *machine) Err() error {
return m.err
}
func (m *machine) text() []byte {
return m.data[m.pb:m.p]
}
// Parse parses the input byte array as a RFC5424 syslog message.
//
// When a valid RFC5424 syslog message is given it outputs its structured representation.
// If the parsing detects an error it returns it with the position where the error occurred.
//
// It can also partially parse input messages returning a partially valid structured representation
// and the error that stopped the parsing.
func (m *machine) Parse(input []byte) (syslog.Message, error) {
m.data = input
m.p = 0
m.pb = 0
m.msgat = 0
m.backslashat = []int{}
m.pe = len(input)
m.eof = len(input)
m.err = nil
output := &syslogMessage{}
{
m.cs = start
}
{
if (m.p) == (m.pe) {
goto _testEof
}
switch m.cs {
case 1:
goto stCase1
case 0:
goto stCase0
case 2:
goto stCase2
case 3:
goto stCase3
case 4:
goto stCase4
case 5:
goto stCase5
case 6:
goto stCase6
case 7:
goto stCase7
case 8:
goto stCase8
case 9:
goto stCase9
case 10:
goto stCase10
case 11:
goto stCase11
case 12:
goto stCase12
case 13:
goto stCase13
case 14:
goto stCase14
case 15:
goto stCase15
case 16:
goto stCase16
case 667:
goto stCase667
case 668:
goto stCase668
case 669:
goto stCase669
case 17:
goto stCase17
case 18:
goto stCase18
case 19:
goto stCase19
case 20:
goto stCase20
case 21:
goto stCase21
case 22:
goto stCase22
case 23:
goto stCase23
case 24:
goto stCase24
case 25:
goto stCase25
case 26:
goto stCase26
case 27:
goto stCase27
case 28:
goto stCase28
case 29:
goto stCase29
case 30:
goto stCase30
case 31:
goto stCase31
case 32:
goto stCase32
case 33:
goto stCase33
case 34:
goto stCase34
case 35:
goto stCase35
case 36:
goto stCase36
case 37:
goto stCase37
case 38:
goto stCase38
case 39:
goto stCase39
case 40:
goto stCase40
case 41:
goto stCase41
case 42:
goto stCase42
case 43:
goto stCase43
case 44:
goto stCase44
case 45:
goto stCase45
case 46:
goto stCase46
case 47:
goto stCase47
case 48:
goto stCase48
case 49:
goto stCase49
case 50:
goto stCase50
case 51:
goto stCase51
case 52:
goto stCase52
case 53:
goto stCase53
case 54:
goto stCase54
case 55:
goto stCase55
case 56:
goto stCase56
case 57:
goto stCase57
case 58:
goto stCase58
case 59:
goto stCase59
case 60:
goto stCase60
case 61:
goto stCase61
case 62:
goto stCase62
case 63:
goto stCase63
case 64:
goto stCase64
case 65:
goto stCase65
case 66:
goto stCase66
case 67:
goto stCase67
case 68:
goto stCase68
case 69:
goto stCase69
case 70:
goto stCase70
case 71:
goto stCase71
case 72:
goto stCase72
case 73:
goto stCase73
case 74:
goto stCase74
case 75:
goto stCase75
case 76:
goto stCase76
case 77:
goto stCase77
case 78:
goto stCase78
case 79:
goto stCase79
case 80:
goto stCase80
case 81:
goto stCase81
case 82:
goto stCase82
case 83:
goto stCase83
case 84:
goto stCase84
case 85:
goto stCase85
case 86:
goto stCase86
case 87:
goto stCase87
case 670:
goto stCase670
case 88:
goto stCase88
case 89:
goto stCase89
case 90:
goto stCase90
case 91:
goto stCase91
case 92:
goto stCase92
case 93:
goto stCase93
case 94:
goto stCase94
case 95:
goto stCase95
case 96:
goto stCase96
case 97:
goto stCase97
case 98:
goto stCase98
case 99:
goto stCase99
case 100:
goto stCase100
case 101:
goto stCase101
case 102:
goto stCase102
case 103:
goto stCase103
case 104:
goto stCase104
case 105:
goto stCase105
case 106:
goto stCase106
case 107:
goto stCase107
case 108:
goto stCase108
case 109:
goto stCase109
case 110:
goto stCase110
case 111:
goto stCase111
case 112:
goto stCase112
case 113:
goto stCase113
case 114:
goto stCase114
case 115:
goto stCase115
case 116:
goto stCase116
case 117:
goto stCase117
case 118:
goto stCase118
case 119:
goto stCase119
case 120:
goto stCase120
case 121:
goto stCase121
case 122:
goto stCase122
case 123:
goto stCase123
case 124:
goto stCase124
case 125:
goto stCase125
case 126:
goto stCase126
case 127:
goto stCase127
case 128:
goto stCase128
case 129:
goto stCase129
case 130:
goto stCase130
case 131:
goto stCase131
case 132:
goto stCase132
case 133:
goto stCase133
case 134:
goto stCase134
case 135:
goto stCase135
case 136:
goto stCase136
case 137:
goto stCase137
case 138:
goto stCase138
case 139:
goto stCase139
case 140:
goto stCase140
case 141:
goto stCase141
case 142:
goto stCase142
case 143:
goto stCase143
case 144:
goto stCase144
case 145:
goto stCase145
case 146:
goto stCase146
case 147:
goto stCase147
case 148:
goto stCase148
case 149:
goto stCase149
case 150:
goto stCase150
case 151:
goto stCase151
case 152:
goto stCase152
case 153:
goto stCase153
case 154:
goto stCase154
case 155:
goto stCase155
case 156:
goto stCase156
case 157:
goto stCase157
case 158:
goto stCase158
case 159:
goto stCase159
case 160:
goto stCase160
case 161:
goto stCase161
case 162:
goto stCase162
case 163:
goto stCase163
case 164:
goto stCase164
case 165:
goto stCase165
case 166:
goto stCase166
case 167:
goto stCase167
case 168:
goto stCase168
case 169:
goto stCase169
case 170:
goto stCase170
case 171:
goto stCase171
case 172:
goto stCase172
case 173:
goto stCase173
case 174:
goto stCase174
case 175:
goto stCase175
case 176:
goto stCase176
case 177:
goto stCase177
case 178:
goto stCase178
case 179:
goto stCase179
case 180:
goto stCase180
case 181:
goto stCase181
case 182:
goto stCase182
case 183:
goto stCase183
case 184:
goto stCase184
case 185:
goto stCase185
case 186:
goto stCase186
case 187:
goto stCase187
case 188:
goto stCase188
case 189:
goto stCase189
case 190:
goto stCase190
case 191:
goto stCase191
case 192:
goto stCase192
case 193:
goto stCase193
case 194:
goto stCase194
case 195:
goto stCase195
case 196:
goto stCase196
case 197:
goto stCase197
case 198:
goto stCase198
case 199:
goto stCase199
case 200:
goto stCase200
case 201:
goto stCase201
case 202:
goto stCase202
case 203:
goto stCase203
case 204:
goto stCase204
case 205:
goto stCase205
case 206:
goto stCase206
case 207:
goto stCase207
case 208:
goto stCase208
case 209:
goto stCase209
case 210:
goto stCase210
case 211:
goto stCase211
case 212:
goto stCase212
case 213:
goto stCase213
case 214:
goto stCase214
case 215:
goto stCase215
case 216:
goto stCase216
case 217:
goto stCase217
case 218:
goto stCase218
case 219:
goto stCase219
case 220:
goto stCase220
case 221:
goto stCase221
case 222:
goto stCase222
case 223:
goto stCase223
case 224:
goto stCase224
case 225:
goto stCase225
case 226:
goto stCase226
case 227:
goto stCase227
case 228:
goto stCase228
case 229:
goto stCase229
case 230:
goto stCase230
case 231:
goto stCase231
case 232:
goto stCase232
case 233:
goto stCase233
case 234:
goto stCase234
case 235:
goto stCase235
case 236:
goto stCase236
case 237:
goto stCase237
case 238:
goto stCase238
case 239:
goto stCase239
case 240:
goto stCase240
case 241:
goto stCase241
case 242:
goto stCase242
case 243:
goto stCase243
case 244:
goto stCase244
case 245:
goto stCase245
case 246:
goto stCase246
case 247:
goto stCase247
case 248:
goto stCase248
case 249:
goto stCase249
case 250:
goto stCase250
case 251:
goto stCase251
case 252:
goto stCase252
case 253:
goto stCase253
case 254:
goto stCase254
case 255:
goto stCase255
case 256:
goto stCase256
case 257:
goto stCase257
case 258:
goto stCase258
case 259:
goto stCase259
case 260:
goto stCase260
case 261:
goto stCase261
case 262:
goto stCase262
case 263:
goto stCase263
case 264:
goto stCase264
case 265:
goto stCase265
case 266:
goto stCase266
case 267:
goto stCase267
case 268:
goto stCase268
case 269:
goto stCase269
case 270:
goto stCase270
case 271:
goto stCase271
case 272:
goto stCase272
case 273:
goto stCase273
case 274:
goto stCase274
case 275:
goto stCase275
case 276:
goto stCase276
case 277:
goto stCase277
case 278:
goto stCase278
case 279:
goto stCase279
case 280:
goto stCase280
case 281:
goto stCase281
case 282:
goto stCase282
case 283:
goto stCase283
case 284:
goto stCase284
case 285:
goto stCase285
case 286:
goto stCase286
case 287:
goto stCase287
case 288:
goto stCase288
case 289:
goto stCase289
case 290:
goto stCase290
case 291:
goto stCase291
case 292:
goto stCase292
case 293:
goto stCase293
case 294:
goto stCase294
case 295:
goto stCase295
case 296:
goto stCase296
case 297:
goto stCase297
case 298:
goto stCase298
case 299:
goto stCase299
case 300:
goto stCase300
case 301:
goto stCase301
case 302:
goto stCase302
case 303:
goto stCase303
case 304:
goto stCase304
case 305:
goto stCase305
case 306:
goto stCase306
case 307:
goto stCase307
case 308:
goto stCase308
case 309:
goto stCase309
case 310:
goto stCase310
case 311:
goto stCase311
case 312:
goto stCase312
case 313:
goto stCase313
case 314:
goto stCase314
case 315:
goto stCase315
case 316:
goto stCase316
case 317:
goto stCase317
case 318:
goto stCase318
case 319:
goto stCase319
case 320:
goto stCase320
case 321:
goto stCase321
case 322:
goto stCase322
case 323:
goto stCase323
case 324:
goto stCase324
case 325:
goto stCase325
case 326:
goto stCase326
case 327:
goto stCase327
case 328:
goto stCase328
case 329:
goto stCase329
case 330:
goto stCase330
case 331:
goto stCase331
case 332:
goto stCase332
case 333:
goto stCase333
case 334:
goto stCase334
case 335:
goto stCase335
case 336:
goto stCase336
case 337:
goto stCase337
case 338:
goto stCase338
case 339:
goto stCase339
case 340:
goto stCase340
case 341:
goto stCase341
case 342:
goto stCase342
case 343:
goto stCase343
case 344:
goto stCase344
case 345:
goto stCase345
case 346:
goto stCase346
case 347:
goto stCase347
case 348:
goto stCase348
case 349:
goto stCase349
case 350:
goto stCase350
case 351:
goto stCase351
case 352:
goto stCase352
case 353:
goto stCase353
case 354:
goto stCase354
case 355:
goto stCase355
case 356:
goto stCase356
case 357:
goto stCase357
case 358:
goto stCase358
case 359:
goto stCase359
case 360:
goto stCase360
case 361:
goto stCase361
case 362:
goto stCase362
case 363:
goto stCase363
case 364:
goto stCase364
case 365:
goto stCase365
case 366:
goto stCase366
case 367:
goto stCase367
case 368:
goto stCase368
case 369:
goto stCase369
case 370:
goto stCase370
case 371:
goto stCase371
case 372:
goto stCase372
case 373:
goto stCase373
case 374:
goto stCase374
case 375:
goto stCase375
case 376:
goto stCase376
case 377:
goto stCase377
case 378:
goto stCase378
case 379:
goto stCase379
case 380:
goto stCase380
case 381:
goto stCase381
case 382:
goto stCase382
case 383:
goto stCase383
case 384:
goto stCase384
case 385:
goto stCase385
case 386:
goto stCase386
case 387:
goto stCase387
case 388:
goto stCase388
case 389:
goto stCase389
case 390:
goto stCase390
case 391:
goto stCase391
case 392:
goto stCase392
case 393:
goto stCase393
case 394:
goto stCase394
case 395:
goto stCase395
case 396:
goto stCase396
case 397:
goto stCase397
case 398:
goto stCase398
case 399:
goto stCase399
case 400:
goto stCase400
case 401:
goto stCase401
case 402:
goto stCase402
case 403:
goto stCase403
case 404:
goto stCase404
case 405:
goto stCase405
case 406:
goto stCase406
case 407:
goto stCase407
case 408:
goto stCase408
case 409:
goto stCase409
case 410:
goto stCase410
case 411:
goto stCase411
case 412:
goto stCase412
case 413:
goto stCase413
case 414:
goto stCase414
case 415:
goto stCase415
case 416:
goto stCase416
case 417:
goto stCase417
case 418:
goto stCase418
case 419:
goto stCase419
case 420:
goto stCase420
case 421:
goto stCase421
case 422:
goto stCase422
case 423:
goto stCase423
case 424:
goto stCase424
case 425:
goto stCase425
case 426:
goto stCase426
case 427:
goto stCase427
case 428:
goto stCase428
case 429:
goto stCase429