/// <summary>
        /// Implements memcached's SASL auth sequence. (See the protocol docs for more details.)
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        private bool Auth(IPooledSocket socket)
        {
            SaslStep currentStep = new SaslStart(this.authenticationProvider);

            socket.Write(currentStep.GetBuffer());

            while (!currentStep.ReadResponse(socket).Success)
            {
                // challenge-response authentication
                if (currentStep.StatusCode == 0x21)
                {
                    currentStep = new SaslContinue(this.authenticationProvider, currentStep.Data);
                    socket.Write(currentStep.GetBuffer());
                }
                else
                {
                    if (log.IsWarnEnabled)
                        log.WarnFormat("Authentication failed, return code: 0x{0:x}", currentStep.StatusCode);

                    // invalid credentials or other error
                    return false;
                }
            }

            return true;
        }
Example #2
0
        /// <summary>
        /// Implements memcached's SASL auth sequence. (See the protocol docs for more details.)
        /// </summary>
        /// <param name="socket"></param>
        /// <returns></returns>
        private bool Auth(IPooledSocket socket)
        {
            SaslStep currentStep = new SaslStart(this.authenticationProvider);

            socket.Write(currentStep.GetBuffer());

            while (!currentStep.ReadResponse(socket).Success)
            {
                // challenge-response authentication
                if (currentStep.StatusCode == 0x21)
                {
                    currentStep = new SaslContinue(this.authenticationProvider, currentStep.Data);
                    socket.Write(currentStep.GetBuffer());
                }
                else
                {
                    if (log.IsWarnEnabled)
                    {
                        log.WarnFormat("Authentication failed, return code: 0x{0:x}", currentStep.StatusCode);
                    }

                    // invalid credentials or other error
                    return(false);
                }
            }

            return(true);
        }
Example #3
0
        public bool ExecuteAsync(IOperation op, Action <bool> next)
        {
            IPooledSocket socket = null;
            var           result = false;

            try
            {
                socket = _pool.Acquire();
                var buffers = op.GetBuffer();
                socket.Write(buffers);

                result = op.ReadResponseAsync(socket, readSuccess =>
                {
                    socket.Dispose();
                    next(readSuccess);
                });
            }
            catch (IOException e)
            {
                Log.Error(e);
            }
            finally
            {
                if (socket != null)
                {
                    _pool.Release(socket);
                }
            }
            return(result);
        }
        bool Authenticate(IPooledSocket socket)
        {
            var       isAuthenticated = true;
            const int authContinue    = 0x21;

            SaslStep step = new SaslStart(_provider);

            socket.Write(step.GetBuffer());
            while (!step.ReadResponse(socket).Success)
            {
                if (step.StatusCode == authContinue)
                {
                    step = new SaslContinue(_provider, step.Data);
                    socket.Write(step.GetBuffer());
                }
                else
                {
                    isAuthenticated = false;
                    break;
                }
            }
            return(isAuthenticated);
        }
        bool Authenticate(IPooledSocket socket)
        {
            var isAuthenticated = true;
            const int authContinue = 0x21;

            SaslStep step = new SaslStart(_provider);
            socket.Write(step.GetBuffer());
            while (!step.ReadResponse(socket).Success)
            {
                if (step.StatusCode == authContinue)
                {
                    step = new SaslContinue(_provider, step.Data);
                    socket.Write(step.GetBuffer());
                }
                else
                {
                    isAuthenticated = false;
                    break;
                }
            }
            return isAuthenticated;
        }
Example #6
0
        public IOperationResult Execute(IOperation op)
        {
            IOperationResult result = new BinaryOperationResult();
            IPooledSocket    socket = null;

            try
            {
                socket = _pool.Acquire();
                if (Log.IsDebugEnabled)
                {
                    Log.DebugFormat("Start execute {0} with {1}", op.Key, socket.InstanceId);
                }
                var buffers = op.GetBuffer();
                socket.Write(buffers);

                result = op.ReadResponse(socket);
                if (result.Success)
                {
                    result.Pass();
                }
            }
            catch (NodeShutdownException e)
            {
                string msg = String.Format("Node Shutdown - {0}.", EndPoint);
                Log.DebugFormat("m:{0} i:{1}\n{2}", msg, op.Key, e);
                result.Fail(msg, e);
                result.StatusCode = StatusCode.NodeShutdown.ToInt();
            }
            catch (QueueTimeoutException e)
            {
                string msg = String.Format("Queue Timeout - {0}.", EndPoint);
                Log.ErrorFormat("m:{0} i:{1}\n{2}", msg, op.Key, e);
                result.Fail(msg, e);
                result.StatusCode = StatusCode.SocketPoolTimeout.ToInt();
            }
            catch (IOException e)
            {
                string msg = String.Format("Exception reading response - {0}", EndPoint);
                Log.ErrorFormat("m:{0} s:{1} i:{2}\n{3}", msg, op.Key,
                                socket == null ? Guid.Empty : socket.InstanceId, e);
                result.Fail(msg, e);
                if (result.StatusCode == null ||
                    result.StatusCode == StatusCode.Success.ToInt())
                {
                    result.StatusCode = StatusCode.InternalError.ToInt();
                }
            }
            catch (Exception e)
            {
                string msg = String.Format("Operation failed - {0}", EndPoint);
                Log.ErrorFormat("m:{0} s:{1} i:{2}\n{3}", msg, op.Key,
                                socket == null ? Guid.Empty : socket.InstanceId, e);
                result.Fail(msg, e);
                if (result.StatusCode == null ||
                    result.StatusCode == StatusCode.Success.ToInt())
                {
                    result.StatusCode = StatusCode.InternalError.ToInt();
                }
            }
            finally
            {
                if (socket != null)
                {
                    if (Log.IsDebugEnabled)
                    {
                        Log.DebugFormat("End execute {0} with {1}", op.Key, socket.InstanceId);
                    }
                    _pool.Release(socket);
                }
            }
            if (Log.IsDebugEnabled)
            {
                const string msg = "Operation {0} :i {1} n: {2} t: {3} m: {4} sc: {5} r: {6}";
                Log.DebugFormat(msg, result.Success ?
                                op.RetryAttempts > 0 ? "succeeded*" : "succeeded" :
                                op.RetryAttempts > 0 ? "failed*" : "failed",
                                op.Key,
                                _endpoint, Thread.CurrentThread.Name,
                                result.Message,
                                Enum.GetName(typeof(StatusCode), result.StatusCode ?? 0),
                                op.RetryAttempts);
            }
            return(result);
        }