package ipstack import ( "fmt" "github.com/pkg/errors" "iptcp/pkg/lnxconfig" "net" "net/netip" ) const ( MAX_IP_PACKET_SIZE = 1400 LOCAL_COST uint32 = 4294967295 // 2^32 - 1 STATIC_COST uint32 = 1 ) // STRUCTS --------------------------------------------------------------------- type Interface struct { Name string IpAddress netip.Addr IpPrefix netip.Prefix RecvSocket net.Conn SocketChannel chan<- bool State bool } type Neighbor struct { VipAddr netip.Addr UdpAddr netip.AddrPort SendSocket net.Conn SocketChannel chan<- bool } type RIPMessage struct { command uint8 numEntries uint8 entries []RIPEntry } type RIPEntry struct { addr netip.Addr cost uint32 mask netip.Prefix } type Hop struct { Cost uint32 VipAsStr string } // GLOBAL VARIABLES (data structures) ------------------------------------------ var myInterfaces []Interface var myNeighbors = make(map[string]Neighbor) // var myRIPNeighbors = make(map[string]Neighbor) type HandlerFunc func(int, string, *[]byte) error var protocolHandlers = make(map[uint16]HandlerFunc) // var routingTable = routingtable.New() var routingTable = make(map[netip.Prefix]Hop) // reference: https://github.com/brown-csci1680/lecture-examples/blob/main/ip-demo/cmd/udp-ip-recv/main.go func createUDPConn(UdpAddr netip.AddrPort, conn *net.Conn) error { listenString := UdpAddr.String() listenAddr, err := net.ResolveUDPAddr("udp4", listenString) if err != nil { return errors.WithMessage(err, "Error resolving address->\t"+listenString) } tmpConn, err := net.ListenUDP("udp4", listenAddr) if err != nil { return errors.WithMessage(err, "Could not bind to UDP port->\t"+listenString) } *conn = tmpConn return nil } func Initialize(lnxFilePath string) error { //if len(os.Args) != 2 { // fmt.Printf("Usage: %s \n", os.Args[0]) // os.Exit(1) //} //lnxFilePath := os.Args[1] // Parse the file lnxConfig, err := lnxconfig.ParseConfig(lnxFilePath) if err != nil { return errors.WithMessage(err, "Error parsing config file->\t"+lnxFilePath) } // 1) initialize the interfaces on this node here and into the routing table for _, iface := range lnxConfig.Interfaces { i := &Interface{ Name: iface.Name, IpAddress: iface.AssignedIP, IpPrefix: iface.AssignedPrefix, RecvSocket: nil, SocketChannel: nil, State: false, } err := createUDPConn(iface.UDPAddr, &i.RecvSocket) if err != nil { return errors.WithMessage(err, "Error creating UDP socket for interface->\t"+iface.Name) } myInterfaces = append(myInterfaces, *i) // add to routing table ifacePrefix := netip.PrefixFrom(iface.AssignedIP, iface.AssignedPrefix.Bits()) routingTable[ifacePrefix] = Hop{STATIC_COST, iface.Name} } // 2) initialize the neighbors connected to the node for _, neighbor := range lnxConfig.Neighbors { n := &Neighbor{ VipAddr: neighbor.DestAddr, UdpAddr: neighbor.UDPAddr, SendSocket: nil, } err := createUDPConn(neighbor.UDPAddr, &n.SendSocket) if err != nil { return errors.WithMessage(err, "Error creating UDP socket for neighbor->\t"+neighbor.DestAddr.String()) } // TODO: make a hash map myNeighbors[neighbor.InterfaceName] = *n // add to routing table neighborPrefix := netip.PrefixFrom(neighbor.DestAddr, 24) routingTable[neighborPrefix] = Hop{LOCAL_COST, neighbor.InterfaceName} } return nil } /* func ListerToInterfaces() { for _, iface := range myInterfaces { go RecvIp(iface) } } func RecvIp(iface Interface) error { for { buffer := make([]byte, MAX_IP_PACKET_SIZE) _, sourceAddr, err := iface.udp.ReadFrom(buffer) if err != nil { log.Panicln("Error reading from UDP socket ", err) } hdr, err := ipv4header.ParseHeader(buffer) if err != nil { fmt.Println("Error parsing header", err) continue } headerSize := hdr.Len headerBytes := buffer[:headerSize] checksumFromHeader := uint16(hdr.Checksum) computedChecksum := ValidateChecksum(headerBytes, checksumFromHeader) var checksumState string if computedChecksum == checksumFromHeader { checksumState = "OK" } else { checksumState = "FAIL" continue } // check ttl ttl := data[8] if ttl == 0 { fmt.Println("TTL is 0") continue } destAddr := netip.AddrFrom(data[16:20]) protocolNum := data[9] if destAddr == iface.addr { // send to handler protocolHandlers[protocolNum](data) // message := buffer[headerSize:] // fmt.Printf("Received IP packet from %s\nHeader: %v\nChecksum: %s\nMessage: %s\n", // sourceAddr.String(), hdr, checksumState, string(message)) } else { // decrement ttl and update checksum data[8] = ttl - 1 data[10] = 0 data[11] = 0 newChecksum := int(ComputeChecksum(data[:headerSize])) data[10] = newChecksum >> 8 data[11] = newChecksum & 0xff // check neighbors for _, neighbor := range iface.neighbors { if neighbor == destAddr { // send to neighbor // SendIp(destAddr, protocolNum, data) } } // check forwarding table } } } func ValidateChecksum(b []byte, fromHeader uint16) uint16 { checksum := header.Checksum(b, fromHeader) return checksum } func SendIp(dst netip.Addr, port uint16, protocolNum uint16, data []byte, iface Interface) error { bindLocalAddr, err := net.ResolveUDPAddr("udp4", iface.UDPAddr.String()) if err != nil { log.Panicln("Error resolving address: ", err) } addrString := fmt.Sprintf("%s:%s", dst, port) remoteAddr, err := net.ResolveUDPAddr("udp4", addrString) if err != nil { log.Panicln("Error resolving address: ", err) } fmt.Printf("Sending to %s:%d\n", remoteAddr.IP.String(), remoteAddr.Port) // Bind on the local UDP port: this sets the source port // and creates a conn conn, err := net.ListenUDP("udp4", bindLocalAddr) if err != nil { log.Panicln("Dial: ", err) } // Start filling in the header message := data[20:] hdr := ipv4header.IPv4Header{ Version: data[0] >> 4, Len: 20, // Header length is always 20 when no IP options TOS: data[1], TotalLen: ipv4header.HeaderLen + len(message), ID: data[4], Flags: data[6] >> 5, FragOff: data[6] & 0x1f, TTL: data[8], Protocol: data[9], Checksum: 0, // Should be 0 until checksum is computed Src: netip.MustParseAddr(iface.addr.String()), Dst: netip.MustParseAddr(dst.String()), Options: []byte{}, } // Assemble the header into a byte array headerBytes, err := hdr.Marshal() if err != nil { log.Fatalln("Error marshalling header: ", err) } // Compute the checksum (see below) // Cast back to an int, which is what the Header structure expects hdr.Checksum = int(ComputeChecksum(headerBytes)) headerBytes, err = hdr.Marshal() if err != nil { log.Fatalln("Error marshalling header: ", err) } bytesToSend := make([]byte, 0, len(headerBytes)+len(message)) bytesToSend = append(bytesToSend, headerBytes...) bytesToSend = append(bytesToSend, []byte(message)...) // Send the message to the "link-layer" addr:port on UDP bytesWritten, err := conn.WriteToUDP(bytesToSend, remoteAddr) if err != nil { log.Panicln("Error writing to socket: ", err) } fmt.Printf("Sent %d bytes\n", bytesWritten) } func ComputeChecksum(b []byte) uint16 { checksum := header.Checksum(b, 0) checksumInv := checksum ^ 0xffff return checksumInv } func ForwardIP(data []byte) error { } func AddRecvHandler(protocolNum uint8, callbackFunc HandlerFunc) error { if protocolHandlers[protocolNum] != nil { fmt.Printf("Warning: Handler for protocol %d already exists", protocolNum) } protocolHandlers[protocolNum] = callbackFunc return nil } func RemoveRecvHandler(protocolNum uint8) error { // consider error if protocolHandlers[protocolNum] == nil { return errors.Errorf("No handler for protocol %d", protocolNum) } delete(protocolHandlers, protocolNum) return nil } // func routeRip(data []byte) (error) { // // deconstruct packet // newRIPMessage := RIPMessage{} // newRIPMessage.command = data[0] // newRIPMessage.numEntries = data[1] // newRIPMessage.entries = make([]RIPEntry, newRIPMessage.numEntries) // } func GetNeighbors() []netip.Addr { return myNeighbors } */ func PrintInterfaces() { for _, iface := range myInterfaces { fmt.Printf("%s\t%s\t%t\n", iface.Name, iface.IpPrefix.String(), iface.State) } } func PrintNeighbors() { for ifaceName, neighbor := range myNeighbors { fmt.Printf("%s\t%s\t%s\n", ifaceName, neighbor.IpAddress.String(), neighbor.UdpAddr.String()) } } func SprintRoutingTable() string { message := "" for prefix, hop := range routingTable { message += fmt.Sprintf("%s\t%s\t%d\n", prefix.String(), hop.VipAsStr, hop.Cost) } return message }