-
Notifications
You must be signed in to change notification settings - Fork 2
/
errors.go
80 lines (66 loc) · 1.49 KB
/
errors.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
package ping
import (
"fmt"
"net"
)
type ICMPError interface {
error
From() net.IP
}
type DstUnreachableCode uint8
const (
NetUnreachable DstUnreachableCode = iota
HostUnreachable
ProtocolUnreachable
PortUnreachable
FragmentationNeeded
SourceRouteFailed
)
func (c DstUnreachableCode) Error() string {
switch c {
case NetUnreachable:
return "net unreachable"
case HostUnreachable:
return "host unreachable"
case ProtocolUnreachable:
return "protocol unreachable"
case PortUnreachable:
return "port unreachable"
case FragmentationNeeded:
return "fragmentation needed and DF set"
case SourceRouteFailed:
return "source route failed"
default:
return "unknown error"
}
}
type DestinationUnreachableError struct {
from net.IP
code DstUnreachableCode
}
func NewDestinationUnreachableError(from net.IP,
code DstUnreachableCode) DestinationUnreachableError {
return DestinationUnreachableError{
from: from,
code: code,
}
}
func (e DestinationUnreachableError) Error() string {
return fmt.Sprintf("from %s: %s", e.From(), e.code.Error())
}
func (e DestinationUnreachableError) From() net.IP {
return e.from
}
func (e DestinationUnreachableError) Unwrap() error {
return e.code
}
type TimeExceededError net.IP
func NewTimeExceededError(from net.IP) TimeExceededError {
return TimeExceededError(from)
}
func (e TimeExceededError) Error() string {
return fmt.Sprintf("from %s: TTL exceeded in transit", e.From())
}
func (e TimeExceededError) From() net.IP {
return net.IP(e)
}