예제 #1
0
		private void ProccessSubscribe(AcceptedRequest tu, IncomingMessageEx request)
		{
			var statusCode = StatusCodes.OK;
			var dialog = dialogManager.GetOrCreate(request.Reader, request.ConnectionAddresses, out statusCode);

			var writer = GetWriter();

			int expires = 0;
			if (statusCode == StatusCodes.OK)
			{
				expires = request.Reader.GetExpires(600, int.MaxValue);

				writer.WriteStatusLine(statusCode);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode, dialog.LocalTag);
				writer.WriteDate(DateTime.UtcNow);
				writer.WriteExpires(expires);
				writer.WriteContentLength(0);
				writer.WriteCRLF();
			}
			else
			{
				writer.WriteResponse(request.Reader, statusCode);
			}

			tu.SendResponse(request, writer);

			if (statusCode == StatusCodes.OK)
				SendNotify(dialog, expires);
		}
예제 #2
0
		private void ProccessService(AcceptedRequest tu, IncomingMessageEx request)
		{
			var account = accounts.GetAccount(request.Reader.RequestUri.Hostport.Host);

			var writer = GetWriter();

			if (account != null)
			{
				var outContent = serviceSoap.ProcessRequest<IAccount>(request.Content, Search, account);

				writer.WriteStatusLine(StatusCodes.OK);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);
				writer.WriteContentType(type, subtype);
				writer.WriteCustomHeaders();
				writer.WriteContentLength();
				writer.WriteCRLF();

				writer.Write(outContent.GenerateToByteArray());
				writer.RewriteContentLength();
			}
			else
			{
				writer.WriteStatusLine(StatusCodes.NotFound);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.NotFound);
				writer.WriteContentLength(0);
				writer.WriteCRLF();
			}

			tu.SendResponse(request, writer);
		}
예제 #3
0
		private void ProccessService(AcceptedRequest tu, IncomingMessageEx request)
		{
			var writer = GetWriter();

			try
			{
				var outContent = mras.ProcessRequest(request.Content);

				writer.WriteStatusLine(StatusCodes.OK);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);
				writer.WriteContentType(type, subtype);
				writer.WriteCustomHeaders();
				writer.WriteContentLength();
				writer.WriteCRLF();

				writer.Write(outContent.GenerateToByteArray());
				writer.RewriteContentLength();
			}
			catch (MrasException)
			{
				writer.WriteResponse(request.Reader, StatusCodes.BadRequest);
			}

			tu.SendResponse(request, writer);
		}
예제 #4
0
        private void ProccessService(AcceptedRequest tu, IncomingMessageEx request)
        {
            var account = accounts.GetAccount(request.Reader.RequestUri.Hostport.Host);

            var writer = GetWriter();

            if (account != null)
            {
                var outContent = serviceSoap.ProcessRequest <IAccount>(request.Content, Search, account);

                writer.WriteStatusLine(StatusCodes.OK);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);
                writer.WriteContentType(type, subtype);
                writer.WriteCustomHeaders();
                writer.WriteContentLength();
                writer.WriteCRLF();

                writer.Write(outContent.GenerateToByteArray());
                writer.RewriteContentLength();
            }
            else
            {
                writer.WriteStatusLine(StatusCodes.NotFound);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.NotFound);
                writer.WriteContentLength(0);
                writer.WriteCRLF();
            }

            tu.SendResponse(request, writer);
        }
예제 #5
0
        private void ProccessService(AcceptedRequest tu, IncomingMessageEx request)
        {
            var writer = GetWriter();

            try
            {
                var outContent = mras.ProcessRequest(request.Content);

                writer.WriteStatusLine(StatusCodes.OK);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);
                writer.WriteContentType(type, subtype);
                writer.WriteCustomHeaders();
                writer.WriteContentLength();
                writer.WriteCRLF();

                writer.Write(outContent.GenerateToByteArray());
                writer.RewriteContentLength();
            }
            catch (MrasException)
            {
                writer.WriteResponse(request.Reader, StatusCodes.BadRequest);
            }

            tu.SendResponse(request, writer);
        }
예제 #6
0
        public override IEnumerable <AcceptedRequest> GetAcceptedRequests()
        {
            yield return(new AcceptedRequest(this)
            {
                Method = Methods.Servicem,
                IsAcceptedRequest = (reader) =>
                                    AcceptedRequest.IsAccepted(reader, serviceContentType, serviceContentSubtype, true),
                IncomingRequest = ProccessService,
            });

            yield return(new AcceptedRequest(this)
            {
                Method = Methods.Subscribem,
                IsAcceptedRequest = (reader) =>
                                    AcceptedRequest.IsAccepted(reader, subscribeContentType, subscribeContentSubtype, true),
                IncomingRequest = ProccessSubscribe,
            });

            yield return(new AcceptedRequest(this)
            {
                Method = Methods.Subscribem,
                IsAcceptedRequest = IsUnsubscribeAccepted,
                IncomingRequest = ProccessUnsubscribe,
            });
        }
예제 #7
0
        private void ProccessSubscribe(AcceptedRequest tu, IncomingMessageEx request)
        {
            var statusCode = StatusCodes.OK;
            var dialog     = dialogManager.GetOrCreate(request.Reader, request.ConnectionAddresses, out statusCode);

            var writer = GetWriter();

            int expires = 0;

            if (statusCode == StatusCodes.OK)
            {
                expires = request.Reader.GetExpires(600, int.MaxValue);

                writer.WriteStatusLine(statusCode);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode, dialog.LocalTag);
                writer.WriteDate(DateTime.UtcNow);
                writer.WriteExpires(expires);
                writer.WriteContentLength(0);
                writer.WriteCRLF();
            }
            else
            {
                writer.WriteResponse(request.Reader, statusCode);
            }

            tu.SendResponse(request, writer);

            if (statusCode == StatusCodes.OK)
            {
                SendNotify(dialog, expires);
            }
        }
예제 #8
0
 public override IEnumerable <AcceptedRequest> GetAcceptedRequests()
 {
     yield return(new AcceptedRequest(this)
     {
         Method = Methods.Servicem,
         IsAcceptedRequest = (reader) =>
                             AcceptedRequest.IsValidContentType(reader, type, subtype) &&
                             AcceptedRequest.IsToUserEmpty(reader),
         IncomingRequest = ProccessService,
     });
 }
예제 #9
0
        private void ProccessOptions(AcceptedRequest tu, IncomingMessageEx request)
        {
            var writer = GetWriter();

            writer.WriteStatusLine(StatusCodes.OK);
            writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);             // statusCode есть во writer'е!!!!!!
            writer.WriteAllow(AllowMethods);
            writer.WriteCRLF();

            tu.SendResponse(request, writer);
        }
예제 #10
0
		private void ProccessOptions(AcceptedRequest tu, IncomingMessageEx request)
		{
			var writer = GetWriter();

			writer.WriteStatusLine(StatusCodes.OK);
			writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK); // statusCode есть во writer'е!!!!!!
			writer.WriteAllow(AllowMethods);
			writer.WriteCRLF();

			tu.SendResponse(request, writer);
		}
예제 #11
0
        private void ProccessRequest(AcceptedRequest tu, IncomingMessageEx request)
        {
            var writer = GetWriter();

            writer.WriteStatusLine(StatusCodes.NotImplemented);
            writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.NotImplemented);
            writer.WriteContentLength(0);
            writer.WriteCRLF();

            tu.SendResponse(request, writer);
        }
예제 #12
0
		private void ProccessRequest(AcceptedRequest tu, IncomingMessageEx request)
		{
			var writer = GetWriter();

			writer.WriteStatusLine(StatusCodes.NotImplemented);
			writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.NotImplemented);
			writer.WriteContentLength(0);
			writer.WriteCRLF();

			tu.SendResponse(request, writer);
		}
예제 #13
0
        private void ProccessService(AcceptedRequest tu, IncomingMessageEx request)
        {
            var         writer     = GetWriter();
            StatusCodes statusCode = StatusCodes.OK;

            string endpointId = null;

            if (request.Reader.Count.ContactCount == 0)
            {
                statusCode = StatusCodes.BadRequest;
            }
            else
            {
                endpointId = (request.Reader.Contact[0].SipInstance.IsValid == true ?
                              request.Reader.Contact[0].SipInstance : request.Reader.Contact[0].AddrSpec1.Value).ToString();
            }

            if (statusCode == StatusCodes.OK)
            {
                try
                {
                    var categories = enhancedPresence.ParsePublication(request.Content);

                    var content = enhancedPresence.ProcessPublication(
                        request.Reader.From.AddrSpec.Value.ToString(), categories, endpointId);

                    writer.WriteStatusLine(statusCode);
                    writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode);
                    writer.WriteContentType(new ByteArrayPart(categories.OutContentType), new ByteArrayPart(categories.OutContentSubtype));
                    // нужен ли контакт
                    writer.WriteCustomHeaders();
                    writer.WriteContentLength();
                    writer.WriteCRLF();

                    int endOfHeaders = writer.Count;
                    writer.Write(content.GenerateToByteArray());
                    writer.RewriteContentLength(writer.Count - endOfHeaders);
                }
                catch (EnhancedPresenceException)
                {
                    statusCode = StatusCodes.BadRequest;
                }
            }

            if (statusCode != StatusCodes.OK)
            {
                writer.WriteResponse(request.Reader, statusCode);
            }

            tu.SendResponse(request, writer);
        }
예제 #14
0
        private void ProccessPublish(AcceptedRequest tu, IncomingMessageEx request)
        {
            StatusCodes statusCode = StatusCodes.OK;

            int expires = request.Reader.GetExpires(600, 900);

            if (request.Reader.IsExpiresTooBrief(60))
            {
                statusCode = StatusCodes.IntervalTooBrief;
            }

            int sipIfMatch = simpleModule.InvalidEtag;

            if (statusCode == StatusCodes.OK)
            {
                if (request.Reader.SipIfMatch.Length == 8)
                {
                    if (HexEncoding.TryParseHex8(request.Reader.SipIfMatch.Bytes, request.Reader.SipIfMatch.Begin, out sipIfMatch) == false)
                    {
                        statusCode = StatusCodes.CallLegTransactionDoesNotExist;
                    }
                }
            }

            if (statusCode == StatusCodes.OK)
            {
                var fromUser = request.Reader.From.AddrSpec.User.ToString();
                var fromHost = request.Reader.From.AddrSpec.Hostport.Host.ToString();
                if (simpleModule.Publish(fromUser + "@" + fromHost, ref sipIfMatch, expires, request.Content) == false)
                {
                    statusCode = StatusCodes.BadRequest;
                }
            }

            var writer = GetWriter();

            if (statusCode == StatusCodes.OK)
            {
                writer.WriteStatusLine(statusCode);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode);
                writer.WriteExpires(expires);
                writer.WriteSipEtag(sipIfMatch);
                writer.WriteCRLF();
            }
            else
            {
                writer.WriteResponse(request.Reader, statusCode);
            }

            SendResponse(request, writer);
        }
예제 #15
0
        private void ProccessUnsubscribe(AcceptedRequest tu, IncomingMessageEx request)
        {
            var writer = GetWriter();

            var dialog = dialogManager.Get(request.Reader);

            if (dialog != null)
            {
                writer.WriteResponse(request.Reader, StatusCodes.OK, dialog.LocalTag);
                dialogManager.Remove(dialog.Id);
            }
            else
            {
                writer.WriteResponse(request.Reader, StatusCodes.OK);
            }

            tu.SendResponse(request, writer);
        }
예제 #16
0
        public override IEnumerable <AcceptedRequest> GetAcceptedRequests()
        {
            yield return(new AcceptedRequest(this)
            {
                Method = Methods.Publishm,
                IsAcceptedRequest = (reader) =>
                                    AcceptedRequest.IsAccepted(reader, presence, application, pidfXml, true),
                IncomingRequest = ProccessPublish,
            });

            yield return(new AcceptedRequest(this)
            {
                Method = Methods.Subscribem,
                IsAcceptedRequest = (reader) =>
                                    AcceptedRequest.IsAccepted(reader, presence, false),
                IncomingRequest = ProccessSubscribe,
            });
        }
예제 #17
0
		private void ProccessPublish(AcceptedRequest tu, IncomingMessageEx request)
		{
			StatusCodes statusCode = StatusCodes.OK;

			int expires = request.Reader.GetExpires(600, 900);

			if (request.Reader.IsExpiresTooBrief(60))
				statusCode = StatusCodes.IntervalTooBrief;

			int sipIfMatch = simpleModule.InvalidEtag;

			if (statusCode == StatusCodes.OK)
			{
				if (request.Reader.SipIfMatch.Length == 8)
					if (HexEncoding.TryParseHex8(request.Reader.SipIfMatch.Bytes, request.Reader.SipIfMatch.Begin, out sipIfMatch) == false)
						statusCode = StatusCodes.CallLegTransactionDoesNotExist;
			}

			if (statusCode == StatusCodes.OK)
			{
				var fromUser = request.Reader.From.AddrSpec.User.ToString();
				var fromHost = request.Reader.From.AddrSpec.Hostport.Host.ToString();
				if (simpleModule.Publish(fromUser + "@" + fromHost, ref sipIfMatch, expires, request.Content) == false)
					statusCode = StatusCodes.BadRequest;
			}

			var writer = GetWriter();

			if (statusCode == StatusCodes.OK)
			{
				writer.WriteStatusLine(statusCode);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode);
				writer.WriteExpires(expires);
				writer.WriteSipEtag(sipIfMatch);
				writer.WriteCRLF();
			}
			else
			{
				writer.WriteResponse(request.Reader, statusCode);
			}

			SendResponse(request, writer);
		}
예제 #18
0
        private void ProccessCancelRequest(AcceptedRequest tu, IncomingMessageEx request)
        {
            var statusCode = StatusCodes.CallLegTransactionDoesNotExist;

            int inviteTransactionKId = Transaction.GetRelaytedInviteServerKId(request.TransactionId);

            if (inviteTransactionKId != Transaction.InvalidKId)
            {
                var requestProxy = requestProxyes.TryGetValue(inviteTransactionKId);

                if (requestProxy != null)
                {
                    CancelClients(requestProxy, request.Reader);
                    statusCode = StatusCodes.OK;
                }
            }

            SendResponse(request, statusCode);
        }
예제 #19
0
        private void ProccessSubscribe(AcceptedRequest tu, IncomingMessageEx request)
        {
            var dialog = dialogManager.GetOrCreate(request.Reader, request.ConnectionAddresses);

            if (dialog == null)
            {
                SendResponse(request, StatusCodes.CallLegTransactionDoesNotExist);
            }
            else if (request.Reader.IsExpiresTooBrief(60))
            {
                SendResponse(request, StatusCodes.IntervalTooBrief);
            }
            else
            {
                int expires = request.Reader.GetExpires(600, 900);


                var dialogId = dialog.Id.ToString();
                var toUser   = request.Reader.To.AddrSpec.User.ToString();
                var toHost   = request.Reader.To.AddrSpec.Hostport.Host.ToString();
                var document = simpleModule.Subscribe(dialogId, toUser + "@" + toHost, expires);


                var statusCode = StatusCodes.OK;
                var writer     = GetWriter();

                writer.WriteStatusLine(statusCode);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode, dialog.LocalTag);
                writer.WriteExpires(expires);
                writer.WriteCRLF();

                SendResponse(request, writer);


                if (document != null)
                {
                    SendNotify(dialog, expires, document);
                }
            }
        }
예제 #20
0
        public void OnIncomingRequest(AcceptedRequest router, IncomingMessageEx message)
        {
            bool repeat;
            var  mode = router.AuthorizationMode;

            do
            {
                repeat = mode == AuthorizationMode.Custom;

                switch (mode)
                {
                case AuthorizationMode.Custom:
                {
                    mode = OnCustomAuthorization(message);
                }
                break;

                case AuthorizationMode.Disabled:
                {
                    router.OnIncomingRequest(message);
                }
                break;

                case AuthorizationMode.Enabled:
                {
                    SipMessageWriter writer;
                    if (Authentication.IsAuthorized(message.Reader, message.Content, out writer))
                    {
                        router.OnIncomingRequest(message);
                    }
                    else if (writer != null)
                    {
                        SendResponse(message, writer);
                    }
                }
                break;
                }
            }while (repeat);
        }
예제 #21
0
		public void OnIncomingRequest(AcceptedRequest router, IncomingMessageEx message)
		{
			bool repeat;
			var mode = router.AuthorizationMode;

			do
			{
				repeat = mode == AuthorizationMode.Custom;

				switch (mode)
				{
					case AuthorizationMode.Custom:
						{
							mode = OnCustomAuthorization(message);
						}
						break;

					case AuthorizationMode.Disabled:
						{
							router.OnIncomingRequest(message);
						}
						break;

					case AuthorizationMode.Enabled:
						{
							SipMessageWriter writer;
							if (Authentication.IsAuthorized(message.Reader, message.Content, out writer))
								router.OnIncomingRequest(message);
							else if (writer != null)
								SendResponse(message, writer);
						}
						break;
				}
			}
			while (repeat);
		}
예제 #22
0
        public override IEnumerable <AcceptedRequest> GetAcceptedRequests()
        {
            yield return(new AcceptedRequest(this)
            {
                Method = Methods.Subscribem,
                AuthorizationMode = AuthorizationMode.Disabled,
                IsAcceptedRequest = (reader) =>
                {
                    int accepted = reader.FindHeaderIndex(HeaderNames.Accept, 0);

                    if (accepted < 0)
                    {
                        return false;
                    }

                    var result = reader.Headers[accepted].Value.EndWith(contentType) && AcceptedRequest.IsToEqualsFrom(reader);

                    return result;
                },
                IncomingRequest = ProccessSubscribe,
            });
        }
예제 #23
0
        private void ProccessRegister(AcceptedRequest tu, IncomingMessageEx request)
        {
            StatusCodes statusCode = StatusCodes.OK;

            bool isStar = HasStarContact(request.Reader);
            var  user   = request.Reader.To.AddrSpec.User;
            var  domain = request.Reader.RequestUri.Hostport.Host;

            if (isStar)
            {
                if (request.Reader.Count.ContactCount > 1 && request.Reader.Expires != 0)
                {
                    statusCode = StatusCodes.BadRequest;
                }
            }
            else
            {
                if (IsExpiresValid(request.Reader) == false)
                {
                    statusCode = StatusCodes.IntervalTooBrief;
                }
            }

            if (statusCode == StatusCodes.OK)
            {
                if (isStar)
                {
                    locationService.RemoveAllBindings(user, domain);
                }
                else
                if (locationService.UpdateBindings(user, domain, request, defaultExpires) == false)
                {
                    statusCode = StatusCodes.Gone;
                }
            }

            var writer = GetWriter();

            if (statusCode == StatusCodes.OK)
            {
                writer.WriteStatusLine(StatusCodes.OK);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);

                int maxExpires = 0;

                foreach (var binding in locationService.GetEnumerableBindings(user, domain))
                {
                    writer.WriteContact(binding.AddrSpec, binding.SipInstance, binding.Expires);

                    if (maxExpires < binding.Expires)
                    {
                        maxExpires = binding.Expires;
                    }
                }

                writer.WriteExpires(maxExpires);
                writer.WriteCustomHeaders();
                writer.WriteContentLength(0);
                writer.WriteCRLF();
            }
            else
            {
                writer.WriteStatusLine(statusCode);
                writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode);
                if (statusCode == StatusCodes.IntervalTooBrief)
                {
                    writer.WriteMinExpires(minExpires);
                }
                writer.WriteContentLength(0);
                writer.WriteCRLF();
            }

            tu.SendResponse(request, writer);
        }
예제 #24
0
		private void ProccessSubscribe(AcceptedRequest tu, IncomingMessageEx request)
		{
			var dialog = dialogManager.GetOrCreate(request.Reader, request.ConnectionAddresses);

			if (dialog == null)
				SendResponse(request, StatusCodes.CallLegTransactionDoesNotExist);
			else if (request.Reader.IsExpiresTooBrief(60))
				SendResponse(request, StatusCodes.IntervalTooBrief);
			else
			{
				int expires = request.Reader.GetExpires(600, 900);


				var dialogId = dialog.Id.ToString();
				var toUser = request.Reader.To.AddrSpec.User.ToString();
				var toHost = request.Reader.To.AddrSpec.Hostport.Host.ToString();
				var document = simpleModule.Subscribe(dialogId, toUser + "@" + toHost, expires);


				var statusCode = StatusCodes.OK;
				var writer = GetWriter();

				writer.WriteStatusLine(statusCode);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode, dialog.LocalTag);
				writer.WriteExpires(expires);
				writer.WriteCRLF();

				SendResponse(request, writer);


				if (document != null)
					SendNotify(dialog, expires, document);
			}
		}
예제 #25
0
		private void ProccessRegister(AcceptedRequest tu, IncomingMessageEx request)
		{
			StatusCodes statusCode = StatusCodes.OK;

			bool isStar = HasStarContact(request.Reader);
			var user = request.Reader.To.AddrSpec.User;
			var domain = request.Reader.RequestUri.Hostport.Host;

			if (isStar)
			{
				if (request.Reader.Count.ContactCount > 1 && request.Reader.Expires != 0)
					statusCode = StatusCodes.BadRequest;
			}
			else
			{
				if (IsExpiresValid(request.Reader) == false)
					statusCode = StatusCodes.IntervalTooBrief;
			}

			if (statusCode == StatusCodes.OK)
			{
				if (isStar)
					locationService.RemoveAllBindings(user, domain);
				else
					if (locationService.UpdateBindings(user, domain, request, defaultExpires) == false)
						statusCode = StatusCodes.Gone;
			}

			var writer = GetWriter();

			if (statusCode == StatusCodes.OK)
			{
				writer.WriteStatusLine(StatusCodes.OK);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, StatusCodes.OK);

				int maxExpires = 0;

				foreach (var binding in locationService.GetEnumerableBindings(user, domain))
				{
					writer.WriteContact(binding.AddrSpec, binding.SipInstance, binding.Expires);

					if (maxExpires < binding.Expires)
						maxExpires = binding.Expires;
				}

				writer.WriteExpires(maxExpires);
				writer.WriteCustomHeaders();
				writer.WriteContentLength(0);
				writer.WriteCRLF();
			}
			else
			{
				writer.WriteStatusLine(statusCode);
				writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode);
				if (statusCode == StatusCodes.IntervalTooBrief)
					writer.WriteMinExpires(minExpires);
				writer.WriteContentLength(0);
				writer.WriteCRLF();
			}

			tu.SendResponse(request, writer);
		}
예제 #26
0
		private void ProccessUnsubscribe(AcceptedRequest tu, IncomingMessageEx request)
		{
			var writer = GetWriter();

			var dialog = dialogManager.Get(request.Reader);

			if (dialog != null)
			{
				writer.WriteResponse(request.Reader, StatusCodes.OK, dialog.LocalTag);
				dialogManager.Remove(dialog.Id);
			}
			else
			{
				writer.WriteResponse(request.Reader, StatusCodes.OK);
			}

			tu.SendResponse(request, writer);
		}
예제 #27
0
		private void ProccessSubscribe(AcceptedRequest tu, IncomingMessageEx request)
		{
			StatusCodes statusCode = StatusCodes.OK;
			string endpointId = string.Empty;

			int expires = request.Reader.GetExpires(600, 900);

			if (request.Reader.IsExpiresTooBrief(60))
				statusCode = StatusCodes.IntervalTooBrief;

			if (statusCode == StatusCodes.OK)
			{
				if (request.Reader.Count.ContactCount == 0)
					statusCode = StatusCodes.BadRequest;
				else
					endpointId = (request.Reader.Contact[0].SipInstance.IsValid == true ?
						request.Reader.Contact[0].SipInstance : request.Reader.Contact[0].AddrSpec1.Value).ToString();
			}

			Dialog dialog = null;
			if (statusCode == StatusCodes.OK)
				dialog = dialogManager.GetOrCreate(request.Reader, request.ConnectionAddresses, out statusCode);

			var writer = GetWriter();

			if (statusCode == StatusCodes.OK)
			{
                try
                {
                    var batchSubscribe = enhancedPresence.ParseSubscribe(request.Content);

                    var contents = enhancedPresence.ProcessSubscription(
                        request.Reader.From.AddrSpec.Value.ToString(),
                        dialog.Id.ToString(),
                        batchSubscribe,
                        (uint)expires,
                        endpointId,
                        dialog);

                    writer.WriteStatusLine(statusCode);
                    writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode, dialog.LocalTag);
                    writer.WriteContact(dialog.LocalEndPoint, dialog.Transport);
                    writer.WriteExpires(expires);
                    writer.WriteEventPresence();
                    writer.WriteSupportedMsBenotify();
                    writer.WriteContentTypeMultipart(contentType);
                    writer.WriteCustomHeaders();
                    writer.WriteContentLength();
                    writer.WriteCRLF();

                    WriteResponseContent(writer, contents);
                }
                catch (EnhancedPresenceException)
                {
                    // TODO: add error info in response
                    statusCode = StatusCodes.BadRequest;
                }
			}

            if (statusCode != StatusCodes.OK)
            {
				writer.WriteResponse(request.Reader, statusCode);
			}

			tu.SendResponse(request, writer);
		}
예제 #28
0
		private void ProccessService(AcceptedRequest tu, IncomingMessageEx request)
		{
			var writer = GetWriter();
			StatusCodes statusCode = StatusCodes.OK;

			string endpointId = null;
			if (request.Reader.Count.ContactCount == 0)
				statusCode = StatusCodes.BadRequest;
			else
				endpointId = (request.Reader.Contact[0].SipInstance.IsValid == true ?
					request.Reader.Contact[0].SipInstance : request.Reader.Contact[0].AddrSpec1.Value).ToString();

			if (statusCode == StatusCodes.OK)
			{
				try
				{
					var categories = enhancedPresence.ParsePublication(request.Content);

					var content = enhancedPresence.ProcessPublication(
						request.Reader.From.AddrSpec.Value.ToString(), categories, endpointId);

					writer.WriteStatusLine(statusCode);
					writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode);
					writer.WriteContentType(new ByteArrayPart(categories.OutContentType), new ByteArrayPart(categories.OutContentSubtype));
					// нужен ли контакт
					writer.WriteCustomHeaders();
					writer.WriteContentLength();
					writer.WriteCRLF();

					int endOfHeaders = writer.Count;
					writer.Write(content.GenerateToByteArray());
					writer.RewriteContentLength(writer.Count - endOfHeaders);
				}
				catch (EnhancedPresenceException)
				{
					statusCode = StatusCodes.BadRequest;
				}
			}

			if (statusCode != StatusCodes.OK)
			{
				writer.WriteResponse(request.Reader, statusCode);
			}

			tu.SendResponse(request, writer);
		}
예제 #29
0
        private void ProccessSubscribe(AcceptedRequest tu, IncomingMessageEx request)
        {
            StatusCodes statusCode = StatusCodes.OK;
            string      endpointId = string.Empty;

            int expires = request.Reader.GetExpires(600, 900);

            if (request.Reader.IsExpiresTooBrief(60))
            {
                statusCode = StatusCodes.IntervalTooBrief;
            }

            if (statusCode == StatusCodes.OK)
            {
                if (request.Reader.Count.ContactCount == 0)
                {
                    statusCode = StatusCodes.BadRequest;
                }
                else
                {
                    endpointId = (request.Reader.Contact[0].SipInstance.IsValid == true ?
                                  request.Reader.Contact[0].SipInstance : request.Reader.Contact[0].AddrSpec1.Value).ToString();
                }
            }

            Dialog dialog = null;

            if (statusCode == StatusCodes.OK)
            {
                dialog = dialogManager.GetOrCreate(request.Reader, request.ConnectionAddresses, out statusCode);
            }

            var writer = GetWriter();

            if (statusCode == StatusCodes.OK)
            {
                try
                {
                    var batchSubscribe = enhancedPresence.ParseSubscribe(request.Content);

                    var contents = enhancedPresence.ProcessSubscription(
                        request.Reader.From.AddrSpec.Value.ToString(),
                        dialog.Id.ToString(),
                        batchSubscribe,
                        (uint)expires,
                        endpointId,
                        dialog);

                    writer.WriteStatusLine(statusCode);
                    writer.CopyViaToFromCallIdRecordRouteCSeq(request.Reader, statusCode, dialog.LocalTag);
                    writer.WriteContact(dialog.LocalEndPoint, dialog.Transport);
                    writer.WriteExpires(expires);
                    writer.WriteEventPresence();
                    writer.WriteSupportedMsBenotify();
                    writer.WriteContentTypeMultipart(contentType);
                    writer.WriteCustomHeaders();
                    writer.WriteContentLength();
                    writer.WriteCRLF();

                    WriteResponseContent(writer, contents);
                }
                catch (EnhancedPresenceException)
                {
                    // TODO: add error info in response
                    statusCode = StatusCodes.BadRequest;
                }
            }

            if (statusCode != StatusCodes.OK)
            {
                writer.WriteResponse(request.Reader, statusCode);
            }

            tu.SendResponse(request, writer);
        }
예제 #30
0
        //private void ProccessRequest(AcceptedRequest tu, IncomingMessageEx request)
        //{
        //    var statusCode = Validate(request.Reader);

        //    if (statusCode == StatusCodes.OK)
        //    {
        //        RequestProxyFactory requestProxy = new RequestProxyFactory(this, tu, request);

        //        try
        //        {
        //            //ByteArrayPart domain, user = request.Reader.RequestUri.User;
        //            //...or from To

        //            var user = request.Reader.To.AddrSpec.User;
        //            var domain = request.Reader.To.AddrSpec.Hostport.Host;

        //            foreach (var binding in locationService.GetEnumerableBindings(user, domain))
        //            {
        //                requestProxy.Forward(binding);
        //            }

        //            if (requestProxy.WasForwarded == false)
        //            {
        //                if (IsFromLocal(request.Reader))
        //                {
        //                    var trunk = trunkManager.GetTrunkByDomain(request.Reader.To.AddrSpec.Hostport.Host);
        //                    if (trunk != null)
        //                        requestProxy.Forward(trunk);
        //                }
        //                else
        //                {
        //                    var trunk = trunkManager.GetTrunkByDomain(request.Reader.From.AddrSpec.Hostport.Host);

        //                    if (trunk != null)
        //                    {
        //                        if (request.Reader.To.Tag.IsValid)
        //                        {
        //                            int tag;
        //                            Dialog dialog1 = null;
        //                            if (HexEncoding.TryParseHex8(request.Reader.To.Tag, out tag))
        //                                dialog1 = trunk.GetDialog1(tag);

        //                            if (dialog1 != null)
        //                                requestProxy.Forward(trunk, tag, dialog1);
        //                        }
        //                        else
        //                        {
        //                            foreach (var binding in locationService.GetEnumerableBindings(trunk.ForwardCallToUri))
        //                            {
        //                                requestProxy.Forward(trunk, binding);
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        finally
        //        {
        //            if (requestProxy.WasForwarded)
        //            {
        //                requestProxy.RequestProxy.Close();

        //                Monitor.Exit(requestProxy.RequestProxy);
        //                RemoveIfFinished(requestProxy.RequestProxy);
        //            }
        //        }

        //        if (requestProxy.WasForwarded == false)
        //            statusCode = StatusCodes.TemporarilyUnavailable;
        //    }

        //    if (statusCode != StatusCodes.OK)
        //    {
        //        var writer = GetWriter();
        //        writer.WriteResponse(request.Reader, statusCode);
        //        tu.SendResponse(request, writer);
        //    }
        //}

        //private void ProccessRequest(AcceptedRequest tu, IncomingMessageEx request)
        //{
        //    var statusCode = Validate(request.Reader);

        //    if (statusCode == StatusCodes.OK)
        //    {
        //        RequestProxy requestProxy = null;

        //        try
        //        {
        //            //ByteArrayPart domain, user = request.Reader.RequestUri.User;
        //            //...or from To

        //            var user = request.Reader.To.AddrSpec.User;
        //            var domain = request.Reader.To.AddrSpec.Hostport.Host;

        //            foreach (var binding in locationService.GetEnumerableBindings(user, domain))
        //            {
        //                CreateLockedRequestProxy(tu, request, ref requestProxy);
        //                var proxie = new LocalProxie(binding, requestProducer.GetTransactionId(request.Reader.Method));
        //                ForwardRequest(requestProxy, proxie, request.Reader, request.Content);
        //            }

        //            if (requestProxy == null)
        //            {
        //                if (IsFromLocal(request.Reader))
        //                {
        //                    var trunk = trunkManager.GetTrunkByDomain(request.Reader.To.AddrSpec.Hostport.Host);
        //                    if (trunk != null)
        //                    {
        //                        var proxie = new LocalTrunkProxie(requestProducer.GetTransactionId(request.Reader.Method), trunk);
        //                        CreateLockedRequestProxy(tu, request, ref requestProxy);
        //                        ForwardRequest(requestProxy, proxie, request.Reader, request.Content);
        //                    }
        //                }
        //                else
        //                {
        //                    var trunk = trunkManager.GetTrunkByDomain(request.Reader.From.AddrSpec.Hostport.Host);

        //                    if (trunk != null)
        //                    {
        //                        if (request.Reader.To.Tag.IsValid)
        //                        {
        //                            int tag;
        //                            Dialog dialog1 = null;
        //                            if (HexEncoding.TryParseHex8(request.Reader.To.Tag, out tag))
        //                                dialog1 = trunk.GetDialog1(tag);

        //                            if (dialog1 != null)
        //                            {
        //                                var proxie = new TrunkDialogProxie(requestProducer.GetTransactionId(request.Reader.Method), trunk, tag, dialog1);
        //                                CreateLockedRequestProxy(tu, request, ref requestProxy);
        //                                ForwardRequest(requestProxy, proxie, request.Reader, request.Content);
        //                            }
        //                        }
        //                        else
        //                        {
        //                            foreach (var binding in locationService.GetEnumerableBindings(trunk.ForwardCallToUri))
        //                            {
        //                                var proxie = new TrunkLocalProxie(requestProducer.GetTransactionId(request.Reader.Method), trunk, binding);
        //                                CreateLockedRequestProxy(tu, request, ref requestProxy);
        //                                ForwardRequest(requestProxy, proxie, request.Reader, request.Content);
        //                            }
        //                        }
        //                    }
        //                }
        //            }
        //        }
        //        finally
        //        {
        //            if (requestProxy != null)
        //            {
        //                requestProxy.Close();

        //                Monitor.Exit(requestProxy);
        //                RemoveIfFinished(requestProxy);
        //            }
        //        }

        //        if (requestProxy == null)
        //            statusCode = StatusCodes.TemporarilyUnavailable;
        //    }

        //    if (statusCode != StatusCodes.OK)
        //    {
        //        var writer = GetWriter();
        //        writer.WriteResponse(request.Reader, statusCode);
        //        tu.SendResponse(request, writer);
        //    }
        //}

        //private void CreateLockedRequestProxy(AcceptedRequest tu, IncomingMessageEx request, ref RequestProxy requestProxy)
        //{
        //    if (requestProxy == null)
        //    {
        //        requestProxy = new RequestProxy(request);
        //        Monitor.Enter(requestProxy);

        //        ////-----------
        //        if (request.Reader.Method != Methods.Ackm)
        //            requestProxyes.Add(requestProxy.ServerTransactionId, requestProxy);
        //        ////-----------

        //        if (request.Reader.Method == Methods.Invitem)
        //        {
        //            var writer = GetWriter();
        //            writer.WriteResponse(request.Reader, StatusCodes.Trying);
        //            tu.SendResponse(request, writer);
        //        }
        //    }
        //}

        //private void ForwardRequest(RequestProxy requestProxy, IProxie proxie, SipMessageReader reader, ArraySegment<byte> content)
        //{
        //    clientTransactionIds.Add(proxie.TransactionId, requestProxy.ServerTransactionId);

        //    ////-----------
        //    if (reader.Method == Methods.Ackm)
        //        proxie.IsFinalReceived = true;
        //    ////-----------

        //    requestProxy.AddProxie(proxie);

        //    var message = GetWriter();
        //    proxie.GenerateForwardedRequest(Authentication, message, reader, content,
        //        requestProxy.ConnectionAddresses, requestProxy.ServerTransactionId);

        //    requestProducer.SendRequest(proxie.ToConnectionAddresses, message, proxie.TransactionId, requestProxy.ServerTransactionId);

        //    proxie.TimerC = (reader.CSeq.Method == Methods.Invitem) ?
        //        timerC.Add(new ServerClientId(requestProxy.ServerTransactionId, proxie.TransactionId)) : timerC.InvalidTimerIndex;
        //}

        private void ProccessRequest(AcceptedRequest tu, IncomingMessageEx request)
        {
            var statusCode = Validate(request.Reader);

            if (statusCode == StatusCodes.OK)
            {
                statusCode = Filter(request.Reader);
            }

            if (statusCode == StatusCodes.OK)
            {
                RequestProxyFactory requestProxy = new RequestProxyFactory(this, request);

                try
                {
                    //ByteArrayPart domain, user = request.Reader.RequestUri.User;
                    //...or from To

                    var method = request.Reader.Method;

                    var user   = request.Reader.To.AddrSpec.User;
                    var domain = request.Reader.To.AddrSpec.Hostport.Host;

                    foreach (var binding in locationService.GetEnumerableBindings(user, domain))
                    {
                        requestProxy.Forward(
                            ProxieFactory.Create(GetTransactionId(method), binding));
                    }

                    if (requestProxy.HasValue == false)
                    {
                        if (IsFromLocal(request.Reader))
                        {
                            var trunk = trunkManager.GetTrunkByDomain(request.Reader.To.AddrSpec.Hostport.Host);
                            if (trunk != null)
                            {
                                requestProxy.Forward(
                                    ProxieFactory.Create(GetTransactionId(method), trunk));
                            }
                        }
                        else
                        {
                            var trunk = trunkManager.GetTrunkByDomain(request.Reader.From.AddrSpec.Hostport.Host);

                            if (trunk != null)
                            {
                                if (request.Reader.To.Tag.IsValid)
                                {
                                    requestProxy.Forward(
                                        ProxieFactory.Create(GetTransactionId(method), trunk, request.Reader.To.Tag));
                                }
                                else
                                {
                                    foreach (var binding in locationService.GetEnumerableBindings(trunk.ForwardCallToUri))
                                    {
                                        requestProxy.Forward(
                                            ProxieFactory.Create(GetTransactionId(method), trunk, binding));
                                    }
                                }
                            }
                        }
                    }
                }
                finally
                {
                    requestProxy.SetAllRequestsSent();
                    SendBestIfAllFinalRecived(requestProxy.Value);

                    requestProxy.Release();

                    RemoveIfFinished(requestProxy.Value);

                    if (requestProxy.HasValue == false)
                    {
                        statusCode = StatusCodes.TemporarilyUnavailable;
                    }
                }
            }

            if (statusCode != StatusCodes.OK)
            {
                SendResponse(request, statusCode);
            }
        }