Пример #1
0
        public void writeData(NetFlow nf)
        {
            //SPARE-PC3\DEVSQL
            LinqToDB.Data.DataConnection cn = LinqToDB.DataProvider.SqlServer.SqlServerTools.CreateDataConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NetworkData;Data Source=SPARE-PC3\\DEVSQL");

            DataContext db = new DataContext(cn.DataProvider, cn.ConnectionString);

            db.Insert(nf);
        }
Пример #2
0
        }//end tostring

        public void WriteToDB()
        {
            DataFunctions df = new DataFunctions();

            string   packet   = this._bytes.Length + "b.";
            ushort   version  = _header.Version;
            ushort   count    = _header.Count;
            long     uptime   = _header.UpTime.Ticks;
            DateTime secs     = _header.Secs;
            uint     sequence = _header.Sequence;
            uint     ID       = _header.ID;

            int a = 0;

            foreach (FlowSet flows in this._flowset)
            {
                a++;
                int    flowsetNum = a;
                ushort flowID     = flows.ID;
                ushort flowLength = flows.Length;

                int i = 1;
                foreach (Template templ in flows.Template)
                {
                    ushort templateID    = templ.ID;
                    ushort templateCount = templ.Count;

                    NetFlowPI.Data.NetFlow netflowData = new NetFlowPI.Data.NetFlow();

                    netflowData.NetFlowID = Guid.NewGuid(); //this is just a generic, unique ID field for each entry

                    //set the properties via the variables above
                    netflowData.NetFlowPacket = packet;
                    netflowData.Version       = version;
                    netflowData.Count         = count;
                    netflowData.UpTime        = uptime;
                    netflowData.Secs          = secs;
                    netflowData.Sequence      = sequence;
                    netflowData.ID            = ID;
                    netflowData.FlowsetNum    = a;
                    netflowData.FlowID        = flowID;
                    netflowData.TemplateID    = templateID;
                    netflowData.TemplateCount = templateCount;

                    foreach (Field fields in templ.Field)
                    {
                        if ((fields.GetTypes() == (UInt16)FieldType.IPV4_DST_ADDR) ||
                            (fields.GetTypes() == (UInt16)FieldType.IPV4_SRC_ADDR) ||
                            (fields.GetTypes() == (UInt16)FieldType.IPV4_NEXT_HOP) ||
                            (fields.GetTypes() == (UInt16)FieldType.IPV6_DST_ADDR) ||
                            (fields.GetTypes() == (UInt16)FieldType.IPV6_SRC_ADDR) ||
                            (fields.GetTypes() == (UInt16)FieldType.IPV6_NEXT_HOP))
                        {
                            if (fields.Value.Count != 0)
                            {
                                string ip = new IPAddress(fields.Value.ToArray()).ToString();
                                CommonFunctions.SetObjectProperty(fields.Type, ip, netflowData);
                            }
                        }
                        else if ((fields.GetTypes() == (UInt16)FieldType.L4_DST_PORT) || (fields.GetTypes() == (UInt16)FieldType.L4_SRC_PORT))
                        {
                            if (fields.Value.Count != 0)
                            {
                                ushort portValue = BitConverter.ToUInt16(fields.Value.ToArray().Reverse().ToArray(), 0);
                                CommonFunctions.SetObjectProperty(fields.Type, portValue, netflowData);
                            }
                        }
                        else
                        {
                            //string temp = "";
                            //foreach (Byte bt in fields.Value)
                            //{
                            //    temp += "0x" + bt.ToString("X") + " ";
                            //}
                            if (fields.Value.Count > 0)
                            {
                                byte[] ta    = fields.Value.ToArray();
                                byte[] tar   = ta.Reverse().ToArray();
                                ushort value = 0;

                                if (tar.Length == 1)
                                {
                                    int valueInt = (int)tar[0];
                                    value = (ushort)valueInt;
                                }
                                else
                                {
                                    value = BitConverter.ToUInt16(tar, 0);
                                }

                                CommonFunctions.SetObjectProperty(fields.Type, value, netflowData);
                            }
                        } //end if
                    }     //end foreach field

                    if (templ.Field.Count > 0)
                    {
                        df.writeData(netflowData);
                        //Console.WriteLine("Writting flow data to the database");
                    }

                    i++;
                }
            } //end for each flowset
        }     //end write to DB