Newer
Older
fprintf(stderr, "No startup structure passed!\n");
return;
}
if(startup->size!=sizeof(ftp_startup_t)) { /* verify size */
sbbs_beep(100,500);
sbbs_beep(300,500);
sbbs_beep(100,500);
fprintf(stderr, "Invalid startup structure!\n");
return;
}
thread_up();
status("Initializing");
#ifdef __unix__ /* Ignore "Broken Pipe" signal */
signal(SIGPIPE,SIG_IGN);
#endif
lprintf("Synchronet FTP Server Version %s%s"
,FTP_VERSION
#ifdef _DEBUG
," Debug"
#else
,""
#endif
);

rswindell
committed
COMPILER_DESC(compiler);
lprintf("Compiled %s %s with %s", __DATE__, __TIME__, compiler);
srand(time(NULL));

rswindell
committed
if(!(startup->options&FTP_OPT_LOCAL_TIMEZONE)) {
if(PUTENV("TZ=UCT0"))
lprintf("!putenv() FAILED");
tzset();
if((t=checktime())!=0) { /* Check binary time */
lprintf("!TIME PROBLEM (%ld)",t);
cleanup(1);
return;
}
}
if(!winsock_startup()) {
cleanup(1);
return;
}
lprintf("Initializing on %.24s with options: %lx"
#ifdef _WIN32
if((socket_mutex=CreateMutex(NULL,FALSE,NULL))==NULL) {
lprintf("!ERROR %d creating socket_mutex", GetLastError());
cleanup(1);
return;
}
/* Initial configuration and load from CNF files */
memset(&scfg, 0, sizeof(scfg));
sprintf(scfg.ctrl_dir, "%.*s",(int)sizeof(scfg.ctrl_dir)-1
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
,startup->ctrl_dir);
lprintf("Loading configuration files from %s", scfg.ctrl_dir);
if(!load_cfg(&scfg, NULL)) {
lprintf("!Failed to load configuration files");
cleanup(1);
return;
}
if(!startup->max_clients) {
startup->max_clients=scfg.sys_nodes;
if(startup->max_clients<10)
startup->max_clients=10;
}
lprintf("Maximum clients: %d",startup->max_clients);
if(!startup->max_inactivity)
startup->max_inactivity=300; /* seconds */
lprintf("Maximum inactivity: %d seconds",startup->max_inactivity);
active_clients=0;
update_clients();
strlwr(scfg.sys_id); /* Use lower-case unix-looking System ID for group name */
for(i=0;i<scfg.total_libs;i++) {
strlwr(scfg.lib[i]->sname);
dotname(scfg.lib[i]->sname,scfg.lib[i]->sname);
}
for(i=0;i<scfg.total_dirs;i++)
strlwr(scfg.dir[i]->code);
/* open a socket and wait for a client */
if((server_socket=open_socket(SOCK_STREAM))==INVALID_SOCKET) {
lprintf("!ERROR %d opening socket", ERROR_VALUE);
cleanup(1);
return;
}
lprintf("%04d FTP socket opened",server_socket);
#if 1
linger.l_onoff=TRUE;
linger.l_linger=5; /* seconds */
if((result=setsockopt(server_socket, SOL_SOCKET, SO_LINGER
,(char *)&linger, sizeof(linger)))!=0) {
lprintf ("%04d !ERROR %d (%d) setting socket options."
,server_socket, result, ERROR_VALUE);
cleanup(1);
return;
}
#endif
/*****************************/
/* Listen for incoming calls */
/*****************************/
memset(&server_addr, 0, sizeof(server_addr));
server_addr.sin_addr.s_addr = htonl(startup->interface_addr);
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(startup->port);
if((result=bind(server_socket, (struct sockaddr *) &server_addr
,sizeof(server_addr)))!=0) {
lprintf("%04d !ERROR %d (%d) binding socket to port %d"
,server_socket, result, ERROR_VALUE,startup->port);
cleanup(1);
return;
}
if((result=listen(server_socket, 1))!= 0) {
lprintf("%04d !ERROR %d (%d) listening on socket"
,server_socket, result, ERROR_VALUE);
cleanup(1);
return;
}
/* signal caller that we've started up successfully */
if(startup->started!=NULL)
startup->started();
lprintf("%04d FTP Server thread started on port %d",server_socket,startup->port);
status(STATUS_WFC);
while(server_socket!=INVALID_SOCKET) {
/* now wait for connection */

rswindell
committed
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
tv.tv_sec=2;
tv.tv_usec=0;
FD_ZERO(&socket_set);
FD_SET(server_socket,&socket_set);
high_socket_set=server_socket+1;
if((i=select(high_socket_set,&socket_set,NULL,NULL,&tv))<1) {
if(i==0) {
mswait(1);
continue;
}
if(ERROR_VALUE==EINTR)
lprintf("0000 FTP Server listening interrupted");
else if(ERROR_VALUE == ENOTSOCK)
lprintf("0000 FTP Server sockets closed");
else
lprintf("0000 !ERROR %d selecting sockets",ERROR_VALUE);
break;
}
client_addr_len = sizeof(client_addr);
client_socket = accept(server_socket, (struct sockaddr *)&client_addr
,&client_addr_len);
if(client_socket == INVALID_SOCKET)
{
lprintf("0000 FTP socket closed while listening");
lprintf("0000 !accept failed (ERROR %d)", ERROR_VALUE);
break;
}
if(startup->socket_open!=NULL)
startup->socket_open(TRUE);
sockets++;
if(active_clients>=startup->max_clients) {
lprintf("%04d !MAXMIMUM CLIENTS (%d) reached, access denied"
,client_socket, startup->max_clients);
sockprintf(client_socket,"421 Maximum active clients reached, please try again later.");
close_socket(&client_socket,__LINE__);
continue;
}
if((ftp=malloc(sizeof(ftp_t)))==NULL) {
lprintf("%04d !ERROR allocating %d bytes of memory for ftp_t"
,client_socket,sizeof(ftp_t));
sockprintf(client_socket,"421 System error, please try again later.");
close_socket(&client_socket,__LINE__);
continue;
}
ftp->socket=client_socket;
ftp->client_addr=client_addr;
_beginthread (ctrl_thread, 0, ftp);
}
if(active_clients) {
lprintf("0000 Waiting for %d active clients to disconnect...", active_clients);
start=time(NULL);
while(active_clients) {
if(time(NULL)-start>TIMEOUT_THREAD_WAIT) {
lprintf("0000 !TIMEOUT waiting for %d active clients ",active_clients);