示例#1
0
        internal static PeerContact ConvertPEER_CONTACTToPeerContact(PEER_CONTACT pc, bool isMyContact)
        {
            PeerContact peerContact = (isMyContact ? new MyContact(): new PeerContact());

            peerContact.PeerName     = new PeerName(pc.pwzPeerName);
            peerContact.DisplayName  = pc.pwzDisplayName;
            peerContact.Nickname     = pc.pwzNickname;
            peerContact.EmailAddress = (pc.pwzEmailAddress != null) ? new MailAddress(pc.pwzEmailAddress) : null;
            if (!isMyContact)
            {
                peerContact.SubscribeAllowed = pc.WatcherPermissions;
            }
            peerContact.IsSubscribed = (isMyContact ? true : pc.fWatch);
            byte[] data = null;

            if (pc.credentials.cbData != 0)
            {
                data = new byte[pc.credentials.cbData];
                Marshal.Copy(pc.credentials.pbData, data, 0, (int)pc.credentials.cbData);
            }

            if (data != null)
            {
                SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_PKCS7*/ 5),
                                                                                   0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/,
                                                                                   IntPtr.Zero,
                                                                                   0x00000001 /*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/,
                                                                                   ref pc.credentials);

                if (certHandle == null || certHandle.IsInvalid)
                {
                    int win32ErrorCode = Marshal.GetLastWin32Error();
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode);
                }
                try{
                    X509Store certStore = new X509Store(certHandle.DangerousGetHandle());
                    peerContact.Credentials = new X509Certificate2(certStore.Certificates[0]);
                }
                finally{
                    if (certHandle != null)
                    {
                        certHandle.Dispose();
                    }
                }
            }

            return(peerContact);
        }
        internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials)
        {
            PEER_CONTACT pc = new PEER_CONTACT();

            pc.pwzDisplayName = peerContact.DisplayName;
            pc.pwzEmailAddress = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString();
            pc.pwzNickname = peerContact.Nickname;
            pc.pwzPeerName = peerContact.PeerName.ToString();
            pc.fWatch = peerContact.IsSubscribed;
            pc.WatcherPermissions = peerContact.SubscribeAllowed;
            PEER_DATA pd = new PEER_DATA();

            if (peerContact.Credentials != null){
                SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2),
                                    0,
                                    IntPtr.Zero,
                                    0x00002000/*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001/*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/,
                                    IntPtr.Zero);
                
                if (certHandle == null || certHandle.IsInvalid){
                    int win32ErrorCode = Marshal.GetLastWin32Error();
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode);
                }
                
                try{
                    X509Store certStore = new X509Store(certHandle.DangerousGetHandle());
                    certStore.Add(peerContact.Credentials as X509Certificate2);
                    bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle,
                                                     0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/,
                                                     2 /*CERT_STORE_SAVE_AS_STORE*/,
                                                     2, /*CERT_STORE_SAVE_TO_MEMORY*/
                                                     ref pd,
                                                     0);
                    
                    if ((pd.cbData != 0) && (returnCode)){
                        safeCredentials = new SafeCollabMemory((int)pd.cbData);
                        pd.pbData = safeCredentials.DangerousGetHandle();
                        returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle,
                                                     0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/,
                                                     2 /*CERT_STORE_SAVE_AS_STORE*/,
                                                     2, /*CERT_STORE_SAVE_TO_MEMORY*/
                                                     ref pd,// Clean up memory from here;
                                                     0);

                    }
                    else{
                        pd.cbData = 0;
                        pd.pbData = IntPtr.Zero;
                    }
                }
                finally{
                    if (certHandle != null) certHandle.Dispose();
                }
            }
            else{
                pd.cbData = 0;
                pd.pbData = IntPtr.Zero;
            }
            pc.credentials = pd;

            return pc;

        }
        internal static PeerContact ConvertPEER_CONTACTToPeerContact(PEER_CONTACT pc, bool isMyContact)
        {
            PeerContact peerContact = (isMyContact ? new MyContact(): new PeerContact());
            peerContact.PeerName = new PeerName(pc.pwzPeerName);
            peerContact.DisplayName = pc.pwzDisplayName;
            peerContact.Nickname = pc.pwzNickname;
            peerContact.EmailAddress = (pc.pwzEmailAddress != null) ? new MailAddress(pc.pwzEmailAddress) : null;
            if(!isMyContact) 
                peerContact.SubscribeAllowed = pc.WatcherPermissions;
            peerContact.IsSubscribed = (isMyContact ? true : pc.fWatch);
            byte[] data = null;

            if (pc.credentials.cbData != 0){
                data = new byte[pc.credentials.cbData];
                Marshal.Copy(pc.credentials.pbData, data, 0, (int)pc.credentials.cbData);
            }

            if (data != null){

                SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_PKCS7*/ 5),
                                                    0x00000001/*X509_ASN_ENCODING*/| 0x00010000/*PKCS_7_ASN_ENCODING*/,
                                                    IntPtr.Zero,
                                                    0x00000001/*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/,
                                                    ref pc.credentials);

                if (certHandle == null || certHandle.IsInvalid){
                    int win32ErrorCode = Marshal.GetLastWin32Error();
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode);
                }
                try{
                    X509Store certStore = new X509Store(certHandle.DangerousGetHandle());
                    peerContact.Credentials = new X509Certificate2(certStore.Certificates[0]);
                }
                finally{
                    if(certHandle != null) certHandle.Dispose();
                }
            }

            return peerContact;
        }
 internal static PeerContact ConvertPEER_CONTACTToPeerContact(PEER_CONTACT pc)
 {
     return ConvertPEER_CONTACTToPeerContact(pc, false);
 }
 internal extern static int PeerCollabAsyncInviteContact(ref PEER_CONTACT pcContact,
                                                         IntPtr pcEndpoint,
                                                         ref PEER_INVITATION pcInvitation,
                                                         SafeWaitHandle hEvent,
                                                         out SafeCollabInvite phInvitation);
 internal extern static int PeerCollabInviteContact(ref PEER_CONTACT pcContact,
                                                    IntPtr pcEndpoint,
                                                    ref PEER_INVITATION pcInvitation,
                                                    out SafeCollabData ppResponse);
 internal extern static int PeerCollabUpdateContact(ref PEER_CONTACT pc);
 internal extern static int PeerCollabEnumEndpoints(ref PEER_CONTACT pcContact,
                                                    out SafeCollabEnum phPeerEnum);
示例#9
0
        private void ApplicationChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "ApplicationChangedCallback() called.");

            if (m_Disposed)
            {
                return;
            }

            while (true)
            {
                ApplicationChangedEventArgs appChangedArgs = null;

                //
                // Get the event data for the fired event
                //
                try{
                    lock (LockAppChangedEvent)
                    {
                        if (m_safeAppChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(m_safeAppChangedEvent,
                                                                                     out eventData);
                    }

                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetApplicationChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.EndPointApplicationChanged)
                    {
                        PEER_EVENT_APPLICATION_CHANGED_DATA appData = ped.applicationChangedData;
                        PEER_APPLICATION pa = (PEER_APPLICATION)Marshal.PtrToStructure(appData.pApplication, typeof(PEER_APPLICATION));

                        PeerApplication peerApplication = CollaborationHelperFunctions.ConvertPEER_APPLICATIONToPeerApplication(pa);;

                        //
                        // Check if the Guid of the fired app is indeed our guid
                        //

                        if (Guid.Equals(m_id, peerApplication.Id))
                        {
                            PeerContact  peerContact  = null;
                            PeerEndPoint peerEndPoint = null;

                            if (appData.pContact != IntPtr.Zero)
                            {
                                PEER_CONTACT pc = (PEER_CONTACT)Marshal.PtrToStructure(appData.pContact, typeof(PEER_CONTACT));
                                peerContact = CollaborationHelperFunctions.ConvertPEER_CONTACTToPeerContact(pc);
                            }

                            if (appData.pEndPoint != IntPtr.Zero)
                            {
                                PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(appData.pEndPoint, typeof(PEER_ENDPOINT));
                                peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                            }

                            appChangedArgs = new ApplicationChangedEventArgs(peerEndPoint,
                                                                             peerContact,
                                                                             appData.changeType,
                                                                             peerApplication);
                        }
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //

                if (appChangedArgs != null)
                {
                    OnApplicationChanged(appChangedArgs);
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving ApplicationChangedCallback().");
        }
 internal extern static int PeerCollabAsyncInviteContact(ref PEER_CONTACT pcContact,
                                                         IntPtr pcEndpoint,
                                                         ref PEER_INVITATION pcInvitation,
                                                         SafeWaitHandle hEvent,
                                                         out SafeCollabInvite phInvitation);
 internal extern static int PeerCollabInviteContact( ref PEER_CONTACT pcContact,
                                                     IntPtr pcEndpoint,
                                                     ref PEER_INVITATION pcInvitation,
                                                     out SafeCollabData ppResponse);
 internal extern static int PeerCollabUpdateContact(ref PEER_CONTACT pc);
 internal extern static int PeerCollabEnumEndpoints(ref PEER_CONTACT pcContact,
                                         out SafeCollabEnum phPeerEnum);
示例#14
0
        internal static PEER_CONTACT ConvertPeerContactToPEER_CONTACT(PeerContact peerContact, ref SafeCollabMemory safeCredentials)
        {
            PEER_CONTACT pc = new PEER_CONTACT();

            pc.pwzDisplayName     = peerContact.DisplayName;
            pc.pwzEmailAddress    = (peerContact.EmailAddress == null) ? null : peerContact.EmailAddress.ToString();
            pc.pwzNickname        = peerContact.Nickname;
            pc.pwzPeerName        = peerContact.PeerName.ToString();
            pc.fWatch             = peerContact.IsSubscribed;
            pc.WatcherPermissions = peerContact.SubscribeAllowed;
            PEER_DATA pd = new PEER_DATA();

            if (peerContact.Credentials != null)
            {
                SafeCertStore certHandle = UnsafeCollabNativeMethods.CertOpenStore(new IntPtr(/*CERT_STORE_PROV_MEMORY*/ 2),
                                                                                   0,
                                                                                   IntPtr.Zero,
                                                                                   0x00002000 /*CERT_STORE_CREATE_NEW_FLAG*/ | 0x00000001 /*CERT_STORE_NO_CRYPT_RELEASE_FLAG*/,
                                                                                   IntPtr.Zero);

                if (certHandle == null || certHandle.IsInvalid)
                {
                    int win32ErrorCode = Marshal.GetLastWin32Error();
                    throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_CredentialsError), win32ErrorCode);
                }

                try{
                    X509Store certStore = new X509Store(certHandle.DangerousGetHandle());
                    certStore.Add(peerContact.Credentials as X509Certificate2);
                    bool returnCode = UnsafeCollabNativeMethods.CertSaveStore(certHandle,
                                                                              0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/,
                                                                              2 /*CERT_STORE_SAVE_AS_STORE*/,
                                                                              2, /*CERT_STORE_SAVE_TO_MEMORY*/
                                                                              ref pd,
                                                                              0);

                    if ((pd.cbData != 0) && (returnCode))
                    {
                        safeCredentials = new SafeCollabMemory((int)pd.cbData);
                        pd.pbData       = safeCredentials.DangerousGetHandle();
                        returnCode      = UnsafeCollabNativeMethods.CertSaveStore(certHandle,
                                                                                  0x00000001 /*X509_ASN_ENCODING*/ | 0x00010000 /*PKCS_7_ASN_ENCODING*/,
                                                                                  2 /*CERT_STORE_SAVE_AS_STORE*/,
                                                                                  2,      /*CERT_STORE_SAVE_TO_MEMORY*/
                                                                                  ref pd, // Clean up memory from here;
                                                                                  0);
                    }
                    else
                    {
                        pd.cbData = 0;
                        pd.pbData = IntPtr.Zero;
                    }
                }
                finally{
                    if (certHandle != null)
                    {
                        certHandle.Dispose();
                    }
                }
            }
            else
            {
                pd.cbData = 0;
                pd.pbData = IntPtr.Zero;
            }
            pc.credentials = pd;

            return(pc);
        }
示例#15
0
 internal static PeerContact ConvertPEER_CONTACTToPeerContact(PEER_CONTACT pc)
 {
     return(ConvertPEER_CONTACTToPeerContact(pc, false));
 }
示例#16
0
        private void NameChangedCallback(object state, bool timedOut)
        {
            SafeCollabData eventData = null;
            int            errorCode = 0;

            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "NameChangedCallback() called.");

            if (m_Disposed)
            {
                return;
            }

            while (true)
            {
                NameChangedEventArgs nameChangedArgs = null;

                //
                // Get the event data for the fired event
                //
                try{
                    lock (LockNameChangedEvent)
                    {
                        if (m_safeNameChangedEvent.IsInvalid)
                        {
                            return;
                        }
                        errorCode = UnsafeCollabNativeMethods.PeerCollabGetEventData(m_safeNameChangedEvent,
                                                                                     out eventData);
                    }

                    if (errorCode == UnsafeCollabReturnCodes.PEER_S_NO_EVENT_DATA)
                    {
                        break;
                    }
                    else if (errorCode != 0)
                    {
                        Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabGetEventData returned with errorcode {0}", errorCode);
                        throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_GetNameChangedDataFailed), errorCode);
                    }

                    PEER_COLLAB_EVENT_DATA ped = (PEER_COLLAB_EVENT_DATA)Marshal.PtrToStructure(eventData.DangerousGetHandle(),
                                                                                                typeof(PEER_COLLAB_EVENT_DATA));
                    if (ped.eventType == PeerCollabEventType.EndPointChanged)
                    {
                        PEER_EVENT_ENDPOINT_CHANGED_DATA epData = ped.endpointChangedData;
                        PeerEndPoint peerEndPoint = null;

                        if (epData.pEndPoint != IntPtr.Zero)
                        {
                            PEER_ENDPOINT pe = (PEER_ENDPOINT)Marshal.PtrToStructure(epData.pEndPoint, typeof(PEER_ENDPOINT));
                            peerEndPoint = CollaborationHelperFunctions.ConvertPEER_ENDPOINTToPeerEndPoint(pe);
                        }

                        if ((peerEndPoint != null) && Equals(peerEndPoint))
                        {
                            PeerContact peerContact = null;

                            if (epData.pContact != IntPtr.Zero)
                            {
                                PEER_CONTACT pc = (PEER_CONTACT)Marshal.PtrToStructure(epData.pContact, typeof(PEER_CONTACT));
                                peerContact = CollaborationHelperFunctions.ConvertPEER_CONTACTToPeerContact(pc);
                            }


                            nameChangedArgs = new NameChangedEventArgs(peerEndPoint,
                                                                       peerContact,
                                                                       peerEndPoint.Name);
                        }
                    }
                }
                finally{
                    if (eventData != null)
                    {
                        eventData.Dispose();
                    }
                }

                //
                // Fire the callback with the marshalled event args data
                //

                if (nameChangedArgs != null)
                {
                    OnNameChanged(nameChangedArgs);

                    //
                    // Change the name with the new name
                    //
                    Name = nameChangedArgs.PeerEndPoint.Name;
                }
            }
            Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving NameChangedCallback().");
        }