aboutsummaryrefslogtreecommitdiff
path: root/cmd
diff options
context:
space:
mode:
authorDavid Doan <daviddoan@Davids-MacBook-Pro-70.local>2023-11-02 18:57:56 -0400
committerDavid Doan <daviddoan@Davids-MacBook-Pro-70.local>2023-11-02 18:57:56 -0400
commit5d7ffd42b638f33d32c01c46f853e26a5028b552 (patch)
treeed7220178ce76a213341f14fb27c6af63e95209a /cmd
parenta8c9b48821c85e072ca9054621928e415540b12c (diff)
milestone 1
Diffstat (limited to 'cmd')
-rw-r--r--cmd/vhost/main.go128
-rw-r--r--cmd/vrouter/main.go8
2 files changed, 88 insertions, 48 deletions
diff --git a/cmd/vhost/main.go b/cmd/vhost/main.go
index c0e5ad8..57f5f7c 100644
--- a/cmd/vhost/main.go
+++ b/cmd/vhost/main.go
@@ -4,9 +4,11 @@ import (
"bufio"
"fmt"
"iptcp/pkg/ipstack"
+ // "iptcp/pkg/tcpstack"
"net/netip"
"os"
"strings"
+ "strconv"
)
func main() {
@@ -22,12 +24,13 @@ func main() {
return
}
ipstack.RegisterProtocolHandler(ipstack.TEST_PROTOCOL)
-
+ ipstack.RegisterProtocolHandler(ipstack.TCP_PROTOCOL)
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
- switch line {
+ tokens := strings.Split(line, " ")
+ switch tokens[0] {
case "li":
fmt.Println("Name\tAddr/Prefix\tState")
fmt.Println(ipstack.SprintInterfaces())
@@ -43,59 +46,90 @@ func main() {
case "exit":
ipstack.CleanUp()
os.Exit(0)
- default:
- if len(line) > 4 {
- if line[:4] == "down" {
- ifaceName := line[5:]
- ipstack.InterfaceDownREPL(ifaceName)
- }
+ case "down":
+ // get interface name
+ ifaceName := tokens[1]
+ ipstack.InterfaceDownREPL(ifaceName)
+ case "up":
+ // get interface name
+ ifaceName := tokens[1]
+ ipstack.InterfaceUpREPL(ifaceName)
+ case "send":
+ // get IP address and message that follows it
+ IPAndMessage := strings.Split(line, " ")
+ ipAddr := IPAndMessage[1]
+ message := IPAndMessage[2:]
- if line[:4] == "send" {
- // get IP address and message that follows it
- IPAndMessage := strings.Split(line, " ")
- ipAddr := IPAndMessage[1]
- message := IPAndMessage[2:]
+ // combine message into one string
+ messageToSend := strings.Join(message, " ")
+ messageToSendBytes := []byte(messageToSend)
- // combine message into one string
- messageToSend := strings.Join(message, " ")
- messageToSendBytes := []byte(messageToSend)
-
- address, _ := netip.ParseAddr(ipAddr)
- hop, err := ipstack.Route(address)
+ address, _ := netip.ParseAddr(ipAddr)
+ hop, err := ipstack.Route(address)
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ myAddr := hop.Interface.IpPrefix.Addr()
+ for _, neighbor := range ipstack.GetNeighbors()[hop.Interface.Name] {
+ if neighbor.VipAddr == address ||
+ neighbor.VipAddr == hop.VIP && hop.Type == "S" {
+ bytesWritten, err := ipstack.SendIP(&myAddr, neighbor, ipstack.TEST_PROTOCOL, messageToSendBytes, ipAddr, nil)
+ fmt.Printf("Sent %d bytes to %s\n", bytesWritten, neighbor.VipAddr.String())
if err != nil {
fmt.Println(err)
- continue
- }
- myAddr := hop.Interface.IpPrefix.Addr()
- for _, neighbor := range ipstack.GetNeighbors()[hop.Interface.Name] {
- if neighbor.VipAddr == address ||
- neighbor.VipAddr == hop.VIP && hop.Type == "S" {
- bytesWritten, err := ipstack.SendIP(&myAddr, neighbor, ipstack.TEST_PROTOCOL, messageToSendBytes, ipAddr, nil)
- fmt.Printf("Sent %d bytes to %s\n", bytesWritten, neighbor.VipAddr.String())
- if err != nil {
- fmt.Println(err)
- }
- }
}
}
}
- if len(line) > 2 {
- if line[:2] == "up" {
- // get interface name
- ifaceName := line[3:]
- ipstack.InterfaceUpREPL(ifaceName)
- }
- } else {
- fmt.Println("Invalid command: ", line)
- fmt.Println("Commands: ")
- fmt.Println(" exit Terminate this program")
- fmt.Println(" li List interfaces")
- fmt.Println(" lr List routes")
- fmt.Println(" ln List available neighbors")
- fmt.Println(" up Enable an interface")
- fmt.Println(" down Disable an interface")
- fmt.Println(" send Send test packet")
+// ********************************************TCP REPL***************************************************************
+ case "a":
+ // accept a connection
+ // get the port number
+ port := line[2:]
+ uint16Port, err := strconv.ParseUint(port, 10, 16)
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ // listen on the port
+ listener, err := ipstack.VListen(uint16(uint16Port))
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ _, err = listener.VAccept()
+ if err != nil {
+ fmt.Println(err)
+ continue
}
+ case "c":
+ // connect to a port
+ vipAddr := tokens[1]
+ port := tokens[2]
+ uint16Port, err := strconv.ParseUint(port, 10, 16)
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ _, err = ipstack.VConnect(vipAddr, uint16(uint16Port))
+ if err != nil {
+ fmt.Println(err)
+ continue
+ }
+ case "ls":
+ // list sockets
+ fmt.Println("SID\tLAddr\t\tLPort\tRAddr\t\tRPort\tRStatus")
+ fmt.Println(ipstack.SprintSockets())
+ default:
+ fmt.Println("Invalid command: ", line)
+ fmt.Println("Commands: ")
+ fmt.Println(" exit Terminate this program")
+ fmt.Println(" li List interfaces")
+ fmt.Println(" lr List routes")
+ fmt.Println(" ln List available neighbors")
+ fmt.Println(" up Enable an interface")
+ fmt.Println(" down Disable an interface")
+ fmt.Println(" send Send test packet")
continue
}
}
diff --git a/cmd/vrouter/main.go b/cmd/vrouter/main.go
index 2736525..f338b80 100644
--- a/cmd/vrouter/main.go
+++ b/cmd/vrouter/main.go
@@ -4,9 +4,11 @@ import (
"bufio"
"fmt"
"iptcp/pkg/ipstack"
+ // "iptcp/pkg/tcpstack"
"net/netip"
"os"
"strings"
+ // "strconv"
)
func main() {
@@ -30,13 +32,17 @@ func main() {
ipstack.RegisterProtocolHandler(ipstack.TEST_PROTOCOL)
// register the rip protocol for the router
ipstack.RegisterProtocolHandler(ipstack.RIP_PROTOCOL)
+ // register the TCP protocol for the handler
+ ipstack.RegisterProtocolHandler(ipstack.TCP_PROTOCOL)
+
// create a scanner to read from stdin for command-line inputs
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
line := scanner.Text()
- switch line {
+ tokens := strings.Split(line, " ")
+ switch tokens[0] {
// print the interfaces
case "li":
fmt.Println("Name\tAddr/Prefix\tState")