Developers.Hash.Guid.UseCamper: Difference between revisions

From Shareaza Wiki
Jump to navigation Jump to search
(Importing page from Tikiwiki)
 
m (1 revision)
 

Latest revision as of 20:09, 20 June 2009

GUID hunt in camper's code

Here's how those two methods changed with camper's code.

CGProfile::Create

Shareaza saves its Gnutella client ID GUID in an XML file. It has a path like:


C:\Documents and Settings\User Name\Application Data\Shareaza\Data\Profile.xml

The Create method runs when it needs to make the GUID for this XML file. If Shareaza can't find the XML file, Create runs each time Shareaza starts.

<source lang="c"> void CGProfile::Create() { Clear();

Hashes::Guid tmp; srand( GetTickCount() );

for ( int nByte = 0 ; nByte < 16 ; nByte++ ) tmp[ nByte ] = uchar( tmp[ nByte ] + rand() ); tmp.validate();

oGUID = tmp;

m_pXML = new CXMLElement( NULL, _T("gProfile") ); m_pXML->AddAttribute( _T("xmlns"), xmlns );

CXMLElement* pGnutella = m_pXML->AddElement( _T("gnutella") ); pGnutella->AddAttribute( _T("guid"), tmp.toString() ); } </source>

Create no longer calls CoCreateGUID. It's not really necessary, since the GUID was spun with calls to rand anyway. The GUID type in camper's code is Hashes::Guid. Making a new GUID of this type, like tmp above, doesn't fill it with an actual GUID. It just makes 16 bytes of space filled with zeroes.

The method seeds the C random number generator with the current tick count. It loops through each byte in the GUID, filling it with a random byte from rand. That's how it makes the GUID. It then sets the guid in the global variable MyProfile.oGUID, and writes it into the XML document.

CNetwork::CreateID

CreateID makes a new unique GUID for each Gnutella packet that Shareaza sends.

<source lang="c"> void CNetwork::CreateID(Hashes::Guid& oID) { oID = MyProfile.oGUID; Hashes::Guid::iterator i = oID.begin(); *i++ += GetTickCount(); *i++ += m_nSequence++; *i++ += rand() * ( RAND_MAX + 1 ) * ( RAND_MAX + 1 ) + rand() * ( RAND_MAX + 1 ) + rand(); *i += rand() * ( RAND_MAX + 1 ) * ( RAND_MAX + 1 ) + rand() * ( RAND_MAX + 1 ) + rand(); } </source>

It starts by copying MyProfile.oGUID, the GUID we made in Create above. It sets up an iterator i that starts out pointing at the first DWORD in the GUID, and can move forward 4 bytes with the ++ operator.

To the first DWORD in the GUID, CreateID adds the current tick count. The second DWORD gets the next sequence number added to it. If CreateID gets called twice in the same millisecond, the first DWORD will be the same, but the second one won't. The last two DWORDs get filled with new random data.