public static PeerName CreateFromPeerHostName(string peerHostName) { //------------------------------------------------- //Check arguments //------------------------------------------------- if (peerHostName == null) { throw new ArgumentNullException("peerHostName"); } if (peerHostName.Length == 0) { throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidPeerHostName), "peerHostName"); } Int32 result; SafePeerData shPeerName = null; string peerName = null; try { result = UnsafeP2PNativeMethods.PeerHostNameToPeerName(peerHostName, out shPeerName); if (result != 0) { throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotGetPeerNameFromPeerHostName), result); } peerName = shPeerName.UnicodeString; } finally { if (shPeerName != null) { shPeerName.Dispose(); } } string authority; string classifier; WeakParsePeerName(peerName, out authority, out classifier); PeerName p = new PeerName(peerName, authority, classifier); Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "PeerName created from PeerHostName - PeerHostName {0} to PeerName PeerName {1} Authority {2} Classfier {3}", peerHostName, peerName, authority, classifier); return(p); }
public static PeerName CreateRelativePeerName( PeerName peerName, string classifier) { //------------------------------------------------- //Check arguments //------------------------------------------------- if (peerName == null) { throw new ArgumentNullException("peerName", SR.GetString(SR.Pnrp_PeerNameCantBeNull)); } if (!peerName.IsSecured && (classifier == null || classifier.Length == 0)) { throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidClassifier), "classifier"); } if (classifier != null && classifier.Length > PEER_MAX_CLASSIFIER_LENGTH) { throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidClassifier), "classifier"); } //-------------------------------------------------- //Normalize using NFC //-------------------------------------------------- if (classifier != null && classifier.Length > 0) { classifier = classifier.Normalize(NormalizationForm.FormC); } Int32 result; SafePeerData shNewPeerName = null; string newPeerName = null; try { //Here there is change made on the native side //when passing secured peer names, it takes string of the form [40hexdigits].claasisifer and a newclassifier //returns [40hexdigits.newclassifier] //But for unsecured peer names it does not take 0.clasfier and newclassfier to return 0.newclassfier. //It expects NULL as the first param. To satisfy this broken finctionality, we are passing null if the //peer name is unsecured. result = UnsafeP2PNativeMethods.PeerCreatePeerName(peerName.IsSecured? peerName.m_PeerName : null, classifier, out shNewPeerName); if (result != 0) { throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotCreateRelativePeerName), result); } newPeerName = shNewPeerName.UnicodeString; } finally { if (shNewPeerName != null) { shNewPeerName.Dispose(); } } string authority; string newClassifier; WeakParsePeerName(newPeerName, out authority, out newClassifier); PeerName p = new PeerName(newPeerName, authority, newClassifier); Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "A new PeerName created from existing PeerName with a new classfier. Existing PeerName {0} Classifier {1} New PeerName {2}", peerName, classifier, p); return(p); }
public PeerName(string classifier, PeerNameType peerNameType) { //------------------------------------------------- //Check arguments //------------------------------------------------- if ((classifier == null || classifier.Length == 0) && peerNameType == PeerNameType.Unsecured) { throw new ArgumentNullException("classifier"); } if (classifier != null && classifier.Length > PEER_MAX_CLASSIFIER_LENGTH) { throw new ArgumentException(SR.GetString(SR.Pnrp_InvalidClassifier), "classifier"); } //-------------------------------------------------- //Normalize using NFC //-------------------------------------------------- if (classifier != null && classifier.Length > 0) { classifier = classifier.Normalize(NormalizationForm.FormC); } //------------------------------------------------- //call the helper to create the PeerName //------------------------------------------------- Int32 result; SafePeerData shNewPeerName = null; SafePeerData shDefaultIdentity = null; try { if (peerNameType == PeerNameType.Unsecured) { result = UnsafeP2PNativeMethods.PeerCreatePeerName((string)null, classifier, out shNewPeerName); if (result != 0) { throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotCreateUnsecuredPeerName), result); } m_Authority = PEERNAME_UNSECURED_AUTHORITY; } else { result = UnsafeP2PNativeMethods.PeerIdentityGetDefault(out shDefaultIdentity); if (result != 0) { throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotGetDefaultIdentity), result); } m_Authority = shDefaultIdentity.UnicodeString; //} result = UnsafeP2PNativeMethods.PeerCreatePeerName(m_Authority, classifier, out shNewPeerName); if (result != 0) { throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotCreateSecuredPeerName), result); } } m_PeerName = shNewPeerName.UnicodeString; m_Classifier = classifier; } finally { if (shNewPeerName != null) { shNewPeerName.Dispose(); } if (shDefaultIdentity != null) { shDefaultIdentity.Dispose(); } } Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "PeerName instance created - PeerName {0} Authority {1} Classfier {2}", m_PeerName, m_Authority, m_Classifier); }
private static void GetCloudOrClouds(string cloudName, bool bGlobalCloudOnly, out CloudCollection clouds, out Cloud cloud) { cloud = null; clouds = null; Logging.Enter(Logging.P2PTraceSource, "Cloud::GetCloudOrClouds()"); //------------------------------------------------- //Demand for the Unrestricted Pnrp Permission //------------------------------------------------- PnrpPermission.UnrestrictedPnrpPermission.Demand(); Int32 result = 0; UInt32 numClouds = 0; SafePeerData ArrayOfCloudInfoStructures = null; if (cloudName == null) { //----------------------------------------- //We need the collection only when we are not //getting a specific cloud //----------------------------------------- clouds = new CloudCollection(); } try { //--------------------------------------------------------------- //No perf hit here, real native call happens only one time if it //did not already happen //--------------------------------------------------------------- UnsafeP2PNativeMethods.PnrpStartup(); result = UnsafeP2PNativeMethods.PeerPnrpGetCloudInfo(out numClouds, out ArrayOfCloudInfoStructures); if (result != 0) { throw PeerToPeerException.CreateFromHr(SR.GetString(SR.Pnrp_CouldNotEnumerateClouds), result); } if (numClouds != 0) { Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "Number of clouds returned {0}", numClouds); IntPtr pPEER_PNRP_CLOUD_INFO = ArrayOfCloudInfoStructures.DangerousGetHandle(); for (ulong i = 0; i < numClouds; i++) { PEER_PNRP_CLOUD_INFO cloudinfo = (PEER_PNRP_CLOUD_INFO)Marshal.PtrToStructure(pPEER_PNRP_CLOUD_INFO, typeof(PEER_PNRP_CLOUD_INFO)); string nativeCloudName = Marshal.PtrToStringUni(cloudinfo.pwzCloudName); pPEER_PNRP_CLOUD_INFO = (IntPtr)((long)pPEER_PNRP_CLOUD_INFO + Marshal.SizeOf(typeof(PEER_PNRP_CLOUD_INFO))); Cloud c = new Cloud(nativeCloudName, (PnrpScope)((int)cloudinfo.dwScope), (int)cloudinfo.dwScopeId); if (cloudName == null && !bGlobalCloudOnly) { clouds.Add(c); continue; } //If a specific cloud by name is required, then test for name //note that scope is PnrpScope.All but we don't test that now if (cloudName != null && cloudName == nativeCloudName) { cloud = c; break; } if (bGlobalCloudOnly && c.Scope == PnrpScope.Global) { cloud = c; break; } } } else { Logging.P2PTraceSource.TraceEvent(TraceEventType.Warning, 0, "No Clouds returned from the native call"); } } finally { if (ArrayOfCloudInfoStructures != null) { ArrayOfCloudInfoStructures.Dispose(); } } if (cloudName != null && cloud == null) { Logging.P2PTraceSource.TraceEvent(TraceEventType.Information, 0, "The specific cloud name {0} asked for is not found", cloudName); } Logging.Leave(Logging.P2PTraceSource, "Cloud::GetCloudOrClouds()"); }