git.ucc.asn.au
/
tpg
/
opendispense2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Door config settings (and some fixes to doorgroup reporting)
[tpg/opendispense2.git]
/
src
/
server
/
server.c
diff --git
a/src/server/server.c
b/src/server/server.c
index
92e26ff
..
cd69224
100644
(file)
--- 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
// Statistics
#define MAX_CONNECTION_QUEUE 5
#define INPUT_BUFFER_SIZE 256
+#define CLIENT_TIMEOUT 10 // Seconds
#define HASH_TYPE SHA1
#define HASH_LENGTH 20
#define HASH_TYPE SHA1
#define HASH_LENGTH 20
@@
-68,6
+69,7
@@
void _SendUserInfo(tClient *Client, int UserID);
void Server_Cmd_USERADD(tClient *Client, char *Args);
void Server_Cmd_USERFLAGS(tClient *Client, char *Args);
// --- Helpers ---
void Server_Cmd_USERADD(tClient *Client, char *Args);
void Server_Cmd_USERFLAGS(tClient *Client, char *Args);
// --- Helpers ---
+void Debug(tClient *Client, const char *Format, ...);
int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
int sendf(int Socket, const char *Format, ...);
int Server_int_ParseFlags(tClient *Client, const char *Str, int *Mask, int *Value);
int sendf(int Socket, const char *Format, ...);
@@
-152,6
+154,18
@@
void Server_Start(void)
return ;
}
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];
// Debug: Print the connection string
if(giDebugLevel >= 2) {
char ipstr[INET_ADDRSTRLEN];
@@
-224,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.
*/
* 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;
while( (bytes = recv(Socket, buf, remspace, 0)) > 0 )
{
char *eol, *start;
@@
-341,7
+356,7
@@
void Server_Cmd_USER(tClient *Client, char *Args)
// Debug!
if( giDebugLevel )
// Debug!
if( giDebugLevel )
-
printf("Client %i authenticating as '%s'\n", Client->ID
, Args);
+
Debug(Client, "Authenticating as '%s'"
, Args);
// Save username
if(Client->Username)
// Save username
if(Client->Username)
@@
-402,7
+417,7
@@
void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
// Check if trusted
if( !Client->bIsTrusted ) {
if(giDebugLevel)
// Check if trusted
if( !Client->bIsTrusted ) {
if(giDebugLevel)
-
printf("Client %i: Untrusted client attempting to AUTOAUTH\n", Client->ID
);
+
Debug(Client, "Untrusted client attempting to AUTOAUTH"
);
sendf(Client->Socket, "401 Untrusted\n");
return ;
}
sendf(Client->Socket, "401 Untrusted\n");
return ;
}
@@
-411,7
+426,7
@@
void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
Client->UID = Bank_GetAcctByName( Args );
if( Client->UID < 0 ) {
if(giDebugLevel)
Client->UID = Bank_GetAcctByName( Args );
if( Client->UID < 0 ) {
if(giDebugLevel)
-
printf("Client %i: Unknown user '%s'\n", Client->ID
, Args);
+
Debug(Client, "Unknown user '%s'"
, Args);
sendf(Client->Socket, "401 Auth Failure\n");
return ;
}
sendf(Client->Socket, "401 Auth Failure\n");
return ;
}
@@
-426,7
+441,7
@@
void Server_Cmd_AUTOAUTH(tClient *Client, char *Args)
Client->bIsAuthed = 1;
if(giDebugLevel)
Client->bIsAuthed = 1;
if(giDebugLevel)
-
printf("Client %i: Auto authenticated as '%s' (%i)\n", Client->ID
, Args, Client->UID);
+
Debug(Client, "Auto authenticated as '%s' (%i)"
, Args, Client->UID);
sendf(Client->Socket, "200 Auth OK\n");
}
sendf(Client->Socket, "200 Auth OK\n");
}
@@
-475,16
+490,24
@@
void Server_Cmd_SETEUSER(tClient *Client, char *Args)
*/
void Server_Cmd_ENUMITEMS(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 ;
}
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 ++ ) {
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
sendf(Client->Socket,
"202 Item %s:%i %i %s\n",
gaItems[i].Handler->Name, gaItems[i].ID, gaItems[i].Price, gaItems[i].Name
@@
-1030,10
+1053,14
@@
void Server_Cmd_USERINFO(tClient *Client, char *Args)
space = strchr(user, ' ');
if(space) *space = '\0';
space = strchr(user, ' ');
if(space) *space = '\0';
+ if( giDebugLevel ) Debug(Client, "User Info '%s'", user);
+
// Get recipient
uid = Bank_GetAcctByName(user);
// Get recipient
uid = Bank_GetAcctByName(user);
+
+ if( giDebugLevel >= 2 ) Debug(Client, "uid = %i", uid);
if( uid == -1 ) {
if( uid == -1 ) {
- sendf(Client->Socket, "404 Invalid user");
+ sendf(Client->Socket, "404 Invalid user
\n
");
return ;
}
return ;
}
@@
-1068,9
+1095,9
@@
void _SendUserInfo(tClient *Client, int UserID)
// TODO: User flags/type
sendf(
// TODO: User flags/type
sendf(
- Client->Socket, "202 User %s %i %s%s\n",
+ Client->Socket, "202 User %s %i %s%s
%s
\n",
Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
Bank_GetAcctName(UserID), Bank_GetBalance(UserID),
- type, disabled
+ type, disabled
, door
);
}
);
}
@@
-1145,6
+1172,10
@@
void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
if( Server_int_ParseFlags(Client, flags, &mask, &value) )
return ;
if( Server_int_ParseFlags(Client, flags, &mask, &value) )
return ;
+ if( giDebugLevel )
+ Debug(Client, "Set %i(%s) flags to %x (masked %x)\n",
+ uid, username, mask, value);
+
// Apply flags
Bank_SetFlags(uid, mask, value);
// Apply flags
Bank_SetFlags(uid, mask, value);
@@
-1153,6
+1184,17
@@
void Server_Cmd_USERFLAGS(tClient *Client, char *Args)
}
// --- INTERNAL HELPERS ---
}
// --- INTERNAL HELPERS ---
+void Debug(tClient *Client, const char *Format, ...)
+{
+ va_list args;
+ //printf("%010i [%i] ", (int)time(NULL), Client->ID);
+ printf("[%i] ", Client->ID);
+ va_start(args, Format);
+ vprintf(Format, args);
+ va_end(args);
+ printf("\n");
+}
+
int sendf(int Socket, const char *Format, ...)
{
va_list args;
int sendf(int Socket, const char *Format, ...)
{
va_list args;
UCC
git Repository :: git.ucc.asn.au