Commit ab579610 authored by rswindell's avatar rswindell
Browse files

Server startup structures now include a private data pointer which is passed

back to callback functions (e.g. lputs, thread_up, etc). This allows servers
to share the same callback functions, eliminating many nearly-identical
functions. This was able to shave quite a bit of redundant code from ntsvcs.c.
This feature is not utilized in sbbs (sbbscon.c) or sbbsctrl (mainformunit.cpp).
parent d72ad325
......@@ -43,7 +43,7 @@
#include "ClientFormUnit.h"
#include "sbbs.h" // filter_ip
void socket_open(BOOL open);
void socket_open(void*, BOOL open);
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
......@@ -98,7 +98,7 @@ void __fastcall TClientForm::CloseSocketMenuItemClick(TObject *Sender)
while(ListItem!=NULL) {
if(closesocket(atoi(ListItem->Caption.c_str()))==0)
socket_open(FALSE);
socket_open(NULL, FALSE);
ListItem=ListView->GetNextItem(ListItem,sdAll,State);
}
}
......
......@@ -85,7 +85,7 @@ TMainForm *MainForm;
DWORD MaxLogLen=20000;
int threads=1;
static void thread_up(BOOL up, BOOL setuid)
static void thread_up(void* p, BOOL up, BOOL setuid)
{
char str[128];
static HANDLE mutex;
......@@ -106,7 +106,7 @@ static void thread_up(BOOL up, BOOL setuid)
int sockets=0;
void socket_open(BOOL open)
void socket_open(void* p, BOOL open)
{
char str[128];
static HANDLE mutex;
......@@ -128,7 +128,7 @@ void socket_open(BOOL open)
int clients=0;
int total_clients=0;
static void client_add(BOOL add)
static void client_add(void* p, BOOL add)
{
char str[128];
......@@ -148,7 +148,7 @@ static void client_add(BOOL add)
MainForm->StatusBar->Panels->Items[3]->Text=Str;
}
static void client_on(BOOL on, int sock, client_t* client, BOOL update)
static void client_on(void* p, BOOL on, int sock, client_t* client, BOOL update)
{
char str[128];
int i,j;
......@@ -177,9 +177,9 @@ static void client_on(BOOL on, int sock, client_t* client, BOOL update)
if(on) {
if(!update)
client_add(TRUE);
client_add(NULL, TRUE);
} else { // Off
client_add(FALSE);
client_add(NULL, FALSE);
if(i>=0)
ClientForm->ListView->Items->Delete(i);
ReleaseMutex(mutex);
......@@ -208,7 +208,7 @@ static void client_on(BOOL on, int sock, client_t* client, BOOL update)
ReleaseMutex(ClientForm->ListMutex);
}
static int bbs_lputs(char *str)
static int bbs_lputs(void* p, char *str)
{
static HANDLE mutex;
......@@ -226,7 +226,7 @@ static int bbs_lputs(char *str)
return(Line.Length());
}
static void bbs_status(char *str)
static void bbs_status(void* p, char *str)
{
static HANDLE mutex;
......@@ -239,7 +239,7 @@ static void bbs_status(char *str)
ReleaseMutex(mutex);
}
static void bbs_clients(int clients)
static void bbs_clients(void* p, int clients)
{
static HANDLE mutex;
static save_clients;
......@@ -258,7 +258,7 @@ static void bbs_clients(int clients)
ReleaseMutex(mutex);
}
static void bbs_terminated(int code)
static void bbs_terminated(void* p, int code)
{
Screen->Cursor=crDefault;
MainForm->TelnetStart->Enabled=true;
......@@ -266,7 +266,7 @@ static void bbs_terminated(int code)
MainForm->TelnetRecycle->Enabled=false;
Application->ProcessMessages();
}
static void bbs_started(void)
static void bbs_started(void* p)
{
Screen->Cursor=crDefault;
MainForm->TelnetStart->Enabled=false;
......@@ -277,7 +277,7 @@ static void bbs_started(void)
static void bbs_start(void)
{
Screen->Cursor=crAppStart;
bbs_status("Starting");
bbs_status(NULL,"Starting");
SAFECOPY(MainForm->bbs_startup.ctrl_dir
,MainForm->CtrlDirectory.c_str());
SAFECOPY(MainForm->bbs_startup.temp_dir
......@@ -315,7 +315,7 @@ static int event_log(char *str)
return(Line.Length());
}
static int service_log(char *str)
static int service_log(void* p, char *str)
{
static HANDLE mutex;
......@@ -333,7 +333,7 @@ static int service_log(char *str)
return(Line.Length());
}
static void services_status(char *str)
static void services_status(void* p, char *str)
{
static HANDLE mutex;
......@@ -346,7 +346,7 @@ static void services_status(char *str)
ReleaseMutex(mutex);
}
static void services_terminated(int code)
static void services_terminated(void* p, int code)
{
Screen->Cursor=crDefault;
MainForm->ServicesStart->Enabled=true;
......@@ -354,7 +354,7 @@ static void services_terminated(int code)
MainForm->ServicesRecycle->Enabled=false;
Application->ProcessMessages();
}
static void services_started(void)
static void services_started(void* p)
{
Screen->Cursor=crDefault;
MainForm->ServicesStart->Enabled=false;
......@@ -363,11 +363,11 @@ static void services_started(void)
Application->ProcessMessages();
}
static void services_clients(int clients)
static void services_clients(void* p, int clients)
{
}
static int mail_lputs(char *str)
static int mail_lputs(void* p, char *str)
{
static HANDLE mutex;
static FILE* LogStream;
......@@ -419,7 +419,7 @@ static int mail_lputs(char *str)
return(Line.Length());
}
static void mail_status(char *str)
static void mail_status(void* p, char *str)
{
static HANDLE mutex;
......@@ -432,7 +432,7 @@ static void mail_status(char *str)
ReleaseMutex(mutex);
}
static void mail_clients(int clients)
static void mail_clients(void* p, int clients)
{
static HANDLE mutex;
......@@ -446,7 +446,7 @@ static void mail_clients(int clients)
ReleaseMutex(mutex);
}
static void mail_terminated(int code)
static void mail_terminated(void* p, int code)
{
Screen->Cursor=crDefault;
MainForm->MailStart->Enabled=true;
......@@ -454,7 +454,7 @@ static void mail_terminated(int code)
MainForm->MailRecycle->Enabled=false;
Application->ProcessMessages();
}
static void mail_started(void)
static void mail_started(void* p)
{
Screen->Cursor=crDefault;
MainForm->MailStart->Enabled=false;
......@@ -465,7 +465,7 @@ static void mail_started(void)
static void mail_start(void)
{
Screen->Cursor=crAppStart;
mail_status("Starting");
mail_status(NULL, "Starting");
SAFECOPY(MainForm->mail_startup.ctrl_dir
,MainForm->CtrlDirectory.c_str());
SAFECOPY(MainForm->mail_startup.host_name
......@@ -475,7 +475,7 @@ static void mail_start(void)
Application->ProcessMessages();
}
static int ftp_lputs(char *str)
static int ftp_lputs(void* p, char *str)
{
static HANDLE mutex;
static FILE* LogStream;
......@@ -543,7 +543,7 @@ static int ftp_lputs(char *str)
return(Line.Length());
}
static void ftp_status(char *str)
static void ftp_status(void* p, char *str)
{
static HANDLE mutex;
......@@ -556,7 +556,7 @@ static void ftp_status(char *str)
ReleaseMutex(mutex);
}
static void ftp_clients(int clients)
static void ftp_clients(void* p, int clients)
{
static HANDLE mutex;
......@@ -570,7 +570,7 @@ static void ftp_clients(int clients)
ReleaseMutex(mutex);
}
static void ftp_terminated(int code)
static void ftp_terminated(void* p, int code)
{
Screen->Cursor=crDefault;
MainForm->FtpStart->Enabled=true;
......@@ -578,7 +578,7 @@ static void ftp_terminated(int code)
MainForm->FtpRecycle->Enabled=false;
Application->ProcessMessages();
}
static void ftp_started(void)
static void ftp_started(void* p)
{
Screen->Cursor=crDefault;
MainForm->FtpStart->Enabled=false;
......@@ -589,7 +589,7 @@ static void ftp_started(void)
static void ftp_start(void)
{
Screen->Cursor=crAppStart;
ftp_status("Starting");
ftp_status(NULL, "Starting");
SAFECOPY(MainForm->ftp_startup.ctrl_dir
,MainForm->CtrlDirectory.c_str());
SAFECOPY(MainForm->ftp_startup.temp_dir
......@@ -854,7 +854,7 @@ void __fastcall TMainForm::TelnetStartExecute(TObject *Sender)
void __fastcall TMainForm::ServicesStartExecute(TObject *Sender)
{
Screen->Cursor=crAppStart;
services_status("Starting");
services_status(NULL, "Starting");
SAFECOPY(MainForm->services_startup.ctrl_dir
,MainForm->CtrlDirectory.c_str());
......@@ -877,7 +877,7 @@ void __fastcall TMainForm::ServicesStartExecute(TObject *Sender)
void __fastcall TMainForm::ServicesStopExecute(TObject *Sender)
{
Screen->Cursor=crAppStart;
services_status("Terminating");
services_status(NULL, "Terminating");
services_terminate();
Application->ProcessMessages();
}
......@@ -886,7 +886,7 @@ void __fastcall TMainForm::ServicesStopExecute(TObject *Sender)
void __fastcall TMainForm::TelnetStopExecute(TObject *Sender)
{
Screen->Cursor=crAppStart;
bbs_status("Terminating");
bbs_status(NULL, "Terminating");
bbs_terminate();
Application->ProcessMessages();
}
......@@ -947,7 +947,7 @@ void __fastcall TMainForm::MailStartExecute(TObject *Sender)
void __fastcall TMainForm::MailStopExecute(TObject *Sender)
{
Screen->Cursor=crAppStart;
mail_status("Terminating");
mail_status(NULL, "Terminating");
mail_terminate();
Application->ProcessMessages();
}
......@@ -996,7 +996,7 @@ void __fastcall TMainForm::FtpStartExecute(TObject *Sender)
void __fastcall TMainForm::FtpStopExecute(TObject *Sender)
{
Screen->Cursor=crAppStart;
ftp_status("Terminating");
ftp_status(NULL, "Terminating");
ftp_terminate();
Application->ProcessMessages();
}
......@@ -2773,8 +2773,8 @@ void __fastcall TMainForm::ViewLogClick(TObject *Sender)
return;
/* Close Mail/FTP logs */
mail_lputs(NULL);
ftp_lputs(NULL);
mail_lputs(NULL,NULL);
ftp_lputs(NULL,NULL);
sprintf(filename,"%sLOGS\\%s%02d%02d%02d.LOG"
,MainForm->cfg.logs_dir
......
......@@ -152,7 +152,7 @@ static int lprintf(char *fmt, ...)
vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
sbuf[sizeof(sbuf)-1]=0;
va_end(argptr);
result=startup->lputs(sbuf);
result=startup->lputs(startup->cbdata,sbuf);
return(result);
}
......@@ -185,32 +185,32 @@ static BOOL winsock_startup(void)
static void status(char* str)
{
if(startup!=NULL && startup->status!=NULL)
startup->status(str);
startup->status(startup->cbdata,str);
}
static void update_clients(void)
{
if(startup!=NULL && startup->clients!=NULL)
startup->clients(active_clients);
startup->clients(startup->cbdata,active_clients);
}
static void client_on(SOCKET sock, client_t* client, BOOL update)
{
if(startup!=NULL && startup->client_on!=NULL)
startup->client_on(TRUE,sock,client,update);
startup->client_on(startup->cbdata,TRUE,sock,client,update);
}
static void client_off(SOCKET sock)
{
if(startup!=NULL && startup->client_on!=NULL)
startup->client_on(FALSE,sock,NULL,FALSE);
startup->client_on(startup->cbdata,FALSE,sock,NULL,FALSE);
}
static void thread_up(BOOL setuid)
{
thread_count++;
if(startup!=NULL && startup->thread_up!=NULL)
startup->thread_up(TRUE, setuid);
startup->thread_up(startup->cbdata,TRUE, setuid);
}
static void thread_down(void)
......@@ -218,7 +218,7 @@ static void thread_down(void)
if(thread_count>0)
thread_count--;
if(startup!=NULL && startup->thread_up!=NULL)
startup->thread_up(FALSE, FALSE);
startup->thread_up(startup->cbdata,FALSE, FALSE);
}
static SOCKET ftp_open_socket(int type)
......@@ -228,7 +228,7 @@ static SOCKET ftp_open_socket(int type)
sock=socket(AF_INET, type, IPPROTO_IP);
if(sock!=INVALID_SOCKET && startup!=NULL && startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
if(sock!=INVALID_SOCKET) {
if(set_socket_options(&scfg, sock, error))
lprintf("%04d !ERROR %s",sock, error);
......@@ -256,7 +256,7 @@ static int ftp_close_socket(SOCKET* sock, int line)
result=closesocket(*sock);
if(result==0 && startup!=NULL && startup->socket_open!=NULL)
startup->socket_open(FALSE);
startup->socket_open(startup->cbdata,FALSE);
sockets--;
......@@ -1907,7 +1907,7 @@ static void filexfer(SOCKADDR_IN* addr, SOCKET ctrl_sock, SOCKET pasv_sock, SOCK
return;
}
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
sockets++;
if(startup->options&FTP_OPT_DEBUG_DATA)
lprintf("%04d DATA socket %d opened",ctrl_sock,*data_sock);
......@@ -2001,7 +2001,7 @@ static void filexfer(SOCKADDR_IN* addr, SOCKET ctrl_sock, SOCKET pasv_sock, SOCK
return;
}
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
sockets++;
if(startup->options&FTP_OPT_DEBUG_DATA)
lprintf("%04d PASV DATA socket %d connected to %s port %u"
......@@ -4446,7 +4446,7 @@ static void cleanup(int code, int line)
lprintf("#### FTP Server thread terminated (%u threads remain, %lu clients served)"
,thread_count, served);
if(startup!=NULL && startup->terminated!=NULL)
startup->terminated(code);
startup->terminated(startup->cbdata,code);
}
const char* DLLCALL ftp_ver(void)
......@@ -4679,7 +4679,7 @@ void DLLCALL ftp_server(void* arg)
/* signal caller that we've started up successfully */
if(startup->started!=NULL)
startup->started();
startup->started(startup->cbdata);
while(server_socket!=INVALID_SOCKET) {
......@@ -4733,7 +4733,7 @@ void DLLCALL ftp_server(void* arg)
break;
}
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
sockets++;
if(trashcan(&scfg,inet_ntoa(client_addr.sin_addr),"ip-silent")) {
......
......@@ -52,16 +52,22 @@ typedef struct {
DWORD options; /* See FTP_OPT definitions */
DWORD js_max_bytes;
DWORD js_cx_stack;
int (*lputs)(char*);
void (*status)(char*);
void (*started)(void);
void (*terminated)(int code);
void (*clients)(int active);
void (*thread_up)(BOOL up, BOOL setuid);
void (*socket_open)(BOOL open);
void (*client_on)(BOOL on, int sock, client_t*, BOOL update);
void* cbdata; /* Private data passed to callbacks */
/* Callbacks (NULL if unused) */
int (*lputs)(void*, char*);
void (*status)(void*, char*);
void (*started)(void*);
void (*terminated)(void*, int code);
void (*clients)(void*, int active);
void (*thread_up)(void*, BOOL up, BOOL setuid);
void (*socket_open)(void*, BOOL open);
void (*client_on)(void*, BOOL on, int sock, client_t*, BOOL update);
BOOL (*seteuid)(BOOL user);
BOOL (*setuid)(BOOL force);
/* Paths */
char ctrl_dir[128];
char index_file_name[64];
char html_index_file[64];
......@@ -70,6 +76,8 @@ typedef struct {
char answer_sound[128];
char hangup_sound[128];
char hack_sound[128];
/* Misc */
char host_name[128];
BOOL recycle_now;
......
......@@ -125,7 +125,7 @@ static int lprintf(char *fmt, ...)
vsnprintf(sbuf,sizeof(sbuf),fmt,argptr);
sbuf[sizeof(sbuf)-1]=0;
va_end(argptr);
return(startup->lputs(sbuf));
return(startup->lputs(startup->cbdata,sbuf));
}
#ifdef _WINSOCKAPI_
......@@ -162,7 +162,7 @@ static void update_clients(void)
return;
}
#endif
startup->clients(active_clients+active_sendmail);
startup->clients(startup->cbdata,active_clients+active_sendmail);
}
}
......@@ -175,7 +175,7 @@ static void client_on(SOCKET sock, client_t* client, BOOL update)
return;
}
#endif
startup->client_on(TRUE,sock,client,update);
startup->client_on(startup->cbdata,TRUE,sock,client,update);
}
}
......@@ -188,7 +188,7 @@ static void client_off(SOCKET sock)
return;
}
#endif
startup->client_on(FALSE,sock,NULL,FALSE);
startup->client_on(startup->cbdata,FALSE,sock,NULL,FALSE);
}
}
......@@ -196,7 +196,7 @@ static void thread_up(BOOL setuid)
{
thread_count++;
if(startup!=NULL && startup->thread_up!=NULL)
startup->thread_up(TRUE,setuid);
startup->thread_up(startup->cbdata,TRUE,setuid);
}
static void thread_down(void)
......@@ -210,7 +210,7 @@ static void thread_down(void)
return;
}
#endif
startup->thread_up(FALSE,FALSE);
startup->thread_up(startup->cbdata,FALSE,FALSE);
}
}
......@@ -221,7 +221,7 @@ SOCKET mail_open_socket(int type)
sock=socket(AF_INET, type, IPPROTO_IP);
if(sock!=INVALID_SOCKET && startup!=NULL && startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
if(sock!=INVALID_SOCKET) {
if(set_socket_options(&scfg, sock,error))
lprintf("%04d !ERROR %s",sock,error);
......@@ -250,7 +250,7 @@ int mail_close_socket(SOCKET sock)
return(-1);
}
#endif
startup->socket_open(FALSE);
startup->socket_open(startup->cbdata,FALSE);
}
sockets--;
if(result!=0) {
......@@ -274,7 +274,7 @@ static void status(char* str)
return;
}
#endif
startup->status(str);
startup->status(startup->cbdata,str);
}
}
......@@ -3420,7 +3420,7 @@ static void cleanup(int code)
lprintf("#### Mail Server thread terminated (%u threads remain, %lu clients served)"
,thread_count, served);
if(startup!=NULL && startup->terminated!=NULL)
startup->terminated(code);
startup->terminated(startup->cbdata,code);
}
const char* DLLCALL mail_ver(void)
......@@ -3693,7 +3693,7 @@ void DLLCALL mail_server(void* arg)
/* signal caller that we've started up successfully */
if(startup->started!=NULL)
startup->started();
startup->started(startup->cbdata);
while(server_socket!=INVALID_SOCKET) {
......@@ -3754,7 +3754,7 @@ void DLLCALL mail_server(void* arg)
break;
}
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
sockets++;
if(trashcan(&scfg,inet_ntoa(client_addr.sin_addr),"ip-silent")) {
......@@ -3809,7 +3809,7 @@ void DLLCALL mail_server(void* arg)
break;
}
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
startup->socket_open(startup->cbdata,TRUE);
sockets++;
if(trashcan(&scfg,inet_ntoa(client_addr.sin_addr),"ip-silent")) {
......
......@@ -57,16 +57,22 @@ typedef struct {
DWORD interface_addr;
DWORD options; /* See MAIL_OPT definitions */
DWORD max_msg_size;
int (*lputs)(char*);
void (*status)(char*);
void (*started)(void);
void (*terminated)(int code);
void (*clients)(int active);
void (*thread_up)(BOOL up, BOOL setuid);
void (*socket_open)(BOOL open);
void (*client_on)(BOOL on, int sock, client_t*, BOOL update);
void* cbdata; /* Private data passed to callbacks */
/* Callbacks (NULL if unused) */
int (*lputs)(void*, char*);
void (*status)(void*, char*);
void (*started)(void*);
void (*terminated)(void*, int code);
void (*clients)(void*, int active);
void (*thread_up)(void*, BOOL up, BOOL setuid);
void (*socket_open)(void*, BOOL open);
void (*client_on)(void*, BOOL on, int sock, client_t*, BOOL update);
BOOL (*seteuid)(BOOL user);
BOOL (*setuid)(BOOL force);
/* Paths */
char ctrl_dir[128];
char relay_server[128];
char dns_server[128];
......@@ -77,6 +83,8 @@ typedef struct {
char inbound_sound[128];
char outbound_sound[128];
char pop3_sound[128];
/* Misc */
char host_name[128];
BOOL recycle_now;
......
......@@ -99,32 +99,32 @@ static bbs_startup_t* startup=NULL;
static void status(char* str)
{
if(startup!=NULL && startup->status!=NULL)
startup->status(str);
startup