From 4673fd3ca06010e74f8223486b0f34e58c7b0a7e Mon Sep 17 00:00:00 2001 From: Dennis Friis Date: Tue, 7 Jul 2009 00:52:08 +0200 Subject: Fixes to socket system to make it cope better with fd depletion. Also added timeout for fullconnect, secured map server from stateless connections. --- src/char/char.c | 27 ++++++------- src/char/int_guild.c | 12 +++--- src/char/int_party.c | 4 +- src/char/int_storage.c | 8 ++-- src/char/inter.c | 12 +++--- src/char_sql/char.c | 12 +++--- src/char_sql/int_guild.c | 4 +- src/char_sql/inter.c | 4 +- src/char_sql/itemdb.c | 4 +- src/common/grfio.c | 39 ++++++++++--------- src/common/lock.c | 7 ++-- src/common/socket.c | 65 +++++++++++++++++++++++++++++++ src/common/socket.h | 16 +++++++- src/ladmin/ladmin.c | 11 +++--- src/login/login.c | 40 +++++++++---------- src/login_sql/login.c | 12 +++--- src/map/atcommand.c | 16 ++++---- src/map/battle.c | 5 ++- src/map/chrif.c | 7 ++-- src/map/clif.c | 15 +++++-- src/map/guild.c | 4 +- src/map/itemdb.c | 21 +++++----- src/map/magic-interpreter-parser.c | 2 +- src/map/magic-interpreter.h | 1 + src/map/map.c | 8 ++-- src/map/mob.c | 16 ++++---- src/map/npc.c | 5 ++- src/map/pc.c | 40 +++++++++---------- src/map/script.c | 12 +++--- src/map/skill.c | 17 ++++---- src/tool/adduser.c | 4 +- src/tool/convert.c | 4 +- src/txt-converter/char/char-converter.c | 20 +++++----- src/txt-converter/login/login-converter.c | 12 +++--- 34 files changed, 292 insertions(+), 194 deletions(-) diff --git a/src/char/char.c b/src/char/char.c index 3035ce1..37580b6 100644 --- a/src/char/char.c +++ b/src/char/char.c @@ -122,7 +122,7 @@ int char_log(char *fmt, ...) { va_start(ap, fmt); - logfp = fopen(char_log_filename, "a"); + logfp = fopen_(char_log_filename, "a"); if (logfp) { if (fmt[0] == '\0') // jump a line if no message fprintf(logfp, RETCODE); @@ -132,7 +132,7 @@ int char_log(char *fmt, ...) { sprintf(tmpstr + 19, ".%03d: %s", (int)tv.tv_usec / 1000, fmt); vfprintf(logfp, tmpstr, ap); } - fclose(logfp); + fclose_(logfp); } va_end(ap); @@ -558,7 +558,7 @@ int mmo_char_init(void) { char_num = 0; - fp = fopen(char_txt, "r"); + fp = fopen_(char_txt, "r"); if (fp == NULL) { printf("Characters file not found: %s.\n", char_txt); char_log("Characters file not found: %s." RETCODE, char_txt); @@ -637,7 +637,7 @@ int mmo_char_init(void) { char_log("%s", line); } } - fclose(fp); + fclose_(fp); if (char_num == 0) { printf("mmo_char_init: No character found in %s.\n", char_txt); @@ -1091,9 +1091,9 @@ void create_online_files(void) { } // write files - fp = fopen(online_txt_filename, "w"); + fp = fopen_(online_txt_filename, "w"); if (fp != NULL) { - fp2 = fopen(online_html_filename, "w"); + fp2 = fopen_(online_html_filename, "w"); if (fp2 != NULL) { // get time time(&time_server); // get time in seconds since 1/1/1970 @@ -1256,9 +1256,9 @@ void create_online_files(void) { } fprintf(fp2, " \n"); fprintf(fp2, "\n"); - fclose(fp2); + fclose_(fp2); } - fclose(fp); + fclose_(fp); } return; @@ -2975,7 +2975,8 @@ int send_users_tologin(int tid, unsigned int tick, int id, int data) { int check_connect_login_server(int tid, unsigned int tick, int id, int data) { if (login_fd <= 0 || session[login_fd] == NULL) { printf("Attempt to connect to login-server...\n"); - login_fd = make_connection(login_ip, login_port); + if ((login_fd = make_connection(login_ip, login_port)) < 0) + return 0; session[login_fd]->func_parse = parse_tologin; realloc_fifo(login_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); WFIFOW(login_fd,0) = 0x2710; @@ -3027,7 +3028,7 @@ int lan_config_read(const char *lancfgName) { for(j = 0; j < 4; j++) subnetmaski[j] = 255; - fp = fopen(lancfgName, "r"); + fp = fopen_(lancfgName, "r"); if (fp == NULL) { printf("LAN support configuration file not found: %s\n", lancfgName); @@ -3079,7 +3080,7 @@ int lan_config_read(const char *lancfgName) { printf("Sub-network mask of the map-server: %d.%d.%d.%d.\n", subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); } } - fclose(fp); + fclose_(fp); // sub-network check of the map-server { @@ -3101,7 +3102,7 @@ int lan_config_read(const char *lancfgName) { int char_config_read(const char *cfgName) { struct hostent *h = NULL; char line[1024], w1[1024], w2[1024]; - FILE *fp = fopen(cfgName, "r"); + FILE *fp = fopen_(cfgName, "r"); if (fp == NULL) { printf("Configuration file not found: %s.\n", cfgName); @@ -3231,7 +3232,7 @@ int char_config_read(const char *cfgName) { char_config_read(w2); } } - fclose(fp); + fclose_(fp); return 0; } diff --git a/src/char/int_guild.c b/src/char/int_guild.c index 665e017..f84964c 100644 --- a/src/char/int_guild.c +++ b/src/char/int_guild.c @@ -357,7 +357,7 @@ int inter_guild_readdb() { FILE *fp; char line[1024]; - fp = fopen("db/exp_guild.txt", "r"); + fp = fopen_("db/exp_guild.txt", "r"); if (fp == NULL) { printf("can't read db/exp_guild.txt\n"); return 1; @@ -369,7 +369,7 @@ int inter_guild_readdb() { guild_exp[i] = atoi(line); i++; } - fclose(fp); + fclose_(fp); return 0; } @@ -387,7 +387,7 @@ int inter_guild_init() { guild_db = numdb_init(); castle_db = numdb_init(); - if ((fp = fopen(guild_txt,"r")) == NULL) + if ((fp = fopen_(guild_txt,"r")) == NULL) return 1; while(fgets(line, sizeof(line)-1, fp)) { j = 0; @@ -414,12 +414,12 @@ int inter_guild_init() { } c++; } - fclose(fp); + fclose_(fp); // printf("int_guild: %s read done (%d guilds)\n", guild_txt, c); c = 0;//カウンタ初期化 - if ((fp = fopen(castle_txt, "r")) == NULL) { + if ((fp = fopen_(castle_txt, "r")) == NULL) { return 1; } @@ -481,7 +481,7 @@ int inter_guild_init() { return 0; } - fclose(fp); + fclose_(fp); return 0; } diff --git a/src/char/int_party.c b/src/char/int_party.c index 0fd58fa..f0b06b6 100644 --- a/src/char/int_party.c +++ b/src/char/int_party.c @@ -84,7 +84,7 @@ int inter_party_init() { party_db = numdb_init(); - if ((fp = fopen(party_txt, "r")) == NULL) + if ((fp = fopen_(party_txt, "r")) == NULL) return 1; while(fgets(line, sizeof(line) - 1, fp)) { @@ -111,7 +111,7 @@ int inter_party_init() { } c++; } - fclose(fp); + fclose_(fp); // printf("int_party: %s read done (%d parties)\n", party_txt, c); return 0; diff --git a/src/char/int_storage.c b/src/char/int_storage.c index 8f7f9d7..2612e17 100644 --- a/src/char/int_storage.c +++ b/src/char/int_storage.c @@ -240,7 +240,7 @@ int inter_storage_init() storage_db = numdb_init(); - fp=fopen(storage_txt,"r"); + fp=fopen_(storage_txt,"r"); if(fp==NULL){ printf("cant't read : %s\n",storage_txt); return 1; @@ -263,12 +263,12 @@ int inter_storage_init() } c++; } - fclose(fp); + fclose_(fp); c = 0; guild_storage_db = numdb_init(); - fp=fopen(guild_storage_txt,"r"); + fp=fopen_(guild_storage_txt,"r"); if(fp==NULL){ printf("cant't read : %s\n",guild_storage_txt); return 1; @@ -291,7 +291,7 @@ int inter_storage_init() } c++; } - fclose(fp); + fclose_(fp); return 0; } diff --git a/src/char/inter.c b/src/char/inter.c index 98e7ad0..6e50432 100644 --- a/src/char/inter.c +++ b/src/char/inter.c @@ -108,7 +108,7 @@ int inter_accreg_init() { accreg_db = numdb_init(); - if( (fp = fopen(accreg_txt, "r")) == NULL) + if( (fp = fopen_(accreg_txt, "r")) == NULL) return 1; while(fgets(line, sizeof(line)-1, fp)){ line[sizeof(line)-1] = '\0'; @@ -126,7 +126,7 @@ int inter_accreg_init() { } c++; } - fclose(fp); + fclose_(fp); // printf("inter: %s read done (%d)\n", accreg_txt, c); return 0; @@ -173,7 +173,7 @@ int inter_config_read(const char *cfgName) { char line[1024], w1[1024], w2[1024]; FILE *fp; - fp = fopen(cfgName, "r"); + fp = fopen_(cfgName, "r"); if (fp == NULL) { printf("file not found: %s\n", cfgName); return 1; @@ -208,7 +208,7 @@ int inter_config_read(const char *cfgName) { inter_config_read(w2); } } - fclose(fp); + fclose_(fp); return 0; } @@ -219,10 +219,10 @@ int inter_log(char *fmt,...) { va_list ap; va_start(ap,fmt); - logfp = fopen(inter_log_filename, "a"); + logfp = fopen_(inter_log_filename, "a"); if (logfp) { vfprintf(logfp, fmt, ap); - fclose(logfp); + fclose_(logfp); } va_end(ap); diff --git a/src/char_sql/char.c b/src/char_sql/char.c index 11b6a49..789a7c7 100644 --- a/src/char_sql/char.c +++ b/src/char_sql/char.c @@ -2586,7 +2586,7 @@ int char_lan_config_read(const char *lancfgName){ FILE *fp; struct hostent * h = NULL; - if ((fp = fopen(lancfgName, "r")) == NULL) { + if ((fp = fopen_(lancfgName, "r")) == NULL) { printf("file not found: %s\n", lancfgName); return 1; } @@ -2619,7 +2619,7 @@ int char_lan_config_read(const char *lancfgName){ printf("set subnetmask : %s\n", w2); } } - fclose(fp); + fclose_(fp); printf("End reading of Lan Support configuration file\n"); return 0; @@ -2667,7 +2667,7 @@ void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */ printf("reading configure: %s\n", cfgName); - if ((fp = fopen(cfgName, "r")) == NULL) { + if ((fp = fopen_(cfgName, "r")) == NULL) { printf("file not found: %s\n", cfgName); exit(1); } @@ -2737,7 +2737,7 @@ void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */ printf("set lowest_gm_level : %s\n",w2); } } - fclose(fp); + fclose_(fp); printf("reading configure done.....\n"); } @@ -2746,7 +2746,7 @@ int char_config_read(const char *cfgName) { char line[1024], w1[1024], w2[1024]; FILE *fp; - if ((fp = fopen(cfgName, "r")) == NULL) { + if ((fp = fopen_(cfgName, "r")) == NULL) { printf("Configuration file not found: %s.\n", cfgName); exit(1); } @@ -2849,7 +2849,7 @@ int char_config_read(const char *cfgName) { char_config_read(w2); } } - fclose(fp); + fclose_(fp); //Read ItemDB do_init_itemdb(); diff --git a/src/char_sql/int_guild.c b/src/char_sql/int_guild.c index 1983896..b9a6e3b 100644 --- a/src/char_sql/int_guild.c +++ b/src/char_sql/int_guild.c @@ -566,7 +566,7 @@ int inter_guild_readdb() char line[1024]; for (i=0;i<100;i++) guild_exp[i]=0; - fp=fopen("db/exp_guild.txt","r"); + fp=fopen_("db/exp_guild.txt","r"); if(fp==NULL){ printf("can't read db/exp_guild.txt\n"); return 1; @@ -578,7 +578,7 @@ int inter_guild_readdb() guild_exp[i]=atoi(line); i++; } - fclose(fp); + fclose_(fp); return 0; } diff --git a/src/char_sql/inter.c b/src/char_sql/inter.c index c8fa9b4..d1ee194 100644 --- a/src/char_sql/inter.c +++ b/src/char_sql/inter.c @@ -157,7 +157,7 @@ int inter_config_read(const char *cfgName) { char line[1024], w1[1024], w2[1024]; FILE *fp; - fp=fopen(cfgName,"r"); + fp=fopen_(cfgName,"r"); if(fp==NULL){ printf("file not found: %s\n", cfgName); return 1; @@ -221,7 +221,7 @@ int inter_config_read(const char *cfgName) { printf ("set login_server_db : %s\n",w2); } } - fclose(fp); + fclose_(fp); printf ("success reading interserver configuration\n"); diff --git a/src/char_sql/itemdb.c b/src/char_sql/itemdb.c index 0bed07c..85c4929 100644 --- a/src/char_sql/itemdb.c +++ b/src/char_sql/itemdb.c @@ -105,7 +105,7 @@ static int itemdb_readdb(void) char *str[32],*p,*np; struct item_data *id; - fp=fopen("db/item_db.txt","r"); + fp=fopen_("db/item_db.txt","r"); if(fp==NULL){ printf("can't read db/item_db.txt\n"); exit(1); @@ -134,7 +134,7 @@ static int itemdb_readdb(void) id->type=atoi(str[3]); } - fclose(fp); + fclose_(fp); printf("read db/item_db.txt done (count=%d)\n",ln); return 0; } diff --git a/src/common/grfio.c b/src/common/grfio.c index 08a8b2a..b5f380c 100644 --- a/src/common/grfio.c +++ b/src/common/grfio.c @@ -30,6 +30,7 @@ #include "utils.h" #include "grfio.h" #include "mmo.h" +#include "socket.h" #ifdef MEMWATCH #include "memwatch.h" @@ -414,7 +415,7 @@ char* grfio_resnametable(char* fname, char *lfname) for(p=&restable[0];*p!=0;p++) if (*p=='\\') *p = '/'; - fp = fopen(restable,"rb"); + fp = fopen_(restable,"rb"); if(fp==NULL) { printf("%s not found\n",restable); exit(1); // 1:not found error @@ -423,11 +424,11 @@ char* grfio_resnametable(char* fname, char *lfname) while(fgets(line,508,fp)){ if((sscanf(line,"%[^#]#%[^#]#",w1,w2)==2) && (sscanf(fname,"%*5s%s",lfname)==1) && (!strcmpi(w1,lfname))){ sprintf(lfname,"data\\%s",w2); - fclose(fp); + fclose_(fp); return lfname; } } - fclose(fp); + fclose_(fp); return fname; } @@ -493,7 +494,7 @@ void* grfio_reads(char *fname, int *size) for(p=&lfname[0];*p!=0;p++) if (*p=='\\') *p = '/'; // * At the time of Unix - in = fopen(lfname,"rb"); + in = fopen_(lfname,"rb"); if(in!=NULL) { if (entry!=NULL && entry->gentry==0) { lentry.declen=entry->declen; @@ -508,7 +509,7 @@ void* grfio_reads(char *fname, int *size) goto errret; } fread(buf2,1,lentry.declen,in); - fclose(in); in = NULL; + fclose_(in); in = NULL; strncpy( lentry.fn, fname, sizeof(lentry.fn)-1 ); lentry.gentry = 0; // 0:LocalFile entry = filelist_modify(&lentry); @@ -530,7 +531,7 @@ void* grfio_reads(char *fname, int *size) goto errret; } gfname = gentry_table[entry->gentry-1]; - in = fopen(gfname,"rb"); + in = fopen_(gfname,"rb"); if(in==NULL) { printf("%s not found\n",gfname); //goto errret; @@ -539,7 +540,7 @@ void* grfio_reads(char *fname, int *size) } fseek(in,entry->srcpos,0); fread(buf,1,entry->srclen_aligned,in); - fclose(in); + fclose_(in); buf2=calloc(entry->declen+1024, 1); if (buf2==NULL) { printf("file decode memory allocate error\n"); @@ -567,7 +568,7 @@ void* grfio_reads(char *fname, int *size) errret: if (buf!=NULL) free(buf); if (buf2!=NULL) free(buf2); - if (in!=NULL) fclose(in); + if (in!=NULL) fclose_(in); exit(1); //return NULL; } @@ -609,7 +610,7 @@ static int grfio_entryread(char *gfname,int gentry) unsigned char *fname; unsigned char *grf_filelist; - fp = fopen(gfname,"rb"); + fp = fopen_(gfname,"rb"); if(fp==NULL) { printf("%s not found\n",gfname); return 1; // 1:not found error @@ -620,7 +621,7 @@ static int grfio_entryread(char *gfname,int gentry) fseek(fp,0,0); // SEEK_SET fread(grf_header,1,0x2e,fp); if(strcmp(grf_header,"Master of Magic") || fseek(fp,getlong(grf_header+0x1e),1)){ // SEEK_CUR - fclose(fp); + fclose_(fp); printf("%s read error\n",gfname); return 2; // 2:file format error } @@ -631,12 +632,12 @@ static int grfio_entryread(char *gfname,int gentry) list_size = grf_size-ftell(fp); grf_filelist = calloc(list_size, 1); if(grf_filelist==NULL){ - fclose(fp); + fclose_(fp); printf("out of memory : grf_filelist\n"); return 3; // 3:memory alloc error } fread(grf_filelist,1,list_size,fp); - fclose(fp); + fclose_(fp); entrys = getlong(grf_header+0x26) - getlong(grf_header+0x22) - 7; @@ -699,26 +700,26 @@ static int grfio_entryread(char *gfname,int gentry) eSize = getlong(eheader+4); // Extend Size if (rSize > grf_size-ftell(fp)) { - fclose(fp); + fclose_(fp); printf("Illegal data format : grf compress entry size\n"); return 4; } rBuf = calloc( rSize , 1); // Get a Read Size if (rBuf==NULL) { - fclose(fp); + fclose_(fp); printf("out of memory : grf compress entry table buffer\n"); return 3; } grf_filelist = calloc( eSize , 1); // Get a Extend Size if (grf_filelist==NULL) { free(rBuf); - fclose(fp); + fclose_(fp); printf("out of memory : grf extract entry table buffer\n"); return 3; } fread(rBuf,1,rSize,fp); - fclose(fp); + fclose_(fp); decode_zip(grf_filelist,&eSize,rBuf,rSize); // Decode function list_size = eSize; free(rBuf); @@ -767,7 +768,7 @@ static int grfio_entryread(char *gfname,int gentry) free(grf_filelist); } else { //****** Grf Other version ****** - fclose(fp); + fclose_(fp); printf("not support grf versions : %04x\n",getlong(grf_header+0x2a)); return 4; } @@ -903,7 +904,7 @@ void grfio_init(char *fname) char line[1024], w1[1024], w2[1024]; int result = 0, result2 = 0, result3 = 0; - data_conf = fopen(fname, "r"); + data_conf = fopen_(fname, "r"); // It will read, if there is grf-files.txt. if (data_conf) { @@ -920,7 +921,7 @@ void grfio_init(char *fname) } } - fclose(data_conf); + fclose_(data_conf); printf("read %s done\n",fname); } // end of reading grf-files.txt diff --git a/src/common/lock.c b/src/common/lock.c index 9a2205b..67001f9 100644 --- a/src/common/lock.c +++ b/src/common/lock.c @@ -1,6 +1,7 @@ #include #include "lock.h" +#include "socket.h" // 書き込みファイルの保護処理 // (書き込みが終わるまで、旧ファイルを保管しておく) @@ -14,9 +15,9 @@ FILE* lock_fopen(const char* filename,int *info) { // 安全なファイル名を得る(手抜き) do { sprintf(newfile,"%s_%04d.tmp",filename,++no); - } while((fp = fopen(newfile,"r")) && (fclose(fp), no<9999) ); + } while((fp = fopen_(newfile,"r")) && (fclose_(fp), no<9999) ); *info = no; - return fopen(newfile,"w"); + return fopen_(newfile,"w"); } // 旧ファイルを削除&新ファイルをリネーム @@ -24,7 +25,7 @@ int lock_fclose(FILE *fp,const char* filename,int *info) { int ret = 0; char newfile[512]; if(fp != NULL) { - ret = fclose(fp); + ret = fclose_(fp); sprintf(newfile,"%s_%04d.tmp",filename,*info); remove(filename); // このタイミングで落ちると最悪。 diff --git a/src/common/socket.c b/src/common/socket.c index f968373..886072e 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -4,6 +4,7 @@ #include #include #include +#include #ifdef LCCWIN32 #define WIN32_LEAN_AND_MEAN @@ -30,6 +31,7 @@ fd_set readfds; int fd_max; +int currentuse; int rfifo_size = 65536; int wfifo_size = 65536; @@ -74,6 +76,7 @@ static int recv_to_fifo(int fd) //{ int i; printf("recv %d : ",fd); for(i=0;irdata_size+i)); } printf("\n");} if(len>0){ session[fd]->rdata_size+=len; + if (!session[fd]->connected) session[fd]->connected = 1; } else if(len<=0){ // value of connection is not necessary the same // if (fd == 4) // Removed [Yor] @@ -117,6 +120,7 @@ static int send_from_fifo(int fd) } else { session[fd]->wdata_size=0; } + if (!session[fd]->connected) session[fd]->connected = 1; } else { printf("set eof :%d\n",fd); session[fd]->eof=1; @@ -146,6 +150,8 @@ static int connect_client(int listen_fd) //printf("connect_client : %d\n",listen_fd); + printf("used: %d, max FDs: %d, SOFT: %d\n", currentuse, FD_SETSIZE, SOFT_LIMIT); + len = sizeof(client_address); fd = accept(listen_fd,(struct sockaddr*)&client_address,&len); @@ -155,6 +161,11 @@ static int connect_client(int listen_fd) perror("accept"); return -1; } + if (!free_fds()) { // gracefully end the connecting if no free FD + printf("softlimit reached, disconnecting : %d\n", fd); + delete_session(fd); + return -1; + } // setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,NULL,0); setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof yes); // reuse fix @@ -186,6 +197,10 @@ static int connect_client(int listen_fd) session[fd]->func_send = send_from_fifo; session[fd]->func_parse = default_func_parse; session[fd]->client_addr = client_address; + session[fd]->created = time(NULL); + session[fd]->connected = 0; + + currentuse++; //printf("new_session : %d %d\n",fd,session[fd]->eof); return fd; @@ -200,6 +215,10 @@ int make_listen_port(int port) fd = socket( AF_INET, SOCK_STREAM, 0 ); if(fd_max<=fd) fd_max=fd+1; + else if (fd == -1) { + perror("connect"); + return -1; + } #ifdef LCCWIN32 { @@ -244,7 +263,10 @@ int make_listen_port(int port) } memset(session[fd],0,sizeof(*session[fd])); session[fd]->func_recv = connect_client; + session[fd]->created = time(NULL); + session[fd]->connected = 1; + currentuse++; return fd; } @@ -257,6 +279,11 @@ int make_connection(long ip,int port) fd = socket( AF_INET, SOCK_STREAM, 0 ); if(fd_max<=fd) fd_max=fd+1; + else if (fd == -1) { + perror("socket"); + return -1; + } + // setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,NULL,0); setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof yes); // reuse fix #ifdef SO_REUSEPORT @@ -292,7 +319,10 @@ int make_connection(long ip,int port) session[fd]->func_recv = recv_to_fifo; session[fd]->func_send = send_from_fifo; session[fd]->func_parse = default_func_parse; + session[fd]->created = time(NULL); + session[fd]->connected = 1; + currentuse++; return fd; } @@ -311,6 +341,13 @@ int delete_session(int fd) free(session[fd]); } session[fd]=NULL; + shutdown(fd, SHUT_RDWR); + close(fd); + currentuse--; + if (currentuse<0) { + printf("delete_session: current sessions negative!\n"); + currentuse=0; + } //printf("delete_session:%d\n",fd); return 0; } @@ -390,6 +427,10 @@ int do_parsepacket(void) for(i=0;iconnected && time(NULL)-session[i]->created > CONNECT_TIMEOUT) { + printf("Session #%d timed out\n", i); + session[i]->eof = 1; + } if(session[i]->rdata_size==0 && session[i]->eof==0) continue; if(session[i]->func_parse){ @@ -405,6 +446,7 @@ int do_parsepacket(void) void do_socket(void) { FD_ZERO(&readfds); + currentuse = 2; } int RFIFOSKIP(int fd,int len) @@ -438,3 +480,26 @@ int Net_Init(void) return(0); } +int fclose_(FILE *fp) +{ + int res = fclose(fp); + if (res == 0) + currentuse--; +// printf("file closed: used: %d\n",currentuse); + return res; +} + +FILE *fopen_(const char *path, const char *mode) +{ + FILE *f = fopen(path, mode); + if (f != NULL) + currentuse++; +// printf("file opened: used: %d\n",currentuse); + return f; +} + +int free_fds() +{ + return (currentuse+1 < SOFT_LIMIT) ? 1 : 0; +} + diff --git a/src/common/socket.h b/src/common/socket.h index fe06e40..b67e660 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -12,6 +12,7 @@ #include #include #endif +#include // define declaration @@ -44,13 +45,14 @@ #define FD_SETSIZE 4096 #endif // __INTERIX - /* Removed Cygwin FD_SETSIZE declarations, now are directly passed on to the compiler through Makefile [Valaris] */ // Struct declaration struct socket_data{ int eof; + time_t created; + int connected; unsigned char *rdata,*wdata; int max_rdata,max_wdata; int rdata_size,wdata_size; @@ -71,6 +73,13 @@ struct socket_data{ #endif +// save file descriptors for important stuff +#define SOFT_LIMIT (FD_SETSIZE - 50) + +// socket timeout to establish a full connection in seconds +#define CONNECT_TIMEOUT 15 + + extern struct socket_data *session[FD_SETSIZE]; extern int rfifo_size,wfifo_size; @@ -93,4 +102,9 @@ void set_defaultparse(int (*defaultparse)(int)); int Net_Init(void); +int fclose_(FILE *fp); +FILE *fopen_(const char *path, const char *mode); + +int free_fds(); + #endif // _SOCKET_H_ diff --git a/src/ladmin/ladmin.c b/src/ladmin/ladmin.c index 3cea295..810be57 100644 --- a/src/ladmin/ladmin.c +++ b/src/ladmin/ladmin.c @@ -263,7 +263,7 @@ int ladmin_log(char *fmt, ...) { va_start(ap, fmt); - logfp = fopen(ladmin_log_filename, "a"); + logfp = fopen_(ladmin_log_filename, "a"); if (logfp) { if (fmt[0] == '\0') // jump a line if no message fprintf(logfp, RETCODE); @@ -273,7 +273,7 @@ int ladmin_log(char *fmt, ...) { sprintf(tmpstr + strlen(tmpstr), ".%03d: %s", (int)tv.tv_usec / 1000, fmt); vfprintf(logfp, tmpstr, ap); } - fclose(logfp); + fclose_(logfp); } va_end(ap); @@ -4228,7 +4228,8 @@ int Connect_login_server() { ladmin_log("Attempt to connect to login-server..." RETCODE); } - login_fd = make_connection(login_ip, loginserverport); + if ((login_fd = make_connection(login_ip, loginserverport)) < 0) + return 0; #ifdef PASSWORDENC if (passenc == 0) { @@ -4284,7 +4285,7 @@ int ladmin_config_read(const char *cfgName) { char line[1024], w1[1024], w2[1024]; FILE *fp; - fp = fopen(cfgName, "r"); + fp = fopen_(cfgName, "r"); if (fp == NULL) { if (defaultlanguage == 'F') { printf("\033[0mFichier de configuration (%s) non trouv.\n", cfgName); @@ -4356,7 +4357,7 @@ int ladmin_config_read(const char *cfgName) { } } } - fclose(fp); + fclose_(fp); login_ip = inet_addr(loginserverip); diff --git a/src/login/login.c b/src/login/login.c index 40da193..6c4ca82 100644 --- a/src/login/login.c +++ b/src/login/login.c @@ -144,7 +144,7 @@ int login_log(char *fmt, ...) { va_start(ap, fmt); - logfp = fopen(login_log_filename, "a"); + logfp = fopen_(login_log_filename, "a"); if (logfp) { if (fmt[0] == '\0') // jump a line if no message fprintf(logfp, RETCODE); @@ -154,7 +154,7 @@ int login_log(char *fmt, ...) { sprintf(tmpstr + strlen(tmpstr), ".%03d: %s", (int)tv.tv_usec / 1000, fmt); vfprintf(logfp, tmpstr, ap); } - fclose(logfp); + fclose_(logfp); } va_end(ap); @@ -194,7 +194,7 @@ int read_gm_account() { else creation_time_GM_account_file = file_stat.st_mtime; - if ((fp = fopen(GM_account_filename, "r")) == NULL) { + if ((fp = fopen_(GM_account_filename, "r")) == NULL) { printf("read_gm_account: GM accounts file [%s] not found.\n", GM_account_filename); printf(" Actually, there is no GM accounts on the server.\n"); login_log("read_gm_account: GM accounts file [%s] not found." RETCODE, GM_account_filename); @@ -239,7 +239,7 @@ int read_gm_account() { } } } - fclose(fp); + fclose_(fp); printf("read_gm_account: file '%s' readed (%d GM accounts found).\n", GM_account_filename, c); login_log("read_gm_account: file '%s' readed (%d GM accounts found)." RETCODE, GM_account_filename, c); @@ -482,7 +482,7 @@ int mmo_auth_init(void) { auth_dat = calloc(sizeof(struct auth_dat) * 256, 1); auth_max = 256; - fp = fopen(account_filename, "r"); + fp = fopen_(account_filename, "r"); if (fp == NULL) { // no account file -> no account -> no login, including char-server (ERROR) printf("\033[1;31mmmo_auth_init: Accounts file [%s] not found.\033[0m\n", account_filename); @@ -744,7 +744,7 @@ int mmo_auth_init(void) { account_id_count = account_id; } } - fclose(fp); + fclose_(fp); if (auth_num == 0) { printf("mmo_auth_init: No account found in %s.\n", account_filename); @@ -1374,13 +1374,13 @@ int parse_fromchar(int fd) { // if we autorise creation if (level_new_gm > 0) { // if we can open the file to add the new GM - if ((fp = fopen(GM_account_filename, "a")) != NULL) { + if ((fp = fopen_(GM_account_filename, "a")) != NULL) { char tmpstr[24]; struct timeval tv; gettimeofday(&tv, NULL); strftime(tmpstr, 23, date_format, gmtime(&(tv.tv_sec))); fprintf(fp, RETCODE "// %s: @GM command on account %d" RETCODE "%d %d" RETCODE, tmpstr, acc, acc, level_new_gm); - fclose(fp); + fclose_(fp); WBUFL(buf,6) = level_new_gm; read_gm_account(); send_GM_accounts(); @@ -1723,7 +1723,7 @@ int parse_fromchar(int fd) { FILE *logfp; char tmpstr[24]; struct timeval tv; - logfp = fopen(login_log_unknown_packets_filename, "a"); + logfp = fopen_(login_log_unknown_packets_filename, "a"); if (logfp) { gettimeofday(&tv, NULL); strftime(tmpstr, 23, date_format, gmtime(&(tv.tv_sec))); @@ -1756,7 +1756,7 @@ int parse_fromchar(int fd) { fprintf(logfp, " %s" RETCODE, tmpstr); } fprintf(logfp, RETCODE); - fclose(logfp); + fclose_(logfp); } } printf("parse_fromchar: Unknown packet 0x%x (from a char-server)! -> disconnection.\n", RFIFOW(fd,0)); @@ -2180,7 +2180,7 @@ int parse_admin(int fd) { char tmpstr[24]; struct timeval tv; if ((fp2 = lock_fopen(GM_account_filename, &lock)) != NULL) { - if ((fp = fopen(GM_account_filename, "r")) != NULL) { + if ((fp = fopen_(GM_account_filename, "r")) != NULL) { gettimeofday(&tv, NULL); strftime(tmpstr, 23, date_format, gmtime(&(tv.tv_sec))); modify_flag = 0; @@ -2206,7 +2206,7 @@ int parse_admin(int fd) { } if (modify_flag == 0) fprintf(fp2, "// %s: 'ladmin' GM level on account %d '%s' (previous level: 0)" RETCODE "%d %d" RETCODE, tmpstr, acc, auth_dat[i].userid, acc, new_gm_level); - fclose(fp); + fclose_(fp); } else { login_log("'ladmin': Attempt to modify of a GM level - impossible to read GM accounts file (account: %s (%d), received GM level: %d, ip: %s)" RETCODE, auth_dat[i].userid, acc, (int)new_gm_level, ip); @@ -2691,7 +2691,7 @@ int parse_admin(int fd) { FILE *logfp; char tmpstr[24]; struct timeval tv; - logfp = fopen(login_log_unknown_packets_filename, "a"); + logfp = fopen_(login_log_unknown_packets_filename, "a"); if (logfp) { gettimeofday(&tv, NULL); strftime(tmpstr, 23, date_format, gmtime(&(tv.tv_sec))); @@ -2724,7 +2724,7 @@ int parse_admin(int fd) { fprintf(logfp, " %s" RETCODE, tmpstr); } fprintf(logfp, RETCODE); - fclose(logfp); + fclose_(logfp); } } login_log("'ladmin': End of connection, unknown packet (ip: %s)" RETCODE, ip); @@ -3150,7 +3150,7 @@ int parse_login(int fd) { FILE *logfp; char tmpstr[24]; struct timeval tv; - logfp = fopen(login_log_unknown_packets_filename, "a"); + logfp = fopen_(login_log_unknown_packets_filename, "a"); if (logfp) { gettimeofday(&tv, NULL); strftime(tmpstr, 23, date_format, gmtime(&(tv.tv_sec))); @@ -3183,7 +3183,7 @@ int parse_login(int fd) { fprintf(logfp, " %s" RETCODE, tmpstr); } fprintf(logfp, RETCODE); - fclose(logfp); + fclose_(logfp); } } login_log("End of connection, unknown packet (ip: %s)" RETCODE, ip); @@ -3226,7 +3226,7 @@ int login_lan_config_read(const char *lancfgName) { for(j = 0; j < 4; j++) subnetmaski[j] = 255; - fp = fopen(lancfgName, "r"); + fp = fopen_(lancfgName, "r"); if (fp == NULL) { printf("***WARNING: LAN Support configuration file is not found: %s\n", lancfgName); @@ -3278,7 +3278,7 @@ int login_lan_config_read(const char *lancfgName) { printf("Sub-network mask of the char-server: %d.%d.%d.%d.\n", subnetmaski[0], subnetmaski[1], subnetmaski[2], subnetmaski[3]); } } - fclose(fp); + fclose_(fp); // log the LAN configuration login_log("The LAN configuration of the server is set:" RETCODE); @@ -3311,7 +3311,7 @@ int login_config_read(const char *cfgName) { char line[1024], w1[1024], w2[1024]; FILE *fp; - fp = fopen(cfgName, "r"); + fp = fopen_(cfgName, "r"); if (fp == NULL) { printf("Configuration file (%s) not found.\n", cfgName); return 1; @@ -3483,7 +3483,7 @@ int login_config_read(const char *cfgName) { } } } - fclose(fp); + fclose_(fp); printf("---End reading of Login Server configuration file.\n"); diff --git a/src/login_sql/login.c b/src/login_sql/login.c index 980e454..a46416c 100644 --- a/src/login_sql/login.c +++ b/src/login_sql/login.c @@ -1399,7 +1399,7 @@ int login_lan_config_read(const char *lancfgName){ char line[1024], w1[1024], w2[1024]; FILE *fp; - fp=fopen(lancfgName, "r"); + fp=fopen_(lancfgName, "r"); if (fp == NULL) { printf("file not found: %s\n", lancfgName); @@ -1427,7 +1427,7 @@ int login_lan_config_read(const char *lancfgName){ printf ("set subnetmask : %s\n",w2); } } - fclose(fp); + fclose_(fp); { unsigned int a0, a1, a2, a3; @@ -1466,7 +1466,7 @@ int login_config_read(const char *cfgName){ char line[1024], w1[1024], w2[1024]; FILE *fp; - fp=fopen(cfgName,"r"); + fp=fopen_(cfgName,"r"); if(fp==NULL){ printf("Configuration file (%s) not found.\n", cfgName); @@ -1557,7 +1557,7 @@ int login_config_read(const char *cfgName){ check_ip_flag = config_switch(w2); } } - fclose(fp); + fclose_(fp); printf ("End reading configuration...\n"); return 0; } @@ -1566,7 +1566,7 @@ void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */ int i; char line[1024], w1[1024], w2[1024]; printf("reading configure: %s\n", cfgName); - FILE *fp=fopen(cfgName,"r"); + FILE *fp=fopen_(cfgName,"r"); if(fp==NULL){ printf("file not found: %s\n",cfgName); exit(1); @@ -1619,7 +1619,7 @@ void sql_config_read(const char *cfgName){ /* Kalaspuff, to get login_db */ strcpy(loginlog_db, w2); } } - fclose(fp); + fclose_(fp); printf("reading configure done.....\n"); } diff --git a/src/map/atcommand.c b/src/map/atcommand.c index 6536e00..eb37561 100644 --- a/src/map/atcommand.c +++ b/src/map/atcommand.c @@ -574,9 +574,9 @@ void log_atcommand(struct map_session_data *sd, const char *fmt, ...) sprintf(fullname, "%s.%04d-%02d", gm_logfile_name, year, month); if (gm_logfile) - fclose(gm_logfile); + fclose_(gm_logfile); - gm_logfile = fopen(fullname, "a"); + gm_logfile = fopen_(fullname, "a"); free(fullname); if (!gm_logfile) { @@ -729,7 +729,7 @@ int msg_config_read(const char *cfgName) { char line[1024], w1[1024], w2[1024]; FILE *fp; - if ((fp = fopen(cfgName, "r")) == NULL) { + if ((fp = fopen_(cfgName, "r")) == NULL) { printf("Messages file not found: %s\n", cfgName); return 1; } @@ -748,7 +748,7 @@ int msg_config_read(const char *cfgName) { } } } - fclose(fp); + fclose_(fp); return 0; } @@ -776,7 +776,7 @@ int atcommand_config_read(const char *cfgName) { AtCommandInfo* p; FILE* fp; - if ((fp = fopen(cfgName, "r")) == NULL) { + if ((fp = fopen_(cfgName, "r")) == NULL) { printf("At commands configuration file not found: %s\n", cfgName); return 1; } @@ -803,7 +803,7 @@ int atcommand_config_read(const char *cfgName) { w2[0] != '%') // symbol of party chat speaking command_symbol = w2[0]; } - fclose(fp); + fclose_(fp); return 0; } @@ -1934,7 +1934,7 @@ int atcommand_help( memset(buf, '\0', sizeof(buf)); - if ((fp = fopen(help_txt, "r")) != NULL) { + if ((fp = fopen_(help_txt, "r")) != NULL) { clif_displaymessage(fd, msg_table[26]); // Help commands: gm_level = pc_isGM(sd); while(fgets(buf, sizeof(buf) - 1, fp) != NULL) { @@ -1951,7 +1951,7 @@ int atcommand_help( else if (gm_level >= atoi(w1)) clif_displaymessage(fd, w2); } - fclose(fp); + fclose_(fp); } else { clif_displaymessage(fd, msg_table[27]); // File help.txt not found. return -1; diff --git a/src/map/battle.c b/src/map/battle.c index 94d64de..c9f1b59 100644 --- a/src/map/battle.c +++ b/src/map/battle.c @@ -17,6 +17,7 @@ #include "mob.h" #include "pc.h" #include "skill.h" +#include "../common/socket.h" #ifdef MEMWATCH #include "memwatch.h" @@ -4527,7 +4528,7 @@ int battle_config_read(const char *cfgName) battle_config.trade_spam_warn = 8; } - fp = fopen(cfgName,"r"); + fp = fopen_(cfgName,"r"); if (fp == NULL) { printf("file not found: %s\n", cfgName); return 1; @@ -4750,7 +4751,7 @@ int battle_config_read(const char *cfgName) if (strcmpi(w1, "import") == 0) battle_config_read(w2); } - fclose(fp); + fclose_(fp); if (--count == 0) { if(battle_config.flooritem_lifetime < 1000) diff --git a/src/map/chrif.c b/src/map/chrif.c index cc9437a..4318d1a 100644 --- a/src/map/chrif.c +++ b/src/map/chrif.c @@ -864,7 +864,7 @@ int chrif_reloadGMdb(void) WFIFOW(char_fd,4) = job_rate; WFIFOW(char_fd,6) = drop_rate; - if ((fp = fopen(motd_txt, "r")) != NULL) { + if ((fp = fopen_(motd_txt, "r")) != NULL) { if (fgets(buf, 250, fp) != NULL) { for(i = 0; buf[i]; i++) { if (buf[i] == '\r' || buf[i] == '\n') { @@ -875,7 +875,7 @@ int chrif_reloadGMdb(void) WFIFOW(char_fd,8) = sizeof(buf) + 10; memcpy(WFIFOP(char_fd,10), buf, sizeof(buf)); } - fclose(fp); + fclose_(fp); } else { WFIFOW(char_fd,8) = sizeof(buf) + 10; memcpy(WFIFOP(char_fd,10), buf, sizeof(buf)); @@ -1113,7 +1113,8 @@ int check_connect_char_server(int tid, unsigned int tick, int id, int data) { if (char_fd <= 0 || session[char_fd] == NULL) { printf("Attempt to connect to char-server...\n"); chrif_state = 0; - char_fd = make_connection(char_ip, char_port); + if ((char_fd = make_connection(char_ip, char_port)) < 0) + return 0; session[char_fd]->func_parse = chrif_parse; realloc_fifo(char_fd, FIFOSIZE_SERVERLINK, FIFOSIZE_SERVERLINK); diff --git a/src/map/clif.c b/src/map/clif.c index 1f88ed8..9357084 100644 --- a/src/map/clif.c +++ b/src/map/clif.c @@ -8339,9 +8339,18 @@ static void (*clif_parse_func_table[0x220])() = { static int clif_parse(int fd) { int packet_len = 0, cmd=0; struct map_session_data *sd=NULL; - + sd = session[fd]->session_data; + if (!sd || (sd && !sd->state.auth)) { + if (RFIFOREST(fd) < 2) { // too small a packet disconnect + session[fd]->eof = 1; + } + if (RFIFOW(fd,0) != 0x72) { // first packet not auth, disconnect + session[fd]->eof = 1; + } + } + // ソスレ托ソスソスソスソスリゑソスソストゑソスソスフで鯉ソスソスnソスソス if (!chrif_isconnect() || session[fd]->eof) { // charソスIソスノ繋ソスソスソスソストなゑソスソスヤは接托ソスソスヨ止 (!chrif_isconnect()) if (sd && sd->state.auth) { @@ -8433,7 +8442,7 @@ static int clif_parse(int fd) { } else if (sd) // not authentified! (refused by char-server or disconnect before to be authentified) printf("\nAccount ID %d.\n", sd->bl.id); - if ((fp = fopen(packet_txt, "a")) == NULL) { + if ((fp = fopen_(packet_txt, "a")) == NULL) { printf("clif.c: cant write [%s] !!! data is lost !!!\n", packet_txt); return 1; } else { @@ -8454,7 +8463,7 @@ static int clif_parse(int fd) { fprintf(fp, "%02X ", RFIFOB(fd,i)); } fprintf(fp, "\n\n"); - fclose(fp); + fclose_(fp); } } #endif diff --git a/src/map/guild.c b/src/map/guild.c index 9e65c33..1509a37 100644 --- a/src/map/guild.c +++ b/src/map/guild.c @@ -64,7 +64,7 @@ static int guild_read_castledb(void) char *str[32],*p; struct guild_castle *gc; - if( (fp=fopen("db/castle_db.txt","r"))==NULL){ + if( (fp=fopen_("db/castle_db.txt","r"))==NULL){ printf("can't read db/castle_db.txt\n"); return -1; } @@ -97,7 +97,7 @@ static int guild_read_castledb(void) ln++; } - fclose(fp); + fclose_(fp); printf("read db/castle_db.txt done (count=%d)\n",ln); return 0; } diff --git a/src/map/itemdb.c b/src/map/itemdb.c index 44b45ad..d9cb429 100644 --- a/src/map/itemdb.c +++ b/src/map/itemdb.c @@ -12,6 +12,7 @@ #include "itemdb.h" #include "script.h" #include "pc.h" +#include "../common/socket.h" #ifdef MEMWATCH #include "memwatch.h" @@ -280,7 +281,7 @@ static int itemdb_readdb(void) for(i=0;i<2;i++){ - fp=fopen(filename[i],"r"); + fp=fopen_(filename[i],"r"); if(fp==NULL){ if(i>0) continue; @@ -348,7 +349,7 @@ static int itemdb_readdb(void) continue; id->equip_script = parse_script(p,lines); } - fclose(fp); + fclose_(fp); printf("read %s done (count=%d)\n",filename[i],ln); } return 0; @@ -388,7 +389,7 @@ static int itemdb_read_randomitem() *pdefault = 0; - if( (fp=fopen(fn,"r"))==NULL ){ + if( (fp=fopen_(fn,"r"))==NULL ){ printf("can't read %s\n",fn); continue; } @@ -424,7 +425,7 @@ static int itemdb_read_randomitem() break; ln++; } - fclose(fp); + fclose_(fp); printf("read %s done (count=%d)\n",fn,*pc); } @@ -442,7 +443,7 @@ static int itemdb_read_itemavail(void) int nameid,j,k; char *str[10],*p; - if( (fp=fopen("db/item_avail.txt","r"))==NULL ){ + if( (fp=fopen_("db/item_avail.txt","r"))==NULL ){ printf("can't read db/item_avail.txt\n"); return -1; } @@ -473,7 +474,7 @@ static int itemdb_read_itemavail(void) id->flag.available = 0; ln++; } - fclose(fp); + fclose_(fp); printf("read db/item_avail.txt done (count=%d)\n",ln); return 0; } @@ -568,7 +569,7 @@ static int itemdb_read_noequip(void) char *str[32],*p; struct item_data *id; - if( (fp=fopen("db/item_noequip.txt","r"))==NULL ){ + if( (fp=fopen_("db/item_noequip.txt","r"))==NULL ){ printf("can't read db/item_noequip.txt\n"); return -1; } @@ -593,7 +594,7 @@ static int itemdb_read_noequip(void) ln++; } - fclose(fp); + fclose_(fp); printf("read db/item_noequip.txt done (count=%d)\n",ln); return 0; } @@ -650,9 +651,9 @@ static int itemdebug(void *key,void *data,va_list ap){ } void itemdebugtxt() { - dfp=fopen("itemdebug.txt","wt"); + dfp=fopen_("itemdebug.txt","wt"); numdb_foreach(item_db,itemdebug); - fclose(dfp); + fclose_(dfp); } */ diff --git a/src/map/magic-interpreter-parser.c b/src/map/magic-interpreter-parser.c index 8cf2312..7ff9fb4 100644 --- a/src/map/magic-interpreter-parser.c +++ b/src/map/magic-interpreter-parser.c @@ -3224,7 +3224,7 @@ magic_init(char *conffile) // must be called after itemdb initialisation INTERN_ASSERT("script_target", VAR_SCRIPTTARGET); INTERN_ASSERT("location", VAR_LOCATION); - magic_frontend_in = fopen(conffile, "r"); + magic_frontend_in = fopen_(conffile, "r"); if (!magic_frontend_in) { fprintf(stderr, "[magic-conf] Magic configuration file `%s' not found -> no magic.\n", conffile); return 0; diff --git a/src/map/magic-interpreter.h b/src/map/magic-interpreter.h index 515b120..4324e53 100644 --- a/src/map/magic-interpreter.h +++ b/src/map/magic-interpreter.h @@ -29,6 +29,7 @@ #include "trade.h" #include "../common/timer.h" +#include "../common/socket.h" #define SPELLARG_NONE 0 /* No spell parameter */ #define SPELLARG_PC 1 /* Spell parameter describes pc (defaults to self) */ diff --git a/src/map/map.c b/src/map/map.c index ee42d3d..86acb62 100644 --- a/src/map/map.c +++ b/src/map/map.c @@ -1454,7 +1454,7 @@ static void map_readwater(char *watertxt) { FILE *fp=NULL; int n=0; - fp=fopen(watertxt,"r"); + fp=fopen_(watertxt,"r"); if(fp==NULL){ printf("file not found: %s\n",watertxt); return; @@ -1475,7 +1475,7 @@ static void map_readwater(char *watertxt) { waterlist[n].waterheight = 3; n++; } - fclose(fp); + fclose_(fp); } /*========================================== @@ -1665,7 +1665,7 @@ int map_config_read(char *cfgName) { FILE *fp; struct hostent *h = NULL; - fp = fopen(cfgName,"r"); + fp = fopen_(cfgName,"r"); if (fp == NULL) { printf("Map configuration file not found at: %s\n", cfgName); exit(1); @@ -1730,7 +1730,7 @@ int map_config_read(char *cfgName) { } } } - fclose(fp); + fclose_(fp); return 0; } diff --git a/src/map/mob.c b/src/map/mob.c index 51a83d2..09395f6 100644 --- a/src/map/mob.c +++ b/src/map/mob.c @@ -3833,7 +3833,7 @@ static int mob_readdb(void) for(i=0;i<2;i++){ - fp=fopen(filename[i],"r"); + fp=fopen_(filename[i],"r"); if(fp==NULL){ if(i>0) continue; @@ -3966,7 +3966,7 @@ static int mob_readdb(void) if (mob_db[class].base_exp == 0) mob_db[class].base_exp = mob_gen_exp(&mob_db[class]); } - fclose(fp); + fclose_(fp); printf("read %s done\n",filename[i]); } return 0; @@ -3984,7 +3984,7 @@ static int mob_readdb_mobavail(void) int class,j,k; char *str[20],*p,*np; - if( (fp=fopen("db/mob_avail.txt","r"))==NULL ){ + if( (fp=fopen_("db/mob_avail.txt","r"))==NULL ){ printf("can't read db/mob_avail.txt\n"); return -1; } @@ -4031,7 +4031,7 @@ static int mob_readdb_mobavail(void) ln++; } - fclose(fp); + fclose_(fp); printf("read db/mob_avail.txt done (count=%d)\n",ln); return 0; } @@ -4054,7 +4054,7 @@ static int mob_read_randommonster(void) for(i=0;i1000 && class<=2000) || class==0) mob_db[class].summonper[i]=per; } - fclose(fp); + fclose_(fp); printf("read %s done\n",mobfile[i]); } return 0; @@ -4153,7 +4153,7 @@ static int mob_readskilldb(void) for(x=0;x<2;x++){ - fp=fopen(filename[x],"r"); + fp=fopen_(filename[x],"r"); if(fp==NULL){ if(x==0) printf("can't read %s\n",filename[x]); @@ -4231,7 +4231,7 @@ static int mob_readskilldb(void) ms->emotion=-1; mob_db[mob_id].maxskill=i+1; } - fclose(fp); + fclose_(fp); printf("read %s done\n",filename[x]); } return 0; diff --git a/src/map/npc.c b/src/map/npc.c index 11725b3..5fe190b 100644 --- a/src/map/npc.c +++ b/src/map/npc.c @@ -21,6 +21,7 @@ #include "pc.h" #include "script.h" #include "skill.h" +#include "../common/socket.h" #ifdef MEMWATCH #include "memwatch.h" @@ -2008,7 +2009,7 @@ int do_init_npc(void) free(nsl->prev); nsl->prev = NULL; } - fp=fopen(nsl->name,"r"); + fp=fopen_(nsl->name,"r"); if (fp==NULL) { printf("file not found : %s\n",nsl->name); exit(1); @@ -2065,7 +2066,7 @@ int do_init_npc(void) npc_parse_mapflag(w1,w2,w3,w4); } } - fclose(fp); + fclose_(fp); printf("\rLoading NPCs [%d]: %-54s",npc_id-START_NPC_NUM,nsl->name); fflush(stdout); } diff --git a/src/map/pc.c b/src/map/pc.c index b3626df..2e75294 100644 --- a/src/map/pc.c +++ b/src/map/pc.c @@ -798,7 +798,7 @@ int pc_authok(int id, int login_id2, time_t connect_until_time, short tmw_versio { char buf[256]; FILE *fp; - if ((fp = fopen(motd_txt, "r")) != NULL) { + if ((fp = fopen_(motd_txt, "r")) != NULL) { while (fgets(buf, sizeof(buf)-1, fp) != NULL) { int i; for(i=0; buf[i]; i++) { @@ -809,7 +809,7 @@ int pc_authok(int id, int login_id2, time_t connect_until_time, short tmw_versio } clif_displaymessage(sd->fd, buf); } - fclose(fp); + fclose_(fp); } } @@ -7330,7 +7330,7 @@ int pc_readdb(void) // ソスKソスvソスoソスソスソスlソスヌみ搾ソスソスソス - fp=fopen("db/exp.txt","r"); + fp=fopen_("db/exp.txt","r"); if(fp==NULL){ printf("can't read db/exp.txt\n"); return 1; @@ -7360,11 +7360,11 @@ int pc_readdb(void) if(i >= battle_config.maximum_level) break; } - fclose(fp); + fclose_(fp); printf("read db/exp.txt done\n"); // JOBソス竦ウソスソスソスlソスP - fp=fopen("db/job_db1.txt","r"); + fp=fopen_("db/job_db1.txt","r"); if(fp==NULL){ printf("can't read db/job_db1.txt\n"); return 1; @@ -7394,11 +7394,11 @@ int pc_readdb(void) if(i==MAX_PC_CLASS) break; } - fclose(fp); + fclose_(fp); printf("read db/job_db1.txt done\n"); // JOBソス{ソス[ソスiソスX - fp=fopen("db/job_db2.txt","r"); + fp=fopen_("db/job_db2.txt","r"); if(fp==NULL){ printf("can't read db/job_db2.txt\n"); return 1; @@ -7422,11 +7422,11 @@ int pc_readdb(void) if(i==MAX_PC_CLASS) break; } - fclose(fp); + fclose_(fp); printf("read db/job_db2.txt done\n"); // JOBソス{ソス[ソスiソスX2 ソス]ソスソスソスEソスp - fp=fopen("db/job_db2-2.txt","r"); + fp=fopen_("db/job_db2-2.txt","r"); if(fp==NULL){ printf("can't read db/job_db2-2.txt\n"); return 1; @@ -7446,12 +7446,12 @@ int pc_readdb(void) if(i==MAX_PC_CLASS) break; } - fclose(fp); + fclose_(fp); printf("read db/job_db2-2.txt done\n"); // ソスXソスLソスソスソスcソスソスソス[ memset(skill_tree,0,sizeof(skill_tree)); - fp=fopen("db/skill_tree.txt","r"); + fp=fopen_("db/skill_tree.txt","r"); if(fp==NULL){ printf("can't read db/skill_tree.txt\n"); return 1; @@ -7480,7 +7480,7 @@ int pc_readdb(void) skill_tree[2][i][j].need[k].lv=atoi(split[k*2+4]); //ソス{ソスqソスEソスヘ良ゑソスソスソスソスソスソスソスソスネゑソスソスフで暫ソスソス } } - fclose(fp); + fclose_(fp); printf("read db/skill_tree.txt done\n"); // ソスソスソスソスソスCソスソスソスeソス[ソスuソスソス @@ -7488,7 +7488,7 @@ int pc_readdb(void) for(j=0;j<10;j++) for(k=0;k<10;k++) attr_fix_table[i][j][k]=100; - fp=fopen("db/attr_fix.txt","r"); + fp=fopen_("db/attr_fix.txt","r"); if(fp==NULL){ printf("can't read db/attr_fix.txt\n"); return 1; @@ -7526,14 +7526,14 @@ int pc_readdb(void) i++; } } - fclose(fp); + fclose_(fp); printf("read db/attr_fix.txt done\n"); // ソスTソスCソスYソス竦ウソスeソス[ソスuソスソス for(i=0;i<3;i++) for(j=0;j<20;j++) atkmods[i][j]=100; - fp=fopen("db/size_fix.txt","r"); + fp=fopen_("db/size_fix.txt","r"); if(fp==NULL){ printf("can't read db/size_fix.txt\n"); return 1; @@ -7555,7 +7555,7 @@ int pc_readdb(void) atkmods[i][j]=atoi(split[j]); i++; } - fclose(fp); + fclose_(fp); printf("read db/size_fix.txt done\n"); // ソスソスソスBソスfソス[ソス^ソスeソス[ソスuソスソス @@ -7566,7 +7566,7 @@ int pc_readdb(void) refinebonus[i][1]=0; refinebonus[i][2]=10; } - fp=fopen("db/refine_db.txt","r"); + fp=fopen_("db/refine_db.txt","r"); if(fp==NULL){ printf("can't read db/refine_db.txt\n"); return 1; @@ -7591,7 +7591,7 @@ int pc_readdb(void) percentrefinery[i][j]=atoi(split[j+3]); i++; } - fclose(fp); //Lupus. close this file!!! + fclose_(fp); //Lupus. close this file!!! printf("read db/refine_db.txt done\n"); return 0; @@ -7619,7 +7619,7 @@ static void pc_statpointdb(void) FILE *stp; - stp=fopen("db/statpoint.txt","r"); + stp=fopen_("db/statpoint.txt","r"); if(stp==NULL){ printf("can't read db/statpoint.txt\n"); @@ -7632,7 +7632,7 @@ static void pc_statpointdb(void) buf_stat = (char *) malloc (end + 1); l = fread(buf_stat,1,end,stp); - fclose(stp); + fclose_(stp); printf("read db/statpoint.txt done (size=%d)\n",l); for(i=0;i<255;i++) { diff --git a/src/map/script.c b/src/map/script.c index 94f03f7..218c3f3 100644 --- a/src/map/script.c +++ b/src/map/script.c @@ -1090,7 +1090,7 @@ static void read_constdb(void) char line[1024],name[1024]; int val,n,i,type; - fp=fopen("db/const.txt","r"); + fp=fopen_("db/const.txt","r"); if(fp==NULL){ printf("can't read db/const.txt\n"); return ; @@ -1111,7 +1111,7 @@ static void read_constdb(void) str_data[n].val=val; } } - fclose(fp); + fclose_(fp); } /*========================================== @@ -6549,7 +6549,7 @@ static int script_load_mapreg() FILE *fp; char line[1024]; - if( (fp=fopen(mapreg_txt,"rt"))==NULL ) + if( (fp=fopen_(mapreg_txt,"rt"))==NULL ) return -1; while(fgets(line,sizeof(line),fp)){ @@ -6576,7 +6576,7 @@ static int script_load_mapreg() numdb_insert(mapreg_db,(i<<24)|s,v); } } - fclose(fp); + fclose_(fp); mapreg_dirty=0; return 0; } @@ -6666,7 +6666,7 @@ int script_config_read(char *cfgName) script_config.check_cmdcount=8192; script_config.check_gotocount=512; - fp=fopen(cfgName,"r"); + fp=fopen_(cfgName,"r"); if(fp==NULL){ printf("file not found: %s\n",cfgName); return 1; @@ -6684,7 +6684,7 @@ int script_config_read(char *cfgName) script_config_read(w2); } } - fclose(fp); + fclose_(fp); return 0; } diff --git a/src/map/skill.c b/src/map/skill.c index 3a777ac..c661dc2 100644 --- a/src/map/skill.c +++ b/src/map/skill.c @@ -21,6 +21,7 @@ #include "pc.h" #include "script.h" #include "skill.h" +#include "../common/socket.h" #ifdef MEMWATCH #include "memwatch.h" @@ -9890,7 +9891,7 @@ int skill_readdb(void) /* The main skill database */ memset(skill_db,0,sizeof(skill_db)); - fp=fopen("db/skill_db.txt","r"); + fp=fopen_("db/skill_db.txt","r"); if(fp==NULL){ printf("can't read db/skill_db.txt\n"); return 1; @@ -9959,10 +9960,10 @@ int skill_readdb(void) for(k=0;k