SecureID.cpp

Go to the documentation of this file.
00001 #include <client/SecureID.h>
00002 #include <net/NetInterface.h>
00003 #include <common/Defines.h>
00004 
00005 #ifdef _WIN32
00006 
00007 #define WIN32_LEAN_AND_MEAN             // Exclude rarely-used stuff from Windows headers
00008 #include <windows.h>
00009 #include <iphlpapi.h>
00010 
00011 #pragma comment(lib, "iphlpapi.lib")
00012 
00013 std::string SecureID::GetPrivateKey(void)
00014 {
00015     IP_ADAPTER_INFO AdapterInfo[16];       // Allocate information for 16 cards
00016     DWORD dwBufLen = sizeof(AdapterInfo);  // Save memory size of buffer
00017 
00018     DWORD dwStatus = GetAdaptersInfo ( AdapterInfo, &dwBufLen); // Get info
00019         if (dwStatus != NO_ERROR) return ""; // Check status
00020 
00021         std::string Key;
00022     for (unsigned int i=0; i<MIN(6, AdapterInfo[0].AddressLength); i++)
00023     {
00024         Key += (((unsigned int)AdapterInfo[0].Address[i])&255);
00025         if (i != 5) Key += ':';
00026     }
00027     return Key;
00028 }
00029 
00030 #elif defined(__DARWIN__)
00031 
00032 std::string SecureID::GetPrivateKey(void)
00033 {
00034         return "0:0:0:0:0:0";
00035 }
00036 
00037 #else
00038 
00039 #include <sys/ioctl.h>
00040 #include <net/if.h>
00041 
00042 std::string SecureID::GetPrivateKey(void)
00043 {
00044         std::string Key;
00045         int sock = socket (AF_INET, SOCK_DGRAM, 0);
00046         if (sock < 0)
00047         {
00048                 return ""; //not a valid socket
00049         }
00050 
00051         struct ifreq dev; //container for the hardware data
00052         struct if_nameindex *NameList = if_nameindex(); //container for the interfaces list
00053         if (NameList == NULL)
00054         {
00055                 close(sock);
00056                 return ""; //cannot list the interfaces
00057         }
00058 
00059         int pos = 0;
00060         std::string InterfaceName;
00061         do
00062         {
00063                 if (NameList[pos].if_index == 0)
00064                 {
00065                         close(sock);
00066                         if_freenameindex(NameList);
00067                         return ""; // no valid interfaces found
00068                 }
00069                 InterfaceName = NameList[pos].if_name;
00070                 pos++;
00071         } while (InterfaceName.substr(0,2) == "lo" || InterfaceName.substr(0,3) == "sit");
00072 
00073         if_freenameindex(NameList); //free the memory
00074 
00075         strcpy (dev.ifr_name, InterfaceName.c_str()); //select from the name
00076         if (ioctl(sock, SIOCGIFHWADDR, &dev) < 0) //get the interface data
00077         {
00078                 close(sock);
00079                 return ""; //cannot list the interfaces
00080         }
00081 
00082     for (int i=0; i<6; i++)
00083     {
00084         Key += (((unsigned int)dev.ifr_hwaddr.sa_data[i])&255);
00085         if (i != 5) Key += ':';
00086     }
00087         close(sock);
00088 
00089     return Key;
00090 }
00091 #endif
00092 
00093 std::string SecureID::MakeID(std::string ServerAddress)
00094 {
00095     std::string Key = GetPrivateKey();
00096         if (Key.empty()) //is the key retrived?
00097     {
00098                 return "";
00099         }
00100 
00101     Key += ServerAddress;
00102     return shacalc.GetHash(sha2::enuSHA256, 
00103                 (const unsigned char *)Key.c_str(), Key.size()); //scrambles the key with sha2
00104 }
00105 
00106 std::string SecureID::getSecureID (unsigned int ip)
00107 {
00108         std::string ServerAddress = NetInterface::getIpName(ip); //the ip we're connected to
00109         return MakeID (ServerAddress); //computes the SUI for that server & returns it
00110 }

Generated on Mon Feb 16 15:14:37 2009 for Scorched3D by  doxygen 1.5.3