Exemple #1
0
        /* <pre>
         *       5.2.1.  INIT Received in COOKIE-WAIT or COOKIE-ECHOED State (Item B)
         *
         *       This usually indicates an initialization collision, i.e., each
         *       endpoint is attempting, at about the same time, to establish an
         *       association with the other endpoint.
         *
         *       Upon receipt of an INIT in the COOKIE-WAIT state, an endpoint MUST
         *       respond with an INIT ACK using the same parameters it sent in its
         *       original INIT chunk (including its Initiate Tag, unchanged).  When
         *       responding, the endpoint MUST send the INIT ACK back to the same
         *       address that the original INIT (sent by this endpoint) was sent.
         *
         *       Upon receipt of an INIT in the COOKIE-ECHOED state, an endpoint MUST
         *       respond with an INIT ACK using the same parameters it sent in its
         *       original INIT chunk (including its Initiate Tag, unchanged), provided
         *       that no NEW address has been added to the forming association.  If
         *       the INIT message indicates that a new address has been added to the
         *       association, then the entire INIT MUST be discarded, and NO changes
         *       should be made to the existing association.  An ABORT SHOULD be sent
         *       in response that MAY include the error 'Restart of an association
         *       with new addresses'.  The error SHOULD list the addresses that were
         *       added to the restarting association.
         *
         *       When responding in either state (COOKIE-WAIT or COOKIE-ECHOED) with
         *       an INIT ACK, the original parameters are combined with those from the
         *       newly received INIT chunk.  The endpoint shall also generate a State
         *       Cookie with the INIT ACK.  The endpoint uses the parameters sent in
         *       its INIT to calculate the State Cookie.
         *
         *       After that, the endpoint MUST NOT change its state, the T1-init timer
         *       shall be left running, and the corresponding TCB MUST NOT be
         *       destroyed.  The normal procedures for handling State Cookies when a
         *       TCB exists will resolve the duplicate INITs to a single association.
         *
         *       For an endpoint that is in the COOKIE-ECHOED state, it MUST populate
         *       its Tie-Tags within both the association TCB and inside the State
         *       Cookie (see Section 5.2.2 for a description of the Tie-Tags).
         *       </pre>
         */
        public virtual Chunk[] inboundInit(InitChunk init)
        {
            Chunk[] reply = null;
            _peerVerTag = init.getInitiateTag();
            _winCredit  = init.getAdRecWinCredit();
            _farTSN     = (uint)(init.getInitialTSN() - 1);

            _maxOutStreams = Math.Min(init.getNumInStreams(), MAXSTREAMS);
            _maxInStreams  = Math.Min(init.getNumOutStreams(), MAXSTREAMS);
            InitAckChunk iac = new InitAckChunk();

            iac.setAdRecWinCredit(MAXBUFF);
            iac.setNumInStreams(_maxInStreams);
            iac.setNumOutStreams(_maxOutStreams);
            iac.setInitialTSN(_nearTSN);
            iac.setInitiateTag(_myVerTag);
            CookieHolder cookie = new CookieHolder();

            cookie.cookieData = new byte[Association.COOKIESIZE];
            cookie.cookieTime = TimeExtension.CurrentTimeMillis();
            _random.NextBytes(cookie.cookieData);
            iac.setCookie(cookie.cookieData);
            _cookies.Add(cookie);

            byte[] fse = init.getFarSupportedExtensions();
            if (fse != null)
            {
                iac.setSupportedExtensions(this.getUnionSupportedExtensions(fse));
            }
            reply    = new Chunk[1];
            reply[0] = iac;
            //logger.LogDebug("SCTP received INIT:" + init.ToString());
            //logger.LogDebug("Replying with init-ack :" + iac.ToString());
            return(reply);
        }
Exemple #2
0
        /*
         *       Ok - confession here - we are not following the RFC.
         *       We don't encode a pile of stuff into the cookie and decode it
         *       when we get the cookie back, then use that data to initialize the Association.
         *       The rationale in the RFC is to protect the assocaition from resource exhaustion
         *       by fake cookies from bad guys - which makes sense if you are a naked SCTP stack on
         *       the internet accepting UDP packets (or IP ones).
         *       We on the other hand have already been through 2 levels of validation with ICE and DTLS,
         *       and have already committed a pile of resource to this connection, so 32 bytes more won't kill us.
         *
         *       The only downside is that if the far end spams us with a pile of inits at speed, we may erase one that we've
         *       replied to and that was about to be a happy camper. Shrug.
         */
        private CookieHolder checkCookieEcho(byte[] cookieData)
        {
            CookieHolder same = null;

            foreach (CookieHolder cookie in _cookies)
            {
                byte[] cd = cookie.cookieData;
                if (cd.Length == cookieData.Length)
                {
                    int i = 0;
                    while (i < cd.Length)
                    {
                        if (cd[i] != cookieData[i])
                        {
                            break;
                        }
                        i++;
                    }
                    if (i == cd.Length)
                    {
                        same = cookie;
                        break;
                    }
                }
            }
            return(same);
        }
Exemple #3
0
        public static Mock <ICookieProvider> CookieProvider(CookieHolder model)
        {
            var mock = new Mock <ICookieProvider>();

            mock.Setup(x => x.GetCookie()).Returns(model);

            return(mock);
        }
Exemple #4
0
 public PhoneManagerAppParamHolder(CookieHolder cookieHolder, string repositoryTestPhoneManagerData, NameValueCollection queryStringCollection, string referrerString, OutputModel sessionModel, string umbracoCurrentPageId)
 {
     CookieProvider      = CookieProvider(cookieHolder).Object;
     RepositoryProvider  = Repository(repositoryTestPhoneManagerData);
     QueryStringProvider = new QueryStringProvider(QueryStringImplementation(queryStringCollection).Object);
     ReferrerProvider    = new ReferrerProvider(ReferrerImplementation(referrerString).Object);
     SessionProvider     = SessionProvider(sessionModel).Object;
     UmbracoProvider     = UmbracoProvider(umbracoCurrentPageId).Object;
 }
        public void PhoneManagerApp_ProcessAllPotentialCandidatePhoneNumbers_WithCookie_WithFoundPhoneNumberNoPersist_ReturnsCookie()
        {
            // Arrange
            // generate test data
            var dataModel = new PhoneManagerModel()
            {
                DefaultCampaignQueryStringKey = "fsource", DefaultPersistDurationInDays = 32
            };

            dataModel.PhoneManagerCampaignDetail = new List <PhoneManagerCampaignDetail>()
            {
                new PhoneManagerCampaignDetail()
                {
                    Id = "1201", TelephoneNumber = "0800 123 4567", CampaignCode = "testcode"
                }
            };
            var testPhoneManagerData = dataModel.ToXmlString();

            var _dataProvider = MockProviders.Repository(testPhoneManagerData);

            var foundRecord = new PhoneManagerCampaignDetail()
            {
                Id = "1201", TelephoneNumber = "FOUND PHONENUMBER"
            };

            var _cookie = new CookieHolder()
            {
                Model =
                    new OutputModel()
                {
                    Id = "1202",
                    TelephoneNumber = "9999 999 9999"
                }
            };

            // generate the required result
            var correctResult = new FinalResultModel()
            {
                OutputCookieHolder = new CookieHolder(),
                OutputModelResult  = new OutputModel()
                {
                    Id = "1202",
                    TelephoneNumber = "9999 999 9999"
                },
                OutputResultSource = OutputSource.ExisitingCookie
            };

            PhoneManager target = new PhoneManager(null, _dataProvider, null, null, null, null);

            //Act
            FinalResultModel retVal = target.ProcessAllPotentialCandidatePhoneNumbers(_cookie, foundRecord);

            //Assert
            Assert.AreEqual(retVal.OutputResultSource, correctResult.OutputResultSource);
            Assert.AreEqual(retVal.OutputModelResult.Id, correctResult.OutputModelResult.Id);
        }
Exemple #6
0
        /// <summary>
        /// Pass all available available PhoneNumber records to decide finally which PhoneNumber to use
        /// </summary>
        internal FinalResultModel ProcessAllPotentialCandidatePhoneNumbers(CookieHolder exisitingCookie, PhoneManagerCampaignDetail foundRecord)
        {
            FinalResultModel result = new FinalResultModel();

            // check if there is an exisiting cookie we can use, and check if we want to use it
            if (exisitingCookie?.Model?.IsValid() ?? false)                                                                                                                       // we have a valid exisiting cookie
            {
                bool useExisitingCookieForSession = true;                                                                                                                         // let's assume we will want to use the existing cookie

                if ((foundRecord?.IsValidToSaveAsCookie() ?? false) && (foundRecord?.OverwritePersistingItem ?? false) && !exisitingCookie.Model.MatchesFoundRecord(foundRecord)) // foundRecordFromCriteria needs persisting and it should override any exisiting cookie
                {
                    useExisitingCookieForSession = false;                                                                                                                         // don't use the cookie as the _foundRecordFromCriteria has requested to overwrite any exisiting cookie
                }

                if (useExisitingCookieForSession)                      // continue using the exisiting cookie - no need to save a newc ookie
                {
                    result.OutputModelResult  = exisitingCookie.Model; // use the cookie data
                    result.OutputResultSource = OutputSource.ExisitingCookie;
                    return(result);
                }
            }

            // check if we have a valid _foundRecordFromCriteria that we can use
            if (foundRecord?.IsValid() ?? false) // we have a valid foundRecordFromCriteria object that we can use
            {
                result.OutputModelResult = new OutputModel()
                {
                    Id = foundRecord.Id,
                    TelephoneNumber  = foundRecord.TelephoneNumber,
                    CampaignCode     = foundRecord.CampaignCode,
                    AltMarketingCode = foundRecord.AltMarketingCode
                };

                if (foundRecord.IsValidToSaveAsCookie()) // it is requesting to be persisted via a cookie
                {
                    result.OutputCookieHolder = new CookieHolder()
                    {
                        Expires = DateTime.Today.AddDays((foundRecord.PersistDurationOverride > 0) ? foundRecord.PersistDurationOverride : _repository.GetDefaultSettings()?.DefaultPersistDurationInDays ?? 0), // persist duration in days - if foundRecord has persistDurationOverride set then use that, otherwise use the default admin setting
                        Model   = result.OutputModelResult
                    };
                }
                result.OutputResultSource = OutputSource.FoundRecordFromCriteria;
                return(result);
            }

            // as a last resort, output placeholder phone number
            result.OutputModelResult = new OutputModel()
            {
                Id = "P",
                TelephoneNumber = AppConstants.LastResortPhoneNumberPlaceholder
            };

            result.OutputResultSource = OutputSource.LastResortPlaceholder;
            return(result);
        }
Exemple #7
0
        private uint howStaleIsMyCookie(CookieHolder cookie)
        {
            uint ret = 0;
            long now = TimeExtension.CurrentTimeMillis();

            if ((now - cookie.cookieTime) < VALIDCOOKIELIFE)
            {
                ret = 0;
            }
            else
            {
                ret = (uint)((now - cookie.cookieTime) - VALIDCOOKIELIFE);
            }
            return(ret);
        }
        /// <summary>
        /// Set cookie adding subvalues for the PhoneManagerResultModel values
        /// </summary>
        /// <param name="model">CookieHolder</param>
        public void SetCookie(CookieHolder model)
        {
            HttpCookie cookie = new HttpCookie(AppConstants.CookieKeys.CookieMainKey);

            cookie.Expires = model.Expires;
            try
            {
                cookie.Value = JsonConvert.SerializeObject(model.Model);
                _cookieImplementation.SetCookie(cookie);
            }
            catch (JsonReaderException)
            {
                throw new ArgumentException($"Can't convert oject to JSON");
            }
        }
        /// <summary>
        /// Get cookie data and map to CookieHolder model
        /// </summary>
        /// <returns>Populated CookieHolder</returns>
        public CookieHolder GetCookie()
        {
            var cookie = _cookieImplementation.GetCookie(AppConstants.CookieKeys.CookieMainKey);

            if (cookie != null)
            {
                var result = new CookieHolder();
                result.Expires = cookie.Expires;
                try
                {
                    result.Model = JsonConvert.DeserializeObject <OutputModel>(cookie.Value);
                    return(result);
                }
                catch (JsonReaderException)
                {
                    throw new ArgumentException($"Provided definition is not valid JSON: {cookie.Value}");
                }
            }
            return(null);
        }