Ajout de la prise en compte d'interface pour le multicast
This commit is contained in:
parent
a0f17fefe1
commit
3edaf6728f
@ -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
|
||||
|
@ -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()
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user