@@ -8,8 +8,12 @@ package conn
88
99import (
1010 "errors"
11+ "fmt"
1112 "net"
13+ "reflect"
14+ "runtime"
1215 "strings"
16+ "unsafe"
1317)
1418
1519// A ReceiveFunc receives a single inbound packet from the network.
@@ -69,6 +73,55 @@ type Endpoint interface {
6973 SrcIP () net.IP
7074}
7175
76+ var (
77+ ErrBindAlreadyOpen = errors .New ("bind is already open" )
78+ ErrWrongEndpointType = errors .New ("endpoint type does not correspond with bind type" )
79+ )
80+
81+ func (fn ReceiveFunc ) PrettyName () string {
82+ ptr := reflect .ValueOf (fn ).Pointer ()
83+ name := runtime .FuncForPC (ptr ).Name ()
84+ // 0. cheese/taco.beansIPv6.func12.func21218-fm
85+ name = strings .TrimSuffix (name , "-fm" )
86+ // 1. cheese/taco.beansIPv6.func12.func21218
87+ if idx := strings .LastIndexByte (name , '/' ); idx != - 1 {
88+ name = name [idx + 1 :]
89+ // 2. taco.beansIPv6.func12.func21218
90+ }
91+ for {
92+ var idx int
93+ for idx = len (name ) - 1 ; idx >= 0 ; idx -- {
94+ if name [idx ] < '0' || name [idx ] > '9' {
95+ break
96+ }
97+ }
98+ if idx == len (name )- 1 {
99+ break
100+ }
101+ const dotFunc = ".func"
102+ if ! strings .HasSuffix (name [:idx + 1 ], dotFunc ) {
103+ break
104+ }
105+ name = name [:idx + 1 - len (dotFunc )]
106+ // 3. taco.beansIPv6.func12
107+ // 4. taco.beansIPv6
108+ }
109+ if idx := strings .LastIndexByte (name , '.' ); idx != - 1 {
110+ name = name [idx + 1 :]
111+ // 5. beansIPv6
112+ }
113+ if name == "" {
114+ return fmt .Sprintf ("%p" , unsafe .Pointer (ptr ))
115+ }
116+ if strings .HasSuffix (name , "IPv4" ) {
117+ return "v4"
118+ }
119+ if strings .HasSuffix (name , "IPv6" ) {
120+ return "v6"
121+ }
122+ return name
123+ }
124+
72125func parseEndpoint (s string ) (* net.UDPAddr , error ) {
73126 // ensure that the host is an IP address
74127
@@ -98,8 +151,3 @@ func parseEndpoint(s string) (*net.UDPAddr, error) {
98151 }
99152 return addr , err
100153}
101-
102- var (
103- ErrBindAlreadyOpen = errors .New ("bind is already open" )
104- ErrWrongEndpointType = errors .New ("endpoint type does not correspond with bind type" )
105- )
0 commit comments