Home | nevrax.com |
|
login_cookie.cppGo to the documentation of this file.00001 00007 /* Copyright, 2001 Nevrax Ltd. 00008 * 00009 * This file is part of NEVRAX NeL Network Services. 00010 * NEVRAX NeL Network Services is free software; you can redistribute it and/or modify 00011 * it under the terms of the GNU General Public License as published by 00012 * the Free Software Foundation; either version 2, or (at your option) 00013 * any later version. 00014 00015 * NEVRAX NeL Network Services is distributed in the hope that it will be useful, but 00016 * WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * General Public License for more details. 00019 00020 * You should have received a copy of the GNU General Public License 00021 * along with NEVRAX NeL Network Services; see the file COPYING. If not, write to the 00022 * Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, 00023 * MA 02111-1307, USA. 00024 */ 00025 00026 #include "stdnet.h" 00027 00028 #include "nel/net/login_cookie.h" 00029 00030 using namespace std; 00031 using namespace NLMISC; 00032 00033 00034 namespace NLNET { 00035 00036 00037 /* 00038 * Comparison == operator 00039 */ 00040 bool operator== (const CLoginCookie &c1, const CLoginCookie &c2) 00041 { 00042 nlassert (c1._Valid && c2._Valid); 00043 00044 return c1._UserAddr==c2._UserAddr && c1._UserKey==c2._UserKey && c1._UserId==c2._UserId; 00045 } 00046 00047 CLoginCookie::CLoginCookie (uint32 addr, uint32 id) : _Valid(true), _UserAddr(addr), _UserId(id) 00048 { 00049 // generates the key for this cookie 00050 _UserKey = generateKey(); 00051 } 00052 00054 00055 uint32 CLoginCookie::generateKey() 00056 { 00057 uint32 t = time (NULL); 00058 srand (time(NULL)); 00059 uint32 r = rand (); 00060 static uint32 n = 0; 00061 n++; 00062 00063 // 12bits for the time (in second) => loop in 1 hour 00064 // 8bits for random => 256 case 00065 // 12bits for the inc number => can generate 4096 keys per second without any problem (if you generate more than this number, you could have 2 same keys) 00066 return (t&0xFFF)<<20 | (r&0xFF)<<12 | (n&0xFFF); 00067 00068 // 12bits for the time (in second) => loop in 1 hour 00069 // 20bits for the inc number => can generate more than 1 million keys per second without any problem (never exceed on my computer) 00070 // return (t&0xFFF)<<20 | (n&0xFFFFF); 00071 } 00072 00073 00074 /* test key generation 00075 void main() 00076 { 00077 set<uint32> myset; 00078 00079 // generates the key for this cookie 00080 while (true) 00081 { 00082 uint32 val = (t&0xFFF)<<20 | (r&0xFF)<<12 | (n&0xFFF); 00083 pair<set<uint32>::iterator,bool> p = myset.insert (val); 00084 if (!p.second) printf("%10u 0x%x already inserted\n", val, val); 00085 } 00086 } 00087 */ 00088 00089 } // NL. |