X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=src%2Fserver%2Fserver.c;h=4379317fd3776f6c446fbccdfc17b9e2493aef47;hb=f2baa7bf5fb9877235369e7bf1abb7cff507dff6;hp=a2279fd18c64a51c5dbe740e05d9b6398f118656;hpb=b42302186308252d40184c2af8a5e1bc329db5e2;p=tpg%2Fopendispense2.git diff --git a/src/server/server.c b/src/server/server.c index a2279fd..4379317 100644 --- a/src/server/server.c +++ b/src/server/server.c @@ -23,6 +23,7 @@ // Statistics #define MAX_CONNECTION_QUEUE 5 #define INPUT_BUFFER_SIZE 256 +#define CLIENT_TIMEOUT 10 // Seconds #define HASH_TYPE SHA1 #define HASH_LENGTH 20 @@ -153,6 +154,18 @@ void Server_Start(void) return ; } + // Set a timeout on the user conneciton + { + struct timeval tv; + tv.tv_sec = CLIENT_TIMEOUT; + tv.tv_usec = 0; + if( setsockopt(client_socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) ) + { + perror("setsockopt"); + return ; + } + } + // Debug: Print the connection string if(giDebugLevel >= 2) { char ipstr[INET_ADDRSTRLEN]; @@ -225,6 +238,7 @@ void Server_HandleClient(int Socket, int bTrusted) * it is saved to the beginning of `inbuf` and `buf` is updated to * the end of it. */ + // TODO: Use select() instead (to give a timeout) while( (bytes = recv(Socket, buf, remspace, 0)) > 0 ) { char *eol, *start; @@ -342,7 +356,7 @@ void Server_Cmd_USER(tClient *Client, char *Args) // Debug! if( giDebugLevel ) - Debug("Authenticating as '%s'", Args); + Debug(Client, "Authenticating as '%s'", Args); // Save username if(Client->Username) @@ -403,7 +417,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args) // Check if trusted if( !Client->bIsTrusted ) { if(giDebugLevel) - Debug("Untrusted client attempting to AUTOAUTH"); + Debug(Client, "Untrusted client attempting to AUTOAUTH"); sendf(Client->Socket, "401 Untrusted\n"); return ; } @@ -412,7 +426,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args) Client->UID = Bank_GetAcctByName( Args ); if( Client->UID < 0 ) { if(giDebugLevel) - Debug("Unknown user '%s'", Args); + Debug(Client, "Unknown user '%s'", Args); sendf(Client->Socket, "401 Auth Failure\n"); return ; } @@ -427,7 +441,7 @@ void Server_Cmd_AUTOAUTH(tClient *Client, char *Args) Client->bIsAuthed = 1; if(giDebugLevel) - Debug("Auto authenticated as '%s' (%i)", Args, Client->UID); + Debug(Client, "Auto authenticated as '%s' (%i)", Args, Client->UID); sendf(Client->Socket, "200 Auth OK\n"); } @@ -476,16 +490,24 @@ void Server_Cmd_SETEUSER(tClient *Client, char *Args) */ void Server_Cmd_ENUMITEMS(tClient *Client, char *Args) { - int i; + int i, count; if( Args != NULL && strlen(Args) ) { sendf(Client->Socket, "407 ENUM_ITEMS takes no arguments\n"); return ; } + + // Count shown items + count = 0; + for( i = 0; i < giNumItems; i ++ ) { + if( gaItems[i].bHidden ) continue; + count ++; + } - sendf(Client->Socket, "201 Items %i\n", giNumItems); + sendf(Client->Socket, "201 Items %i\n", count); for( i = 0; i < giNumItems; i ++ ) { + if( gaItems[i].bHidden ) continue; sendf(Client->Socket, "202 Item %s:%i %i %s\n", gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name @@ -1031,13 +1053,14 @@ void Server_Cmd_USERINFO(tClient *Client, char *Args) space = strchr(user, ' '); if(space) *space = '\0'; - if( giDebugLevel ) - Debug(Client, "User Info '%s'", user); + if( giDebugLevel ) Debug(Client, "User Info '%s'", user); // Get recipient uid = Bank_GetAcctByName(user); + + if( giDebugLevel >= 2 ) Debug(Client, "uid = %i", uid); if( uid == -1 ) { - sendf(Client->Socket, "404 Invalid user"); + sendf(Client->Socket, "404 Invalid user\n"); return ; } @@ -1160,9 +1183,10 @@ void Server_Cmd_USERFLAGS(tClient *Client, char *Args) void Debug(tClient *Client, const char *Format, ...) { va_list args; - printf("%010lli [%i] ", time(NULL), Client->ID); + //printf("%010i [%i] ", (int)time(NULL), Client->ID); + printf("[%i] ", Client->ID); va_start(args, Format); - vprintf(NULL, 0, Format, args); + vprintf(Format, args); va_end(args); printf("\n"); }