summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorcelest <celest@54d463be-8e91-2dee-dedb-b68131a5f0ec>2005-02-26 08:38:22 +0000
committercelest <celest@54d463be-8e91-2dee-dedb-b68131a5f0ec>2005-02-26 08:38:22 +0000
commit8d96e1c00a9d7dd4562ec525e40384be0bf726e1 (patch)
tree25fb03614d065afd2ace4ed3b1a7b6ba27ae8ac3 /src
parentccd3e0e2d45eac452406eab7dfcfa7a08a369042 (diff)
downloadhercules-8d96e1c00a9d7dd4562ec525e40384be0bf726e1.tar.gz
hercules-8d96e1c00a9d7dd4562ec525e40384be0bf726e1.tar.bz2
hercules-8d96e1c00a9d7dd4562ec525e40384be0bf726e1.tar.xz
hercules-8d96e1c00a9d7dd4562ec525e40384be0bf726e1.zip
* Added jA's dummy socket to prevent fd crashes
git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/branches/stable@1183 54d463be-8e91-2dee-dedb-b68131a5f0ec
Diffstat (limited to 'src')
-rw-r--r--src/common/socket.c18
-rw-r--r--src/common/socket.h30
2 files changed, 33 insertions, 15 deletions
diff --git a/src/common/socket.c b/src/common/socket.c
index 02ba49cb9..ad4e7d2e2 100644
--- a/src/common/socket.c
+++ b/src/common/socket.c
@@ -54,6 +54,9 @@ struct socket_data *session[FD_SETSIZE];
static int null_parse(int fd);
static int (*default_func_parse)(int) = null_parse;
+// fdが不正な時に代わりに読み書きするバッファ
+unsigned char socket_dummy[SOCKET_DUMMY_SIZE];
+
static int null_console_parse(char *buf);
static int (*default_console_parse)(char*) = null_console_parse;
@@ -431,7 +434,10 @@ int delete_session(int fd)
int realloc_fifo(int fd,int rfifo_size,int wfifo_size)
{
- struct socket_data *s=session[fd];
+ struct socket_data *s;
+
+ if (fd <= 0) return 0;
+ s = session[fd];
if( s->max_rdata != rfifo_size && s->rdata_size < rfifo_size){
RECREATE(s->rdata, unsigned char, rfifo_size);
s->max_rdata = rfifo_size;
@@ -445,7 +451,10 @@ int realloc_fifo(int fd,int rfifo_size,int wfifo_size)
int WFIFOSET(int fd,int len)
{
- struct socket_data *s=session[fd];
+ struct socket_data *s;
+
+ if (fd <= 0) return 0;
+ s = session[fd];
if (s == NULL || s->wdata == NULL)
return 0;
if( s->wdata_size+len+16384 > s->max_wdata ){
@@ -531,7 +540,10 @@ void do_socket(void)
int RFIFOSKIP(int fd,int len)
{
- struct socket_data *s=session[fd];
+ struct socket_data *s;
+
+ if (fd <= 0) return 0;
+ s = session[fd];
if (s->rdata_size-s->rdata_pos-len<0) {
fprintf(stderr,"too many skip\n");
diff --git a/src/common/socket.h b/src/common/socket.h
index 7ee7b7689..52581faf7 100644
--- a/src/common/socket.h
+++ b/src/common/socket.h
@@ -22,24 +22,30 @@ extern time_t stall_time_;
// define declaration
-#define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos))
-#define RFIFOB(fd,pos) (*(unsigned char*)(session[fd]->rdata+session[fd]->rdata_pos+(pos)))
-#define RFIFOW(fd,pos) (*(unsigned short*)(session[fd]->rdata+session[fd]->rdata_pos+(pos)))
-#define RFIFOL(fd,pos) (*(unsigned int*)(session[fd]->rdata+session[fd]->rdata_pos+(pos)))
+// fdが不正な時に代わりに読み書きするバッファ
+#define SOCKET_DUMMY_SIZE 32768
+extern unsigned char socket_dummy[SOCKET_DUMMY_SIZE];
+
+#define RFIFOSPACE(fd) (fd <= 0 ? SOCKET_DUMMY_SIZE : session[fd]->max_rdata-session[fd]->rdata_size)
+#define RFIFOP(fd,pos) (fd <= 0 ? socket_dummy : session[fd]->rdata+session[fd]->rdata_pos+(pos))
+// use function instead of macro.
+#define RFIFOB(fd,pos) (*(unsigned char*)RFIFOP(fd,pos))
+#define RFIFOW(fd,pos) (*(unsigned short*)RFIFOP(fd,pos))
+#define RFIFOL(fd,pos) (*(unsigned int*)RFIFOP(fd,pos))
+#define RFIFOREST(fd) (fd <= 0 ? 0 : session[fd]->rdata_size-session[fd]->rdata_pos)
+#define RFIFOFLUSH(fd) (fd <= 0 ? 0 : memmove(session[fd]->rdata,RFIFOP(fd,0),RFIFOREST(fd)),session[fd]->rdata_size=RFIFOREST(fd),session[fd]->rdata_pos=0)
//#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size-session[fd]->rdata_pos-(len)<0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos+=(len)))
-#define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos)
-#define RFIFOFLUSH(fd) (memmove(session[fd]->rdata,RFIFOP(fd,0),RFIFOREST(fd)),session[fd]->rdata_size=RFIFOREST(fd),session[fd]->rdata_pos=0)
-#define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size)
+
#define RBUFP(p,pos) (((unsigned char*)(p))+(pos))
#define RBUFB(p,pos) (*(unsigned char*)RBUFP((p),(pos)))
#define RBUFW(p,pos) (*(unsigned short*)RBUFP((p),(pos)))
#define RBUFL(p,pos) (*(unsigned int*)RBUFP((p),(pos)))
-#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size)
-#define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos))
-#define WFIFOB(fd,pos) (*(unsigned char*)(session[fd]->wdata+session[fd]->wdata_size+(pos)))
-#define WFIFOW(fd,pos) (*(unsigned short*)(session[fd]->wdata+session[fd]->wdata_size+(pos)))
-#define WFIFOL(fd,pos) (*(unsigned int*)(session[fd]->wdata+session[fd]->wdata_size+(pos)))
+#define WFIFOSPACE(fd) (fd <= 0 ? SOCKET_DUMMY_SIZE : session[fd]->max_wdata-session[fd]->wdata_size)
+#define WFIFOP(fd,pos) (fd <= 0 ? socket_dummy : session[fd]->wdata+session[fd]->wdata_size+(pos))
+#define WFIFOB(fd,pos) (*(unsigned char*)WFIFOP(fd,pos))
+#define WFIFOW(fd,pos) (*(unsigned short*)WFIFOP(fd,pos))
+#define WFIFOL(fd,pos) (*(unsigned int*)WFIFOP(fd,pos))
// use function instead of macro.
//#define WFIFOSET(fd,len) (session[fd]->wdata_size = (session[fd]->wdata_size+(len)+2048 < session[fd]->max_wdata) ? session[fd]->wdata_size+len : session[fd]->wdata_size)
#define WBUFP(p,pos) (((unsigned char*)(p))+(pos))