Skip to content

Commit

Permalink
add manage-loopback option
Browse files Browse the repository at this point in the history
Summary:
Similar to:

https://www.internalfb.com/code/fbsource/[f9568a8498fc11ad7909787d68dbbfe3b2318b98]/fbcode/dns/gounboundwrapper/cmds/unboundwrapper/main.go?lines=31

Reviewed By: t3lurid3

Differential Revision: D52297428

fbshipit-source-id: de8ed0216ab3521dbb7b3fd3c7f2ae7a626b2ce9
  • Loading branch information
pmazzini authored and facebook-github-bot committed Dec 20, 2023
1 parent f0661e4 commit 184fad0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 11 deletions.
1 change: 1 addition & 0 deletions cmd/ntpresponder/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func main() {
flag.BoolVar(&debugger, "pprof", false, "Enable pprof")
flag.BoolVar(&s.ListenConfig.ShouldAnnounce, "announce", false, "Advertize IPs")
flag.DurationVar(&s.ExtraOffset, "extraoffset", 0, "Extra offset to return to clients")
flag.BoolVar(&s.ManageLoopback, "manage-loopback", true, "Add/remove IPs. If false, these must be managed elsewhere")

flag.Parse()
s.ListenConfig.IPs.SetDefault()
Expand Down
6 changes: 6 additions & 0 deletions ntp/responder/server/ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ const ipv6Mask = 64

// AddIPOnInterface adds ip to interface
func (s *Server) addIPToInterface(vip net.IP) error {
if !s.ManageLoopback {
return nil
}
log.Infof("Adding %s to %s", vip, s.ListenConfig.Iface)
// Add IPs to the interface
iface, err := net.InterfaceByName(s.ListenConfig.Iface)
Expand All @@ -43,6 +46,9 @@ func (s *Server) addIPToInterface(vip net.IP) error {

// deleteIPFromInterface deletes ip from interface
func (s *Server) deleteIPFromInterface(vip net.IP) error {
if !s.ManageLoopback {
return nil
}
log.Infof("Deleting %s to %s", vip, s.ListenConfig.Iface)
// Delete IPs to the interface
iface, err := net.InterfaceByName(s.ListenConfig.Iface)
Expand Down
10 changes: 8 additions & 2 deletions ntp/responder/server/ip_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ const testIP = "1.2.3.4"

func TestAddIPToInterfaceError(t *testing.T) {
lc := ListenConfig{Iface: "lol-does-not-exist"}
s := &Server{ListenConfig: lc}
s := &Server{
ListenConfig: lc,
ManageLoopback: true,
}
err := s.addIPToInterface(net.ParseIP(testIP))
require.NotNil(t, err)
}

func TestDeleteIPFromInterfaceError(t *testing.T) {
lc := ListenConfig{Iface: "lol-does-not-exist"}
s := &Server{ListenConfig: lc}
s := &Server{
ListenConfig: lc,
ManageLoopback: true,
}
err := s.deleteIPFromInterface(net.ParseIP(testIP))
require.NotNil(t, err)
}
19 changes: 10 additions & 9 deletions ntp/responder/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,16 @@ type task struct {

// Server is a type for UDP server which handles connections.
type Server struct {
ListenConfig ListenConfig
Workers int
Announce Announce
Stats Stats
Checker Checker
tasks chan task
ExtraOffset time.Duration
RefID string
Stratum int
ListenConfig ListenConfig
Workers int
Announce Announce
Stats Stats
Checker Checker
tasks chan task
ExtraOffset time.Duration
RefID string
Stratum int
ManageLoopback bool
}

// Start UDP server.
Expand Down

0 comments on commit 184fad0

Please sign in to comment.