private static string GetRemoteHostname(AssociationParameters association)
        {
            string remoteHostname = null;
            try
            {
                 if (association.RemoteEndPoint != null)
                 {
                     try
                     {
                         IPHostEntry entry = Dns.GetHostEntry(association.RemoteEndPoint.Address);
                         remoteHostname = entry.HostName;
                     }
                     catch 
                     {
                         remoteHostname = association.RemoteEndPoint.Address.ToString();
                     }
                 }
            }
            catch (Exception e)
            {
                remoteHostname = null;
                Platform.Log(LogLevel.Warn, e, "Unable to resolve remote host name for auditing.");
            }

            return remoteHostname;
        }
Example #2
0
        protected override DicomPresContextResult OnVerifyAssociation(AssociationParameters association, byte pcid)
        {
            if (!Device.AllowMPPS)
            {
                return DicomPresContextResult.RejectUser;
            }

            return DicomPresContextResult.Accept;
        }
Example #3
0
 private DicomClient(AssociationParameters assoc, IDicomClientHandler handler)
 {
     _remoteEndPoint = assoc.RemoteEndPoint;
     _socket = null;
     _network = null;
     _closedEvent = null;
     _timeout = 10;
     _handler = handler;
     _assoc = assoc;
 }
        public static bool VerifyAssociation(IDicomServerContext context, AssociationParameters assoParams, 
            out DicomRejectResult result, out DicomRejectReason reason)
        {
            string calledAET = (assoParams.CalledAE ?? "").Trim();
            string callingAET = (assoParams.CallingAE ?? "").Trim();

            result = DicomRejectResult.Permanent;
            reason = DicomRejectReason.NoReasonGiven;

            return true;
        }
Example #5
0
        protected override DicomPresContextResult OnVerifyAssociation(AssociationParameters association, byte pcid)
        {
//             if (!Device.AllowWorkList)
//             {
//                 return DicomPresContextResult.RejectUser;
//             }

            return DicomPresContextResult.Accept; 
        }
Example #6
0
 protected override void OnReceiveAssociateAccept(AssociationParameters association)
 {
     try
     {
         _handler.OnReceiveAssociateAccept(this, association as ClientAssociationParameters);
     }
     catch (Exception e)
     {
         OnUserException(e, "Unexpected exception on OnReceiveAssociateAccept");
     }
 }
Example #7
0
        /// <summary>
        /// Connection to a remote DICOM application via TLS.
        /// </summary>
        /// <param name="assoc"></param>
        /// <param name="handler"></param>
        /// <returns></returns>
        public static DicomClient ConnectTLS(AssociationParameters assoc, IDicomClientHandler handler)
        {
            DicomClient client = new DicomClient(assoc, handler);
            client.ConnectTLS();
            return client;
		}
        /// <summary>
        /// Event handler called when an association has been aborted.
        /// </summary>
        /// <param name="assoc">The aborted association</param>
        /// <param name="reason">The abort reason</param>
        protected void OnAssociationAborted(AssociationParameters assoc, DicomAbortReason reason)
        {
            if (_assocStats == null)
                return;

            // update the association statistics
            _assocStats.IncomingBytes = assoc.TotalBytesRead;
            _assocStats.OutgoingBytes = assoc.TotalBytesSent;

            // signal stop recording.. the statistic object will fill out whatever 
            // it needs at this point based on what we have set
            _assocStats.End();
        }
Example #9
0
        /// <summary>
        /// Method used to send an association request.
        /// </summary>
        /// <param name="associate">The parameters used in the association request.</param>
        public void SendAssociateRequest(AssociationParameters associate)
        {
            _assoc = associate;
            var pdu = new AAssociateRQ(_assoc);

        	State = DicomAssociationState.Sta5_AwaitingAAssociationACOrReject;

            EnqueuePdu(pdu.Write());

        }
Example #10
0
        private static bool NegotiateAssociation(AssociationParameters cp, ServerAssociationParameters sp)
        {
            foreach (DicomPresContext clientContext in cp.GetPresentationContexts())
            {
                TransferSyntax selectedSyntax = null;
				foreach (DicomPresContext serverContext in sp.GetPresentationContexts())
				{
					if (clientContext.AbstractSyntax.Uid.Equals(serverContext.AbstractSyntax.Uid))
					{
						foreach (TransferSyntax ts in serverContext.GetTransfers())
						{
							if (clientContext.HasTransfer(ts))
							{
								selectedSyntax = ts;
								break;
							}
						}						
					}

					if (selectedSyntax != null)
						break;
				}

                if (selectedSyntax != null)
                {
                    clientContext.ClearTransfers();
                    clientContext.AddTransfer(selectedSyntax);
                    clientContext.SetResult(DicomPresContextResult.Accept);
                }
                else
                {
                    // No contexts accepted, set if abstract or transfer syntax reject
                    clientContext.SetResult(0 == sp.FindAbstractSyntax(clientContext.AbstractSyntax)
                                                ? DicomPresContextResult.RejectAbstractSyntaxNotSupported
                                                : DicomPresContextResult.RejectTransferSyntaxesNotSupported);
                }
            }
            bool anyValidContexts = false;

            foreach (DicomPresContext clientContext in cp.GetPresentationContexts())
            {
                if (clientContext.Result == DicomPresContextResult.Accept)
                {
                    anyValidContexts = true;
                    break;
                }
            }
            if (anyValidContexts == false)
            {
                return false;
            }      

            return true;
        }
Example #11
0
        public static Device LookupDevice(ServerPartition partition, AssociationParameters association,
                                          out bool isNew)
        {
            isNew = false;

            Device device = null;

            using (var ctx = new PacsContext())
            {
                var part = (from p in ctx.ServerPartitions select p).FirstOrDefault();
                if (part != null)
                {
                    device = part.Devices.FirstOrDefault(d => d.AeTitle.Equals(association.CallingAE));
                }

                if (device == null)
                {
                    if (!partition.AcceptAnyDevice)
                    {
                        return null;
                    }

                    if (partition.AutoInsertDevice)
                    {
                        device = new Device
                        {
                            AeTitle = association.CallingAE,
                            Enabled = true,
                            Description = string.Format("AE: {0}", association.CallingAE),
                            //TODO
                            Port = 104,
                            AllowQuery = true,
                            AllowRetrieve = true,
                            AllowStorage = true,
                            ServerPartitionPK = part.Id,
                            LastAccessTime = DateTime.Now
                        };

                        ctx.Devices.Add(device);
                        ctx.SaveChanges();

                        isNew = true;
                    }
                }

                if (device != null)
                {
                    if (device.Dhcp && !association.RemoteEndPoint.Address.ToString().Equals(device.Hostname))
                    {
                        device.Hostname = association.RemoteEndPoint.Address.ToString();

                        device.LastAccessTime = DateTime.Now;
                        ctx.SaveChanges();
                    }
                    else if (!isNew)
                    {
                        device.LastAccessTime = DateTime.Now;
                        ctx.SaveChanges();
                    }
                }
            }

            return device;
        }
        protected AssociationParameters(AssociationParameters parameters)
        {
            DisableNagle = NetworkSettings.Default.DisableNagle;
            RemoteMaxOperationsPerformed = 1;
            RemoteMaxOperationsInvoked = 1;
            TotalDimseReceived = 0;
            TotalBytesSent = 0;
            TotalBytesRead = 0;
            ApplicationContextName = parameters.ApplicationContextName;
            CalledAE = parameters.CalledAE;
            CallingAE = parameters.CallingAE;
            ImplementationClass = parameters.ImplementationClass;
            ImplementationVersion = parameters.ImplementationVersion;
            LocalEndPoint = parameters.LocalEndPoint;
            LocalMaximumPduLength = parameters.LocalMaximumPduLength;
            RemoteMaximumPduLength = parameters.RemoteMaximumPduLength;
            ReadTimeout = parameters.ReadTimeout;
            ReceiveBufferSize = parameters.ReceiveBufferSize;
            RemoteEndPoint = parameters.RemoteEndPoint;
            SendBufferSize = parameters.SendBufferSize;
            WriteTimeout = parameters.WriteTimeout;
            ConnectTimeout = parameters.ConnectTimeout;

            foreach (byte id in parameters._presContexts.Keys)
            {
                AddPresentationContext(id,parameters._presContexts[id].AbstractSyntax);

                foreach (TransferSyntax ts in parameters._presContexts[id].GetTransfers())
                {
                    AddTransferSyntax(id,ts);
                }

                SetRoleSelect(id, parameters._presContexts[id].GetRoleSelect());

            }
        }
        /// <summary>
        /// Event handlers called when association has been established.
        /// </summary>
        /// <param name="assoc">The association</param>
        protected void OnAssociationEstablished(AssociationParameters assoc)
        {
            if (_assocStats == null)
                _assocStats = new TransmissionStatistics(string.Format("DICOM association from {0} [{1}:{2}] to {3}",
                                    assoc.CallingAE,
                                    assoc.RemoteEndPoint.Address,
                                    assoc.RemoteEndPoint.Port,
                                    assoc.CalledAE));

            // start recording
            _assocStats.Begin();
        }
        /// <summary>
        /// Event handler called while a DICOM message has been sent.
        /// </summary>
        /// <param name="assoc">The association</param>
        /// <param name="dcmMsg">The request DICOM message sent</param>
        private void OnDicomMessageSent(
                                AssociationParameters assoc,
                                DicomMessage dcmMsg)
        {
            if (_assocStats == null)
                return;

            // update the association stats
            _assocStats.IncomingBytes = assoc.TotalBytesRead;
            _assocStats.OutgoingBytes = assoc.TotalBytesSent;

            _assocStats.OutgoingMessages++;
        }
Example #15
0
        private bool ProcessRawPDU(RawPDU raw)
        {
            try
            {
                switch (raw.Type)
                {
                    case 0x01:
                        {
                            _assoc = new ServerAssociationParameters();
                            var pdu = new AAssociateRQ(_assoc);
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta3_AwaitingLocalAAssociationResponsePrimative;
                            OnReceiveAssociateRequest(_assoc as ServerAssociationParameters);

                            if (State != DicomAssociationState.Sta13_AwaitingTransportConnectionClose &&
                                State != DicomAssociationState.Sta6_AssociationEstablished)
                            {
                                Platform.Log(LogLevel.Error, "Association incorrectly not accepted or rejected, aborting.");
                                return false;
                            }

                            //if derived class call SendAssociateAccept, it has fired this event
                            //if (AssociationEstablished != null)
                            //    AssociationEstablished(_assoc);

                            return true;
                        }
                    case 0x02:
                        {
                            var pdu = new AAssociateAC(_assoc);
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta6_AssociationEstablished;

                            OnReceiveAssociateAccept(_assoc);

                            if (AssociationEstablished != null)
                                AssociationEstablished(_assoc);

                            return true;
                        }
                    case 0x03:
                        {
                            var pdu = new AAssociateRJ();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose;

                            if (AssociationRejected != null)
                                AssociationRejected(pdu.Source, pdu.Reason);

                            OnReceiveAssociateReject(pdu.Result, pdu.Source, pdu.Reason);

                            return true;
                        }
                    case 0x04:
                        {
                            var pdu = new PDataTF();
                            pdu.Read(raw);
                            return ProcessPDataTF(pdu);
                        }
                    case 0x05:
                        {
                            var pdu = new AReleaseRQ();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta8_AwaitingAReleaseRPLocalUser;

                            OnReceiveReleaseRequest();

                            return true;
                        }
                    case 0x06:
                        {
                            var pdu = new AReleaseRP();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta13_AwaitingTransportConnectionClose;

                            if (AssociationReleased != null)
                                AssociationReleased(_assoc);

                            OnReceiveReleaseResponse();

                            return true;
                        }
                    case 0x07:
                        {
                            var pdu = new AAbort();
                            pdu.Read(raw);
                            State = DicomAssociationState.Sta1_Idle;

                            if (AssociationAborted != null)
                                AssociationAborted(_assoc, pdu.Reason);

                            OnReceiveAbort(pdu.Source, pdu.Reason);

                            return true;
                        }
                    case 0xFF:
                        {
                            Platform.Log(LogLevel.Error, "Unexpected PDU type: 0xFF.  Potential parsing error.");
                            return false;
                        }
                    default:
                        throw new DicomNetworkException("Unknown PDU type");
                }
            }
            catch (Exception e)
            {
                OnNetworkError(e, true);

                if (NetworkError != null)
                    NetworkError(e);

                Platform.Log(LogLevel.Error, e, "Unexpected exception when processing PDU.");
                return false;
            }
        }
Example #16
0
 public AAssociateAC(AssociationParameters assoc)
 {
     _assoc = assoc;
 }
Example #17
0
 protected virtual void OnReceiveAssociateAccept(AssociationParameters association)
 {
 }
Example #18
0
 protected override DicomPresContextResult OnVerifyAssociation(AssociationParameters association,
                                                               byte pcid)
 {
     return DicomPresContextResult.Accept;
 }
Example #19
0
        /// <summary>
        /// Method to send an association accept.
        /// </summary>
        /// <param name="associate">The parameters to use for the association accept.</param>
        public void SendAssociateAccept(AssociationParameters associate)
        {
            if (State != DicomAssociationState.Sta3_AwaitingLocalAAssociationResponsePrimative)
            {
                Platform.Log(LogLevel.Error, "Error attempting to send association accept at invalid time in association.");
                SendAssociateAbort(DicomAbortSource.ServiceProvider, DicomAbortReason.NotSpecified);
                throw new DicomNetworkException(
                    "Attempting to send association accept at invalid time in association, aborting");
            }
            var pdu = new AAssociateAC(_assoc);

            EnqueuePdu(pdu.Write());

            State = DicomAssociationState.Sta6_AssociationEstablished;

            if (AssociationEstablished != null)
                AssociationEstablished(_assoc);
        }
        /// <summary>
        /// Event handler called when an association has been released.
        /// </summary>
        /// <param name="assoc">The association</param>
        protected void OnAssociationReleased(AssociationParameters assoc)
        {
            if (_assocStats == null)
                return;

            // update the association statistics
            _assocStats.IncomingBytes = assoc.TotalBytesRead;
            _assocStats.OutgoingBytes = assoc.TotalBytesSent;

            // signal stop recording.. the statistic object will fill out whatever 
            // it needs at this point based on what we have set
            _assocStats.End();

            if (_logInformation)
                StatisticsLogger.Log(LogLevel.Info, _assocStats);
        }