//------------------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------------------ // <Snippet7> // If more than one endpoint for a contact exists, let the user choose which to use. //Parameters: // pContact - contact to pick an endpoint for // return value: pPeerEndpoint - the endpoint the user picked private static PeerEndPoint PickEndpointForContact(PeerContact pContact) { PeerEndPointCollection endPointCollection = pContact.PeerEndPoints; if (endPointCollection == null) { Console.WriteLine("Cannot return endpoint for contact {0} -- PeerEndPointCollection is null.", pContact); return(null); } if (endPointCollection.Count == 0) { Console.WriteLine("Cannot return endpoint for contact {0} -- PeerEndPointCollection is empty.", pContact); return(null); } foreach (PeerEndPoint pep in endPointCollection) { Console.WriteLine("PeerEndPoint is {0}:", pep); Console.WriteLine("PeerEndPoint information:\n Name: {0}\n IP Address: {1}\n Port: {2}\n", pep.Name, pep.EndPoint.Address, pep.EndPoint.Port); } return(endPointCollection[0]); }
internal NameChangedEventArgs(PeerEndPoint peerEndPoint, PeerContact peerContact, string name) { m_peerEndPoint = peerEndPoint; m_peerContact = peerContact; m_name = name; }
internal SubscriptionListChangedEventArgs(PeerEndPoint peerEndPoint, PeerContact peerContact, PeerChangeType peerChangeType) { m_peerEndPoint = peerEndPoint; m_peerContact = peerContact; m_peerChangeType = peerChangeType; }
// // Adds this peer to the contact manager // public PeerContact AddToContactManager(string displayName, string nickname, MailAddress emailAddress) { Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering AddToContactManager with Display name: {0}" + " Nickname: {1} and Email Address: {2}", displayName, nickname, emailAddress); PeerContact peerContact = null; try{ peerContact = PeerCollaboration.ContactManager.CreateContact(this); PeerCollaboration.ContactManager.AddContact(peerContact); } catch (Exception e) { throw new PeerToPeerException(SR.GetString(SR.Collab_AddToContactMgrFailed), (e.InnerException != null ? e.InnerException : e)); } peerContact.DisplayName = displayName; peerContact.Nickname = nickname; peerContact.EmailAddress = emailAddress; try{ PeerCollaboration.ContactManager.UpdateContact(peerContact); } catch (Exception e) { throw new PeerToPeerException(SR.GetString(SR.Collab_AddToContactMgrFailed) + " " + SR.GetString(SR.Collab_AddToContactMgrFailedUpdate), (e.InnerException != null ? e.InnerException : e)); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving AddToContactManager."); return(peerContact); }
internal CreateContactCompletedEventArgs( PeerContact peerContact, Exception error, bool cancelled, object userToken) : base(error, cancelled, userToken) { m_peerContact = peerContact; }
// // Adds this peer to the contact manager // public PeerContact AddToContactManager() { Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering AddToContactManager."); PeerContact peerContact = null; try{ peerContact = PeerCollaboration.ContactManager.CreateContact(this); PeerCollaboration.ContactManager.AddContact(peerContact); } catch (Exception e) { throw new PeerToPeerException(SR.GetString(SR.Collab_AddToContactMgrFailed), (e.InnerException != null ? e.InnerException : e)); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving AddToContactManager."); return(peerContact); }
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); }
void ContactManager_CreateContactCompleted(object sender, CreateContactCompletedEventArgs e) { switch (e.UserState.ToString()) { case "Setup": try { PeerContact existContact = PeerCollaboration.ContactManager.GetContact(e.PeerContact.PeerName); PeerCollaboration.ContactManager.UpdateContact(e.PeerContact); } catch (PeerToPeerException ex) { AddLog("ContactManager_CreateContactCompleted:" + ex.Message, LogType.System); PeerCollaboration.ContactManager.AddContact(e.PeerContact); } e.PeerContact.SubscribeCompleted += peerContact_SubscribeCompleted; e.PeerContact.SubscribeAsync(peerObject); AddLog("[Setup]ContactManager_CreateContactCompleted", LogType.System); break; case "Added": try { PeerContact existContact = PeerCollaboration.ContactManager.GetContact(e.PeerContact.PeerName); PeerCollaboration.ContactManager.UpdateContact(e.PeerContact); } catch (PeerToPeerException ex) { AddLog("ContactManager_CreateContactCompleted:" + ex.Message, LogType.System); PeerCollaboration.ContactManager.AddContact(e.PeerContact); } e.PeerContact.SubscribeCompleted += peerContact_SubscribeCompleted; e.PeerContact.SubscribeAsync(peerObject); AddLog("[Added]ContactManager_CreateContactCompleted", LogType.System); break; case "Deleted": e.PeerContact.SubscribeCompleted -= peerContact_SubscribeCompleted; PeerCollaboration.ContactManager.DeleteContact(e.PeerContact); AddLog("[Deleted]ContactManager_CreateContactCompleted", LogType.System); break; } }
void AddLog(PeerContact peerContact, PeerObject peerObject) { AddLog(peerContact.Nickname, peerObject); }
public void UpdateContact(PeerContact peerContact) { if (m_Disposed) throw new ObjectDisposedException(this.GetType().FullName); PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand(); if (peerContact == null) throw new ArgumentNullException("peerContact"); if (peerContact.PeerName == null) throw new ArgumentException(SR.GetString(SR.Collab_NoPeerNameInContact)); if (Logging.P2PTraceSource.Switch.ShouldTrace(TraceEventType.Information)){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering UpdateContact() with following peercontact"); peerContact.TracePeerContact(); } SafeCollabMemory safeCredentials = null; int errorCode = 0; try{ PEER_CONTACT pc = CollaborationHelperFunctions.ConvertPeerContactToPEER_CONTACT(peerContact, ref safeCredentials); errorCode = UnsafeCollabNativeMethods.PeerCollabUpdateContact(ref pc); } finally{ if (safeCredentials != null) safeCredentials.Dispose(); } if (errorCode == UnsafeCollabReturnCodes.PEER_E_CONTACT_NOT_FOUND){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "Contact not found in Contact Manager"); throw new ArgumentException(SR.GetString(SR.Collab_ContactNotFound), "peerContact"); } else if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabUpdateContact returned with errorcode {0}", errorCode); throw (PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_UpdateContactFailed), errorCode)); } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving UpdateContact() successfully."); }
public void AddContact(PeerContact peerContact) { if (m_Disposed) throw new ObjectDisposedException(this.GetType().FullName); PeerCollaborationPermission.UnrestrictedPeerCollaborationPermission.Demand(); if (peerContact == null) throw new ArgumentNullException("peerContact"); string contactXml = null; if (peerContact.ContactXml == null){ try{ contactXml = peerContact.ToXml(); } catch (PeerToPeerException e){ throw new PeerToPeerException(SR.GetString(SR.Collab_AddContactFailedNoXml), e.InnerException); } } else{ contactXml = peerContact.ContactXml; } if (Logging.P2PTraceSource.Switch.ShouldTrace(TraceEventType.Information)){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering AddContact() with following peercontact"); peerContact.TracePeerContact(); } SafeCollabData contact = null; int errorCode = 0; try{ errorCode = UnsafeCollabNativeMethods.PeerCollabAddContact(contactXml, out contact); } finally{ if(contact != null) contact.Dispose(); } if (errorCode == UnsafeCollabReturnCodes.PEER_E_ALREADY_EXISTS){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabAddContact returned with errorcode {0}. Contact already exists.", errorCode); throw new ArgumentException(SR.GetString(SR.Collab_AddContactFailed) + " " + SR.GetString(SR.Collab_ContactExists)); } else if (errorCode != 0){ Logging.P2PTraceSource.TraceEvent(TraceEventType.Error, 0, "PeerCollabAddContact returned with errorcode {0}", errorCode); throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Collab_AddContactFailed), errorCode); } peerContact.JustCreated = false; Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving AddContact() successfully."); }
public static bool TryParse(BDictionary message, out GetPeersResponse response) { response = null; if (message.TryGetValue("t", out BString transactionId) && transactionId.Length > 0 && message.TryGetValue("a", out BDictionary arguments) && arguments.TryGetValue("id", out BString nodeId) && nodeId.Length == NodeID.Size && arguments.TryGetValue("token", out BString token) && token.Length > 0) { if (arguments.TryGetValue("values", out BList values)) { PeerContact[] peers = new PeerContact[values.Count]; for (int i = 0; i < peers.Length; i++) { BObject value = values[i]; if (value is BString peer) { if (peer.Length == IPv4PeerContact.CompactInfoSize) { peers[i] = new IPv4PeerContact(peer); } else if (peer.Length == IPv6PeerContact.CompactInfoSize) { peers[i] = new IPv6PeerContact(peer); } else { return(false); } } else { return(false); } } response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), peers); } else { IPv4Node[] nodes = null; IPv6Node[] nodes6 = null; if (arguments.TryGetValue("nodes", out BString nodeBytes)) { if (!IPv4Node.TryParseCompactNodeInfos(nodeBytes.Bytes, out nodes)) { return(false); } } if (arguments.TryGetValue("nodes6", out BString node6Bytes)) { if (!IPv6Node.TryParseCompactNodeInfos(nodeBytes.Bytes, out nodes6)) { return(false); } } if (nodes != null) { if (nodes6 != null) { response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), nodes, nodes6); } else { response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), nodes); } } else if (nodes6 != null) { response = new GetPeersResponse(new TransactionID(transactionId), new NodeID(nodeId), new PeerToken(token), nodes6); } } } return(response != null); }
void AddLog(PeerContact peerContact, PeerObject peerObject) { AddLog(peerContact.Nickname, peerObject); }
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); }
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()."); }
// // Deletes a contact from the users windows address book // public void DeleteContact(PeerContact peerContact) { Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Entering DeleteContact()."); if (peerContact == null) throw new ArgumentNullException("peerContact"); DeleteContact(peerContact.PeerName); Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Leaving DeleteContact() successfully."); }
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; }
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()."); }