@@ -203,17 +203,45 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
203
203
// Announcement is identical to the ARP Probe described above,
204
204
// except that now the sender and target IP addresses are both
205
205
// set to the host's newly selected IPv4 address.
206
- if pkt , err = arp . NewPacket ( arp . OperationRequest , mac , tpa , tha , tpa ); err != nil {
206
+ if err = AnnounceArpAddress ( nic , ip , mac , announceNum , announceInterval ); err != nil {
207
207
return nil , err
208
208
}
209
209
210
+ return nil , nil
211
+ }
212
+
213
+ func AnnounceArpAddress (nic , ip string , mac net.HardwareAddr , announceNum int , announceInterval time.Duration ) error {
214
+ klog .Infof ("announce arp address nic %s , ip %s, with mac %v " , nic , ip , mac )
215
+ netInterface , err := net .InterfaceByName (nic )
216
+ if err != nil {
217
+ return err
218
+ }
219
+
220
+ client , err := arp .Dial (netInterface )
221
+ if err != nil {
222
+ return err
223
+ }
224
+ defer client .Close ()
225
+
226
+ tpa , err := netip .ParseAddr (ip )
227
+ if err != nil {
228
+ klog .Errorf ("failed to parse IP address %s: %v " , ip , err )
229
+ return err
230
+ }
231
+ tha := net.HardwareAddr {0 , 0 , 0 , 0 , 0 , 0 }
232
+ pkt , err := arp .NewPacket (arp .OperationRequest , mac , tpa , tha , tpa )
233
+ if err != nil {
234
+ return err
235
+ }
236
+
237
+ dstMac := net.HardwareAddr {0xff , 0xff , 0xff , 0xff , 0xff , 0xff }
210
238
for i := 0 ; i < announceNum ; i ++ {
211
239
c := time .NewTimer (announceInterval )
212
240
if err = client .SetDeadline (time .Now ().Add (announceInterval )); err != nil {
213
- return nil , err
241
+ return err
214
242
}
215
243
if err = client .WriteTo (pkt , dstMac ); err != nil {
216
- return nil , err
244
+ return err
217
245
}
218
246
if i == announceNum - 1 {
219
247
// the last one, no need to wait
@@ -223,5 +251,5 @@ func ArpDetectIPConflict(nic, ip string, mac net.HardwareAddr) (net.HardwareAddr
223
251
}
224
252
}
225
253
226
- return nil , nil
254
+ return nil
227
255
}
0 commit comments