a go dns packet parser
1package magna 2 3import ( 4 "fmt" 5) 6 7// BufferOverflowError represents an error when there is a buffer overflow. 8type BufferOverflowError struct { 9 Length int 10 Offset int 11} 12 13func (e *BufferOverflowError) Error() string { 14 return fmt.Sprintf("buffer overflow: attempted to read past buffer length %d at offset %d", e.Length, e.Offset) 15} 16 17func (e *BufferOverflowError) Is(target error) bool { 18 _, ok := target.(*BufferOverflowError) 19 return ok 20} 21 22// InvalidLabelError represents an error when an invalid label length is encountered. 23type InvalidLabelError struct { 24 Length int 25} 26 27func (e *InvalidLabelError) Error() string { 28 if e.Length > 63 { 29 return fmt.Sprintf("invalid domain label: length %d exceeds maximum 63", e.Length) 30 } 31 if e.Length == 0 { 32 return "invalid domain label: zero length label encountered" 33 } 34 35 // XXX: this should be unreachable 36 return fmt.Sprintf("invalid domain label: unexpected length %d", e.Length) 37} 38 39func (e *InvalidLabelError) Is(target error) bool { 40 _, ok := target.(*InvalidLabelError) 41 return ok 42} 43 44// DomainCompressionError represents an error related to domain compression. 45type DomainCompressionError struct{} 46 47func (e *DomainCompressionError) Error() string { 48 return "invalid domain compression: pointer loop detected" 49} 50 51func (e *DomainCompressionError) Is(target error) bool { 52 _, ok := target.(*DomainCompressionError) 53 return ok 54}