Click here to Skip to main content
15,884,099 members
Articles / Programming Languages / C#

Peer Graph - Peers and Connections

Rate me:
Please Sign up or sign in to vote.
3.19/5 (7 votes)
17 Nov 20055 min read 51.1K   1K   20  
Peer Graph - Peers and Connections using Microsoft's Peer-to-Peer technology.
using System;
using System.Runtime.InteropServices;

namespace Peer.NameResolution
{
	internal enum WSAESETSERVICEOP
	{
		Register  = 0,
		Deregister = 1,
		Delete = 2
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct WSAData 
	{
		public short wVersion;
		public short wHighVersion;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x101)] public string szDescription;
		[MarshalAs(UnmanagedType.ByValTStr, SizeConst=0x81)] public string szSystemStatus;
		public short iMaxSockets;
		public short iMaxUdpDg;
		public IntPtr lpVendorInfo;
	};

	internal enum WSAECOMPARATOR
	{
		Equal = 0,
		NotLess = 1
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct WSAVERSION
	{
		public int dwVersion;
		public WSAECOMPARATOR ecHow;
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct SOCKADDR_IN6
	{
		public short sin6_family;
		public ushort sin6_port;
		public uint sin6_flowinfo;
		[MarshalAs(UnmanagedType.ByValArray, SizeConst=0x10)]
		public byte[] sin6_addr;
		public uint sin6_scope_id;
	}
 
	[StructLayout(LayoutKind.Sequential)]
	internal struct SOCKET_ADDRESS 
	{
		public IntPtr lpSockaddr;		// LPSOCKADDR
		public int iSockaddrLength;
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct CSADDR_INFO 
	{
		public SOCKET_ADDRESS LocalAddr;
		public SOCKET_ADDRESS RemoteAddr;
		public int iSocketType;
		public int iProtocol;
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct BLOB 
	{
		public int cbSize;
		public IntPtr pBlobData;
	}
 
	[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
	internal struct WSAQUERYSET
	{
		public int dwSize;
		public string lpszServiceInstanceName;
		public IntPtr lpServiceClassId;			// LPGUID
		public IntPtr lpVersion;				// LPWSAVERSION
		public IntPtr lpszComment;
		public int dwNameSpace;
		public IntPtr lpNSProviderId;			// LPGUID
		public IntPtr lpszContext;				// string
		public int dwNumberOfProtocols;
		public IntPtr lpafpProtocols;			// LPAFPROTOCOLS
		public IntPtr lpszQueryString;			// can be NULL
		public int dwNumberOfCsAddrs;
		public IntPtr lpcsaBuffer;				// LPCSADDR_INFO
		public int dwOutputFlags;
		public IntPtr lpBlob;					// LPBLOB
	};

	public enum PNRP_SCOPE 
	{
		Any = 0,
		Global = 1,
		SiteLocal = 2,
		LinkLocal = 3
	};

	internal enum PNRP_CLOUD_STATE 
	{
		Virtual = 0,			//  Not initialized
		Synchronizing = 1,		//  The cache is initializing
		Active = 2,				//  Cloud is active
		Dead = 3,				//  Initialized but has had a problem
		Disabled = 4,			//  Cloud is not disbaled in registery
		NoNet = 5,				//  Was active, but now has lost access to net
		Alone = 6				//  Cloud is in standalone mode
	};

	internal enum PNRP_CLOUD_FLAGS
	{
		NoFlags = 0,
		NameLocal = 1,			//  Name not valid on other computers
		ResolveOnly = 2,
		FullParticipant = 4
	};

	internal enum PNRP_REGISTERED_ID_STATE
	{
		OK = 1,					//  Id is active in cloud
		Problem = 2				//  Id is no longer registered in cloud
	};

	public enum PNRP_RESOLVE_CRITERIA
	{
		Default = 0,
		RemotePeerName = 1,
		NearestRemotePeerName = 2,
		NonCurrentProcessPeerName = 3,
		NearestNonCurrentProcessPeerName = 4,
		AnyPeerName = 5,
		NearestPeerName = 6
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct PNRP_CLOUD_ID 
	{
		public short AddressFamily;		// should be AF_INET6
		public PNRP_SCOPE Scope;		// Global, site, or link
		public uint ScopeId;			// specifies interface
	};

	[StructLayout(LayoutKind.Sequential)]
	internal struct PNRPCLOUDINFO
	{
		public int dwSize;						// size of this struct
		public PNRP_CLOUD_ID Cloud;				// network cloud information
		public PNRP_CLOUD_STATE enCloudState;	// state of cloud
		public PNRP_CLOUD_FLAGS enCloudFlags;	// flags for cloud
	};

	internal enum PNRP_EXTENDED_PAYLOAD_TYPE
	{
		None = 0,
		Binary = 1,
		String = 2
	};

	[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Unicode)]
	internal struct PNRPINFO
	{
		public int dwSize;
		public IntPtr lpwszIdentity;
		public int nMaxResolve;
		public int dwTimeout;
		public int dwLifetime;
		public PNRP_RESOLVE_CRITERIA enResolveCriteria;
		public int dwFlags;
		public SOCKET_ADDRESS saHint;
		public PNRP_REGISTERED_ID_STATE enNameState;
	}
 
	internal enum WSALookup
	{
		LUP_DEEP                = 0x0001,
		LUP_CONTAINERS          = 0x0002,
		LUP_NOCONTAINERS        = 0x0004,
		LUP_NEAREST             = 0x0008,
		LUP_RETURN_NAME         = 0x0010,
		LUP_RETURN_TYPE         = 0x0020,
		LUP_RETURN_VERSION      = 0x0040,
		LUP_RETURN_COMMENT      = 0x0080,
		LUP_RETURN_ADDR         = 0x0100,
		LUP_RETURN_BLOB         = 0x0200,
		LUP_RETURN_ALIASES      = 0x0400,
		LUP_RETURN_QUERY_STRING = 0x0800,
		LUP_RETURN_ALL          = 0x0FF0,
		LUP_RES_SERVICE         = 0x8000,
		LUP_FLUSHCACHE			= 0x1000,
		LUP_FLUSHPREVIOUS		= 0x2000
	};

	internal class PnrpNative
	{
		[DllImport("p2p.dll", CharSet=CharSet.Unicode)]
		internal static extern int PeerNameToPeerHostName(string pwszPnrpNameStr, System.Text.StringBuilder pwszDnsNameStr, ref int pcchDnsName);

		[DllImport("p2p.dll", CharSet=CharSet.Unicode)]
		internal static extern int PeerHostNameToPeerName(string pwszDnsNameStr, System.Text.StringBuilder pwszPnrpNameStr, ref int pcchPnrpName);

		[DllImport("pnrpnsp.dll", CharSet=CharSet.Unicode)]
		internal static extern int PeerPnrpToDnsName(string pwszPnrpNameStr, System.Text.StringBuilder pwszDnsNameStr, ref int pcchDnsName);

		[DllImport("pnrpnsp.dll", CharSet=CharSet.Unicode)]
		internal static extern int PeerDnsToPnrpName(string pwszDnsNameStr, System.Text.StringBuilder pwszPnrpNameStr, ref int pcchPnrpName);

		[DllImport("ws2_32.dll")]
		internal static extern int WSAStartup(short wVersionRequested, ref WSAData lpWSAData);

		[DllImport("ws2_32.dll")]
		internal static extern int WSACleanup();
 
		[DllImport("ws2_32.dll")]
		internal static extern int WSAGetLastError();

		[DllImport("ws2_32.dll", CharSet=CharSet.Unicode)]
		internal static extern int WSALookupServiceBegin(IntPtr lpqsRestrictions, WSALookup dwControlFlags, out IntPtr lphLookup);

		[DllImport("ws2_32.dll", CharSet=CharSet.Unicode)]
		internal static extern int WSALookupServiceNext(IntPtr hLookup, int dwControlFlags, ref int lpdwBufferLength, IntPtr Results);
		//[MarshalAs(UnmanagedType.CustomMarshaler, MarshalType="", MarshalTypeRef=typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetMarshaler), MarshalCookie="Managed")] PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet Query, PnrpPeerResolver.UnsafePnrpNativeMethods.WsaNspControlFlags dwControlFlags, out PnrpPeerResolver.UnsafePnrpNativeMethods.CriticalLookupHandle hLookup);

		[DllImport("ws2_32.dll")]
		internal static extern int WSALookupServiceEnd(IntPtr hLookup);

		[DllImport("ws2_32.dll", CharSet=CharSet.Unicode)]
		internal static extern int WSASetService(IntPtr lpqsRegInfo, WSAESETSERVICEOP essoperation, int dwControlFlags);
		//[MarshalAs(UnmanagedType.CustomMarshaler, MarshalType="", MarshalTypeRef=typeof(PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySetMarshaler), MarshalCookie="Managed")] PnrpPeerResolver.UnsafePnrpNativeMethods.WsaQuerySet Query, PnrpPeerResolver.UnsafePnrpNativeMethods.WsaSetServiceOp essOperation, int dwControlFlags);

		//
		// Scope
		//
		public const string WSZ_SCOPE_GLOBAL = @"GLOBAL";
		public const string WSZ_SCOPE_SITELOCAL = @"SITELOCAL";
		public const string WSZ_SCOPE_LINKLOCAL = @"LINKLOCAL";

		//
		// WSA Error codes
		//
		public const int WSAEFAULT = 10014; 	// The system detected an invalid pointer address in attempting to use a pointer argument in a call.
		public const int WSA_E_NO_MORE = 10110;	// No more results can be returned by WSALookupServiceNext.
	}
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada
Adrian Moore is the Development Manager for the SCADA Vision system developed by ABB Inc in Calgary, Alberta.

He has been interested in compilers, parsers, real-time database systems and peer-to-peer solutions since the early 90's. In his spare time, he is currently working on a SQL parser for querying .NET DataSets (http://www.queryadataset.com).

Adrian is a Microsoft MVP for Windows Networking.

Comments and Discussions