@@ -12,36 +12,57 @@ var (
12
12
effectiveListener = DefaultListener {}
13
13
)
14
14
15
- type DefaultListener struct {}
15
+ type controller func ( network , address string , fd uintptr ) error
16
16
17
- func (* DefaultListener ) Listen (ctx context.Context , addr net.Addr , sockopt * SocketConfig ) (net.Listener , error ) {
18
- var lc net.ListenConfig
17
+ type DefaultListener struct {
18
+ contollers []controller
19
+ }
19
20
20
- if sockopt != nil {
21
- lc .Control = func (network , address string , c syscall.RawConn ) error {
22
- return c .Control (func (fd uintptr ) {
21
+ func getControlFunc (ctx context.Context , sockopt * SocketConfig , contollers []controller ) func (network , address string , c syscall.RawConn ) error {
22
+ return func (network , address string , c syscall.RawConn ) error {
23
+ return c .Control (func (fd uintptr ) {
24
+ if sockopt != nil {
23
25
if err := applyInboundSocketOptions (network , fd , sockopt ); err != nil {
24
26
newError ("failed to apply socket options to incoming connection" ).Base (err ).WriteToLog (session .ExportIDToError (ctx ))
25
27
}
26
- })
27
- }
28
+ }
29
+
30
+ for _ , controller := range contollers {
31
+ if err := controller (network , address , fd ); err != nil {
32
+ newError ("failed to apply external controller" ).Base (err ).WriteToLog (session .ExportIDToError (ctx ))
33
+ }
34
+ }
35
+ })
36
+ }
37
+ }
38
+
39
+ func (dl * DefaultListener ) Listen (ctx context.Context , addr net.Addr , sockopt * SocketConfig ) (net.Listener , error ) {
40
+ var lc net.ListenConfig
41
+
42
+ if sockopt != nil || len (dl .contollers ) > 0 {
43
+ lc .Control = getControlFunc (ctx , sockopt , dl .contollers )
28
44
}
29
45
30
46
return lc .Listen (ctx , addr .Network (), addr .String ())
31
47
}
32
48
33
- func (* DefaultListener ) ListenPacket (ctx context.Context , addr net.Addr , sockopt * SocketConfig ) (net.PacketConn , error ) {
49
+ func (dl * DefaultListener ) ListenPacket (ctx context.Context , addr net.Addr , sockopt * SocketConfig ) (net.PacketConn , error ) {
34
50
var lc net.ListenConfig
35
51
36
- if sockopt != nil {
37
- lc .Control = func (network , address string , c syscall.RawConn ) error {
38
- return c .Control (func (fd uintptr ) {
39
- if err := applyInboundSocketOptions (network , fd , sockopt ); err != nil {
40
- newError ("failed to apply socket options to incoming connection" ).Base (err ).WriteToLog (session .ExportIDToError (ctx ))
41
- }
42
- })
43
- }
52
+ if sockopt != nil || len (dl .contollers ) > 0 {
53
+ lc .Control = getControlFunc (ctx , sockopt , dl .contollers )
44
54
}
45
55
46
56
return lc .ListenPacket (ctx , addr .Network (), addr .String ())
47
57
}
58
+
59
+ // RegisterListenerController adds a controller to the effective system listener.
60
+ // The controller can be used to operate on file descriptors before they are put into use.
61
+ func RegisterListenerController (controller func (network , address string , fd uintptr ) error ) error {
62
+ if controller == nil {
63
+ return newError ("nil listener controller" )
64
+ }
65
+
66
+ effectiveListener .contollers = append (effectiveListener .contollers , controller )
67
+ return nil
68
+ }
0 commit comments