Skip to content

Commit

Permalink
libdeng2|libshell|Server: Password challenge waived for local connect…
Browse files Browse the repository at this point in the history
…ions

Also, the challenge packet is now using a regular packet type
identifier.
  • Loading branch information
skyjake committed Feb 12, 2013
1 parent 09b3d8a commit 1a60d1a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 22 deletions.
37 changes: 20 additions & 17 deletions doomsday/client/src/network/sys_network.cpp
Expand Up @@ -73,13 +73,16 @@ typedef struct netnode_s {
int sock;
char name[256];

// The node is owned by a client in the game. This becomes true
/// The node is owned by a client in the game. This becomes true
// when the client issues the JOIN request.
boolean hasJoined;

// This is the client's remote address.
/// This is the client's remote address.
ipaddress_t addr;

/// The node is connecting from the local host.
boolean isFromLocalHost;

expectedresponder_t expectedResponder;
} netnode_t;

Expand Down Expand Up @@ -377,8 +380,13 @@ static boolean registerNewSocket(int sock)
// Add this socket to the set of client sockets.
LegacyNetwork_SocketSet_Add(sockSet, sock);

DEBUG_VERBOSE2_Message(("N_RegisterNewSocket: Socket #%i registered as node %i.\n", sock, i));
// The address where we should be sending data.
LegacyNetwork_GetPeerAddress(sock, node->addr.host, sizeof(node->addr.host), &node->addr.port);

node->isFromLocalHost = LegacyNetwork_IsLocal(sock);

LOG_VERBOSE("Socket #%i from %s registered as node %i (local:%b)")
<< sock << node->addr.host << i << node->isFromLocalHost;
return true;
}
}
Expand All @@ -403,16 +411,6 @@ static boolean joinNode(nodeid_t id, int clientProtocol, const char *name)

node = &netNodes[id];

// The address where we should be sending data.
LegacyNetwork_GetPeerAddress(node->sock, node->addr.host, sizeof(node->addr.host), &node->addr.port);

if(verbose)
{
char buf[80];
N_IPToString(buf, &node->addr);
Con_Message("Node %i listens at %s.\n", id, buf);
}

// Convert the network node into a real client node.
node->hasJoined = true;

Expand Down Expand Up @@ -518,6 +516,8 @@ static void switchNodeToShellMode(nodeid_t node)
*/
static boolean serverHandleNodeRequest(nodeid_t node, const char *command, int length)
{
LOG_AS("serverHandleNodeRequest");

int sock = netNodes[node].sock;
serverinfo_t info;
ddstring_t msg;
Expand All @@ -537,7 +537,7 @@ static boolean serverHandleNodeRequest(nodeid_t node, const char *command, int l
Str_Appendf(&msg, "Info\n");
Sv_InfoToString(&info, &msg);

DEBUG_VERBOSE_Message(("serverHandleNodeRequest: Sending: %s\n", Str_Text(&msg)));
LOG_DEBUG("Info reply:\n%s") << Str_Text(&msg);

LegacyNetwork_Send(sock, Str_Text(&msg), Str_Length(&msg));
Str_Free(&msg);
Expand All @@ -546,10 +546,11 @@ static boolean serverHandleNodeRequest(nodeid_t node, const char *command, int l
{
if(length == 5)
{
if(strlen(netPassword) > 0)
// Password is not required for connections from the local computer.
if(strlen(netPassword) > 0 && !netNodes[node].isFromLocalHost)
{
// Need to ask for a password, too.
LegacyNetwork_Send(sock, "Password?", 9);
LegacyNetwork_Send(sock, "Psw?", 4);
return true;
}
}
Expand Down Expand Up @@ -606,6 +607,7 @@ static boolean serverHandleNodeRequest(nodeid_t node, const char *command, int l
else
{
// Too bad, scoundrel! Goodbye.
LOG_WARNING("Received an invalid request from node %i.") << node;
N_TerminateNode(node);
return false;
}
Expand Down Expand Up @@ -653,7 +655,7 @@ void N_ServerListenUnjoinedNodes(void)
if(node->sock && LegacyNetwork_IsDisconnected(node->sock))
{
// Close this socket & node.
Con_Message("Connection to client closed on node %i.\n", i);
LOG_INFO("Connection to client closed on node %i.") << i;
N_TerminateNode(i);
continue;
}
Expand Down Expand Up @@ -748,6 +750,7 @@ void N_PrintNetworkStatus(void)

Con_Message("Configuration:\n");
Con_Message(" port for hosting games (net-ip-port): %i\n", Con_GetInteger("net-ip-port"));
Con_Message(" shell password (server-password): \"%s\"\n", netPassword);
}

void N_ServerUpdateBeacon()
Expand Down
1 change: 1 addition & 0 deletions doomsday/libdeng2/include/de/c_wrapper.h
Expand Up @@ -116,6 +116,7 @@ DENG2_PUBLIC int LegacyNetwork_Accept(int serverSocket);
DENG2_PUBLIC int LegacyNetwork_Open(char const *ipAddress, unsigned short port);
DENG2_PUBLIC void LegacyNetwork_GetPeerAddress(int socket, char *host, int hostMaxSize, unsigned short *port);
DENG2_PUBLIC int LegacyNetwork_IsDisconnected(int socket);
DENG2_PUBLIC int LegacyNetwork_IsLocal(int socket);
DENG2_PUBLIC void LegacyNetwork_Close(int socket);

DENG2_PUBLIC int LegacyNetwork_Send(int socket, void const *data, int size);
Expand Down
1 change: 1 addition & 0 deletions doomsday/libdeng2/include/de/legacy/legacynetwork.h
Expand Up @@ -48,6 +48,7 @@ class DENG2_PUBLIC LegacyNetwork
void close(int socket);
de::Address peerAddress(int socket) const;
bool isOpen(int socket);
bool isLocal(int socket);

int sendBytes(int socket, IByteArray const &data);
bool receiveBlock(int socket, Block &data);
Expand Down
5 changes: 5 additions & 0 deletions doomsday/libdeng2/include/de/net/socket.h
Expand Up @@ -169,6 +169,11 @@ class DENG2_PUBLIC Socket : public QObject, public Transmitter
*/
bool isOpen() const;

/**
* Determines if the socket is on the local computer.
*/
bool isLocal() const;

/**
* Determines whether there are any incoming messages waiting.
*/
Expand Down
6 changes: 6 additions & 0 deletions doomsday/libdeng2/src/c_wrapper.cpp
Expand Up @@ -311,6 +311,12 @@ int LegacyNetwork_IsDisconnected(int socket)
return !DENG2_LEGACYNETWORK().isOpen(socket);
}

int LegacyNetwork_IsLocal(int socket)
{
if(!socket) return 0;
return DENG2_LEGACYNETWORK().isLocal(socket);
}

int LegacyNetwork_BytesReady(int socket)
{
if(!socket) return 0;
Expand Down
6 changes: 6 additions & 0 deletions doomsday/libdeng2/src/legacy/legacynetwork.cpp
Expand Up @@ -155,6 +155,12 @@ bool LegacyNetwork::isOpen(int socket)
return d->sockets[socket]->isOpen();
}

bool LegacyNetwork::isLocal(int socket)
{
if(!d->sockets.contains(socket)) return false;
return d->sockets[socket]->isLocal();
}

de::Address LegacyNetwork::peerAddress(int socket) const
{
DENG2_ASSERT(d->sockets.contains(socket));
Expand Down
12 changes: 12 additions & 0 deletions doomsday/libdeng2/src/net/socket.cpp
Expand Up @@ -624,6 +624,18 @@ bool Socket::isOpen() const
return d->socket && d->socket->state() != QTcpSocket::UnconnectedState;
}

bool Socket::isLocal() const
{
QHostAddress const host = peerAddress().host();
if(host == QHostAddress::LocalHost) return true;
QHostInfo info = QHostInfo::fromName(QHostInfo::localHostName());
foreach(QHostAddress addr, info.addresses())
{
if(addr == host) return true;
}
return false;
}

void Socket::socketDisconnected()
{
emit disconnected();
Expand Down
6 changes: 1 addition & 5 deletions doomsday/libshell/src/protocol.cpp
Expand Up @@ -41,11 +41,7 @@ ChallengePacket::ChallengePacket() : Packet(CHALLENGE_PACKET_TYPE)

Packet *ChallengePacket::fromBlock(Block const &block)
{
if(block == "Password?")
{
return new ChallengePacket;
}
return 0;
return constructFromBlock<ChallengePacket>(block, CHALLENGE_PACKET_TYPE);
}

// LogEntryPacket ------------------------------------------------------------
Expand Down

0 comments on commit 1a60d1a

Please sign in to comment.