Ajout de la prise en compte d'interface pour le multicast

This commit is contained in:
Jérémi N ‘EndMove’ 2022-03-14 11:40:23 +01:00
parent a0f17fefe1
commit 3edaf6728f
Signed by: EndMove
GPG Key ID: 65C4A02E1F5371A4
3 changed files with 31 additions and 19 deletions

View File

@ -3,6 +3,7 @@ package network
import ( import (
"StoreBackEnd/pkg/protocol/repository" "StoreBackEnd/pkg/protocol/repository"
"StoreBackEnd/pkg/protocol/rules/writers" "StoreBackEnd/pkg/protocol/rules/writers"
"StoreBackEnd/pkg/utils"
"fmt" "fmt"
"net" "net"
"time" "time"
@ -48,6 +49,7 @@ func (client ClientMulticast) Run() {
// Resolve interface addr // Resolve interface addr
lAddr, failedRIA := ResolveInterfaceAddr(client.netInter) lAddr, failedRIA := ResolveInterfaceAddr(client.netInter)
if failedRIA { if failedRIA {
println("Error : No IPv4 found in interface")
return return
} }
@ -81,21 +83,18 @@ func (client ClientMulticast) ResolveAddr() (*net.UDPAddr, bool) {
// ResolveInterfaceAddr Resolves the network interface address. // ResolveInterfaceAddr Resolves the network interface address.
func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) { func ResolveInterfaceAddr(inter string) (*net.UDPAddr, bool) {
//ief, _ := net.InterfaceByName(inter) ip, err := utils.RetrieveIPv4FromInterface(inter)
//addrs, _ := ief.Addrs() if err != nil {
//for _, addr := range addrs { println("Error : " + err.Error())
// if utils.IsIPv4(addr.String()) { return nil, true
// println("Selected IP", addr.String()) }
// lAddr, _ := net.ResolveUDPAddr("udp", "127.0.0.1:15502") println("Selected IP : " + ip.String())
// return lAddr, false return &net.UDPAddr{IP: ip}, false
// }
//}
return nil, false
} }
// DialUdp Ouvre une connection UDP // DialUdp Ouvre une connection UDP
func (client ClientMulticast) DialUdp(lAddr *net.UDPAddr, rAddr *net.UDPAddr) (*net.UDPConn, bool) { 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 { if errDial != nil {
println(errDial.Error()) println(errDial.Error())
return nil, true return nil, true

View File

@ -44,11 +44,3 @@ func copyFile(currentSize int, fileSize int, reader *bufio.Reader, buffer []byte
} }
return false, false return false, false
} }
func fileExists(filename string) bool {
info, err := os.Stat(filename)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}

View File

@ -1,6 +1,7 @@
package utils package utils
import ( import (
"errors"
"fmt" "fmt"
"net" "net"
"strings" "strings"
@ -29,6 +30,26 @@ func NetworkLister() {
print("\n") 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 // IsIPv4 Check if the ip is a v4 or v6 ip
func IsIPv4(addr string) bool { func IsIPv4(addr string) bool {
return strings.Count(addr, ":") < 2 return strings.Count(addr, ":") < 2