How do I make sure that client requesting data has been logged in?

Started by
2 comments, last by hplus0603 13 years, 11 months ago
I'm using raknet (3.73). I have a server and client log into the server. I want only clients that have been logged in to be able to request data from the server. However, I do not know of a simple way of doing this other than checking each incoming packet and comparing them against my 'authenticated user's list'. Is there a simpler way of making sure that clients requesting data are actually logged in?
Advertisement
Basically what you can do is create a client context class and associate it with the client GUID. You create a new object for each connection that is accepted. Whenever you receive a packet, you obtain the client context object for that GUID. From there, you can check against its internal state. When the connection is lost, you remove the client context object.

Here's some short snippets I threw together that should get you pointed in the right direction:
...struct ClientContext{	RakNetGUID guid;	SystemAddress clientID;	bool isLoggedIn;	ClientContext(const RakNetGUID & guid_, const SystemAddress & clientID_) 		: guid(guid_), isLoggedIn(false), clientID(clientID_)	{	}};...std::map<uint64_t, ClientContext *> clients;// If you are going to be running a multi-threaded setup where 'clients' might// be accessed from different threads, then you need to create a // synchronization object to serialize access!...// Inside the event handling loop for RakNet (I'm using the chat example as base code)case ID_NEW_INCOMING_CONNECTION:{	ClientContext * client = new ClientContext(p->guid, p->systemAddress);	clients.insert(std::make_pair(p->guid.g, client));} break;...case ID_DISCONNECTION_NOTIFICATION:// You will want to handle ID_CONNECTION_LOST and any others that result in disconnection as well.{	std::map<uint64_t, ClientContext *>::iterator itr = clients.find(p->guid.g);	if(itr == clients.end())		break;	ClientContext * client = itr->second;	clients.erase(itr);	// Execute any logic before the client is deleted as needed	delete client;} break;...// For any of your defined packetscase ID_USER_PACKET_ENUM:{	std::map<uint64_t, ClientContext *>::iterator itr = clients.find(p->guid.g);	if(itr == clients.end())		break; // Ignore packet, sanity check	ClientContext * client = itr->second;	// Now use 'client' as you need, for example:	// This would go before any commands that require login. You do need to	// check the state for *each* one! You must always validate state when	// it comes to networking if you want to reduce the chance of your	// system being exploited.	if(client -> isLoggedIn == false)	{		server->CloseConnection(client -> clientID, true);		break;	}} break;...


Hope that helps!
Thanks!
For a higher-level overview of approaches, I suggest the Forum FAQ, and specifically the following article (an improvement of which can be found in Game Programming Gems 7):
Authentication for Games
enum Bool { True, False, FileNotFound };

This topic is closed to new replies.

Advertisement