From 3edaf6728f27b36d74a45c6a830a05b6372f62d4 Mon Sep 17 00:00:00 2001 From: EndMove Date: Mon, 14 Mar 2022 11:40:23 +0100 Subject: [PATCH] Ajout de la prise en compte d'interface pour le multicast --- pkg/network/ClientMulticast.go | 21 ++++++++++----------- pkg/utils/FileReceiver.go | 8 -------- pkg/utils/Network.go | 21 +++++++++++++++++++++ 3 files changed, 31 insertions(+), 19 deletions(-) diff --git a/pkg/network/ClientMulticast.go b/pkg/network/ClientMulticast.go index 49f97c6..8c82753 100644 --- a/pkg/network/ClientMulticast.go +++ b/pkg/network/ClientMulticast.go @@ -3,6 +3,7 @@ package network import ( "StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/rules/writers" + "StoreBackEnd/pkg/utils" "fmt" "net" "time" @@ -48,6 +49,7 @@ func (client ClientMulticast) Run() { // Resolve interface addr lAddr, failedRIA := ResolveInterfaceAddr(client.netInter) if failedRIA { + println("Error : No IPv4 found in interface") return } @@ -81,21 +83,18 @@ func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) { // ResolveInterfaceAddr Resolves the network interface address. func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) { - //ief, _ := net.InterfaceByName(inter) - //addrs, _ := ief.Addrs() - //for _, addr := range addrs { - // if utils.IsIPv4(addr.String()) { - // println("Selected IP", addr.String()) - // lAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:15502") - // return lAddr, false - // } - //} - return nil, false + ip, err := utils.RetrieveIPv4FromInterface(inter) + if err != nil { + println("Error : " + err.Error()) + return nil, true + } + println("Selected IP : " + ip.String()) + return &net.UDPAddr{IP: ip}, false } // DialUdp Ouvre une connection UDP func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) { - con, errDial := net.DialUDP("udp", nil, rAddr) + con, errDial := net.DialUDP("udp", lAddr, rAddr) if errDial != nil { println(errDial.Error()) return nil, true diff --git a/pkg/utils/FileReceiver.go b/pkg/utils/FileReceiver.go index fc4e075..aaac2fe 100644 --- a/pkg/utils/FileReceiver.go +++ b/pkg/utils/FileReceiver.go @@ -44,11 +44,3 @@ func copyFile(currentSize int, fileSize int, reader *bufio.Reader, buffer []byte } return false, false } - -func fileExists(filename string) bool { - info, err := os.Stat(filename) - if os.IsNotExist(err) { - return false - } - return !info.IsDir() -} diff --git a/pkg/utils/Network.go b/pkg/utils/Network.go index 607d42c..a8ceddc 100644 --- a/pkg/utils/Network.go +++ b/pkg/utils/Network.go @@ -1,6 +1,7 @@ package utils import ( + "errors" "fmt" "net" "strings" @@ -29,6 +30,26 @@ func NetworkLister() { print("\n") } +// RetrieveIPv4FromInterface Retrieve the first IPv4 from an interface. +func RetrieveIPv4FromInterface(inter string) (net.IP, error) { + ief, err := net.InterfaceByName(inter) + addrs, err := ief.Addrs() + if err != nil { + return nil, err + } + for _, addr := range addrs { + switch v := addr.(type) { + case *net.IPNet: + //if !v.IP.IsLoopback() { + if v.IP.To4() != nil { //Verify if IP is IPV4 + return v.IP, nil + } + //} + } + } + return nil, errors.New("no IPv4 found") +} + // IsIPv4 Check if the ip is a v4 or v6 ip func IsIPv4(addr string) bool { return strings.Count(addr, ":") < 2