func (s *Server) systemdServe() { // We will usually have at least one TCP socket and one UDP socket. // PacketConns are UDP sockets, Listeners are TCP sockets. // To make things more annoying, both can (and usually will) have nil // entries for the file descriptors that don't match. pconns, err := activation.PacketConns(false) if err != nil { glog.Fatalf("Error getting systemd packet conns: %v", err) } listeners, err := activation.Listeners(false) if err != nil { glog.Fatalf("Error getting systemd listeners: %v", err) } var wg sync.WaitGroup for _, pconn := range pconns { if pconn == nil { continue } wg.Add(1) go func(c net.PacketConn) { defer wg.Done() glog.Infof("Activate on packet connection (UDP)") err := dns.ActivateAndServe(nil, c, dns.HandlerFunc(s.Handler)) glog.Fatalf("Exiting UDP listener: %v", err) }(pconn) } for _, lis := range listeners { if lis == nil { continue } wg.Add(1) go func(l net.Listener) { defer wg.Done() glog.Infof("Activate on listening socket (TCP)") err := dns.ActivateAndServe(l, nil, dns.HandlerFunc(s.Handler)) glog.Fatalf("Exiting TCP listener: %v", err) }(lis) } wg.Wait() // We should only get here if there were no useful sockets. glog.Fatalf("No systemd sockets, did you forget the .socket?") }
func dnsDispatch() { udpConn, err := net.ListenUDP("udp", dnsThis) if err != nil { kilog.Critical("%v", err.Error()) os.Exit(-1) } // hack to make Windows happy nameToIP["dns.msftncsi.com."] = "131.107.255.255" dns.ActivateAndServe(nil, udpConn, dns.HandlerFunc(dnsHandle)) }
// Run is a blocking operation that starts the server listening on the DNS ports. func (s *server) Run() error { mux := dns.NewServeMux() mux.Handle(".", s) dnsReadyMsg := func(addr, net string) { if s.config.DNSSEC == "" { log.Printf("skydns: ready for queries on %s for %s://%s [rcache %d]", s.config.Domain, net, addr, s.config.RCache) } else { log.Printf("skydns: ready for queries on %s for %s://%s [rcache %d], signing with %s [scache %d]", s.config.Domain, net, addr, s.config.RCache, s.config.DNSSEC, s.config.SCache) } } if s.config.Systemd { packetConns, err := activation.PacketConns(false) if err != nil { return err } listeners, err := activation.Listeners(true) if err != nil { return err } if len(packetConns) == 0 && len(listeners) == 0 { return fmt.Errorf("no UDP or TCP sockets supplied by systemd") } for _, p := range packetConns { if u, ok := p.(*net.UDPConn); ok { s.group.Add(1) go func() { defer s.group.Done() if err := dns.ActivateAndServe(nil, u, mux); err != nil { log.Fatalf("skydns: %s", err) } }() dnsReadyMsg(u.LocalAddr().String(), "udp") } } for _, l := range listeners { if t, ok := l.(*net.TCPListener); ok { s.group.Add(1) go func() { defer s.group.Done() if err := dns.ActivateAndServe(t, nil, mux); err != nil { log.Fatalf("skydns: %s", err) } }() dnsReadyMsg(t.Addr().String(), "tcp") } } } else { s.group.Add(1) go func() { defer s.group.Done() if err := dns.ListenAndServe(s.config.DnsAddr, "tcp", mux); err != nil { log.Fatalf("skydns: %s", err) } }() dnsReadyMsg(s.config.DnsAddr, "tcp") s.group.Add(1) go func() { defer s.group.Done() if err := dns.ListenAndServe(s.config.DnsAddr, "udp", mux); err != nil { log.Fatalf("skydns: %s", err) } }() dnsReadyMsg(s.config.DnsAddr, "udp") } s.group.Wait() return nil }
// Run is a blocking operation that starts the server listening on the DNS ports. func (s *server) Run() error { mux := dns.NewServeMux() mux.Handle(".", s) dnsReadyMsg := func(addr, net string) { rCacheState := "disabled" if s.config.RCache > 0 { rCacheState = fmt.Sprintf("capacity: %d", s.config.RCache) } log.Infof("Ready for queries on %s://%s [cache: %s]", net, addr, rCacheState) } if s.config.Systemd { packetConns, err := activation.PacketConns(false) if err != nil { return err } listeners, err := activation.Listeners(true) if err != nil { return err } if len(packetConns) == 0 && len(listeners) == 0 { return fmt.Errorf("No UDP or TCP sockets supplied by systemd") } for _, p := range packetConns { if u, ok := p.(*net.UDPConn); ok { s.group.Add(1) go func() { defer s.group.Done() if err := dns.ActivateAndServe(nil, u, mux); err != nil { log.Fatalf("%s", err) } }() dnsReadyMsg(u.LocalAddr().String(), "udp") } } for _, l := range listeners { if t, ok := l.(*net.TCPListener); ok { s.group.Add(1) go func() { defer s.group.Done() if err := dns.ActivateAndServe(t, nil, mux); err != nil { log.Fatalf("%s", err) } }() dnsReadyMsg(t.Addr().String(), "tcp") } } } else { s.group.Add(1) go func() { defer s.group.Done() if err := dns.ListenAndServe(s.config.DnsAddr, "tcp", mux); err != nil { log.Fatalf("%s", err) } }() dnsReadyMsg(s.config.DnsAddr, "tcp") s.group.Add(1) go func() { defer s.group.Done() if err := dns.ListenAndServe(s.config.DnsAddr, "udp", mux); err != nil { log.Fatalf("%s", err) } }() dnsReadyMsg(s.config.DnsAddr, "udp") } s.group.Wait() return nil }