aboutsummaryrefslogtreecommitdiff
path: root/cmd/vhost/main.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/vhost/main.go')
-rw-r--r--cmd/vhost/main.go91
1 files changed, 54 insertions, 37 deletions
diff --git a/cmd/vhost/main.go b/cmd/vhost/main.go
index 02158c4..4684c2f 100644
--- a/cmd/vhost/main.go
+++ b/cmd/vhost/main.go
@@ -3,22 +3,25 @@ package main
import (
"bufio"
"fmt"
- "os"
"iptcp/pkg/ipstack"
+ "net/netip"
+ "os"
"strings"
)
func main() {
-
- if len(os.Args) != 2 {
+ if len(os.Args) == 1 {
fmt.Printf("Usage: %s <configFile>\n", os.Args[0])
os.Exit(1)
}
- fileName := os.Args[1]
-
- go ipstack.Initialize(fileName)
+ fileName := os.Args[2]
+ err := ipstack.Initialize(fileName)
+ if err != nil {
+ return
+ }
+ ipstack.RegisterProtocolHandler(ipstack.TEST_PROTOCOL)
scanner := bufio.NewScanner(os.Stdin)
@@ -53,40 +56,54 @@ func main() {
// combine message into one string
messageToSend := strings.Join(message, " ")
messageToSendBytes := []byte(messageToSend)
-
- // check if ipAddr is in neighbor table
- iface, err := ipstack.GetNeighborByIP(ipAddr)
- if err != nil {
- fmt.Println(err)
- // check if ipAddr is in routing table
- iface, err = ipstack.GetRouteByIP(ipAddr)
- if err != nil {
- fmt.Println(err)
- continue
- }
-
- for _, interfaces := range ipstack.GetInterfaces() {
- if interfaces.Name == iface.Name {
- err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr, nil)
- if err != nil {
- fmt.Println(err)
- }
- break
+ hop, err := ipstack.LongestPrefix(netip.MustParseAddr(ipAddr))
+ myAddr := hop.Interface.IpPrefix.Addr()
+ for _, neighbor := range ipstack.GetNeighbors()[hop.Interface.Name] {
+ if neighbor.VipAddr == netip.MustParseAddr(ipAddr) {
+ err = ipstack.SendIP(&myAddr, neighbor, ipstack.TEST_PROTOCOL, messageToSendBytes, ipAddr, nil)
+ if err != nil {
+ fmt.Println(err)
}
}
- continue
- }
- for _, interfaces := range ipstack.GetInterfaces() {
- if interfaces.Name == iface.Name {
- err = ipstack.SendIP(interfaces, iface, 0, messageToSendBytes, ipAddr, nil)
- if err != nil {
- fmt.Println(err)
- }
- break
- }
}
- continue
+ //
+ // // check if ipAddr is in neighbor table
+ // iface, err := ipstack.GetNeighborByIP(ipAddr)
+ // if err != nil {
+ // fmt.Println(err)
+ //
+ // // check if ipAddr is in routing table
+ // iface, err = ipstack.GetRouteByIP(ipAddr)
+ // if err != nil {
+ // fmt.Println(err)
+ // continue
+ // }
+ //
+ // for _, interfaces := range ipstack.GetInterfaces() {
+ // if interfaces.Name == iface.Name {
+ // src := interfaces.IpPrefix.Addr()
+ // err = ipstack.SendIP(&src, iface, 0, messageToSendBytes, ipAddr, nil)
+ // if err != nil {
+ // fmt.Println(err)
+ // }
+ // break
+ // }
+ // }
+ // continue
+ // }
+ // for _, interfaces := range ipstack.GetInterfaces() {
+ // if interfaces.Name == iface.Name {
+ // src := interfaces.IpPrefix.Addr()
+ // err = ipstack.SendIP(&src, iface, 0, messageToSendBytes, ipAddr, nil)
+ // if err != nil {
+ // fmt.Println(err)
+ // }
+ // break
+ // }
+ // }
+ // continue
+ //}
}
}
if len(line) > 2 {
@@ -99,4 +116,4 @@ func main() {
continue
}
}
-} \ No newline at end of file
+}