Developers.Hash.Guid: Difference between revisions
(Importing page from Tikiwiki) |
m (1 revision) |
(No difference)
|
Latest revision as of 20:09, 20 June 2009
GUIDs
how shareaza generates guids how peer to peer networks use them where the win32 api is used to make a guid, and where it is not used where do they get encoded and decoded setting parts inside a guid to special values for gnutella
What GUIDs are
globally unique identifiers no two the same, even in a network environment really useful to peer to peer, allow for resources that don't clash without a central server, peer to peer networks also don't want a central server
GUIDs in peer-to-peer networks
gnutella, edonkey2000, bittorrent [1]
GUID types
In Windows, the type that holds a GUID is called GUID. Shareaza defines its own type, named GGUID, in StdAfx.h:
<source lang="c"> typedef union { BYTE n[16]; BYTE b[16]; DWORD w[4]; } GGUID; </source>
The code uses union to look at the same memory different ways. You can access the 16 bytes of a GUID individually with n or b, or as an array of 4 DWORDs with w.
A GUID takes up 16 bytes, which is 128 bits, and can be expressed with 32 hexadecimal characters. Both GUID and GGUID are just 16 bytes of memory:
<source lang="c"> // Find the size of the Windows and Shareaza GUID types DWORD size; size = sizeof(GUID); // Windows GUID type, 16 bytes size = sizeof(GGUID); // Shareaza GGUID type, also 16 bytes </source>
- Does GGUID mean Gnutella GUID?
In GProfile.h, the class CGProfile defines a GGUID named GUID:
<source lang="c"> class CGProfile : public CComObject { // Construction public: CGProfile(); virtual ~CGProfile();
// Attributes public: GGUID GUID; </source>
To get a new, unique GUID from the system, call CoCreateGUID. In GProfile.cpp, the corresponding Create method uses this Windows function.
<source lang="c"> void CGProfile::Create() { Clear();
CoCreateGuid( (::GUID*)&GUID ); </source>
The Shareaza 2.0 code appears to be generating GGUID values without using CoCreateGUID. In Network.cpp, the CreateID method fills the value of a GGUID using the tick count, an incremented count, and the rand function.
<source lang="c"> void CNetwork::CreateID(GGUID* pID) { CSingleLock pLock( &m_pSection, TRUE );
*pID = MyProfile.GUID;
DWORD *pNum = (DWORD*)pID; pNum[0] += GetTickCount(); pNum[1] += ( m_nSequence++ ); pNum[2] += rand() * rand(); pNum[3] += rand() * rand(); } </source>