public CachedRequest(Memcached.Opcode opc, byte[] bkey, byte[] bdata, int offset, int count)
 {
     Opcode = (int)opc;
     Key = ValidateKey(bkey);
     Data = bdata;
     DataOffset = offset;
     DataCount = count;
 }
 public CachedRequest(Memcached.Opcode opc, byte[] bkey = null, byte[] bdata = null)
 {
     Opcode = (int)opc;
     Key = ValidateKey(bkey);
     Data = bdata;
     if (bdata != null)
         DataCount = bdata.Length;
 }
 public CachedRequest(Memcached.Opcode opc, string skey, byte[] bdata = null)
 {
     Opcode = (int)opc;
     if (!string.IsNullOrEmpty(skey))
         Key = ValidateKey(skey);
     Data = bdata;
     if (bdata != null)
         DataCount = bdata.Length;
 }
 public void SetUpAttribute()
 {
     _memcached = new Memcached(
         ConfigurationManager.AppSettings["memcachedAddress"],
         Int32.Parse(ConfigurationManager.AppSettings["memcachedPort"]),
         ConfigurationManager.AppSettings["memcachedZone"],
         ConfigurationManager.AppSettings["memcachedUsername"],
         ConfigurationManager.AppSettings["memcachedPassword"]);
 }
        public MemcachedService()
        {
            _logger = LogManager.GetLogger(this.GetType().Name);
            _logger.Debug(string.Format("Instanciation du service {0} ({1})...\r\nExécution 64 bits: {2}\r\nPf: {3}", _serviceName, _applicationPath, IntPtr.Size == 8, Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)));
            this.ServiceName = _serviceName;
            EventLog.Source = _serviceName;
            this.EventLog.Log = "Application";
            this.AutoLog = true;

            this.CanHandlePowerEvent = false;
            this.CanHandleSessionChangeEvent = false;
            this.CanPauseAndContinue = false;
            this.CanStop = true;

            _memCached = new Memcached();
        }
        public MemcachedService()
        {
            _logger = LogManager.GetLogger(this.GetType().Name);
            _logger.Debug(string.Format("Instanciation du service {0} ({1})...\r\nExécution 64 bits: {2}\r\nPf: {3}", _serviceName, _applicationPath, IntPtr.Size == 8, Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles)));
            this.ServiceName  = _serviceName;
            EventLog.Source   = _serviceName;
            this.EventLog.Log = "Application";
            this.AutoLog      = true;

            this.CanHandlePowerEvent         = false;
            this.CanHandleSessionChangeEvent = false;
            this.CanPauseAndContinue         = false;
            this.CanStop = true;

            _memCached = new Memcached();
        }
        private static APICryptoResult ValidToken(Func <string, string> hash, Func <string, string, string> decrypt, string securityKey, string token)
        {
            var result = APICrypto.Decrypt(hash, decrypt, securityKey, token);

            if (result.Success)
            {
                var clientTime = new DateTime(result.Timestamp);
                //防止重放请求,防止服务器时间不一致
                if (DateTime.Now.Subtract(clientTime) > timeOut)
                {
                    result.Success = false;
                    result.Message = "该请求已过期";
                    return(result);
                }
                else
                {
                    if (Memcached.AddCacheExplicit(result.Id, DateTime.Now.ToString("G"), (int)timeOut.TotalMinutes))
                    {
                        return(result);
                    }
                    else
                    {
                        if (Memcached.GetCache(result.Id) != null)
                        {
                            //如果已存在KEY,则为重放请求
                            result.Success = false;
                            result.Message = "token已过期";
                            return(result);
                        }
                        else
                        {
                            logger.Info("缓存服务异常,身份验证已放行");
                            //缓存服务异常
                            return(result);
                        }
                    }
                }
            }
            else  //签名不合法
            {
                result.Success = false;
                result.Message = "无效的token";
                return(result);
            }
        }
Exemple #8
0
        private static void Talk_OnTalk(Dictionary <string, string> v)
        {
            int pid, port;

            if (v["type"] == "AttachMemcached")
            {
                pid  = Helper.ConvertTo <int>(v["mpid"]);
                port = Helper.ConvertTo <int>(v["mport"]);

                if (pid > 0 && port > 0)
                {
                    Log.Warning("Attaching the memcached for pid {0}", pid);


                    DataCacheInstance = Memcached.AttachExist("GeneralCache", (ushort)port, pid);
                }
            }
        }
 public CachedRequest(Memcached.Opcode opc)
 {
     Opcode = (int)opc;
 }
Exemple #10
0
        static void Main(string[] args)
        {
            int    mpid;
            ushort mport;

            Log.Init("backend");

            Log.EnableLogType(LogType.Critical);
            Log.EnableLogType(LogType.Verbose);
            Log.EnableLogType(LogType.Info);
            Log.EnableLogType(LogType.Error);
            Log.EnableLogType(LogType.Warning);

            if (!Config.Get().IsOK)
            {
                Log.Critical("Some of required config settings missing.");
                return;
            }

            Log.DisableAll();
            Log.EnableLogType((LogType)Config.Get().LogLevel);

            Log.Info("Booting up memcached instance");

            mpid  = TryGetOpt <int>("-mpid", args, 0);
            mport = TryGetOpt <ushort>("-mport", args, 0);

            if (mpid > 0 && mport > 0)
            {
                Log.Warning("Attach requested at pid: {0} and port {1}", mpid, mport);
                DataCacheInstance = Memcached.AttachExist("GeneralCache", mport, mpid);
            }
            else
            {
                DataCacheInstance = Memcached.Create("GeneralCache", 512, 11211);
            }

            if (Program.DataCacheInstance == null)
            {
                Log.Critical("Memcached could not started");
            }
            else
            {
                Log.Info("Memcached ok");
            }



            Init();

            Console.CancelKeyPress += Console_CancelKeyPress;

            while (running)
            {
                Thread.Sleep(10);
            }

            Uninit();

            Console.WriteLine("All resources released. press any key to exit");

            Log._Finalize();

            Console.ReadKey();
            Environment.Exit(0);
        }
Exemple #11
0
    // This method describes the main logic functionality of the Server
    // FIXME override Processor_Loop?
    protected static void SwitchLogic()
    {
        ulong local_icmp_code_type, local_chksum_udp, d, u;    //, ipv4;
        uint  i = 0, free = 0, mem_cnt = 0;
        byte  ii = 0, local_magic_num = 0, local_opcode = 0;

        uint  pkt_size = 0;
        bool  exist = false, is_ipv4 = false, is_udp = false, is_icmp = false;
        uint  cam_addr = 0, tmp_addr = 0, addr = 0;
        bool  good_IP_checksum = false, error = false;
        ulong local_key_value, local_extras, local_flag;

        while (true)     // Process packets indefinately
        {
            pkt_size = ReceiveFrame();

            // Extract information from the Ethernet, IP, TCP, UDP frames
            for (i = 0; i <= 11; i++)
            {
                d = tdata[i];
                u = tuser_low[i];
                Kiwi.Pause();
                Extract_headers(i, d, u);
            }


            Kiwi.Pause();
            // We need to store the shared-threat variables here
            // otherwise if we use it explicity we get long compilation times
            is_ipv4              = IPv4;
            is_udp               = proto_UDP;
            is_icmp              = proto_ICMP;
            local_magic_num      = magic_num;
            local_opcode         = opcode;
            local_icmp_code_type = ICMP_code_type;
            local_chksum_udp     = chksum_UDP;
            local_key_value      = key_value;
            local_extras         = extras;
            local_flag           = flag;


            Kiwi.Pause();
            // #############################
            // # Server Logic -- START
            // #############################

            // #######################################################################################
            // #           MEMCACHED SERVER
            if (is_ipv4 && is_udp)
            {
                chksumIP         = Emu.Protocols.IPv4.calc_IP_checksum(dataplane.tdata);
                good_IP_checksum = (chksumIP == (ulong)0x00);
                //Kiwi.Pause();



                if ((local_magic_num == Memcached.REQUEST) && good_IP_checksum)        //&& (local_chksum_udp==(ulong)0x00ffff))
                {
                    // Interpret Memcached opcode as CAM operation.
                    var cam_opcode = Memcached.Memcached_Operation(local_opcode);
                    // Execute CAM operation.
                    addr = CAM_Control(cam_opcode, key);
                    Kiwi.Pause();
                    // Execute Memcached operation.
                    switch (local_opcode)
                    {
                    case Memcached.SET_op:
                        VALUES_MEM[addr] = key_value;
                        EXTRAS_MEM[addr] = extras;
                        break;

                    case Memcached.GET_op:
                        key_value = VALUES_MEM[addr];
                        flag      = EXTRAS_MEM[addr];
                        break;

                    default:
                        break;
                    }

                    error = addr == (uint)MEM_SIZE;
                    Kiwi.Pause();

                    chksum_UDP = (ulong)0x00;
                    chksumIP   = 0x00;
                    swap_multiple_fields(is_udp, is_icmp);
                    Kiwi.Pause();
                    // Create the response packet + reset appropriate fields(ex. UDP checksum)
                    switch (local_opcode)
                    {
                    case Memcached.SET_op:
                        pkt_size = Memcached.Memcached_SET(ref dataplane.tdata, IP_total_length, UDP_total_length);
                        // Set the correct metadata for the datapath
                        // Fixed size response packet for SET - DELETE success
                        tuser_low[0] = (src_port << 24) | (src_port << 16) | (ulong)74;
                        tkeep[9]     = (byte)0x03;
                        //pkt_size = 8;
                        break;

                    case Memcached.DELETE_op:
                        pkt_size = Memcached_DELETE(error);
                        break;

                    case Memcached.GET_op:
                        pkt_size = Memcached_GET(error);
                        break;
                    }
                    Kiwi.Pause();

                    tmp = tdata[3];
                    Kiwi.Pause();

                    chksumIP = Emu.Protocols.IPv4.calc_IP_checksum(dataplane.tdata);
                    Kiwi.Pause();
                    // Set the new IP checksum - as we dont change any info in the header,
                    // the checksum should remane the same - but anyway recalc and put it back
                    tdata[3] = (chksumIP >> 8 | (chksumIP & (ulong)0x00ff) << 8) | tmp;
                    Kiwi.Pause();

                    // Here is the UDP checksum - clear it and preserve the other data
                    tmp = tdata[5];             //we have already clear the UDP checksum
                    Kiwi.Pause();
                    // The 4th element in the buffer is the start of the UDP frame
                    for (i = 4; i <= pkt_size; i++)
                    {
                        tmp2 = (i != 4) ? tdata[i] : tdata[i] >> 16;
                        Kiwi.Pause();
                        chksum_UDP = UDP.calc_UDP_checksum(chksum_UDP, tmp2);
                    }
                    tmp3 = (tdata[4] & (ulong)0xffff000000000000) | (ulong)0x001100; // Here is the new UDP length + proto type
                    tmp2 = src_ip << 32 | dst_ip;                                    // (optimization) src, dst IPs
                    Kiwi.Pause();
                    chksum_UDP = UDP.calc_UDP_checksum(chksum_UDP, tmp3);            // (optimization) 11U = 0x11 = UDP proto_type , UDP length
                    Kiwi.Pause();
                    chksum_UDP = UDP.calc_UDP_checksum(chksum_UDP, tmp2);
                    Kiwi.Pause();
                    // 1's complement of the result
                    tmp2 = (ulong)((chksum_UDP ^ ~(ulong)0x00) & (ulong)0x00ffff);
                    Kiwi.Pause();
                    // make it back to little endian
                    tmp3 = (ulong)((tmp2 >> 8) | (ulong)(tmp2 & (ulong)0x00ff) << 8);
                    // Set the new UDP checksum
                    tdata[5] = tmp | tmp3;
                }
            }
            // #
            // ###########################################################################################


            Kiwi.Pause();



            // #############################
            // # Server Logic -- END
            // #############################

            // Procedure calchksum_ICMPl for transmiting packet
            SendFrame(pkt_size);
            //End of frame, ready for next frame
            IPv4       = false;
            proto_UDP  = false;
            proto_ICMP = false;
            chksum_UDP = 0x00;
            pkt_size   = 0x00;
        }
    }