blob: 22ae3322c01a6d657a79601b696d0d2596286cd0 [file] [log] [blame]
David K. Bainbridge732957f2016-10-06 22:36:59 -07001// Copyright 2016 Open Networking Laboratory
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package main
15
16import (
17 "github.com/tatsushid/go-fastping"
18 "net"
19 "time"
20)
21
22// NOTE: the go-fastping utility calls its handlers (OnRecv, OnIdle) from a single thread as such
23// the code below does not have to serialize access to the "nonverified" array as access is
24// serialized the the fastping utility calling from a single thread.
25
26// verifyLeases verifies that the lease is valid by using an ICMP ping
27func (app *application) verifyLeases(leases map[string]*Lease) (map[string]*Lease, error) {
28 nonverified := make(map[string]bool)
29
30 // Populate the non-verified list from all the leases and then we will remove those
31 // that are verified
32 for ip, _ := range leases {
33 nonverified[ip] = true
34 }
35
36 pinger := fastping.NewPinger()
37 for _, lease := range leases {
38 pinger.AddIPAddr(&net.IPAddr{IP: lease.IPAddress})
39 }
40
41 if app.VerifyWithUDP {
42 pinger.Network("udp")
43 }
44 pinger.MaxRTT = app.VerifyTimeout
45
46 // when a ping response is received remove that lease from the non-verified list
47 pinger.OnRecv = func(addr *net.IPAddr, rtt time.Duration) {
48 app.log.Infof("Verified lease for IP address '%s' with RTT of '%s'", addr.String(), rtt)
49 delete(nonverified, addr.String())
50 }
51 err := pinger.Run()
52 if err != nil {
53 return nil, err
54 }
55
56 // Remove unverified leases from list
57 for ip, _ := range nonverified {
58 app.log.Infof("Discarding lease for IP address '%s', could not be verified", ip)
59 delete(leases, ip)
60 }
61
62 return leases, nil
63}