func NewConnectionFromPacket(packet []byte) (Connection, error) { ipheader, err := ipv4.ParseHeader(packet) if err != nil { return Connection{}, err } tcpheader, err := tcp.ParseHeader(packet[ipheader.Len:]) if err != nil { return Connection{}, err } return NewConnectionFromPacketAndIPHeaderAndTCPHeader(packet, ipheader, tcpheader) }
func (t *connectionManager) Process() error { var wg sync.WaitGroup for i := 0; i < t.configuration.Manager.Agents; i++ { wg.Add(1) go func() { defer wg.Done() for { message := <-t.reader.output ipheader, err := ipv4.ParseHeader(message) if err != nil { log.Println("Error: Parsing IPHeader:" + err.Error()) log.Printf("Message:%v\n", message) continue } if int(ipheader.Flags) < 2 { log.Printf("Warning: More Fragments Flag Is Set\n") } tcpheader, err := tcp.ParseHeader(message[ipheader.Len:]) if err != nil { log.Println("Error: Parsing TCPHeader:" + err.Error()) log.Printf("Message:%v\n", message) continue } connectionhelper, err := datastore.GetConnectionHelper() if err != nil { log.Printf("Error: Getting Connection Helper: %v\n", err.Error()) return } connectionhelper.Lock() connection, err := connectionhelper.GetConnection(ipheader.Src, tcpheader.SourcePort, ipheader.Dst, tcpheader.DestinationPort) if err != nil { log.Printf("Error: Getting Connection: %v\n", err.Error()) connectionhelper.Unlock() return } if bytes.Equal(connection.SourceIP(), net.IP{}) { //No connection found connection, err = datastore.NewConnectionFromPacketAndIPHeaderAndTCPHeader(message, ipheader, tcpheader) if err != nil { log.Printf("Error: Creating New Connection: %v\n", err.Error()) connectionhelper.Unlock() continue } err = connectionhelper.SetConnection(connection) if err != nil { log.Printf("Error: Saving New Connection: %v\n", err.Error()) connectionhelper.Unlock() continue } } else { //Connection found //We need to find out if the connection is still for this device and user devicehelper, err := datastore.GetDeviceHelper() if err != nil { log.Printf("Error: Getting Device Helper: %v\n", err.Error()) connectionhelper.Unlock() return } device, err := devicehelper.GetDeviceByIP(ipheader.Src) if err != nil { log.Printf("Error: Getting Source Device By IP(%v) \"%v\"\n", ipheader.Src.String(), err.Error()) } if bytes.Equal(device.MACAddress, net.HardwareAddr{}) { device, err = devicehelper.GetDeviceByIP(ipheader.Dst) if err != nil { log.Printf("Error: Getting Destination Device By IP(%v) \"%v\"\n", ipheader.Dst.String(), err.Error()) } } //If we have a device, is now or it's empty currentuser := "" if device.GetActiveUser() != nil { currentuser = device.GetActiveUser().Username } //Device, User or Number of In Memory Packets Breached... if !bytes.Equal(device.MACAddress, connection.DeviceID()) || (connection.Username() != currentuser) || connection.NumberOfPackets() >= t.configuration.Manager.MaxPackets { //Device or User has changed. err = connectionhelper.RemoveConnection(connection) if err != nil { log.Printf("Error: Removing Connection: %v\n", err.Error()) connectionhelper.Unlock() continue } //If a channel is registered and we had no errors closing it. if t.manager.output != nil { select { case t.manager.output <- connection: default: log.Println("Error: Manager Queue is full! Discarding Closed Connection..") } } //Create A New Connection connection, err := datastore.NewConnectionFromPacketAndIPHeaderAndTCPHeader(message, ipheader, tcpheader) if err != nil { log.Printf("Error: Creating New Connection After Remove: %v\n", err.Error()) connectionhelper.Unlock() continue } err = connectionhelper.SetConnection(connection) if err != nil { log.Printf("Error: Saving New Connection After Remove: %v\n", err.Error()) connectionhelper.Unlock() continue } } else { err = connection.AddPacket(message) if err != nil { log.Printf("Error: Adding Packet: %v\n", err.Error()) connectionhelper.Unlock() continue } err = connectionhelper.SetConnection(connection) if err != nil { log.Printf("Error: Saving Packet after adding packet: %v\n", err.Error()) connectionhelper.Unlock() continue } } } connectionhelper.Unlock() } }() } wg.Wait() log.Println("Error: We seem to be existing packet processing??") return errors.New("Error: Packet Processing Exited?") }