From f799d9bf2c55b51519a33affeaa4aa10a7886a26 Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Mon, 6 May 2019 23:55:33 +0300 Subject: [PATCH] cli: don't halt on "protocol not supported" error (fixes #32) --- internal/cli/run.go | 47 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 38 insertions(+), 9 deletions(-) diff --git a/internal/cli/run.go b/internal/cli/run.go index cf61e5d..1865e10 100644 --- a/internal/cli/run.go +++ b/internal/cli/run.go @@ -9,8 +9,10 @@ import ( "net" "net/http" "net/http/pprof" + "os" "strings" "sync" + "syscall" "github.com/libp2p/go-reuseport" "github.com/prometheus/client_golang/prometheus" @@ -303,9 +305,10 @@ func getListeners(v *viper.Viper, l *zap.Logger) []listener { } l.Warn("using", zap.Stringer("a", a)) toListen = append(toListen, listener{ - adrr: strings.Replace(normalized, "0.0.0.0", a.IP.String(), -1), - net: "udp", - u: u, + fromAny: true, + adrr: strings.Replace(normalized, "0.0.0.0", a.IP.String(), -1), + net: "udp", + u: u, }) } } else { @@ -320,6 +323,25 @@ func getListeners(v *viper.Viper, l *zap.Logger) []listener { return toListen } +func protocolNotSupported(err error) bool { + switch err := err.(type) { + case syscall.Errno: + switch err { + case syscall.EPROTONOSUPPORT, syscall.ENOPROTOOPT: + return true + } + case *os.SyscallError: + switch err := err.Err.(type) { + case syscall.Errno: + switch err { + case syscall.EPROTONOSUPPORT, syscall.ENOPROTOOPT: + return true + } + } + } + return false +} + func getRoot(v *viper.Viper) *cobra.Command { cmd := &cobra.Command{ Use: "gortcd", @@ -335,10 +357,16 @@ func getRoot(v *viper.Viper) *cobra.Command { for _, ln := range listeners { go func() { defer wg.Done() - l.Info("gortc/gortcd listening", zap.String("addr", ln.adrr), - zap.String("network", "udp")) + lg := l.With(zap.String("addr", ln.adrr), zap.String("network", "udp")) + lg.Info("gortc/gortcd listening") if err := ListenUDPAndServe(ln.net, ln.adrr, ln.u); err != nil { - l.Fatal("failed to listen", zap.Error(err)) + if ln.fromAny && protocolNotSupported(err) { + // See /~https://github.com/gortc/gortcd/issues/32 + // Should be ok to make it non configurable. + lg.Warn("failed to listen", zap.Error(err)) + } else { + lg.Fatal("failed to listen", zap.Error(err)) + } } }() } @@ -362,7 +390,8 @@ func getRoot(v *viper.Viper) *cobra.Command { } type listener struct { - net string - adrr string - u *server.Updater + net string + adrr string + u *server.Updater + fromAny bool // as part of 0.0.0.0 }