@@ -107,10 +107,7 @@ func (ip *icmpProxy) Request(ctx context.Context, pk *packet.ICMP, responder *pa
107107 tracing .EndWithErrorStatus (span , err )
108108 return err
109109 }
110- span .SetAttributes (
111- attribute .Int ("originalEchoID" , originalEcho .ID ),
112- attribute .Int ("seq" , originalEcho .Seq ),
113- )
110+ observeICMPRequest (ip .logger , span , pk .Src .String (), pk .Dst .String (), originalEcho .ID , originalEcho .Seq )
114111
115112 shouldReplaceFunnelFunc := createShouldReplaceFunnelFunc (ip .logger , responder .datagramMuxer , pk , originalEcho .ID )
116113 newFunnelFunc := func () (packet.Funnel , error ) {
@@ -156,14 +153,8 @@ func (ip *icmpProxy) Request(ctx context.Context, pk *packet.ICMP, responder *pa
156153 Int ("originalEchoID" , originalEcho .ID ).
157154 Msg ("New flow" )
158155 go func () {
159- defer ip .srcFunnelTracker .Unregister (funnelID , icmpFlow )
160- if err := ip .listenResponse (ctx , icmpFlow ); err != nil {
161- ip .logger .Debug ().Err (err ).
162- Str ("src" , pk .Src .String ()).
163- Str ("dst" , pk .Dst .String ()).
164- Int ("originalEchoID" , originalEcho .ID ).
165- Msg ("Failed to listen for ICMP echo response" )
166- }
156+ ip .listenResponse (ctx , icmpFlow )
157+ ip .srcFunnelTracker .Unregister (funnelID , icmpFlow )
167158 }()
168159 }
169160 if err := icmpFlow .sendToDst (pk .Dst , pk .Message ); err != nil {
@@ -179,17 +170,17 @@ func (ip *icmpProxy) Serve(ctx context.Context) error {
179170 return ctx .Err ()
180171}
181172
182- func (ip * icmpProxy ) listenResponse (ctx context.Context , flow * icmpEchoFlow ) error {
173+ func (ip * icmpProxy ) listenResponse (ctx context.Context , flow * icmpEchoFlow ) {
183174 buf := make ([]byte , mtu )
184175 for {
185- retryable , err := ip .handleResponse (ctx , flow , buf )
186- if err != nil && ! retryable {
187- return err
176+ if done := ip .handleResponse (ctx , flow , buf ); done {
177+ return
188178 }
189179 }
190180}
191181
192- func (ip * icmpProxy ) handleResponse (ctx context.Context , flow * icmpEchoFlow , buf []byte ) (retryableErr bool , err error ) {
182+ // Listens for ICMP response and handles error logging
183+ func (ip * icmpProxy ) handleResponse (ctx context.Context , flow * icmpEchoFlow , buf []byte ) (done bool ) {
193184 _ , span := flow .responder .replySpan (ctx , ip .logger )
194185 defer flow .responder .exportSpan ()
195186
@@ -199,33 +190,36 @@ func (ip *icmpProxy) handleResponse(ctx context.Context, flow *icmpEchoFlow, buf
199190
200191 n , from , err := flow .originConn .ReadFrom (buf )
201192 if err != nil {
193+ if flow .IsClosed () {
194+ tracing .EndWithErrorStatus (span , fmt .Errorf ("flow was closed" ))
195+ return true
196+ }
197+ ip .logger .Error ().Err (err ).Str ("socket" , flow .originConn .LocalAddr ().String ()).Msg ("Failed to read from ICMP socket" )
202198 tracing .EndWithErrorStatus (span , err )
203- return false , err
199+ return true
204200 }
205201 reply , err := parseReply (from , buf [:n ])
206202 if err != nil {
207203 ip .logger .Error ().Err (err ).Str ("dst" , from .String ()).Msg ("Failed to parse ICMP reply" )
208204 tracing .EndWithErrorStatus (span , err )
209- return true , err
205+ return false
210206 }
211207 if ! isEchoReply (reply .msg ) {
212208 err := fmt .Errorf ("Expect ICMP echo reply, got %s" , reply .msg .Type )
213209 ip .logger .Debug ().Str ("dst" , from .String ()).Msgf ("Drop ICMP %s from reply" , reply .msg .Type )
214210 tracing .EndWithErrorStatus (span , err )
215- return true , err
211+ return false
216212 }
217- span .SetAttributes (
218- attribute .String ("dst" , reply .from .String ()),
219- attribute .Int ("echoID" , reply .echo .ID ),
220- attribute .Int ("seq" , reply .echo .Seq ),
221- )
213+
222214 if err := flow .returnToSrc (reply ); err != nil {
223- ip .logger .Debug ().Err (err ).Str ("dst" , from .String ()).Msg ("Failed to send ICMP reply" )
215+ ip .logger .Error ().Err (err ).Str ("dst" , from .String ()).Msg ("Failed to send ICMP reply" )
224216 tracing .EndWithErrorStatus (span , err )
225- return true , err
217+ return false
226218 }
219+
220+ observeICMPReply (ip .logger , span , from .String (), reply .echo .ID , reply .echo .Seq )
227221 tracing .End (span )
228- return true , nil
222+ return false
229223}
230224
231225// Only linux uses flow3Tuple as FunnelID
0 commit comments