Example #1
0
        private void baseSend(object obj)
        {
            AbstractSocketData sockeData      = obj as AbstractSocketData;
            SocketMessager     socketMessager = new SocketMessager();

            socketMessager.encode(sockeData);
            byte[] dataBytes = socketMessager.DataBytes;
            if (dataBytes != null && dataBytes.Length > 0)
            {
                System.Net.Sockets.Socket socket = null;
                try
                {
                    System.Net.EndPoint remoteEP = new System.Net.IPEndPoint(this.ipAddress, this.port);
                    socket = new System.Net.Sockets.Socket(System.Net.Sockets.AddressFamily.InterNetwork, System.Net.Sockets.SocketType.Dgram, System.Net.Sockets.ProtocolType.Udp);
                    socket.SendTo(dataBytes, dataBytes.Length, System.Net.Sockets.SocketFlags.None, remoteEP);
                    socket.Shutdown(System.Net.Sockets.SocketShutdown.Send);
                }
                catch (System.Exception)
                {
                }
                finally
                {
                    if (socket != null)
                    {
                        socket.Close();
                    }
                }
            }
        }
Example #2
0
        private void waitCallback(object obj)
        {
            SocketMessager     socketMessager = (SocketMessager)obj;
            AbstractSocketData socketData     = socketMessager.decode();

            if (this.ResultHandler != null)
            {
                this.ResultHandler(this, new SocketDataEventArgs(socketData));
            }
        }
Example #3
0
        public void encode(AbstractSocketData sockeData)
        {
            if (sockeData == null)
            {
                return;
            }
            System.Type type = sockeData.GetType();
            if (!AttributeHelper.needEncoded(type))
            {
                return;
            }
            string assamble = sockeData.getAssamble();

            if (string.IsNullOrEmpty(assamble))
            {
                throw new System.Exception(type.ToString() + ".getAssamble() is not implemented.");
            }
            sockeData.Type = type.ToString() + "," + assamble;
            System.Reflection.FieldInfo[] fieldInfos    = ReflactUtil.getFieldInfos(type);
            System.Text.StringBuilder     stringBuilder = new System.Text.StringBuilder();
            System.Reflection.FieldInfo[] array         = fieldInfos;
            for (int i = 0; i < array.Length; i++)
            {
                System.Reflection.FieldInfo fieldInfo = array[i];
                if (AttributeHelper.needEncoded(fieldInfo))
                {
                    string name  = fieldInfo.Name;
                    object value = fieldInfo.GetValue(sockeData);
                    stringBuilder.Append(name + "{&}" + value).Append("{|}");
                }
            }
            this.dataBytes = System.Text.Encoding.UTF8.GetBytes(stringBuilder.ToString());
            if (this.dataBytes == null)
            {
                this.dataBytes = new byte[0];
            }
            this.dataLenth = this.dataBytes.Length;
        }
Example #4
0
 public SocketDataEventArgs(AbstractSocketData socketData)
 {
     this.socketData = socketData;
 }
Example #5
0
        public TrapMessage Handler(AbstractSocketData socketData)
        {
            if (socketData == null)
            {
                return(null);
            }
            TrapMessage result;

            try
            {
                int         dataLenth       = socketData.DataLenth;
                byte[]      dataBytes       = socketData.DataBytes;
                int         protocolVersion = SnmpPacket.GetProtocolVersion(dataBytes, dataLenth);
                TrapMessage trapMessage;
                SnmpPacket  snmpPacket;
                if (protocolVersion == 0)
                {
                    trapMessage = new TrapV1Message();
                    snmpPacket  = new SnmpV1TrapPacket();
                    ((SnmpV1TrapPacket)snmpPacket).decode(dataBytes, dataLenth);
                }
                else
                {
                    if (protocolVersion == 1)
                    {
                        trapMessage = new TrapV2Message();
                        snmpPacket  = new SnmpV2Packet();
                        ((SnmpV2Packet)snmpPacket).decode(dataBytes, dataLenth);
                        if (snmpPacket.Pdu.Type != PduType.V2Trap)
                        {
                            throw new SnmpException("Invalid SNMP version 2 packet type received.");
                        }
                    }
                    else
                    {
                        trapMessage = new TrapV3Message();
                        snmpPacket  = new SnmpV3Packet();
                        UserSecurityModel uSM = ((SnmpV3Packet)snmpPacket).GetUSM(dataBytes, dataLenth);
                        if (uSM.EngineId.Length <= 0)
                        {
                            throw new SnmpException("Invalid packet. Authoritative engine id is not set.");
                        }
                        if (uSM.SecurityName.Length <= 0)
                        {
                            throw new SnmpException("Invalid packet. Security name is not set.");
                        }
                        if (this.usmConfigs.Count > 0)
                        {
                            UsmConfig usmConfig = this.FindPeer(uSM.EngineId.ToString(), uSM.SecurityName.ToString());
                            if (usmConfig == null)
                            {
                                throw new SnmpException("SNMP packet from unknown peer.");
                            }
                            ((SnmpV3Packet)snmpPacket).USM.Authentication = (AuthenticationDigests)usmConfig.Authentication;
                            ((SnmpV3Packet)snmpPacket).USM.Privacy        = (PrivacyProtocols)usmConfig.Privacy;
                            if (usmConfig.Privacy != Privacy.None)
                            {
                                ((SnmpV3Packet)snmpPacket).USM.PrivacySecret.Set(usmConfig.PrivacySecret);
                            }
                            if (usmConfig.Authentication != Authentication.None)
                            {
                                ((SnmpV3Packet)snmpPacket).USM.AuthenticationSecret.Set(usmConfig.AuthenticationSecret);
                            }
                        }
                        ((SnmpV3Packet)snmpPacket).decode(dataBytes, dataLenth);
                        if (snmpPacket.Pdu.Type != PduType.V2Trap)
                        {
                            throw new SnmpException("Invalid SNMP version 3 packet type received.");
                        }
                    }
                }
                trapMessage.AgentIpAddress = socketData.Target;
                trapMessage.Port           = socketData.Port;
                SnmpTrapHandler.configTrap(trapMessage, protocolVersion, snmpPacket);
                this.configVb(trapMessage, protocolVersion, snmpPacket);
                result = trapMessage;
            }
            catch (System.Exception ex)
            {
                System.Console.WriteLine(ex.Message);
                result = null;
            }
            return(result);
        }
Example #6
0
        public AbstractSocketData decode()
        {
            string @string = System.Text.Encoding.UTF8.GetString(this.dataBytes, 0, this.dataLenth);

            string[] separator = new string[]
            {
                "{|}"
            };
            string[] array = @string.Split(separator, System.StringSplitOptions.RemoveEmptyEntries);
            if (array == null || array.Length < 1)
            {
                return(this.getSocketData(this.dataLenth, this.dataBytes, this.target, this.port));
            }
            string[] separator2 = new string[]
            {
                "{&}"
            };
            System.Collections.Generic.IDictionary <string, object> dictionary = new System.Collections.Generic.Dictionary <string, object>();
            string text = string.Empty;

            string[] array2 = array;
            for (int i = 0; i < array2.Length; i++)
            {
                string   text2  = array2[i];
                string[] array3 = text2.Split(separator2, System.StringSplitOptions.RemoveEmptyEntries);
                if (array3 != null && array3.Length == 2)
                {
                    if ("type".Equals(array3[0]))
                    {
                        text = array3[1];
                    }
                    else
                    {
                        dictionary.Add(array3[0], array3[1]);
                    }
                }
            }
            if (text == null || text.Equals(""))
            {
                return(this.getSocketData(this.dataLenth, this.dataBytes, this.target, this.port));
            }
            System.Type type = System.Type.GetType(text);
            if (type == null)
            {
                throw new System.Exception("getAssamble() is implemented incorrectly.");
            }
            AbstractSocketData abstractSocketData = null;

            try
            {
                abstractSocketData = (AbstractSocketData)ReflactUtil.newInstance(type, null);
            }
            catch (System.Exception)
            {
                throw new System.Exception("There is no default constructor in this type " + type);
            }
            System.Reflection.FieldInfo[] fieldInfos = ReflactUtil.getFieldInfos(type);
            System.Reflection.FieldInfo[] array4     = fieldInfos;
            for (int j = 0; j < array4.Length; j++)
            {
                System.Reflection.FieldInfo fieldInfo = array4[j];
                string name = fieldInfo.Name;
                if (dictionary.ContainsKey(name))
                {
                    string value = (string)dictionary[name];
                    if (fieldInfo.FieldType.Equals(typeof(byte)))
                    {
                        fieldInfo.SetValue(abstractSocketData, System.Convert.ToByte(value));
                    }
                    else
                    {
                        if (fieldInfo.FieldType.Equals(typeof(char)))
                        {
                            fieldInfo.SetValue(abstractSocketData, System.Convert.ToChar(value));
                        }
                        else
                        {
                            if (fieldInfo.FieldType.Equals(typeof(short)))
                            {
                                fieldInfo.SetValue(abstractSocketData, System.Convert.ToInt16(value));
                            }
                            else
                            {
                                if (fieldInfo.FieldType.Equals(typeof(int)))
                                {
                                    fieldInfo.SetValue(abstractSocketData, System.Convert.ToInt32(value));
                                }
                                else
                                {
                                    if (fieldInfo.FieldType.Equals(typeof(long)))
                                    {
                                        fieldInfo.SetValue(abstractSocketData, System.Convert.ToInt64(value));
                                    }
                                    else
                                    {
                                        if (fieldInfo.FieldType.Equals(typeof(double)))
                                        {
                                            fieldInfo.SetValue(abstractSocketData, System.Convert.ToDouble(value));
                                        }
                                        else
                                        {
                                            if (fieldInfo.FieldType.Equals(typeof(float)))
                                            {
                                                fieldInfo.SetValue(abstractSocketData, System.Convert.ToSingle(value));
                                            }
                                            else
                                            {
                                                if (fieldInfo.FieldType.Equals(typeof(string)))
                                                {
                                                    fieldInfo.SetValue(abstractSocketData, value);
                                                }
                                                else
                                                {
                                                    if (fieldInfo.FieldType.Equals(typeof(bool)))
                                                    {
                                                        fieldInfo.SetValue(abstractSocketData, System.Convert.ToBoolean(value));
                                                    }
                                                    else
                                                    {
                                                        if (fieldInfo.FieldType.Equals(typeof(System.DateTime)))
                                                        {
                                                            fieldInfo.SetValue(abstractSocketData, System.Convert.ToDateTime(value));
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return(abstractSocketData);
        }