From 04190264ba7007c84a69b21ac65b3779841cc04b Mon Sep 17 00:00:00 2001 From: FlavioJS Date: Wed, 29 Nov 2006 20:07:22 +0000 Subject: - Probably fixed the Segmentation Faults we've been having. Description: A player quits and it's session is freed and set to NULL, but the char server already sent a packet with player information (registers, storage, ...). If a message is sent in consequence of updating that info, a segmentation fault happens because session[sd->fd] is already NULL. Fix: make shure the session of the target player is valid before processing the rest of the char server's message. - Some minor cleanups. git-svn-id: https://rathena.svn.sourceforge.net/svnroot/rathena/trunk@9366 54d463be-8e91-2dee-dedb-b68131a5f0ec --- src/common/socket.c | 2720 ++++++++++++++++++++++---------------------- src/common/socket.h | 352 +++--- src/common/strlib.c | 388 +++---- src/map/intif.c | 3153 ++++++++++++++++++++++++++------------------------- 4 files changed, 3309 insertions(+), 3304 deletions(-) (limited to 'src') diff --git a/src/common/socket.c b/src/common/socket.c index f9262f424..4651f1dac 100644 --- a/src/common/socket.c +++ b/src/common/socket.c @@ -1,1360 +1,1360 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder - -#include -#include -#include -#include - -#ifdef __WIN32 -#define __USE_W32_SOCKETS -#include -#include -#include - -typedef int socklen_t; -#else -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef SIOCGIFCONF -#include // SIOCGIFCONF on Solaris, maybe others? [Shinomori] -#endif - -#endif - -#ifdef _WIN32 -#define SEBADF WSAENOTSOCK -#define serrno WSAGetLastError() -#else -#define SEBADF EBADF -#define serrno errno -#endif - -#include -#include - -#include "../common/socket.h" -#include "../common/mmo.h" // [Valaris] thanks to fov -#include "../common/timer.h" -#include "../common/malloc.h" -#include "../common/showmsg.h" - -fd_set readfds; -int fd_max; -time_t last_tick; -time_t stall_time = 60; -int ip_rules = 1; - -// reuse port -#ifndef SO_REUSEPORT - #define SO_REUSEPORT 15 -#endif - -#ifndef TCP_FRAME_LEN -#define TCP_FRAME_LEN 1024 -#endif - -static int mode_neg=1; -static unsigned int frame_size=TCP_FRAME_LEN; - -#ifndef MINICORE -enum { - ACO_DENY_ALLOW=0, - ACO_ALLOW_DENY, - ACO_MUTUAL_FAILTURE, -}; - -static struct _access_control *access_allow; -static struct _access_control *access_deny; -static int access_order=ACO_DENY_ALLOW; -static int access_allownum=0; -static int access_denynum=0; -static int access_debug=0; -static int ddos_count = 10; -static int ddos_interval = 3000; -static int ddos_autoreset = 600*1000; -#endif - - -// values derived from freya -// a player that send more than 2k is probably a hacker without be parsed -// biggest known packet: S 0153 .w .?B -> 24x24 256 color .bmp (0153 + len.w + 1618/1654/1756 bytes) -size_t rfifo_size = (16*1024); -size_t wfifo_size = (16*1024); - -#define CONVIP(ip) ip&0xFF,(ip>>8)&0xFF,(ip>>16)&0xFF,ip>>24 - -struct socket_data *session[FD_SETSIZE]; - -static int null_parse(int fd); -static int (*default_func_parse)(int) = null_parse; - -static int null_console_parse(char *buf); -static int (*default_console_parse)(char*) = null_console_parse; -#ifndef MINICORE -static int connect_check(unsigned int ip); -#else - #define connect_check(n) 1 -#endif - -/*====================================== - * CORE : Set function - *-------------------------------------- - */ -void set_defaultparse(int (*defaultparse)(int)) -{ - default_func_parse = defaultparse; -} - -void set_nonblocking(int fd, int yes) { - // I don't think we need this - // TCP_NODELAY BOOL Disables the Nagle algorithm for send coalescing. - if(mode_neg) - setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof yes); - - // FIONBIO Use with a nonzero argp parameter to enable the nonblocking mode of socket s. - // The argp parameter is zero if nonblocking is to be disabled. -#ifdef __WIN32 - ioctlsocket(fd, FIONBIO, &yes); -#else - ioctl(fd,FIONBIO,&yes); -#endif -} - -static void setsocketopts(int fd) -{ - int yes = 1; // reuse fix -#ifndef WIN32 - // set SO_REAUSEADDR to true, unix only. on windows this option causes - // the previous owner of the socket to give up, which is not desirable - // in most cases, neither compatible with unix. - setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof(yes)); -#ifdef SO_REUSEPORT - setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,(char *)&yes,sizeof(yes)); -#endif -#endif - setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof(yes)); -// setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &wfifo_size , sizeof(rfifo_size )); -// setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &rfifo_size , sizeof(rfifo_size )); -#ifdef __WIN32 -{ //set SO_LINGER option (from Freya) - //(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/closesocket_2.asp) - struct linger opt; - opt.l_onoff = 1; - opt.l_linger = 0; - if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&opt, sizeof(opt))) - ShowWarning("setsocketopts: Unable to set SO_LINGER mode for connection %d!\n",fd); -} -#endif -} - -/*====================================== - * CORE : Socket Sub Function - *-------------------------------------- - */ -static void set_eof(int fd) -{ //Marks a connection eof and invokes the parse_function to disconnect it right away. [Skotlex] - if (session_isActive(fd)) - session[fd]->eof=1; -} - -static int recv_to_fifo(int fd) -{ - int len; - - if( (fd<0) || (fd>=FD_SETSIZE) || (NULL==session[fd]) ) - return -1; - - if(session[fd]->eof) - return -1; - -#ifdef __WIN32 - len=recv(fd,(char *)session[fd]->rdata+session[fd]->rdata_size, RFIFOSPACE(fd), 0); - if (len == SOCKET_ERROR) { - if (WSAGetLastError() == WSAECONNABORTED) { - ShowWarning("recv_to_fifo: Software caused connection abort on session #%d\n", fd); - FD_CLR(fd, &readfds); //Remove the socket so the select() won't hang on it. -// exit(1); //Windows can't really recover from this one. [Skotlex] - } - if (WSAGetLastError() != WSAEWOULDBLOCK) { -// ShowDebug("recv_to_fifo: error %d, ending connection #%d\n", WSAGetLastError(), fd); - set_eof(fd); - } - return 0; - } -#else - len=read(fd,session[fd]->rdata+session[fd]->rdata_size, RFIFOSPACE(fd)); - if (len == -1) - { - if (errno == ECONNABORTED) - { - ShowFatalError("recv_to_fifo: Network broken (Software caused connection abort on session #%d)\n", fd); -// exit(1); //Temporal debug, maybe this can be fixed. - } - if (errno != EAGAIN) { //Connection error. -// perror("closing session: recv_to_fifo"); - set_eof(fd); - } - return 0; - } -#endif - if (len <= 0) { //Normal connection end. - set_eof(fd); - return 0; - } - - session[fd]->rdata_size+=len; - session[fd]->rdata_tick = last_tick; - return 0; -} - -static int send_from_fifo(int fd) -{ - int len; - if( !session_isValid(fd) ) - return -1; - -// if (s->eof) // if we close connection, we can not send last information (you're been disconnected, etc...) [Yor] -// return -1; -/* - // clear write buffer if not connected <- I really like more the idea of sending the last information. [Skotlex] - if( session[fd]->eof ) - { - session[fd]->wdata_size = 0; - return -1; - } -*/ - - if (session[fd]->wdata_size == 0) - return 0; - -#ifdef __WIN32 - len=send(fd, (const char *)session[fd]->wdata,session[fd]->wdata_size, 0); - if (len == SOCKET_ERROR) { - if (WSAGetLastError() == WSAECONNABORTED) { - ShowWarning("send_from_fifo: Software caused connection abort on session #%d\n", fd); - session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] - set_eof(fd); - FD_CLR(fd, &readfds); //Remove the socket so the select() won't hang on it. - } - if (WSAGetLastError() != WSAEWOULDBLOCK) { -// ShowDebug("send_from_fifo: error %d, ending connection #%d\n", WSAGetLastError(), fd); - session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] - set_eof(fd); - } - return 0; - } -#else - len=send(fd, session[fd]->wdata, session[fd]->wdata_size, MSG_NOSIGNAL); - if (len == -1) - { - if (errno == ECONNABORTED) - { - ShowWarning("send_from_fifo: Network broken (Software caused connection abort on session #%d)\n", fd); - session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] - set_eof(fd); - } - if (errno != EAGAIN) { -// perror("closing session: send_from_fifo"); - session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] - set_eof(fd); - } - return 0; - } -#endif - - //{ int i; ShowMessage("send %d : ",fd); for(i=0;iwdata[i]); } ShowMessage("\n");} - if(len>0){ - if((unsigned int)lenwdata_size){ - memmove(session[fd]->wdata,session[fd]->wdata+len,session[fd]->wdata_size-len); - session[fd]->wdata_size-=len; - } else { - session[fd]->wdata_size=0; - } - } - return 0; -} - -void flush_fifo(int fd) -{ - if(session[fd] != NULL && session[fd]->func_send == send_from_fifo) - { - set_nonblocking(fd, 1); - send_from_fifo(fd); - set_nonblocking(fd, 0); - } -} - -void flush_fifos(void) -{ - int i; - for(i=1;ifunc_send == send_from_fifo) - send_from_fifo(i); -} - -static int null_parse(int fd) -{ - ShowMessage("null_parse : %d\n",fd); - session[fd]->rdata_pos = session[fd]->rdata_size; //RFIFOSKIP(fd, RFIFOREST(fd)); simplify calculation - return 0; -} - -/*====================================== - * CORE : Socket Function - *-------------------------------------- - */ - -static int connect_client(int listen_fd) -{ - int fd; - struct sockaddr_in client_address; -#ifdef __WIN32 - int len; -#else - socklen_t len; -#endif - //ShowMessage("connect_client : %d\n",listen_fd); - - len=sizeof(client_address); - - fd = accept(listen_fd,(struct sockaddr*)&client_address,&len); -#ifdef __WIN32 - if (fd == SOCKET_ERROR || fd == INVALID_SOCKET || fd < 0) { - ShowError("accept failed (code %i)!\n", WSAGetLastError()); - return -1; - } -#else - if(fd==-1) { - perror("accept"); - return -1; - } -#endif - - if(fd_max<=fd) fd_max=fd+1; - - setsocketopts(fd); - -#ifdef __WIN32 - { - unsigned long val = 1; - if (ioctlsocket(fd, FIONBIO, &val) != 0) - ShowError("Couldn't set the socket to non-blocking mode (code %d)!\n", WSAGetLastError()); - } -#else - if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) - perror("connect_client (set nonblock)"); -#endif - - if (ip_rules && !connect_check(*(unsigned int*)(&client_address.sin_addr))) { - do_close(fd); - return -1; - } else - FD_SET(fd,&readfds); - - CREATE(session[fd], struct socket_data, 1); - CREATE_A(session[fd]->rdata, unsigned char, rfifo_size); - CREATE_A(session[fd]->wdata, unsigned char, wfifo_size); - - session[fd]->max_rdata = (int)rfifo_size; - session[fd]->max_wdata = (int)wfifo_size; - session[fd]->func_recv = recv_to_fifo; - session[fd]->func_send = send_from_fifo; - if(!session[listen_fd]->func_parse) - session[fd]->func_parse = default_func_parse; - else - session[fd]->func_parse = session[listen_fd]->func_parse; - session[fd]->client_addr = client_address; - session[fd]->rdata_tick = last_tick; - session[fd]->type = SESSION_UNKNOWN; // undefined type - - //ShowMessage("new_session : %d %d\n",fd,session[fd]->eof); - return fd; -} - -int make_listen_bind(long ip,int port) -{ - struct sockaddr_in server_address; - int fd; - int result; - - fd = (int)socket( AF_INET, SOCK_STREAM, 0 ); - -#ifdef __WIN32 - if (fd == INVALID_SOCKET) { - ShowError("socket() creation failed (code %d)!\n", fd, WSAGetLastError()); - exit(1); - } -#else - if (fd == -1) { - perror("make_listen_port:socket()"); - exit(1); - } -#endif - -#ifdef __WIN32 - { - unsigned long val = 1; - if (ioctlsocket(fd, FIONBIO, &val) != 0) - ShowError("Couldn't set the socket to non-blocking mode (code %d)!\n", WSAGetLastError()); - } -#else - if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) - perror("make_listen_bind (set nonblock)"); -#endif - - setsocketopts(fd); - - server_address.sin_family = AF_INET; - server_address.sin_addr.s_addr = ip; - server_address.sin_port = htons((unsigned short)port); - - result = bind(fd, (struct sockaddr*)&server_address, sizeof(server_address)); -#ifdef __WIN32 - if( result == SOCKET_ERROR ) { - ShowError("bind failed (socket %d, code %d)!\n", fd, WSAGetLastError()); - exit(1); - } -#else - if( result == -1 ) { - perror("bind"); - exit(1); - } -#endif - result = listen( fd, 5 ); -#ifdef __WIN32 - if( result == SOCKET_ERROR ) { - ShowError("listen failed (socket %d, code %d)!\n", fd, WSAGetLastError()); - exit(1); - } -#else - if( result != 0) { /* error */ - perror("listen"); - exit(1); - } -#endif - if ( fd < 0 || fd > FD_SETSIZE ) - { //Crazy error that can happen in Windows? (info from Freya) - ShowFatalError("listen() returned invalid fd %d!\n",fd); - exit(1); - } - - if(fd_max<=fd) fd_max=fd+1; - FD_SET(fd, &readfds ); - - CREATE(session[fd], struct socket_data, 1); - - malloc_set(session[fd],0,sizeof(*session[fd])); - session[fd]->func_recv = connect_client; - - return fd; -} - -int make_listen_port(int port) -{ - return make_listen_bind(INADDR_ANY,port); -} - -// Console Reciever [Wizputer] -int console_recieve(int i) { - int n; - char *buf; - - CREATE_A(buf, char, 64); - malloc_tsetdword(buf,0,sizeof(64)); - - n = read(0, buf , 64); - if ( n < 0 ) - ShowError("Console input read error\n"); - else - { - ShowNotice ("Sorry, the console is currently non-functional.\n"); -// session[0]->func_console(buf); - } - - aFree(buf); - return 0; -} - -void set_defaultconsoleparse(int (*defaultparse)(char*)) -{ - default_console_parse = defaultparse; -} - -static int null_console_parse(char *buf) -{ - ShowMessage("null_console_parse : %s\n",buf); - return 0; -} - -// function parse table -// To-do: -- use dynamic arrays -// -- add a register_parse_func(); -struct func_parse_table func_parse_table[SESSION_MAX]; - -int default_func_check (struct socket_data *sd) { return 1; } - -void func_parse_check (struct socket_data *sd) -{ - int i; - for (i = SESSION_HTTP; i < SESSION_MAX; i++) { - if (func_parse_table[i].func && - func_parse_table[i].check && - func_parse_table[i].check(sd) != 0) - { - sd->type = i; - sd->func_parse = func_parse_table[i].func; - return; - } - } - - // undefined -- treat as raw socket (using default parse) - sd->type = SESSION_RAW; -} - -// Console Input [Wizputer] -int start_console(void) { - - //Until a better plan is came up with... can't be using session[0] anymore! [Skotlex] - ShowNotice("The console is currently nonfunctional.\n"); - return 0; - - FD_SET(0,&readfds); - - if (!session[0]) { // dummy socket already uses fd 0 - CREATE(session[0], struct socket_data, 1); - } - malloc_set(session[0],0,sizeof(*session[0])); - - session[0]->func_recv = console_recieve; - session[0]->func_console = default_console_parse; - - return 0; -} - -int make_connection(long ip,int port) -{ - struct sockaddr_in server_address; - int fd; - int result; - - fd = (int)socket( AF_INET, SOCK_STREAM, 0 ); - -#ifdef __WIN32 - if (fd == INVALID_SOCKET) { - ShowError("socket() creation failed (code %d)!\n", fd, WSAGetLastError()); - return -1; - } -#else - if (fd == -1) { - perror("make_connection:socket()"); - return -1; - } -#endif - - setsocketopts(fd); - - server_address.sin_family = AF_INET; - server_address.sin_addr.s_addr = ip; - server_address.sin_port = htons((unsigned short)port); - - ShowStatus("Connecting to %d.%d.%d.%d:%i\n", - (ip)&0xFF,(ip>>8)&0xFF,(ip>>16)&0xFF,(ip>>24)&0xFF,port); - - result = connect(fd, (struct sockaddr *)(&server_address), sizeof(struct sockaddr_in)); -#ifdef __WIN32 - if( result == SOCKET_ERROR ) { - ShowError("connect failed (socket %d, code %d)!\n", fd, WSAGetLastError()); - do_close(fd); - return -1; - } -#else - if (result < 0) { //This is only used when the map/char server try to connect to each other, so it can be handled. [Skotlex] - perror("make_connection"); - do_close(fd); - return -1; - } -#endif -//Now the socket can be made non-blocking. [Skotlex] -#ifdef __WIN32 - { - unsigned long val = 1; - if (ioctlsocket(fd, FIONBIO, &val) != 0) - ShowError("Couldn't set the socket to non-blocking mode (code %d)!\n", WSAGetLastError()); - } -#else - if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) - perror("make_connection (set nonblock)"); -#endif - - if (fd_max <= fd) - fd_max = fd + 1; - FD_SET(fd,&readfds); - - CREATE(session[fd], struct socket_data, 1); - CREATE_A(session[fd]->rdata, unsigned char, rfifo_size); - CREATE_A(session[fd]->wdata, unsigned char, wfifo_size); - - session[fd]->max_rdata = (int)rfifo_size; - session[fd]->max_wdata = (int)wfifo_size; - session[fd]->func_recv = recv_to_fifo; - session[fd]->func_send = send_from_fifo; - session[fd]->func_parse = default_func_parse; - session[fd]->rdata_tick = last_tick; - - return fd; -} - -void free_session_mem(int fd) -{ - if (session[fd]){ - if (session[fd]->rdata) - aFree(session[fd]->rdata); - if (session[fd]->wdata) - aFree(session[fd]->wdata); - if (session[fd]->session_data) - aFree(session[fd]->session_data); - aFree(session[fd]); - session[fd] = NULL; - } -} - -int delete_session(int fd) -{ - if (fd <= 0 || fd >= FD_SETSIZE) - return -1; - FD_CLR(fd, &readfds); - free_session_mem(fd); - //ShowMessage("delete_session:%d\n",fd); - return 0; -} - -int realloc_fifo(int fd,unsigned int rfifo_size,unsigned int wfifo_size) -{ - if( !session_isValid(fd) ) - return 0; - - if( session[fd]->max_rdata != rfifo_size && session[fd]->rdata_size < rfifo_size){ - RECREATE(session[fd]->rdata, unsigned char, rfifo_size); - session[fd]->max_rdata = rfifo_size; - } - - if( session[fd]->max_wdata != wfifo_size && session[fd]->wdata_size < wfifo_size){ - RECREATE(session[fd]->wdata, unsigned char, wfifo_size); - session[fd]->max_wdata = wfifo_size; - } - return 0; -} - -int realloc_writefifo(int fd, size_t addition) -{ - size_t newsize; - - if( !session_isValid(fd) ) // might not happen - return 0; - - if( session[fd]->wdata_size + (int)addition > session[fd]->max_wdata ) - { // grow rule; grow in multiples of wfifo_size - newsize = wfifo_size; - while( session[fd]->wdata_size + addition > newsize ) newsize += newsize; - } - else if( session[fd]->max_wdata>=FIFOSIZE_SERVERLINK) { - //Inter-server adjust. [Skotlex] - if ((session[fd]->wdata_size+(int)addition)*4 < session[fd]->max_wdata) - newsize = session[fd]->max_wdata/2; - else - return 0; //No change - } else if( session[fd]->max_wdata>(int)wfifo_size && - (session[fd]->wdata_size+(int)addition)*4 < session[fd]->max_wdata ) - { // shrink rule, shrink by 2 when only a quater of the fifo is used, don't shrink below 4*addition - newsize = session[fd]->max_wdata/2; - } - else // no change - return 0; - - RECREATE(session[fd]->wdata, unsigned char, newsize); - session[fd]->max_wdata = (int)newsize; - - return 0; -} - -int WFIFOSET(int fd,int len) -{ - size_t newreserve; - struct socket_data *s = session[fd]; - - if( !session_isValid(fd) || s->wdata == NULL ) - return 0; - - // we have written len bytes to the buffer already before calling WFIFOSET - if(s->wdata_size+len > s->max_wdata) - { // actually there was a buffer overflow already - unsigned char *sin_addr = (unsigned char *)&s->client_addr.sin_addr; - ShowFatalError("socket: Buffer Overflow. Connection %d (%d.%d.%d.%d) has written %d byteson a %d/%d bytes buffer.\n", fd, - sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3], len, s->wdata_size, s->max_wdata); - ShowDebug("Likely command that caused it: 0x%x\n", - (*(unsigned short*)(s->wdata+s->wdata_size))); - // no other chance, make a better fifo model - exit(1); - } - - s->wdata_size += len; - // always keep a wfifo_size reserve in the buffer - // For inter-server connections, let the reserve be 1/8th of the link size. - newreserve = s->wdata_size + (s->max_wdata>=FIFOSIZE_SERVERLINK?FIFOSIZE_SERVERLINK<<3:wfifo_size); - - if(s->wdata_size >= frame_size) - send_from_fifo(fd); - - // realloc after sending - // readfifo does not need to be realloced at all - // Even the inter-server buffer may need reallocating! [Skotlex] - realloc_writefifo(fd, newreserve); - - return 0; -} - -int do_sendrecv(int next) -{ - fd_set rfd,efd; //Added the Error Set so that such sockets can be made eof. They are the same as the rfd for now. [Skotlex] - struct sockaddr_in addr_check; - struct timeval timeout; - int ret,i,size; - - last_tick = time(0); - - - //PRESEND Need to do this to ensure that the clients get something to do - //which hopefully will cause them to send packets. [Meruru] - for (i = 1; i < fd_max; i++) - { - if(!session[i]) - continue; - - if(session[i]->wdata_size && session[i]->func_send) - session[i]->func_send(i); - } - - timeout.tv_sec = next/1000; - timeout.tv_usec = next%1000*1000; - - for(memcpy(&rfd, &readfds, sizeof(rfd)), - memcpy(&efd, &readfds, sizeof(efd)); - (ret = select(fd_max, &rfd, NULL, &efd, &timeout))<0; - memcpy(&rfd, &readfds, sizeof(rfd)), - memcpy(&efd, &readfds, sizeof(efd))) - { - if(serrno != SEBADF) - return 0; - - //Well then the error is due to a bad socket. Lets find and remove it - //and try again - for(i = 1; i < fd_max; i++) - { - if(!session[i]) - continue; - - //check the validity of the socket. Does what the last thing did - //just alot faster [Meruru] - size = sizeof(struct sockaddr); - if(getsockname(i,(struct sockaddr*)&addr_check,&size)<0) - if(serrno == SEBADF) //See the #defines at the top - { - free_session_mem(i); //free the bad session - continue; - } - - FD_SET(i,&readfds); - ret = i; - } - - fd_max = ret; - } - - //ok under windows to use FD_ISSET is FUCKING stupid - //because windows uses an array so lets do them part by part [Meruru] -#ifdef _WIN32 - //Do the socket sets. Unlike linux which uses a bit mask windows uses - //a array. So calls to FS_ISSET are SLOW AS SHIT. So we have to do - //a special case for them which actually turns out ok [Meruru] - for(i=0;i<(int)rfd.fd_count;i++) - { - if(session[rfd.fd_array[i]] && - session[rfd.fd_array[i]]->func_recv) - session[rfd.fd_array[i]]->func_recv(rfd.fd_array[i]); - } - for(i=0;i<(int)efd.fd_count;i++) - set_eof(efd.fd_array[i]); - - for (i = 1; i < fd_max; i++) - { - if(!session[i]) - continue; - - //POSTSEND: Does write EVER BLOCK? NO!! not unless WE ARE CURRENTLY SENDING SOMETHING - //Or just have opened a connection and dont know if its ready - //And since eA isn't multi threaded and all the sockets are non blocking THIS ISNT A PROBLEM! [Meruru] - - if(session[i]->wdata_size && session[i]->func_send) - session[i]->func_send(i); - - if(session[i] && session[i]->eof) //The session check is for when the connection ended in func_parse - { //Finally, even if there is no data to parse, connections signalled eof should be closed, so we call parse_func [Skotlex] - if (session[i]->func_parse) - session[i]->func_parse(i); //This should close the session inmediately. - } - } - -#else //where under linux its just a bit check so its smart [Meruru] - - for (i = 1; i < fd_max; i++){ - if(!session[i]) - continue; - - if(FD_ISSET(i,&efd)){ - //ShowMessage("error:%d\n",i); - ShowDebug("do_sendrecv: Connection error on Session %d.\n", i); - set_eof(i); - continue; - } - - - if(FD_ISSET(i,&rfd)){ - //ShowMessage("read:%d\n",i); - if(session[i]->func_recv) - session[i]->func_recv(i); - } - - //Does write EVER BLOCK. NO not unless WE ARE CURRENTALLY SENDING SOMETHING - //And sence eA isnt multi threaded THIS ISNT A PROBLEM! - if(session[i]->wdata_size && session[i]->func_send) - session[i]->func_send(i); - - if(session[i] && session[i]->eof) //The session check is for when the connection ended in func_parse - { //Finally, even if there is no data to parse, connections signalled eof should be closed, so we call parse_func [Skotlex] - if (session[i]->func_parse) - session[i]->func_parse(i); //This should close the session inmediately. - } - } -#endif - - return 0; -} - -int do_parsepacket(void) -{ - int i; - struct socket_data *sd; - for(i = 1; i < fd_max; i++){ - sd = session[i]; - if(!sd) - continue; - if (sd->rdata_tick && DIFF_TICK(last_tick,sd->rdata_tick) > stall_time) { - ShowInfo ("Session #%d timed out\n", i); - sd->eof = 1; - } - if(sd->rdata_size == 0 && sd->eof == 0) - continue; - if(sd->func_parse){ - if(sd->type == SESSION_UNKNOWN) - func_parse_check(sd); - if(sd->type != SESSION_UNKNOWN) - sd->func_parse(i); - if(!session[i]) - continue; - /* after parse, check client's RFIFO size to know if there is an invalid packet (too big and not parsed) */ - if (session[i]->rdata_size == rfifo_size && session[i]->max_rdata == rfifo_size) { - session[i]->eof = 1; - continue; - } - } - RFIFOFLUSH(i); - } - return 0; -} - -/* DDoS 攻撃対策 */ -#ifndef MINICORE -struct _access_control { - unsigned int ip; - unsigned int mask; -}; - -struct _connect_history { - struct _connect_history *next; - struct _connect_history *prev; - int status; - int count; - unsigned int ip; - unsigned int tick; -}; -static struct _connect_history *connect_history[0x10000]; -static int connect_check_(unsigned int ip); - -// 接続できるかどうかの確認 -// false : 接続OK -// true : 接続NG -static int connect_check(unsigned int ip) { - int result = connect_check_(ip); - if(access_debug) { - ShowMessage("connect_check: Connection from %d.%d.%d.%d %s\n", - CONVIP(ip),result ? "allowed." : "denied!"); - } - return result; -} - -static int connect_check_(unsigned int ip) { - struct _connect_history *hist = connect_history[ip & 0xFFFF]; - struct _connect_history *hist_new; - int i,is_allowip = 0,is_denyip = 0,connect_ok = 0; - - // allow , deny リストに入っているか確認 - for(i = 0;i < access_allownum; i++) { - if((ip & access_allow[i].mask) == (access_allow[i].ip & access_allow[i].mask)) { - if(access_debug) { - ShowMessage("connect_check: Found match from allow list:%d.%d.%d.%d IP:%d.%d.%d.%d Mask:%d.%d.%d.%d\n", - CONVIP(ip), - CONVIP(access_allow[i].ip), - CONVIP(access_allow[i].mask)); - } - is_allowip = 1; - break; - } - } - for(i = 0;i < access_denynum; i++) { - if((ip & access_deny[i].mask) == (access_deny[i].ip & access_deny[i].mask)) { - if(access_debug) { - ShowMessage("connect_check: Found match from deny list:%d.%d.%d.%d IP:%d.%d.%d.%d Mask:%d.%d.%d.%d\n", - CONVIP(ip), - CONVIP(access_deny[i].ip), - CONVIP(access_deny[i].mask)); - } - is_denyip = 1; - break; - } - } - // コネクト出来るかどうか確認 - // connect_ok - // 0 : 無条件に拒否 - // 1 : 田代砲チェックの結果次第 - // 2 : 無条件に許可 - switch(access_order) { - case ACO_DENY_ALLOW: - default: - if(is_allowip) { - connect_ok = 2; - } else if(is_denyip) { - connect_ok = 0; - } else { - connect_ok = 1; - } - break; - case ACO_ALLOW_DENY: - if(is_denyip) { - connect_ok = 0; - } else if(is_allowip) { - connect_ok = 2; - } else { - connect_ok = 1; - } - break; - case ACO_MUTUAL_FAILTURE: - if(is_allowip) { - connect_ok = 2; - } else { - connect_ok = 0; - } - break; - } - - // 接続履歴を調べる - while(hist) { - if(ip == hist->ip) { - // 同じIP発見 - if(hist->status) { - // ban フラグが立ってる - return (connect_ok == 2 ? 1 : 0); - } else if(DIFF_TICK(gettick(),hist->tick) < ddos_interval) { - // ddos_interval秒以内にリクエスト有り - hist->tick = gettick(); - if(hist->count++ >= ddos_count) { - // ddos 攻撃を検出 - hist->status = 1; - ShowWarning("connect_check: DDOS Attack detected from %d.%d.%d.%d!\n", - CONVIP(ip)); - return (connect_ok == 2 ? 1 : 0); - } else { - return connect_ok; - } - } else { - // ddos_interval秒以内にリクエスト無いのでタイマークリア - hist->tick = gettick(); - hist->count = 0; - return connect_ok; - } - } - hist = hist->next; - } - // IPリストに無いので新規作成 - hist_new = (struct _connect_history *) aCalloc(1,sizeof(struct _connect_history)); - hist_new->ip = ip; - hist_new->tick = gettick(); - if(connect_history[ip & 0xFFFF] != NULL) { - hist = connect_history[ip & 0xFFFF]; - hist->prev = hist_new; - hist_new->next = hist; - } - connect_history[ip & 0xFFFF] = hist_new; - return connect_ok; -} - -static int connect_check_clear(int tid,unsigned int tick,int id,int data) { - int i; - int clear = 0; - int list = 0; - struct _connect_history *hist , *hist2; - for(i = 0;i < 0x10000 ; i++) { - hist = connect_history[i]; - while(hist) { - if ((DIFF_TICK(tick,hist->tick) > ddos_interval * 3 && !hist->status) || - (DIFF_TICK(tick,hist->tick) > ddos_autoreset && hist->status)) { - // clear data - hist2 = hist->next; - if(hist->prev) { - hist->prev->next = hist->next; - } else { - connect_history[i] = hist->next; - } - if(hist->next) { - hist->next->prev = hist->prev; - } - aFree(hist); - hist = hist2; - clear++; - } else { - hist = hist->next; - } - list++; - } - } - if(access_debug) { - ShowMessage("connect_check_clear: Cleared %d of %d from IP list.\n", clear, list); - } - return list; -} - -// IPマスクチェック -int access_ipmask(const char *str,struct _access_control* acc) -{ - unsigned int mask=0,i=0,m,ip, a0,a1,a2,a3; - if( !strcmp(str,"all") ) { - ip = 0; - mask = 0; - } else { - if( sscanf(str,"%d.%d.%d.%d%n",&a0,&a1,&a2,&a3,&i)!=4 || i==0) { - ShowError("access_ipmask: Unknown format %s!\n",str); - return 0; - } - ip = (a3 << 24) | (a2 << 16) | (a1 << 8) | a0; - - if(sscanf(str+i,"/%d.%d.%d.%d",&a0,&a1,&a2,&a3)==4 ){ - mask = (a3 << 24) | (a2 << 16) | (a1 << 8) | a0; - } else if(sscanf(str+i,"/%d",&m) == 1) { - for(i=0;i> 1) | 0x80000000; - } - mask = ntohl(mask); - } else { - mask = 0xFFFFFFFF; - } - } - if(access_debug) { - ShowMessage("access_ipmask: Loaded IP:%d.%d.%d.%d mask:%d.%d.%d.%d\n", - CONVIP(ip), CONVIP(mask)); - } - acc->ip = ip; - acc->mask = mask; - return 1; -} -#endif - -int socket_config_read(const char *cfgName) { - int i; - char line[1024],w1[1024],w2[1024]; - FILE *fp; - - fp=fopen(cfgName, "r"); - if(fp==NULL){ - ShowError("File not found: %s\n", cfgName); - return 1; - } - while(fgets(line,1020,fp)){ - if(line[0] == '/' && line[1] == '/') - continue; - i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2); - if(i!=2) - continue; - if(strcmpi(w1,"stall_time")==0){ - stall_time = atoi(w2); - #ifndef MINICORE - } else if(strcmpi(w1,"enable_ip_rules")==0){ - if(strcmpi(w2,"yes")==0) - ip_rules = 1; - else if(strcmpi(w2,"no")==0) - ip_rules = 0; - else ip_rules = atoi(w2); - } else if(strcmpi(w1,"order")==0){ - access_order=atoi(w2); - if(strcmpi(w2,"deny,allow")==0) access_order=ACO_DENY_ALLOW; - if(strcmpi(w2,"allow,deny")==0) access_order=ACO_ALLOW_DENY; - if(strcmpi(w2,"mutual-failure")==0) access_order=ACO_MUTUAL_FAILTURE; - } else if(strcmpi(w1,"allow")==0){ - access_allow = (struct _access_control *) aRealloc(access_allow,(access_allownum+1)*sizeof(struct _access_control)); - if(access_ipmask(w2,&access_allow[access_allownum])) { - access_allownum++; - } - } else if(strcmpi(w1,"deny")==0){ - access_deny = (struct _access_control *) aRealloc(access_deny,(access_denynum+1)*sizeof(struct _access_control)); - if(access_ipmask(w2,&access_deny[access_denynum])) { - access_denynum++; - } - } else if(!strcmpi(w1,"ddos_interval")){ - ddos_interval = atoi(w2); - } else if(!strcmpi(w1,"ddos_count")){ - ddos_count = atoi(w2); - } else if(!strcmpi(w1,"ddos_autoreset")){ - ddos_autoreset = atoi(w2); - } else if(!strcmpi(w1,"debug")){ - if(strcmpi(w2,"yes")==0) - access_debug = 1; - else if(strcmpi(w2,"no")==0) - access_debug = 0; - else access_debug = atoi(w2); - #endif - } else if (strcmpi(w1, "mode_neg") == 0) - { - if(strcmpi(w2,"yes")==0) - mode_neg = 1; - else if(strcmpi(w2,"no")==0) - mode_neg = 0; - else mode_neg = atoi(w2); - } else if (strcmpi(w1, "frame_size") == 0) - frame_size = strtoul(w2, NULL, 10); - else if (strcmpi(w1, "import") == 0) - socket_config_read(w2); - } - fclose(fp); - return 0; -} - -int RFIFOSKIP(int fd,int len) -{ - struct socket_data *s; - - if ( !session_isActive(fd) ) //Nullpo error here[Kevin] - return 0; - - s = session[fd]; - - if ((signed int)(s->rdata_size-s->rdata_pos-len)<0) { - //fprintf(stderr,"too many skip\n"); - //exit(1); - //better than a COMPLETE program abort // TEST! :) - ShowError("too many skip (%d) now skipped: %d (FD: %d)\n", len, RFIFOREST(fd), fd); - len = RFIFOREST(fd); - } - s->rdata_pos = s->rdata_pos+len; - return 0; -} - - -unsigned int addr_[16]; // ip addresses of local host (host byte order) -unsigned int naddr_ = 0; // # of ip addresses - -void socket_final (void) -{ - int i; -#ifndef MINICORE - struct _connect_history *hist , *hist2; - for(i = 0; i < 0x10000; i++) { - hist = connect_history[i]; - while(hist) { - hist2 = hist->next; - aFree(hist); - hist = hist2; - } - } - if (access_allow) - aFree(access_allow); - if (access_deny) - aFree(access_deny); -#endif - - for (i = 1; i < fd_max; i++) { - if(session[i]) - delete_session(i); - } - - // session[0] のダミーデータを削除 - aFree(session[0]->rdata); - aFree(session[0]->wdata); - aFree(session[0]); -} - -//Closes a socket. -//Needed to simplify shutdown code as well as manage the subtle differences in socket management from Windows and *nix. -void do_close(int fd) -{ -//We don't really care if these closing functions return an error, we are just shutting down and not reusing this socket. -#ifdef __WIN32 -// shutdown(fd, SD_BOTH); //FIXME: Shutdown requires winsock2.h! What would be the proper shutting down method for winsock1? - if (session[fd] && session[fd]->func_send == send_from_fifo) - session[fd]->func_send(fd); //Flush the data as it is gonna be closed down, but it may not succeed as it is a nonblocking socket! [Skotlex] - closesocket(fd); -#else - if (close(fd)) - perror("do_close: close"); -#endif - if (session[fd]) - delete_session(fd); -} - -void socket_init (void) -{ - char *SOCKET_CONF_FILENAME = "conf/packet_athena.conf"; -#ifdef __WIN32 - char** a; - unsigned int i; - char fullhost[255]; - struct hostent* hent; - - /* Start up the windows networking */ - WORD version_wanted = MAKEWORD(1, 1); //Demand at least WinSocket version 1.1 (from Freya) - WSADATA wsaData; - - if ( WSAStartup(version_wanted, &wsaData) != 0 ) { - ShowFatalError("SYSERR: WinSock not available!\n"); - exit(1); - } - - if(gethostname(fullhost, sizeof(fullhost)) == SOCKET_ERROR) { - ShowError("Ugg.. no hostname defined!\n"); - return; - } - - // XXX This should look up the local IP addresses in the registry - // instead of calling gethostbyname. However, the way IP addresses - // are stored in the registry is annoyingly complex, so I'll leave - // this as T.B.D. - hent = gethostbyname(fullhost); - if (hent == NULL) { - ShowError("Cannot resolve our own hostname to a IP address"); - return; - } - - a = hent->h_addr_list; - for(i = 0; a[i] != 0 && i < 16; ++i) { - unsigned long addr1 = ntohl(*(unsigned long*) a[i]); - addr_[i] = addr1; - } - naddr_ = i; -#else - int pos; - int fdes = socket(AF_INET, SOCK_STREAM, 0); - char buf[16 * sizeof(struct ifreq)]; - struct ifconf ic; - - // The ioctl call will fail with Invalid Argument if there are more - // interfaces than will fit in the buffer - ic.ifc_len = sizeof(buf); - ic.ifc_buf = buf; - if(ioctl(fdes, SIOCGIFCONF, &ic) == -1) { - ShowError("SIOCGIFCONF failed!\n"); - return; - } - - for(pos = 0; pos < ic.ifc_len;) - { - struct ifreq * ir = (struct ifreq *) (ic.ifc_buf + pos); - - struct sockaddr_in * a = (struct sockaddr_in *) &(ir->ifr_addr); - - if(a->sin_family == AF_INET) { - u_long ad = ntohl(a->sin_addr.s_addr); - if(ad != INADDR_LOOPBACK && ad != INADDR_ANY) { - addr_[naddr_ ++] = ad; - if(naddr_ == 16) - break; - } - } - - #if defined(_AIX) || defined(__APPLE__) - pos += ir->ifr_addr.sa_len; // For when we port athena to run on Mac's :) - pos += sizeof(ir->ifr_name); - #else - pos += sizeof(struct ifreq); - #endif - } -#endif - - FD_ZERO(&readfds); - - socket_config_read(SOCKET_CONF_FILENAME); - - // initialise last send-receive tick - last_tick = time(0); - - // session[0] Was for the console (whatever that was?), but is now currently used for disconnected sessions of the map - // server, and as such, should hold enough buffer (it is a vacuum so to speak) as it is never flushed. [Skotlex] - CREATE(session[0], struct socket_data, 1); - CREATE_A(session[0]->rdata, unsigned char, 2*rfifo_size); - CREATE_A(session[0]->wdata, unsigned char, 2*wfifo_size); - session[0]->max_rdata = (int)2*rfifo_size; - session[0]->max_wdata = (int)2*wfifo_size; - - malloc_set (func_parse_table, 0, sizeof(func_parse_table)); - func_parse_table[SESSION_RAW].check = default_func_check; - func_parse_table[SESSION_RAW].func = default_func_parse; - -#ifndef MINICORE - // とりあえず5分ごとに不要なデータを削除する - add_timer_func_list(connect_check_clear, "connect_check_clear"); - add_timer_interval(gettick()+1000,connect_check_clear,0,0,300*1000); -#endif -} - - -bool session_isValid(int fd) -{ //End of Exam has pointed out that fd==0 is actually an unconnected session! [Skotlex] - //But this is not so true, it is used... for... something. The console uses it, would this not cause problems? [Skotlex] - return ( (fd>0) && (fdeof ); -} - -in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str) { - struct hostent *h = gethostbyname(hostname); - char ip_buf[16]; - unsigned char ip2[4]; - if (!h) return 0; - if (ip == NULL) ip = ip2; - ip[0] = (unsigned char) h->h_addr[0]; - ip[1] = (unsigned char) h->h_addr[1]; - ip[2] = (unsigned char) h->h_addr[2]; - ip[3] = (unsigned char) h->h_addr[3]; - if (ip_str == NULL) ip_str = ip_buf; - sprintf(ip_str, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); - return inet_addr(ip_str); -} +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL +// For more information, see LICENCE in the main folder + +#include +#include +#include +#include + +#ifdef __WIN32 +#define __USE_W32_SOCKETS +#include +#include +#include + +typedef int socklen_t; +#else +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef SIOCGIFCONF +#include // SIOCGIFCONF on Solaris, maybe others? [Shinomori] +#endif + +#endif + +#ifdef _WIN32 +#define SEBADF WSAENOTSOCK +#define serrno WSAGetLastError() +#else +#define SEBADF EBADF +#define serrno errno +#endif + +#include +#include + +#include "../common/socket.h" +#include "../common/mmo.h" // [Valaris] thanks to fov +#include "../common/timer.h" +#include "../common/malloc.h" +#include "../common/showmsg.h" + +fd_set readfds; +int fd_max; +time_t last_tick; +time_t stall_time = 60; +int ip_rules = 1; + +// reuse port +#ifndef SO_REUSEPORT + #define SO_REUSEPORT 15 +#endif + +#ifndef TCP_FRAME_LEN +#define TCP_FRAME_LEN 1024 +#endif + +static int mode_neg=1; +static size_t frame_size=TCP_FRAME_LEN; + +#ifndef MINICORE +enum { + ACO_DENY_ALLOW=0, + ACO_ALLOW_DENY, + ACO_MUTUAL_FAILTURE, +}; + +static struct _access_control *access_allow; +static struct _access_control *access_deny; +static int access_order=ACO_DENY_ALLOW; +static int access_allownum=0; +static int access_denynum=0; +static int access_debug=0; +static int ddos_count = 10; +static int ddos_interval = 3000; +static int ddos_autoreset = 600*1000; +#endif + + +// values derived from freya +// a player that send more than 2k is probably a hacker without be parsed +// biggest known packet: S 0153 .w .?B -> 24x24 256 color .bmp (0153 + len.w + 1618/1654/1756 bytes) +size_t rfifo_size = (16*1024); +size_t wfifo_size = (16*1024); + +#define CONVIP(ip) ip&0xFF,(ip>>8)&0xFF,(ip>>16)&0xFF,ip>>24 + +struct socket_data *session[FD_SETSIZE]; + +static int null_parse(int fd); +static int (*default_func_parse)(int) = null_parse; + +static int null_console_parse(char *buf); +static int (*default_console_parse)(char*) = null_console_parse; +#ifndef MINICORE +static int connect_check(unsigned int ip); +#else + #define connect_check(n) 1 +#endif + +/*====================================== + * CORE : Set function + *-------------------------------------- + */ +void set_defaultparse(int (*defaultparse)(int)) +{ + default_func_parse = defaultparse; +} + +void set_nonblocking(int fd, int yes) { + // I don't think we need this + // TCP_NODELAY BOOL Disables the Nagle algorithm for send coalescing. + if(mode_neg) + setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof yes); + + // FIONBIO Use with a nonzero argp parameter to enable the nonblocking mode of socket s. + // The argp parameter is zero if nonblocking is to be disabled. +#ifdef __WIN32 + ioctlsocket(fd, FIONBIO, &yes); +#else + ioctl(fd,FIONBIO,&yes); +#endif +} + +static void setsocketopts(int fd) +{ + int yes = 1; // reuse fix +#ifndef WIN32 + // set SO_REAUSEADDR to true, unix only. on windows this option causes + // the previous owner of the socket to give up, which is not desirable + // in most cases, neither compatible with unix. + setsockopt(fd,SOL_SOCKET,SO_REUSEADDR,(char *)&yes,sizeof(yes)); +#ifdef SO_REUSEPORT + setsockopt(fd,SOL_SOCKET,SO_REUSEPORT,(char *)&yes,sizeof(yes)); +#endif +#endif + setsockopt(fd,IPPROTO_TCP,TCP_NODELAY,(char *)&yes,sizeof(yes)); +// setsockopt(fd, SOL_SOCKET, SO_SNDBUF, (char *) &wfifo_size , sizeof(rfifo_size )); +// setsockopt(fd, SOL_SOCKET, SO_RCVBUF, (char *) &rfifo_size , sizeof(rfifo_size )); +#ifdef __WIN32 +{ //set SO_LINGER option (from Freya) + //(http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winsock/winsock/closesocket_2.asp) + struct linger opt; + opt.l_onoff = 1; + opt.l_linger = 0; + if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&opt, sizeof(opt))) + ShowWarning("setsocketopts: Unable to set SO_LINGER mode for connection %d!\n",fd); +} +#endif +} + +/*====================================== + * CORE : Socket Sub Function + *-------------------------------------- + */ +static void set_eof(int fd) +{ //Marks a connection eof and invokes the parse_function to disconnect it right away. [Skotlex] + if (session_isActive(fd)) + session[fd]->eof=1; +} + +static int recv_to_fifo(int fd) +{ + int len; + + if( (fd<0) || (fd>=FD_SETSIZE) || (NULL==session[fd]) ) + return -1; + + if(session[fd]->eof) + return -1; + +#ifdef __WIN32 + len=recv(fd,(char *)session[fd]->rdata+session[fd]->rdata_size, RFIFOSPACE(fd), 0); + if (len == SOCKET_ERROR) { + if (WSAGetLastError() == WSAECONNABORTED) { + ShowWarning("recv_to_fifo: Software caused connection abort on session #%d\n", fd); + FD_CLR(fd, &readfds); //Remove the socket so the select() won't hang on it. +// exit(1); //Windows can't really recover from this one. [Skotlex] + } + if (WSAGetLastError() != WSAEWOULDBLOCK) { +// ShowDebug("recv_to_fifo: error %d, ending connection #%d\n", WSAGetLastError(), fd); + set_eof(fd); + } + return 0; + } +#else + len=read(fd,session[fd]->rdata+session[fd]->rdata_size, RFIFOSPACE(fd)); + if (len == -1) + { + if (errno == ECONNABORTED) + { + ShowFatalError("recv_to_fifo: Network broken (Software caused connection abort on session #%d)\n", fd); +// exit(1); //Temporal debug, maybe this can be fixed. + } + if (errno != EAGAIN) { //Connection error. +// perror("closing session: recv_to_fifo"); + set_eof(fd); + } + return 0; + } +#endif + if (len <= 0) { //Normal connection end. + set_eof(fd); + return 0; + } + + session[fd]->rdata_size+=len; + session[fd]->rdata_tick = last_tick; + return 0; +} + +static int send_from_fifo(int fd) +{ + int len; + if( !session_isValid(fd) ) + return -1; + +// if (s->eof) // if we close connection, we can not send last information (you're been disconnected, etc...) [Yor] +// return -1; +/* + // clear write buffer if not connected <- I really like more the idea of sending the last information. [Skotlex] + if( session[fd]->eof ) + { + session[fd]->wdata_size = 0; + return -1; + } +*/ + + if (session[fd]->wdata_size == 0) + return 0; + +#ifdef __WIN32 + len=send(fd, (const char *)session[fd]->wdata,session[fd]->wdata_size, 0); + if (len == SOCKET_ERROR) { + if (WSAGetLastError() == WSAECONNABORTED) { + ShowWarning("send_from_fifo: Software caused connection abort on session #%d\n", fd); + session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] + set_eof(fd); + FD_CLR(fd, &readfds); //Remove the socket so the select() won't hang on it. + } + if (WSAGetLastError() != WSAEWOULDBLOCK) { +// ShowDebug("send_from_fifo: error %d, ending connection #%d\n", WSAGetLastError(), fd); + session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] + set_eof(fd); + } + return 0; + } +#else + len=send(fd, session[fd]->wdata, session[fd]->wdata_size, MSG_NOSIGNAL); + if (len == -1) + { + if (errno == ECONNABORTED) + { + ShowWarning("send_from_fifo: Network broken (Software caused connection abort on session #%d)\n", fd); + session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] + set_eof(fd); + } + if (errno != EAGAIN) { +// perror("closing session: send_from_fifo"); + session[fd]->wdata_size = 0; //Clear the send queue as we can't send anymore. [Skotlex] + set_eof(fd); + } + return 0; + } +#endif + + //{ int i; ShowMessage("send %d : ",fd); for(i=0;iwdata[i]); } ShowMessage("\n");} + if(len>0){ + if((size_t)lenwdata_size){ + memmove(session[fd]->wdata,session[fd]->wdata+len,session[fd]->wdata_size-len); + session[fd]->wdata_size-=len; + } else { + session[fd]->wdata_size=0; + } + } + return 0; +} + +void flush_fifo(int fd) +{ + if(session[fd] != NULL && session[fd]->func_send == send_from_fifo) + { + set_nonblocking(fd, 1); + send_from_fifo(fd); + set_nonblocking(fd, 0); + } +} + +void flush_fifos(void) +{ + int i; + for(i=1;ifunc_send == send_from_fifo) + send_from_fifo(i); +} + +static int null_parse(int fd) +{ + ShowMessage("null_parse : %d\n",fd); + session[fd]->rdata_pos = session[fd]->rdata_size; //RFIFOSKIP(fd, RFIFOREST(fd)); simplify calculation + return 0; +} + +/*====================================== + * CORE : Socket Function + *-------------------------------------- + */ + +static int connect_client(int listen_fd) +{ + int fd; + struct sockaddr_in client_address; +#ifdef __WIN32 + int len; +#else + socklen_t len; +#endif + //ShowMessage("connect_client : %d\n",listen_fd); + + len=sizeof(client_address); + + fd = accept(listen_fd,(struct sockaddr*)&client_address,&len); +#ifdef __WIN32 + if ( fd == INVALID_SOCKET ) { + ShowError("accept failed (code %i)!\n", WSAGetLastError()); + return -1; + } +#else + if(fd==-1) { + perror("accept"); + return -1; + } +#endif + + if(fd_max<=fd) fd_max=fd+1; + + setsocketopts(fd); + +#ifdef __WIN32 + { + unsigned long val = 1; + if (ioctlsocket(fd, FIONBIO, &val) != 0) + ShowError("Couldn't set the socket to non-blocking mode (code %d)!\n", WSAGetLastError()); + } +#else + if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) + perror("connect_client (set nonblock)"); +#endif + + if (ip_rules && !connect_check(*(unsigned int*)(&client_address.sin_addr))) { + do_close(fd); + return -1; + } else + FD_SET(fd,&readfds); + + CREATE(session[fd], struct socket_data, 1); + CREATE(session[fd]->rdata, unsigned char, rfifo_size); + CREATE(session[fd]->wdata, unsigned char, wfifo_size); + + session[fd]->max_rdata = rfifo_size; + session[fd]->max_wdata = wfifo_size; + session[fd]->func_recv = recv_to_fifo; + session[fd]->func_send = send_from_fifo; + if(!session[listen_fd]->func_parse) + session[fd]->func_parse = default_func_parse; + else + session[fd]->func_parse = session[listen_fd]->func_parse; + session[fd]->client_addr = client_address; + session[fd]->rdata_tick = last_tick; + session[fd]->type = SESSION_UNKNOWN; // undefined type + + //ShowMessage("new_session : %d %d\n",fd,session[fd]->eof); + return fd; +} + +int make_listen_bind(long ip,int port) +{ + struct sockaddr_in server_address; + int fd; + int result; + + fd = (int)socket( AF_INET, SOCK_STREAM, 0 ); + +#ifdef __WIN32 + if (fd == INVALID_SOCKET) { + ShowError("socket() creation failed (code %d)!\n", fd, WSAGetLastError()); + exit(1); + } +#else + if (fd == -1) { + perror("make_listen_port:socket()"); + exit(1); + } +#endif + +#ifdef __WIN32 + { + unsigned long val = 1; + if (ioctlsocket(fd, FIONBIO, &val) != 0) + ShowError("Couldn't set the socket to non-blocking mode (code %d)!\n", WSAGetLastError()); + } +#else + if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) + perror("make_listen_bind (set nonblock)"); +#endif + + setsocketopts(fd); + + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = ip; + server_address.sin_port = htons((unsigned short)port); + + result = bind(fd, (struct sockaddr*)&server_address, sizeof(server_address)); +#ifdef __WIN32 + if( result == SOCKET_ERROR ) { + ShowError("bind failed (socket %d, code %d)!\n", fd, WSAGetLastError()); + exit(1); + } +#else + if( result == -1 ) { + perror("bind"); + exit(1); + } +#endif + result = listen( fd, 5 ); +#ifdef __WIN32 + if( result == SOCKET_ERROR ) { + ShowError("listen failed (socket %d, code %d)!\n", fd, WSAGetLastError()); + exit(1); + } +#else + if( result != 0) { /* error */ + perror("listen"); + exit(1); + } +#endif + if ( fd < 0 || fd > FD_SETSIZE ) + { //Crazy error that can happen in Windows? (info from Freya) + ShowFatalError("listen() returned invalid fd %d!\n",fd); + exit(1); + } + + if(fd_max<=fd) fd_max=fd+1; + FD_SET(fd, &readfds ); + + CREATE(session[fd], struct socket_data, 1); + + malloc_set(session[fd],0,sizeof(*session[fd])); + session[fd]->func_recv = connect_client; + + return fd; +} + +int make_listen_port(int port) +{ + return make_listen_bind(INADDR_ANY,port); +} + +// Console Reciever [Wizputer] +int console_recieve(int i) { + int n; + char *buf; + + CREATE(buf, char, 64); + malloc_tsetdword(buf,0,sizeof(64)); + + n = read(0, buf , 64); + if ( n < 0 ) + ShowError("Console input read error\n"); + else + { + ShowNotice ("Sorry, the console is currently non-functional.\n"); +// session[0]->func_console(buf); + } + + aFree(buf); + return 0; +} + +void set_defaultconsoleparse(int (*defaultparse)(char*)) +{ + default_console_parse = defaultparse; +} + +static int null_console_parse(char *buf) +{ + ShowMessage("null_console_parse : %s\n",buf); + return 0; +} + +// function parse table +// To-do: -- use dynamic arrays +// -- add a register_parse_func(); +struct func_parse_table func_parse_table[SESSION_MAX]; + +int default_func_check (struct socket_data *sd) { return 1; } + +void func_parse_check (struct socket_data *sd) +{ + int i; + for (i = SESSION_HTTP; i < SESSION_MAX; i++) { + if (func_parse_table[i].func && + func_parse_table[i].check && + func_parse_table[i].check(sd) != 0) + { + sd->type = i; + sd->func_parse = func_parse_table[i].func; + return; + } + } + + // undefined -- treat as raw socket (using default parse) + sd->type = SESSION_RAW; +} + +// Console Input [Wizputer] +int start_console(void) { + + //Until a better plan is came up with... can't be using session[0] anymore! [Skotlex] + ShowNotice("The console is currently nonfunctional.\n"); + return 0; + + FD_SET(0,&readfds); + + if (!session[0]) { // dummy socket already uses fd 0 + CREATE(session[0], struct socket_data, 1); + } + malloc_set(session[0],0,sizeof(*session[0])); + + session[0]->func_recv = console_recieve; + session[0]->func_console = default_console_parse; + + return 0; +} + +int make_connection(long ip,int port) +{ + struct sockaddr_in server_address; + int fd; + int result; + + fd = (int)socket( AF_INET, SOCK_STREAM, 0 ); + +#ifdef __WIN32 + if (fd == INVALID_SOCKET) { + ShowError("socket() creation failed (code %d)!\n", fd, WSAGetLastError()); + return -1; + } +#else + if (fd == -1) { + perror("make_connection:socket()"); + return -1; + } +#endif + + setsocketopts(fd); + + server_address.sin_family = AF_INET; + server_address.sin_addr.s_addr = ip; + server_address.sin_port = htons((unsigned short)port); + + ShowStatus("Connecting to %d.%d.%d.%d:%i\n", + (ip)&0xFF,(ip>>8)&0xFF,(ip>>16)&0xFF,(ip>>24)&0xFF,port); + + result = connect(fd, (struct sockaddr *)(&server_address), sizeof(struct sockaddr_in)); +#ifdef __WIN32 + if( result == SOCKET_ERROR ) { + ShowError("connect failed (socket %d, code %d)!\n", fd, WSAGetLastError()); + do_close(fd); + return -1; + } +#else + if (result < 0) { //This is only used when the map/char server try to connect to each other, so it can be handled. [Skotlex] + perror("make_connection"); + do_close(fd); + return -1; + } +#endif +//Now the socket can be made non-blocking. [Skotlex] +#ifdef __WIN32 + { + unsigned long val = 1; + if (ioctlsocket(fd, FIONBIO, &val) != 0) + ShowError("Couldn't set the socket to non-blocking mode (code %d)!\n", WSAGetLastError()); + } +#else + if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) + perror("make_connection (set nonblock)"); +#endif + + if (fd_max <= fd) + fd_max = fd + 1; + FD_SET(fd,&readfds); + + CREATE(session[fd], struct socket_data, 1); + CREATE(session[fd]->rdata, unsigned char, rfifo_size); + CREATE(session[fd]->wdata, unsigned char, wfifo_size); + + session[fd]->max_rdata = rfifo_size; + session[fd]->max_wdata = wfifo_size; + session[fd]->func_recv = recv_to_fifo; + session[fd]->func_send = send_from_fifo; + session[fd]->func_parse = default_func_parse; + session[fd]->rdata_tick = last_tick; + + return fd; +} + +void free_session_mem(int fd) +{ + if (session[fd]){ + if (session[fd]->rdata) + aFree(session[fd]->rdata); + if (session[fd]->wdata) + aFree(session[fd]->wdata); + if (session[fd]->session_data) + aFree(session[fd]->session_data); + aFree(session[fd]); + session[fd] = NULL; + } +} + +int delete_session(int fd) +{ + if (fd <= 0 || fd >= FD_SETSIZE) + return -1; + FD_CLR(fd, &readfds); + free_session_mem(fd); + //ShowMessage("delete_session:%d\n",fd); + return 0; +} + +int realloc_fifo(int fd,unsigned int rfifo_size,unsigned int wfifo_size) +{ + if( !session_isValid(fd) ) + return 0; + + if( session[fd]->max_rdata != rfifo_size && session[fd]->rdata_size < rfifo_size){ + RECREATE(session[fd]->rdata, unsigned char, rfifo_size); + session[fd]->max_rdata = rfifo_size; + } + + if( session[fd]->max_wdata != wfifo_size && session[fd]->wdata_size < wfifo_size){ + RECREATE(session[fd]->wdata, unsigned char, wfifo_size); + session[fd]->max_wdata = wfifo_size; + } + return 0; +} + +int realloc_writefifo(int fd, size_t addition) +{ + size_t newsize; + + if( !session_isValid(fd) ) // might not happen + return 0; + + if( session[fd]->wdata_size + addition > session[fd]->max_wdata ) + { // grow rule; grow in multiples of wfifo_size + newsize = wfifo_size; + while( session[fd]->wdata_size + addition > newsize ) newsize += newsize; + } + else if( session[fd]->max_wdata>=FIFOSIZE_SERVERLINK) { + //Inter-server adjust. [Skotlex] + if ((session[fd]->wdata_size+addition)*4 < session[fd]->max_wdata) + newsize = session[fd]->max_wdata/2; + else + return 0; //No change + } else if( session[fd]->max_wdata>wfifo_size && + (session[fd]->wdata_size+addition)*4 < session[fd]->max_wdata ) + { // shrink rule, shrink by 2 when only a quater of the fifo is used, don't shrink below 4*addition + newsize = session[fd]->max_wdata/2; + } + else // no change + return 0; + + RECREATE(session[fd]->wdata, unsigned char, newsize); + session[fd]->max_wdata = newsize; + + return 0; +} + +int WFIFOSET(int fd,int len) +{ + size_t newreserve; + struct socket_data *s = session[fd]; + + if( !session_isValid(fd) || s->wdata == NULL ) + return 0; + + // we have written len bytes to the buffer already before calling WFIFOSET + if(s->wdata_size+len > s->max_wdata) + { // actually there was a buffer overflow already + unsigned char *sin_addr = (unsigned char *)&s->client_addr.sin_addr; + ShowFatalError("socket: Buffer Overflow. Connection %d (%d.%d.%d.%d) has written %d byteson a %d/%d bytes buffer.\n", fd, + sin_addr[0], sin_addr[1], sin_addr[2], sin_addr[3], len, s->wdata_size, s->max_wdata); + ShowDebug("Likely command that caused it: 0x%x\n", + (*(unsigned short*)(s->wdata+s->wdata_size))); + // no other chance, make a better fifo model + exit(1); + } + + s->wdata_size += len; + // always keep a wfifo_size reserve in the buffer + // For inter-server connections, let the reserve be 1/8th of the link size. + newreserve = s->wdata_size + (s->max_wdata>=FIFOSIZE_SERVERLINK?FIFOSIZE_SERVERLINK<<3:wfifo_size); + + if(s->wdata_size >= frame_size) + send_from_fifo(fd); + + // realloc after sending + // readfifo does not need to be realloced at all + // Even the inter-server buffer may need reallocating! [Skotlex] + realloc_writefifo(fd, newreserve); + + return 0; +} + +int do_sendrecv(int next) +{ + fd_set rfd,efd; //Added the Error Set so that such sockets can be made eof. They are the same as the rfd for now. [Skotlex] + struct sockaddr_in addr_check; + struct timeval timeout; + int ret,i,size; + + last_tick = time(0); + + + //PRESEND Need to do this to ensure that the clients get something to do + //which hopefully will cause them to send packets. [Meruru] + for (i = 1; i < fd_max; i++) + { + if(!session[i]) + continue; + + if(session[i]->wdata_size && session[i]->func_send) + session[i]->func_send(i); + } + + timeout.tv_sec = next/1000; + timeout.tv_usec = next%1000*1000; + + for(memcpy(&rfd, &readfds, sizeof(rfd)), + memcpy(&efd, &readfds, sizeof(efd)); + (ret = select(fd_max, &rfd, NULL, &efd, &timeout))<0; + memcpy(&rfd, &readfds, sizeof(rfd)), + memcpy(&efd, &readfds, sizeof(efd))) + { + if(serrno != SEBADF) + return 0; + + //Well then the error is due to a bad socket. Lets find and remove it + //and try again + for(i = 1; i < fd_max; i++) + { + if(!session[i]) + continue; + + //check the validity of the socket. Does what the last thing did + //just alot faster [Meruru] + size = sizeof(struct sockaddr); + if(getsockname(i,(struct sockaddr*)&addr_check,&size)<0) + if(serrno == SEBADF) //See the #defines at the top + { + free_session_mem(i); //free the bad session + continue; + } + + FD_SET(i,&readfds); + ret = i; + } + + fd_max = ret; + } + + //ok under windows to use FD_ISSET is FUCKING stupid + //because windows uses an array so lets do them part by part [Meruru] +#ifdef _WIN32 + //Do the socket sets. Unlike linux which uses a bit mask windows uses + //a array. So calls to FS_ISSET are SLOW AS SHIT. So we have to do + //a special case for them which actually turns out ok [Meruru] + for(i=0;i<(int)rfd.fd_count;i++) + { + if(session[rfd.fd_array[i]] && + session[rfd.fd_array[i]]->func_recv) + session[rfd.fd_array[i]]->func_recv(rfd.fd_array[i]); + } + for(i=0;i<(int)efd.fd_count;i++) + set_eof(efd.fd_array[i]); + + for (i = 1; i < fd_max; i++) + { + if(!session[i]) + continue; + + //POSTSEND: Does write EVER BLOCK? NO!! not unless WE ARE CURRENTLY SENDING SOMETHING + //Or just have opened a connection and dont know if its ready + //And since eA isn't multi threaded and all the sockets are non blocking THIS ISNT A PROBLEM! [Meruru] + + if(session[i]->wdata_size && session[i]->func_send) + session[i]->func_send(i); + + if(session[i]->eof) //The session check is for when the connection ended in func_parse + { //Finally, even if there is no data to parse, connections signalled eof should be closed, so we call parse_func [Skotlex] + if (session[i]->func_parse) + session[i]->func_parse(i); //This should close the session inmediately. + } + } + +#else //where under linux its just a bit check so its smart [Meruru] + + for (i = 1; i < fd_max; i++){ + if(!session[i]) + continue; + + if(FD_ISSET(i,&efd)){ + //ShowMessage("error:%d\n",i); + ShowDebug("do_sendrecv: Connection error on Session %d.\n", i); + set_eof(i); + continue; + } + + + if(FD_ISSET(i,&rfd)){ + //ShowMessage("read:%d\n",i); + if(session[i]->func_recv) + session[i]->func_recv(i); + } + + //Does write EVER BLOCK. NO not unless WE ARE CURRENTALLY SENDING SOMETHING + //And sence eA isnt multi threaded THIS ISNT A PROBLEM! + if(session[i]->wdata_size && session[i]->func_send) + session[i]->func_send(i); + + if(session[i] && session[i]->eof) //The session check is for when the connection ended in func_parse + { //Finally, even if there is no data to parse, connections signalled eof should be closed, so we call parse_func [Skotlex] + if (session[i]->func_parse) + session[i]->func_parse(i); //This should close the session inmediately. + } + } +#endif + + return 0; +} + +int do_parsepacket(void) +{ + int i; + struct socket_data *sd; + for(i = 1; i < fd_max; i++){ + sd = session[i]; + if(!sd) + continue; + if (sd->rdata_tick && DIFF_TICK(last_tick,sd->rdata_tick) > stall_time) { + ShowInfo ("Session #%d timed out\n", i); + sd->eof = 1; + } + if(sd->rdata_size == 0 && sd->eof == 0) + continue; + if(sd->func_parse){ + if(sd->type == SESSION_UNKNOWN) + func_parse_check(sd); + if(sd->type != SESSION_UNKNOWN) + sd->func_parse(i); + if(!session[i]) + continue; + /* after parse, check client's RFIFO size to know if there is an invalid packet (too big and not parsed) */ + if (session[i]->rdata_size == rfifo_size && session[i]->max_rdata == rfifo_size) { + session[i]->eof = 1; + continue; + } + } + RFIFOFLUSH(i); + } + return 0; +} + +/* DDoS 攻撃対策 */ +#ifndef MINICORE +struct _access_control { + unsigned int ip; + unsigned int mask; +}; + +struct _connect_history { + struct _connect_history *next; + struct _connect_history *prev; + int status; + int count; + unsigned int ip; + unsigned int tick; +}; +static struct _connect_history *connect_history[0x10000]; +static int connect_check_(unsigned int ip); + +// 接続できるかどうかの確認 +// false : 接続OK +// true : 接続NG +static int connect_check(unsigned int ip) { + int result = connect_check_(ip); + if(access_debug) { + ShowMessage("connect_check: Connection from %d.%d.%d.%d %s\n", + CONVIP(ip),result ? "allowed." : "denied!"); + } + return result; +} + +static int connect_check_(unsigned int ip) { + struct _connect_history *hist = connect_history[ip & 0xFFFF]; + struct _connect_history *hist_new; + int i,is_allowip = 0,is_denyip = 0,connect_ok = 0; + + // allow , deny リストに入っているか確認 + for(i = 0;i < access_allownum; i++) { + if((ip & access_allow[i].mask) == (access_allow[i].ip & access_allow[i].mask)) { + if(access_debug) { + ShowMessage("connect_check: Found match from allow list:%d.%d.%d.%d IP:%d.%d.%d.%d Mask:%d.%d.%d.%d\n", + CONVIP(ip), + CONVIP(access_allow[i].ip), + CONVIP(access_allow[i].mask)); + } + is_allowip = 1; + break; + } + } + for(i = 0;i < access_denynum; i++) { + if((ip & access_deny[i].mask) == (access_deny[i].ip & access_deny[i].mask)) { + if(access_debug) { + ShowMessage("connect_check: Found match from deny list:%d.%d.%d.%d IP:%d.%d.%d.%d Mask:%d.%d.%d.%d\n", + CONVIP(ip), + CONVIP(access_deny[i].ip), + CONVIP(access_deny[i].mask)); + } + is_denyip = 1; + break; + } + } + // コネクト出来るかどうか確認 + // connect_ok + // 0 : 無条件に拒否 + // 1 : 田代砲チェックの結果次第 + // 2 : 無条件に許可 + switch(access_order) { + case ACO_DENY_ALLOW: + default: + if(is_allowip) { + connect_ok = 2; + } else if(is_denyip) { + connect_ok = 0; + } else { + connect_ok = 1; + } + break; + case ACO_ALLOW_DENY: + if(is_denyip) { + connect_ok = 0; + } else if(is_allowip) { + connect_ok = 2; + } else { + connect_ok = 1; + } + break; + case ACO_MUTUAL_FAILTURE: + if(is_allowip) { + connect_ok = 2; + } else { + connect_ok = 0; + } + break; + } + + // 接続履歴を調べる + while(hist) { + if(ip == hist->ip) { + // 同じIP発見 + if(hist->status) { + // ban フラグが立ってる + return (connect_ok == 2 ? 1 : 0); + } else if(DIFF_TICK(gettick(),hist->tick) < ddos_interval) { + // ddos_interval秒以内にリクエスト有り + hist->tick = gettick(); + if(hist->count++ >= ddos_count) { + // ddos 攻撃を検出 + hist->status = 1; + ShowWarning("connect_check: DDOS Attack detected from %d.%d.%d.%d!\n", + CONVIP(ip)); + return (connect_ok == 2 ? 1 : 0); + } else { + return connect_ok; + } + } else { + // ddos_interval秒以内にリクエスト無いのでタイマークリア + hist->tick = gettick(); + hist->count = 0; + return connect_ok; + } + } + hist = hist->next; + } + // IPリストに無いので新規作成 + hist_new = (struct _connect_history *) aCalloc(1,sizeof(struct _connect_history)); + hist_new->ip = ip; + hist_new->tick = gettick(); + if(connect_history[ip & 0xFFFF] != NULL) { + hist = connect_history[ip & 0xFFFF]; + hist->prev = hist_new; + hist_new->next = hist; + } + connect_history[ip & 0xFFFF] = hist_new; + return connect_ok; +} + +static int connect_check_clear(int tid,unsigned int tick,int id,int data) { + int i; + int clear = 0; + int list = 0; + struct _connect_history *hist , *hist2; + for(i = 0;i < 0x10000 ; i++) { + hist = connect_history[i]; + while(hist) { + if ((DIFF_TICK(tick,hist->tick) > ddos_interval * 3 && !hist->status) || + (DIFF_TICK(tick,hist->tick) > ddos_autoreset && hist->status)) { + // clear data + hist2 = hist->next; + if(hist->prev) { + hist->prev->next = hist->next; + } else { + connect_history[i] = hist->next; + } + if(hist->next) { + hist->next->prev = hist->prev; + } + aFree(hist); + hist = hist2; + clear++; + } else { + hist = hist->next; + } + list++; + } + } + if(access_debug) { + ShowMessage("connect_check_clear: Cleared %d of %d from IP list.\n", clear, list); + } + return list; +} + +// IPマスクチェック +int access_ipmask(const char *str,struct _access_control* acc) +{ + unsigned int mask=0,i=0,m,ip, a0,a1,a2,a3; + if( !strcmp(str,"all") ) { + ip = 0; + mask = 0; + } else { + if( sscanf(str,"%d.%d.%d.%d%n",&a0,&a1,&a2,&a3,&i)!=4 || i==0) { + ShowError("access_ipmask: Unknown format %s!\n",str); + return 0; + } + ip = (a3 << 24) | (a2 << 16) | (a1 << 8) | a0; + + if(sscanf(str+i,"/%d.%d.%d.%d",&a0,&a1,&a2,&a3)==4 ){ + mask = (a3 << 24) | (a2 << 16) | (a1 << 8) | a0; + } else if(sscanf(str+i,"/%d",&m) == 1) { + for(i=0;i> 1) | 0x80000000; + } + mask = ntohl(mask); + } else { + mask = 0xFFFFFFFF; + } + } + if(access_debug) { + ShowMessage("access_ipmask: Loaded IP:%d.%d.%d.%d mask:%d.%d.%d.%d\n", + CONVIP(ip), CONVIP(mask)); + } + acc->ip = ip; + acc->mask = mask; + return 1; +} +#endif + +int socket_config_read(const char *cfgName) { + int i; + char line[1024],w1[1024],w2[1024]; + FILE *fp; + + fp=fopen(cfgName, "r"); + if(fp==NULL){ + ShowError("File not found: %s\n", cfgName); + return 1; + } + while(fgets(line,1020,fp)){ + if(line[0] == '/' && line[1] == '/') + continue; + i=sscanf(line,"%[^:]: %[^\r\n]",w1,w2); + if(i!=2) + continue; + if(strcmpi(w1,"stall_time")==0){ + stall_time = atoi(w2); + #ifndef MINICORE + } else if(strcmpi(w1,"enable_ip_rules")==0){ + if(strcmpi(w2,"yes")==0) + ip_rules = 1; + else if(strcmpi(w2,"no")==0) + ip_rules = 0; + else ip_rules = atoi(w2); + } else if(strcmpi(w1,"order")==0){ + access_order=atoi(w2); + if(strcmpi(w2,"deny,allow")==0) access_order=ACO_DENY_ALLOW; + if(strcmpi(w2,"allow,deny")==0) access_order=ACO_ALLOW_DENY; + if(strcmpi(w2,"mutual-failure")==0) access_order=ACO_MUTUAL_FAILTURE; + } else if(strcmpi(w1,"allow")==0){ + access_allow = (struct _access_control *) aRealloc(access_allow,(access_allownum+1)*sizeof(struct _access_control)); + if(access_ipmask(w2,&access_allow[access_allownum])) { + access_allownum++; + } + } else if(strcmpi(w1,"deny")==0){ + access_deny = (struct _access_control *) aRealloc(access_deny,(access_denynum+1)*sizeof(struct _access_control)); + if(access_ipmask(w2,&access_deny[access_denynum])) { + access_denynum++; + } + } else if(!strcmpi(w1,"ddos_interval")){ + ddos_interval = atoi(w2); + } else if(!strcmpi(w1,"ddos_count")){ + ddos_count = atoi(w2); + } else if(!strcmpi(w1,"ddos_autoreset")){ + ddos_autoreset = atoi(w2); + } else if(!strcmpi(w1,"debug")){ + if(strcmpi(w2,"yes")==0) + access_debug = 1; + else if(strcmpi(w2,"no")==0) + access_debug = 0; + else access_debug = atoi(w2); + #endif + } else if (strcmpi(w1, "mode_neg") == 0) + { + if(strcmpi(w2,"yes")==0) + mode_neg = 1; + else if(strcmpi(w2,"no")==0) + mode_neg = 0; + else mode_neg = atoi(w2); + } else if (strcmpi(w1, "frame_size") == 0) + frame_size = (size_t)strtoul(w2, NULL, 10); + else if (strcmpi(w1, "import") == 0) + socket_config_read(w2); + } + fclose(fp); + return 0; +} + +int RFIFOSKIP(int fd,int len) +{ + struct socket_data *s; + + if ( !session_isActive(fd) ) //Nullpo error here[Kevin] + return 0; + + s = session[fd]; + + if ( s->rdata_size - s->rdata_pos - len < 0 ) { + //fprintf(stderr,"too many skip\n"); + //exit(1); + //better than a COMPLETE program abort // TEST! :) + ShowError("too many skip (%d) now skipped: %d (FD: %d)\n", len, RFIFOREST(fd), fd); + len = RFIFOREST(fd); + } + s->rdata_pos = s->rdata_pos+len; + return 0; +} + + +unsigned int addr_[16]; // ip addresses of local host (host byte order) +unsigned int naddr_ = 0; // # of ip addresses + +void socket_final (void) +{ + int i; +#ifndef MINICORE + struct _connect_history *hist , *hist2; + for(i = 0; i < 0x10000; i++) { + hist = connect_history[i]; + while(hist) { + hist2 = hist->next; + aFree(hist); + hist = hist2; + } + } + if (access_allow) + aFree(access_allow); + if (access_deny) + aFree(access_deny); +#endif + + for (i = 1; i < fd_max; i++) { + if(session[i]) + delete_session(i); + } + + // session[0] のダミーデータを削除 + aFree(session[0]->rdata); + aFree(session[0]->wdata); + aFree(session[0]); +} + +//Closes a socket. +//Needed to simplify shutdown code as well as manage the subtle differences in socket management from Windows and *nix. +void do_close(int fd) +{ +//We don't really care if these closing functions return an error, we are just shutting down and not reusing this socket. +#ifdef __WIN32 +// shutdown(fd, SD_BOTH); //FIXME: Shutdown requires winsock2.h! What would be the proper shutting down method for winsock1? + if (session[fd] && session[fd]->func_send == send_from_fifo) + session[fd]->func_send(fd); //Flush the data as it is gonna be closed down, but it may not succeed as it is a nonblocking socket! [Skotlex] + closesocket(fd); +#else + if (close(fd)) + perror("do_close: close"); +#endif + if (session[fd]) + delete_session(fd); +} + +void socket_init (void) +{ + char *SOCKET_CONF_FILENAME = "conf/packet_athena.conf"; +#ifdef __WIN32 + char** a; + unsigned int i; + char fullhost[255]; + struct hostent* hent; + + /* Start up the windows networking */ + WORD version_wanted = MAKEWORD(1, 1); //Demand at least WinSocket version 1.1 (from Freya) + WSADATA wsaData; + + if ( WSAStartup(version_wanted, &wsaData) != 0 ) { + ShowFatalError("SYSERR: WinSock not available!\n"); + exit(1); + } + + if(gethostname(fullhost, sizeof(fullhost)) == SOCKET_ERROR) { + ShowError("Ugg.. no hostname defined!\n"); + return; + } + + // XXX This should look up the local IP addresses in the registry + // instead of calling gethostbyname. However, the way IP addresses + // are stored in the registry is annoyingly complex, so I'll leave + // this as T.B.D. + hent = gethostbyname(fullhost); + if (hent == NULL) { + ShowError("Cannot resolve our own hostname to a IP address"); + return; + } + + a = hent->h_addr_list; + for(i = 0; a[i] != 0 && i < 16; ++i) { + unsigned long addr1 = ntohl(*(unsigned long*) a[i]); + addr_[i] = addr1; + } + naddr_ = i; +#else + int pos; + int fdes = socket(AF_INET, SOCK_STREAM, 0); + char buf[16 * sizeof(struct ifreq)]; + struct ifconf ic; + + // The ioctl call will fail with Invalid Argument if there are more + // interfaces than will fit in the buffer + ic.ifc_len = sizeof(buf); + ic.ifc_buf = buf; + if(ioctl(fdes, SIOCGIFCONF, &ic) == -1) { + ShowError("SIOCGIFCONF failed!\n"); + return; + } + + for(pos = 0; pos < ic.ifc_len;) + { + struct ifreq * ir = (struct ifreq *) (ic.ifc_buf + pos); + + struct sockaddr_in * a = (struct sockaddr_in *) &(ir->ifr_addr); + + if(a->sin_family == AF_INET) { + u_long ad = ntohl(a->sin_addr.s_addr); + if(ad != INADDR_LOOPBACK && ad != INADDR_ANY) { + addr_[naddr_ ++] = ad; + if(naddr_ == 16) + break; + } + } + + #if defined(_AIX) || defined(__APPLE__) + pos += ir->ifr_addr.sa_len; // For when we port athena to run on Mac's :) + pos += sizeof(ir->ifr_name); + #else + pos += sizeof(struct ifreq); + #endif + } +#endif + + FD_ZERO(&readfds); + + socket_config_read(SOCKET_CONF_FILENAME); + + // initialise last send-receive tick + last_tick = time(0); + + // session[0] Was for the console (whatever that was?), but is now currently used for disconnected sessions of the map + // server, and as such, should hold enough buffer (it is a vacuum so to speak) as it is never flushed. [Skotlex] + CREATE(session[0], struct socket_data, 1); + CREATE(session[0]->rdata, unsigned char, 2*rfifo_size); + CREATE(session[0]->wdata, unsigned char, 2*wfifo_size); + session[0]->max_rdata = 2*rfifo_size; + session[0]->max_wdata = 2*wfifo_size; + + malloc_set(func_parse_table, 0, sizeof(func_parse_table)); + func_parse_table[SESSION_RAW].check = default_func_check; + func_parse_table[SESSION_RAW].func = default_func_parse; + +#ifndef MINICORE + // とりあえず5分ごとに不要なデータを削除する + add_timer_func_list(connect_check_clear, "connect_check_clear"); + add_timer_interval(gettick()+1000,connect_check_clear,0,0,300*1000); +#endif +} + + +bool session_isValid(int fd) +{ //End of Exam has pointed out that fd==0 is actually an unconnected session! [Skotlex] + //But this is not so true, it is used... for... something. The console uses it, would this not cause problems? [Skotlex] + return ( (fd>0) && (fdeof ); +} + +in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str) { + struct hostent *h = gethostbyname(hostname); + char ip_buf[16]; + unsigned char ip2[4]; + if (!h) return 0; + if (ip == NULL) ip = ip2; + ip[0] = (unsigned char) h->h_addr[0]; + ip[1] = (unsigned char) h->h_addr[1]; + ip[2] = (unsigned char) h->h_addr[2]; + ip[3] = (unsigned char) h->h_addr[3]; + if (ip_str == NULL) ip_str = ip_buf; + sprintf(ip_str, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]); + return inet_addr(ip_str); +} diff --git a/src/common/socket.h b/src/common/socket.h index ae13353b2..eb3a78f7e 100644 --- a/src/common/socket.h +++ b/src/common/socket.h @@ -1,176 +1,176 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder - -#ifndef _SOCKET_H_ -#define _SOCKET_H_ - -#include - -#ifdef __WIN32 -#define __USE_W32_SOCKETS -#include -typedef long in_addr_t; -#else -#include -#include -#include -#endif -#include -#include "../common/malloc.h" -#include "cbasetypes.h" - -extern time_t last_tick; -extern time_t stall_time; - -// define declaration - -#define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size) -#ifdef TURBO -#define RFIFOHEAD(fd) char *rbPtr ## fd = session[fd]->rdata+session[fd]->rdata_pos -#define RFIFOP(fd,pos) (&rbPtr ## fd[pos]) -#else -//Make it a comment so it does not disrupts the rest of code. -#define RFIFOHEAD(fd) // -#define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) -#endif -// 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 long*)RFIFOP(fd,pos)) -#define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) -#define RFIFOFLUSH(fd) \ - if(session[fd]->rdata_size == session[fd]->rdata_pos) \ - { session[fd]->rdata_size = session[fd]->rdata_pos = 0; } else { \ - session[fd]->rdata_size -= session[fd]->rdata_pos; \ - memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \ - 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 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 long*)RBUFP((p),(pos))) - -#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) -#ifdef TURBO -#define WFIFOHEAD(fd, x) char *wbPtr ## fd = fd?(session[fd]->wdata+session[fd]->wdata_size):0; -#define WFIFOP(fd,pos) (&wbPtr ## fd[pos]) -#else -#define WFIFOHEAD(fd, size) { if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); } - -#define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) -#endif -#define WFIFOB(fd,pos) (*(unsigned char*)WFIFOP(fd,pos)) -#define WFIFOW(fd,pos) (*(unsigned short*)WFIFOP(fd,pos)) -#define WFIFOL(fd,pos) (*(unsigned long*)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)) -#define WBUFB(p,pos) (*(unsigned char*)((p) + (pos))) -#define WBUFW(p,pos) (*(unsigned short*)((p) + (pos))) -#define WBUFL(p,pos) (*(unsigned long*)((p) + (pos))) - -//FD_SETSIZE must be modified on the project files/Makefile, since a change here won't affect -// dependant windows libraries. -/* -#ifdef __WIN32 -//The default FD_SETSIZE is kinda small for windows systems. - #ifdef FD_SETSIZE - #undef FD_SETSIZE - #endif -#define FD_SETSIZE 4096 -#endif -*/ -#ifdef __INTERIX -#define FD_SETSIZE 4096 -#endif // __INTERIX - -/* Removed Cygwin FD_SETSIZE declarations, now are directly passed on to the compiler through Makefile [Valaris] */ - -// Session type -enum SessionType { - SESSION_UNKNOWN = -1, - SESSION_RAW = 0, - SESSION_HTTP = 1, -//----------------- - SESSION_MAX = 2 -}; - -// Struct declaration - -struct socket_data{ - unsigned char eof; - unsigned char *rdata, *wdata; - unsigned int max_rdata, max_wdata; - unsigned int rdata_size, wdata_size; - int rdata_pos; - time_t rdata_tick; - struct sockaddr_in client_addr; - int (*func_recv)(int); - int (*func_send)(int); - int (*func_parse)(int); - int (*func_console)(char*); - void* session_data; - void* session_data2; - enum SessionType type; -}; - -// Parse functions table -struct func_parse_table { - int (*func)(int); - int (*check)(struct socket_data *); -}; -extern struct func_parse_table func_parse_table[SESSION_MAX]; - - -// Data prototype declaration - -extern struct socket_data *session[FD_SETSIZE]; - -extern int fd_max; - -////////////////////////////////// -// some checking on sockets -extern bool session_isValid(int fd); -extern bool session_isActive(int fd); -////////////////////////////////// - -// Function prototype declaration - -int make_listen_port(int); -int make_listen_bind(long,int); -int make_connection(long,int); -int delete_session(int); -int realloc_fifo(int fd,unsigned int rfifo_size,unsigned int wfifo_size); -int realloc_writefifo(int fd, size_t addition); -int WFIFOSET(int fd,int len); -int RFIFOSKIP(int fd,int len); - -int do_sendrecv(int next); -int do_parsepacket(void); -void do_close(int fd); -void socket_init(void); -void socket_final(void); - -extern void flush_fifo(int fd); -extern void flush_fifos(void); -extern void set_nonblocking(int fd, int yes); - -int start_console(void); - -void set_defaultparse(int (*defaultparse)(int)); -void set_defaultconsoleparse(int (*defaultparse)(char*)); - -//Resolves the hostname and stores the string representation of the string in ip. -//Meant to simplify calls to gethostbyname without the need of all the -//required network includes. -//hostname is the name to resolve. -//ip is an array of char[4] where the individual parts of the ip are stored (optional) -//ip_str is a char[16] where the whole ip is stored in string notation (optional) -in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str); - -extern unsigned int addr_[16]; // ip addresses of local host (host byte order) -extern unsigned int naddr_; // # of ip addresses -#endif // _SOCKET_H_ +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL +// For more information, see LICENCE in the main folder + +#ifndef _SOCKET_H_ +#define _SOCKET_H_ + +#include + +#ifdef __WIN32 +#define __USE_W32_SOCKETS +#include +typedef long in_addr_t; +#else +#include +#include +#include +#endif +#include +#include "../common/malloc.h" +#include "cbasetypes.h" + +extern time_t last_tick; +extern time_t stall_time; + +// define declaration + +#define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size) +#ifdef TURBO +#define RFIFOHEAD(fd) char *rbPtr ## fd = session[fd]->rdata+session[fd]->rdata_pos +#define RFIFOP(fd,pos) (&rbPtr ## fd[pos]) +#else +//Make it a comment so it does not disrupts the rest of code. +#define RFIFOHEAD(fd) // +#define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos)) +#endif +// 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 long*)RFIFOP(fd,pos)) +#define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos) +#define RFIFOFLUSH(fd) \ + if(session[fd]->rdata_size == session[fd]->rdata_pos) \ + { session[fd]->rdata_size = session[fd]->rdata_pos = 0; } else { \ + session[fd]->rdata_size -= session[fd]->rdata_pos; \ + memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \ + 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 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 long*)RBUFP((p),(pos))) + +#define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size) +#ifdef TURBO +#define WFIFOHEAD(fd, x) char *wbPtr ## fd = fd?(session[fd]->wdata+session[fd]->wdata_size):0; +#define WFIFOP(fd,pos) (&wbPtr ## fd[pos]) +#else +#define WFIFOHEAD(fd, size) { if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); } + +#define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos)) +#endif +#define WFIFOB(fd,pos) (*(unsigned char*)WFIFOP(fd,pos)) +#define WFIFOW(fd,pos) (*(unsigned short*)WFIFOP(fd,pos)) +#define WFIFOL(fd,pos) (*(unsigned long*)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)) +#define WBUFB(p,pos) (*(unsigned char*)((p) + (pos))) +#define WBUFW(p,pos) (*(unsigned short*)((p) + (pos))) +#define WBUFL(p,pos) (*(unsigned long*)((p) + (pos))) + +//FD_SETSIZE must be modified on the project files/Makefile, since a change here won't affect +// dependant windows libraries. +/* +#ifdef __WIN32 +//The default FD_SETSIZE is kinda small for windows systems. + #ifdef FD_SETSIZE + #undef FD_SETSIZE + #endif +#define FD_SETSIZE 4096 +#endif +*/ +#ifdef __INTERIX +#define FD_SETSIZE 4096 +#endif // __INTERIX + +/* Removed Cygwin FD_SETSIZE declarations, now are directly passed on to the compiler through Makefile [Valaris] */ + +// Session type +enum SessionType { + SESSION_UNKNOWN = -1, + SESSION_RAW = 0, + SESSION_HTTP = 1, +//----------------- + SESSION_MAX = 2 +}; + +// Struct declaration + +struct socket_data{ + unsigned char eof; + unsigned char *rdata, *wdata; + size_t max_rdata, max_wdata; + size_t rdata_size, wdata_size; + size_t rdata_pos; + time_t rdata_tick; + struct sockaddr_in client_addr; + int (*func_recv)(int); + int (*func_send)(int); + int (*func_parse)(int); + int (*func_console)(char*); + void* session_data; + void* session_data2; + enum SessionType type; +}; + +// Parse functions table +struct func_parse_table { + int (*func)(int); + int (*check)(struct socket_data *); +}; +extern struct func_parse_table func_parse_table[SESSION_MAX]; + + +// Data prototype declaration + +extern struct socket_data *session[FD_SETSIZE]; + +extern int fd_max; + +////////////////////////////////// +// some checking on sockets +extern bool session_isValid(int fd); +extern bool session_isActive(int fd); +////////////////////////////////// + +// Function prototype declaration + +int make_listen_port(int); +int make_listen_bind(long,int); +int make_connection(long,int); +int delete_session(int); +int realloc_fifo(int fd,unsigned int rfifo_size,unsigned int wfifo_size); +int realloc_writefifo(int fd, size_t addition); +int WFIFOSET(int fd,int len); +int RFIFOSKIP(int fd,int len); + +int do_sendrecv(int next); +int do_parsepacket(void); +void do_close(int fd); +void socket_init(void); +void socket_final(void); + +extern void flush_fifo(int fd); +extern void flush_fifos(void); +extern void set_nonblocking(int fd, int yes); + +int start_console(void); + +void set_defaultparse(int (*defaultparse)(int)); +void set_defaultconsoleparse(int (*defaultparse)(char*)); + +//Resolves the hostname and stores the string representation of the string in ip. +//Meant to simplify calls to gethostbyname without the need of all the +//required network includes. +//hostname is the name to resolve. +//ip is an array of char[4] where the individual parts of the ip are stored (optional) +//ip_str is a char[16] where the whole ip is stored in string notation (optional) +in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str); + +extern unsigned int addr_[16]; // ip addresses of local host (host byte order) +extern unsigned int naddr_; // # of ip addresses +#endif // _SOCKET_H_ diff --git a/src/common/strlib.c b/src/common/strlib.c index 8cff2a878..92601f9b1 100644 --- a/src/common/strlib.c +++ b/src/common/strlib.c @@ -1,194 +1,194 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder - -#include -#include -#include -#include - -#include "strlib.h" -#include "utils.h" -#include "../common/malloc.h" - -//----------------------------------------------- -// string lib. -char* jstrescape (char* pt) { - //copy from here - char *ptr; - int i =0, j=0; - - //copy string to temporary - CREATE_A(ptr, char, J_MAX_MALLOC_SIZE); - strcpy(ptr,pt); - - while (ptr[i] != '\0') { - switch (ptr[i]) { - case '\'': - pt[j++] = '\\'; - pt[j++] = ptr[i++]; - break; - case '\\': - pt[j++] = '\\'; - pt[j++] = ptr[i++]; - break; - case '%': - pt[j++] = '_'; i++; - break; - default: - pt[j++] = ptr[i++]; - } - } - pt[j++] = '\0'; - aFree (ptr); - return &pt[0]; -} - -char* jstrescapecpy (char* pt,char* spt) { - //copy from here - //WARNING: Target string pt should be able to hold strlen(spt)*2, as each time - //a escape character is found, the target's final length increases! [Skotlex] - int i =0, j=0; - - if (!spt) { //Return an empty string [Skotlex] - pt[0] = '\0'; - return &pt[0]; - } - - while (spt[i] != '\0') { - switch (spt[i]) { - case '\'': - pt[j++] = '\\'; - pt[j++] = spt[i++]; - break; - case '\\': - pt[j++] = '\\'; - pt[j++] = spt[i++]; - break; - case '%': - pt[j++] = '_'; i++; - break; - default: - pt[j++] = spt[i++]; - } - } - pt[j++] = '\0'; - return &pt[0]; -} -int jmemescapecpy (char* pt,char* spt, int size) { - //copy from here - int i =0, j=0; - - while (i < size) { - switch (spt[i]) { - case '\'': - pt[j++] = '\\'; - pt[j++] = spt[i++]; - break; - case '\\': - pt[j++] = '\\'; - pt[j++] = spt[i++]; - break; - case '%': - pt[j++] = '_'; i++; - break; - default: - pt[j++] = spt[i++]; - } - } - // copy size is 0 ~ (j-1) - return j; -} - -//----------------------------------------------------- -// Function to suppress control characters in a string. -//----------------------------------------------------- -//int remove_control_chars(char *str) { -int remove_control_chars(unsigned char *str) { - int i; - int change = 0; - - for(i = 0; str[i]; i++) { - if (str[i] < 32) { - str[i] = '_'; - change = 1; - } - } - - return change; -} - -//Trims a string, also removes illegal characters such as \t and reduces continous spaces to a single one. by [Foruken] -char *trim(char *str, const char *delim) -{ - char *strp = strtok(str,delim); - char buf[1024]; - char *bufp = buf; - malloc_tsetdword(buf,0,sizeof buf); - - while(strp) { - strcpy(bufp, strp); - bufp = bufp + strlen(strp); - strp = strtok(NULL, delim); - if (strp) { - strcpy(bufp," "); - bufp++; - } - } - strcpy(str,buf); - return str; -} - - -//stristr: Case insensitive version of strstr, code taken from -//http://www.daniweb.com/code/snippet313.html, Dave Sinkula -// -const char *stristr(const char *haystack, const char *needle) -{ - if ( !*needle ) - { - return haystack; - } - for ( ; *haystack; ++haystack ) - { - if ( toupper(*haystack) == toupper(*needle) ) - { - /* - * Matched starting char -- loop through remaining chars. - */ - const char *h, *n; - for ( h = haystack, n = needle; *h && *n; ++h, ++n ) - { - if ( toupper(*h) != toupper(*n) ) - { - break; - } - } - if ( !*n ) /* matched all of 'needle' to null termination */ - { - return haystack; /* return the start of the match */ - } - } - } - return 0; -} - -#ifdef __WIN32 -char *_strtok_r(char *s1, const char *s2, char **lasts) -{ - char *ret; - - if (s1 == NULL) - s1 = *lasts; - while(*s1 && strchr(s2, *s1)) - ++s1; - if(*s1 == '\0') - return NULL; - ret = s1; - while(*s1 && !strchr(s2, *s1)) - ++s1; - if(*s1) - *s1++ = '\0'; - *lasts = s1; - return ret; -} -#endif +// Copyright (c) Athena Dev Teams - Licensed under GNU GPL +// For more information, see LICENCE in the main folder + +#include +#include +#include +#include + +#include "strlib.h" +#include "utils.h" +#include "../common/malloc.h" + +//----------------------------------------------- +// string lib. +char* jstrescape (char* pt) { + //copy from here + char *ptr; + int i =0, j=0; + + //copy string to temporary + CREATE(ptr, char, J_MAX_MALLOC_SIZE); + strcpy(ptr,pt); + + while (ptr[i] != '\0') { + switch (ptr[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = ptr[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = ptr[i++]; + break; + case '%': + pt[j++] = '_'; i++; + break; + default: + pt[j++] = ptr[i++]; + } + } + pt[j++] = '\0'; + aFree(ptr); + return &pt[0]; +} + +char* jstrescapecpy (char* pt,char* spt) { + //copy from here + //WARNING: Target string pt should be able to hold strlen(spt)*2, as each time + //a escape character is found, the target's final length increases! [Skotlex] + int i =0, j=0; + + if (!spt) { //Return an empty string [Skotlex] + pt[0] = '\0'; + return &pt[0]; + } + + while (spt[i] != '\0') { + switch (spt[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '%': + pt[j++] = '_'; i++; + break; + default: + pt[j++] = spt[i++]; + } + } + pt[j++] = '\0'; + return &pt[0]; +} +int jmemescapecpy (char* pt,char* spt, int size) { + //copy from here + int i =0, j=0; + + while (i < size) { + switch (spt[i]) { + case '\'': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '\\': + pt[j++] = '\\'; + pt[j++] = spt[i++]; + break; + case '%': + pt[j++] = '_'; i++; + break; + default: + pt[j++] = spt[i++]; + } + } + // copy size is 0 ~ (j-1) + return j; +} + +//----------------------------------------------------- +// Function to suppress control characters in a string. +//----------------------------------------------------- +//int remove_control_chars(char *str) { +int remove_control_chars(unsigned char *str) { + int i; + int change = 0; + + for(i = 0; str[i]; i++) { + if (str[i] < 32) { + str[i] = '_'; + change = 1; + } + } + + return change; +} + +//Trims a string, also removes illegal characters such as \t and reduces continous spaces to a single one. by [Foruken] +char *trim(char *str, const char *delim) +{ + char *strp = strtok(str,delim); + char buf[1024]; + char *bufp = buf; + malloc_tsetdword(buf,0,sizeof buf); + + while(strp) { + strcpy(bufp, strp); + bufp = bufp + strlen(strp); + strp = strtok(NULL, delim); + if (strp) { + strcpy(bufp," "); + bufp++; + } + } + strcpy(str,buf); + return str; +} + + +//stristr: Case insensitive version of strstr, code taken from +//http://www.daniweb.com/code/snippet313.html, Dave Sinkula +// +const char *stristr(const char *haystack, const char *needle) +{ + if ( !*needle ) + { + return haystack; + } + for ( ; *haystack; ++haystack ) + { + if ( toupper(*haystack) == toupper(*needle) ) + { + /* + * Matched starting char -- loop through remaining chars. + */ + const char *h, *n; + for ( h = haystack, n = needle; *h && *n; ++h, ++n ) + { + if ( toupper(*h) != toupper(*n) ) + { + break; + } + } + if ( !*n ) /* matched all of 'needle' to null termination */ + { + return haystack; /* return the start of the match */ + } + } + } + return 0; +} + +#ifdef __WIN32 +char *_strtok_r(char *s1, const char *s2, char **lasts) +{ + char *ret; + + if (s1 == NULL) + s1 = *lasts; + while(*s1 && strchr(s2, *s1)) + ++s1; + if(*s1 == '\0') + return NULL; + ret = s1; + while(*s1 && !strchr(s2, *s1)) + ++s1; + if(*s1) + *s1++ = '\0'; + *lasts = s1; + return ret; +} +#endif diff --git a/src/map/intif.c b/src/map/intif.c index 025d76c9b..40cd52136 100644 --- a/src/map/intif.c +++ b/src/map/intif.c @@ -1,1574 +1,1579 @@ -// Copyright (c) Athena Dev Teams - Licensed under GNU GPL -// For more information, see LICENCE in the main folder - -#include -#include -#include -#include -#include -#include - -#include "../common/showmsg.h" -#include "../common/socket.h" -#include "../common/timer.h" -#include "../common/nullpo.h" -#include "../common/malloc.h" -#include "map.h" -#include "battle.h" -#include "chrif.h" -#include "clif.h" -#include "pc.h" -#include "intif.h" -#include "storage.h" -#include "party.h" -#include "guild.h" -#include "pet.h" -#include "atcommand.h" -#include "mercenary.h" //albator - -static const int packet_len_table[]={ - -1,-1,27,-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3800-0x380f - -1, 7, 0, 0, 0, 0, 0, 0, -1,11, 0, 0, 0, 0, 0, 0, //0x3810 - 39,-1,15,15, 14,19, 7,-1, 0, 0, 0, 0, 0, 0, 0, 0, //0x3820 - 10,-1,15, 0, 79,19, 7,-1, 0,-1,-1,-1, 14,67,186,-1, //0x3830 - 9, 9,-1,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3840 - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 11,-1, 7, 3, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3880 - -1,-1, 7, 3, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3890 Homunculus [albator] -}; - -extern int char_fd; // inter serverのfdはchar_fdを使う -#define inter_fd (char_fd) // エイリアス - -//----------------------------------------------------------------- -// inter serverへの送信 - -int CheckForCharServer(void) { - return ((char_fd <= 0) || session[char_fd] == NULL || session[char_fd]->wdata == NULL); -} - -// pet -int intif_create_pet(int account_id,int char_id,short pet_class,short pet_lv,short pet_egg_id, - short pet_equip,short intimate,short hungry,char rename_flag,char incuvate,char *pet_name) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 24 + NAME_LENGTH); - WFIFOW(inter_fd,0) = 0x3080; - WFIFOL(inter_fd,2) = account_id; - WFIFOL(inter_fd,6) = char_id; - WFIFOW(inter_fd,10) = pet_class; - WFIFOW(inter_fd,12) = pet_lv; - WFIFOW(inter_fd,14) = pet_egg_id; - WFIFOW(inter_fd,16) = pet_equip; - WFIFOW(inter_fd,18) = intimate; - WFIFOW(inter_fd,20) = hungry; - WFIFOB(inter_fd,22) = rename_flag; - WFIFOB(inter_fd,23) = incuvate; - memcpy(WFIFOP(inter_fd,24),pet_name,NAME_LENGTH); - WFIFOSET(inter_fd,24+NAME_LENGTH); - - return 0; -} - -int intif_request_petdata(int account_id,int char_id,int pet_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 14); - WFIFOW(inter_fd,0) = 0x3081; - WFIFOL(inter_fd,2) = account_id; - WFIFOL(inter_fd,6) = char_id; - WFIFOL(inter_fd,10) = pet_id; - WFIFOSET(inter_fd,14); - - return 0; -} - -int intif_save_petdata(int account_id,struct s_pet *p) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, sizeof(struct s_pet) + 8); - WFIFOW(inter_fd,0) = 0x3082; - WFIFOW(inter_fd,2) = sizeof(struct s_pet) + 8; - WFIFOL(inter_fd,4) = account_id; - memcpy(WFIFOP(inter_fd,8),p,sizeof(struct s_pet)); - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - - return 0; -} - -int intif_delete_petdata(int pet_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,6); - WFIFOW(inter_fd,0) = 0x3083; - WFIFOL(inter_fd,2) = pet_id; - WFIFOSET(inter_fd,6); - - return 1; -} -int intif_rename_pet(struct map_session_data *sd,char *name) -{ - if (CheckForCharServer()) - return 1; - - WFIFOHEAD(inter_fd,NAME_LENGTH+11); - WFIFOW(inter_fd,0) = 0x3084; - WFIFOL(inter_fd,2) = sd->status.account_id; - WFIFOL(inter_fd,6) = sd->status.char_id; - memcpy(WFIFOP(inter_fd,10),name, NAME_LENGTH); - WFIFOSET(inter_fd,NAME_LENGTH+11); - return 0; -} - - -// GMメッセージを送信 -int intif_GMmessage(char* mes,int len,int flag) -{ - int lp = (flag&0x10) ? 8 : 4; - - // Send to the local players - clif_GMmessage(NULL, mes, len, flag); - - if (CheckForCharServer()) - return 0; - - if (other_mapserver_count < 1) - return 0; //No need to send. - - WFIFOHEAD(inter_fd,lp + len + 4); - WFIFOW(inter_fd,0) = 0x3000; - WFIFOW(inter_fd,2) = lp + len + 4; - WFIFOL(inter_fd,4) = 0xFF000000; //"invalid" color signals standard broadcast. - WFIFOL(inter_fd,8) = 0x65756c62; - memcpy(WFIFOP(inter_fd,4+lp), mes, len); - WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); - return 0; -} - -int intif_announce(char* mes,int len, unsigned long color, int flag) -{ - // Send to the local players - if(color == 0xFE000000) // This is main chat message [LuzZza] - clif_MainChatMessage(mes); - else - clif_announce(NULL, mes, len, color, flag); - - if (CheckForCharServer()) - return 0; - - if (other_mapserver_count < 1) - return 0; //No need to send. - - WFIFOHEAD(inter_fd, 8 + len); - WFIFOW(inter_fd,0) = 0x3000; - WFIFOW(inter_fd,2) = 8 + len; - WFIFOL(inter_fd,4) = color; - memcpy(WFIFOP(inter_fd,8), mes, len); - WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); - return 0; -} - -// The transmission of Wisp/Page to inter-server (player not found on this server) -int intif_wis_message(struct map_session_data *sd, char *nick, char *mes, int mes_len) { - nullpo_retr(0, sd); - if (CheckForCharServer()) - return 0; - - if (other_mapserver_count < 1) - { //Character not found. - clif_wis_end(sd->fd, 1); - return 0; - } - - WFIFOHEAD(inter_fd,mes_len + 52); - WFIFOW(inter_fd,0) = 0x3001; - WFIFOW(inter_fd,2) = mes_len + 52; - memcpy(WFIFOP(inter_fd,4), sd->status.name, NAME_LENGTH); - memcpy(WFIFOP(inter_fd,4+NAME_LENGTH), nick, NAME_LENGTH); - memcpy(WFIFOP(inter_fd,4+2*NAME_LENGTH), mes, mes_len); - WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); - - if (battle_config.etc_log) - ShowInfo("intif_wis_message from %s to %s (message: '%s')\n", sd->status.name, nick, mes); - - return 0; -} - -// The reply of Wisp/page -int intif_wis_replay(int id, int flag) { - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,7); - WFIFOW(inter_fd,0) = 0x3002; - WFIFOL(inter_fd,2) = id; - WFIFOB(inter_fd,6) = flag; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target - WFIFOSET(inter_fd,7); - - if (battle_config.etc_log) - ShowInfo("intif_wis_replay: id: %d, flag:%d\n", id, flag); - - return 0; -} - -// The transmission of GM only Wisp/Page from server to inter-server -int intif_wis_message_to_gm(char *Wisp_name, int min_gm_level, char *mes) { - int mes_len; - if (CheckForCharServer()) - return 0; - mes_len = strlen(mes) + 1; // + null - WFIFOHEAD(inter_fd, mes_len + 30); - WFIFOW(inter_fd,0) = 0x3003; - WFIFOW(inter_fd,2) = mes_len + 30; - memcpy(WFIFOP(inter_fd,4), Wisp_name, NAME_LENGTH); - WFIFOW(inter_fd,4+NAME_LENGTH) = (short)min_gm_level; - memcpy(WFIFOP(inter_fd,6+NAME_LENGTH), mes, mes_len); - WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); - - if (battle_config.etc_log) - ShowNotice("intif_wis_message_to_gm: from: '%s', min level: %d, message: '%s'.\n", Wisp_name, min_gm_level, mes); - - return 0; -} - -int intif_regtostr(char* str, struct global_reg *reg, int qty) { - int len =0, i; - - for (i = 0; i < qty; i++) { - len+= sprintf(str+len, "%s", reg[i].str)+1; //We add 1 to consider the '\0' in place. - len+= sprintf(str+len, "%s", reg[i].value)+1; - } - return len; -} - -//Request for saving registry values. -int intif_saveregistry(struct map_session_data *sd, int type) -{ - struct global_reg *reg; - int count; - - if (CheckForCharServer()) - return -1; - - switch (type) { - case 3: //Character reg - reg = sd->save_reg.global; - count = sd->save_reg.global_num; - sd->state.reg_dirty &= ~0x4; - break; - case 2: //Account reg - reg = sd->save_reg.account; - count = sd->save_reg.account_num; - sd->state.reg_dirty &= ~0x2; - break; - case 1: //Account2 reg - reg = sd->save_reg.account2; - count = sd->save_reg.account2_num; - sd->state.reg_dirty &= ~0x1; - break; - default: //Broken code? - if (battle_config.error_log) - ShowError("intif_saveregistry: Invalid type %d\n", type); - return -1; - } - WFIFOHEAD(inter_fd, 288 * MAX_REG_NUM+13); - WFIFOW(inter_fd,0)=0x3004; - WFIFOL(inter_fd,4)=sd->status.account_id; - WFIFOL(inter_fd,8)=sd->status.char_id; - WFIFOB(inter_fd,12)=type; - if(count ==0){ - WFIFOW(inter_fd,2)=13; - }else{ - int i,p; - for (p=13,i = 0; i < count; i++) { - if (reg[i].str[0] && reg[i].value != 0) { - p+= sprintf(WFIFOP(inter_fd,p), "%s", reg[i].str)+1; //We add 1 to consider the '\0' in place. - p+= sprintf(WFIFOP(inter_fd,p), "%s", reg[i].value)+1; - } - } - WFIFOW(inter_fd,2)=p; - } - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - return 0; -} - -//Request the registries for this player. -int intif_request_registry(struct map_session_data *sd, int flag) -{ - nullpo_retr(0, sd); - - sd->save_reg.account2_num = -1; - sd->save_reg.account_num = -1; - sd->save_reg.global_num = -1; - - if (CheckForCharServer()) - return 0; - - WFIFOHEAD(inter_fd,6); - WFIFOW(inter_fd,0) = 0x3005; - WFIFOL(inter_fd,2) = sd->status.account_id; - WFIFOL(inter_fd,6) = sd->status.char_id; - WFIFOB(inter_fd,10) = (flag&1?1:0); //Request Acc Reg 2 - WFIFOB(inter_fd,11) = (flag&2?1:0); //Request Acc Reg - WFIFOB(inter_fd,12) = (flag&4?1:0); //Request Char Reg - WFIFOSET(inter_fd,13); - - return 0; -} - -// 倉庫データ要求 -int intif_request_storage(int account_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,6); - WFIFOW(inter_fd,0) = 0x3010; - WFIFOL(inter_fd,2) = account_id; - WFIFOSET(inter_fd,6); - return 0; -} -// 倉庫データ送信 -int intif_send_storage(struct storage *stor) -{ - if (CheckForCharServer()) - return 0; - nullpo_retr(0, stor); - WFIFOHEAD(inter_fd,sizeof(struct storage)+8); - WFIFOW(inter_fd,0) = 0x3011; - WFIFOW(inter_fd,2) = sizeof(struct storage)+8; - WFIFOL(inter_fd,4) = stor->account_id; - memcpy( WFIFOP(inter_fd,8),stor, sizeof(struct storage) ); - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - return 0; -} - -int intif_request_guild_storage(int account_id,int guild_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,10); - WFIFOW(inter_fd,0) = 0x3018; - WFIFOL(inter_fd,2) = account_id; - WFIFOL(inter_fd,6) = guild_id; - WFIFOSET(inter_fd,10); - return 0; -} -int intif_send_guild_storage(int account_id,struct guild_storage *gstor) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,sizeof(struct guild_storage)+12); - WFIFOW(inter_fd,0) = 0x3019; - WFIFOW(inter_fd,2) = sizeof(struct guild_storage)+12; - WFIFOL(inter_fd,4) = account_id; - WFIFOL(inter_fd,8) = gstor->guild_id; - memcpy( WFIFOP(inter_fd,12),gstor, sizeof(struct guild_storage) ); - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - return 0; -} - -// パーティ作成要求 -int intif_create_party(struct party_member *member,char *name,int item,int item2) -{ - if (CheckForCharServer()) - return 0; - nullpo_retr(0, member); - - WFIFOHEAD(inter_fd,64); - WFIFOW(inter_fd,0) = 0x3020; - WFIFOW(inter_fd,2) = 30+sizeof(struct party_member); - memcpy(WFIFOP(inter_fd,4),name, NAME_LENGTH); - WFIFOB(inter_fd,28)= item; - WFIFOB(inter_fd,29)= item2; - memcpy(WFIFOP(inter_fd,30), member, sizeof(struct party_member)); - WFIFOSET(inter_fd,WFIFOW(inter_fd, 2)); - return 0; -} -// パーティ情報要求 -int intif_request_partyinfo(int party_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,6); - WFIFOW(inter_fd,0) = 0x3021; - WFIFOL(inter_fd,2) = party_id; - WFIFOSET(inter_fd,6); -// if(battle_config.etc_log) -// printf("intif: request party info\n"); - return 0; -} -// パーティ追加要求 -int intif_party_addmember(int party_id,struct party_member *member) -{ - if (CheckForCharServer()) - return 0; - - WFIFOHEAD(inter_fd,42); - WFIFOW(inter_fd,0)=0x3022; - WFIFOW(inter_fd,2)=8+sizeof(struct party_member); - WFIFOL(inter_fd,4)=party_id; - memcpy(WFIFOP(inter_fd,8),member,sizeof(struct party_member)); - WFIFOSET(inter_fd,WFIFOW(inter_fd, 2)); - return 1; -} -// パーティ設定変更 -int intif_party_changeoption(int party_id,int account_id,int exp,int item) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,14); - WFIFOW(inter_fd,0)=0x3023; - WFIFOL(inter_fd,2)=party_id; - WFIFOL(inter_fd,6)=account_id; - WFIFOW(inter_fd,10)=exp; - WFIFOW(inter_fd,12)=item; - WFIFOSET(inter_fd,14); - return 0; -} -// パーティ脱退要求 -int intif_party_leave(int party_id,int account_id, int char_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,14); - WFIFOW(inter_fd,0)=0x3024; - WFIFOL(inter_fd,2)=party_id; - WFIFOL(inter_fd,6)=account_id; - WFIFOL(inter_fd,10)=char_id; - WFIFOSET(inter_fd,14); - return 0; -} -// パーティ移動要求 -int intif_party_changemap(struct map_session_data *sd,int online) -{ - if (CheckForCharServer()) - return 0; - if(!sd) - return 0; - - WFIFOHEAD(inter_fd,19); - WFIFOW(inter_fd,0)=0x3025; - WFIFOL(inter_fd,2)=sd->status.party_id; - WFIFOL(inter_fd,6)=sd->status.account_id; - WFIFOL(inter_fd,10)=sd->status.char_id; - WFIFOW(inter_fd,14)=sd->mapindex; - WFIFOB(inter_fd,16)=online; - WFIFOW(inter_fd,17)=sd->status.base_level; - WFIFOSET(inter_fd,19); - return 1; -} -// パーティー解散要求 -int intif_break_party(int party_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,6); - WFIFOW(inter_fd,0)=0x3026; - WFIFOL(inter_fd,2)=party_id; - WFIFOSET(inter_fd,6); - return 0; -} -// パーティ会話送信 -int intif_party_message(int party_id,int account_id,char *mes,int len) -{ - if (CheckForCharServer()) - return 0; - - if (other_mapserver_count < 1) - return 0; //No need to send. - - WFIFOHEAD(inter_fd,len + 12); - WFIFOW(inter_fd,0)=0x3027; - WFIFOW(inter_fd,2)=len+12; - WFIFOL(inter_fd,4)=party_id; - WFIFOL(inter_fd,8)=account_id; - memcpy(WFIFOP(inter_fd,12),mes,len); - WFIFOSET(inter_fd,len+12); - return 0; -} -// パーティ競合チェック要求 -int intif_party_checkconflict(int party_id,int account_id,int char_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,10 + NAME_LENGTH); - WFIFOW(inter_fd,0)=0x3028; - WFIFOL(inter_fd,2)=party_id; - WFIFOL(inter_fd,6)=account_id; - WFIFOL(inter_fd,10)=char_id; - WFIFOSET(inter_fd,14); - return 0; -} - -int intif_party_leaderchange(int party_id,int account_id,int char_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,14); - WFIFOW(inter_fd,0)=0x3029; - WFIFOL(inter_fd,2)=party_id; - WFIFOL(inter_fd,6)=account_id; - WFIFOL(inter_fd,10)=char_id; - WFIFOSET(inter_fd,14); - return 0; -} - - -// ギルド作成要求 -int intif_guild_create(const char *name,const struct guild_member *master) -{ - if (CheckForCharServer()) - return 0; - nullpo_retr(0, master); - - WFIFOHEAD(inter_fd,sizeof(struct guild_member)+(8+NAME_LENGTH)); - WFIFOW(inter_fd,0)=0x3030; - WFIFOW(inter_fd,2)=sizeof(struct guild_member)+(8+NAME_LENGTH); - WFIFOL(inter_fd,4)=master->account_id; - memcpy(WFIFOP(inter_fd,8),name,NAME_LENGTH); - memcpy(WFIFOP(inter_fd,8+NAME_LENGTH),master,sizeof(struct guild_member)); - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - return 0; -} -// ギルド情報要求 -int intif_guild_request_info(int guild_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,6); - WFIFOW(inter_fd,0) = 0x3031; - WFIFOL(inter_fd,2) = guild_id; - WFIFOSET(inter_fd,6); - return 0; -} -// ギルドメンバ追加要求 -int intif_guild_addmember(int guild_id,struct guild_member *m) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,sizeof(struct guild_member)+8); - WFIFOW(inter_fd,0) = 0x3032; - WFIFOW(inter_fd,2) = sizeof(struct guild_member)+8; - WFIFOL(inter_fd,4) = guild_id; - memcpy(WFIFOP(inter_fd,8),m,sizeof(struct guild_member)); - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - return 0; -} - -int intif_guild_change_gm(int guild_id, const char* name, int len) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, len + 8); - WFIFOW(inter_fd, 0)=0x3033; - WFIFOW(inter_fd, 2)=len+8; - WFIFOL(inter_fd, 4)=guild_id; - memcpy(WFIFOP(inter_fd,8),name,len); - WFIFOSET(inter_fd,len+8); - return 0; -} - -// ギルドメンバ脱退/追放要求 -int intif_guild_leave(int guild_id,int account_id,int char_id,int flag,const char *mes) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 55); - WFIFOW(inter_fd, 0) = 0x3034; - WFIFOL(inter_fd, 2) = guild_id; - WFIFOL(inter_fd, 6) = account_id; - WFIFOL(inter_fd,10) = char_id; - WFIFOB(inter_fd,14) = flag; - memcpy(WFIFOP(inter_fd,15),mes,40); - WFIFOSET(inter_fd,55); - return 0; -} -// ギルドメンバのオンライン状況/Lv更新要求 -int intif_guild_memberinfoshort(int guild_id, - int account_id,int char_id,int online,int lv,int class_) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 19); - WFIFOW(inter_fd, 0) = 0x3035; - WFIFOL(inter_fd, 2) = guild_id; - WFIFOL(inter_fd, 6) = account_id; - WFIFOL(inter_fd,10) = char_id; - WFIFOB(inter_fd,14) = online; - WFIFOW(inter_fd,15) = lv; - WFIFOW(inter_fd,17) = class_; - WFIFOSET(inter_fd,19); - return 0; -} -// ギルド解散通知 -int intif_guild_break(int guild_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 6); - WFIFOW(inter_fd, 0) = 0x3036; - WFIFOL(inter_fd, 2) = guild_id; - WFIFOSET(inter_fd,6); - return 0; -} -// ギルド会話送信 -int intif_guild_message(int guild_id,int account_id,char *mes,int len) -{ - if (CheckForCharServer()) - return 0; - - if (other_mapserver_count < 1) - return 0; //No need to send. - - WFIFOHEAD(inter_fd, len + 12); - WFIFOW(inter_fd,0)=0x3037; - WFIFOW(inter_fd,2)=len+12; - WFIFOL(inter_fd,4)=guild_id; - WFIFOL(inter_fd,8)=account_id; - memcpy(WFIFOP(inter_fd,12),mes,len); - WFIFOSET(inter_fd,len+12); - - return 0; -} -// ギルド競合チェック要求 -int intif_guild_checkconflict(int guild_id,int account_id,int char_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 14); - WFIFOW(inter_fd, 0)=0x3038; - WFIFOL(inter_fd, 2)=guild_id; - WFIFOL(inter_fd, 6)=account_id; - WFIFOL(inter_fd,10)=char_id; - WFIFOSET(inter_fd,14); - return 0; -} -// ギルド基本情報変更要求 -int intif_guild_change_basicinfo(int guild_id,int type,const void *data,int len) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, len + 10); - WFIFOW(inter_fd,0)=0x3039; - WFIFOW(inter_fd,2)=len+10; - WFIFOL(inter_fd,4)=guild_id; - WFIFOW(inter_fd,8)=type; - memcpy(WFIFOP(inter_fd,10),data,len); - WFIFOSET(inter_fd,len+10); - return 0; -} -// ギルドメンバ情報変更要求 -int intif_guild_change_memberinfo(int guild_id,int account_id,int char_id, - int type,const void *data,int len) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, len + 18); - WFIFOW(inter_fd, 0)=0x303a; - WFIFOW(inter_fd, 2)=len+18; - WFIFOL(inter_fd, 4)=guild_id; - WFIFOL(inter_fd, 8)=account_id; - WFIFOL(inter_fd,12)=char_id; - WFIFOW(inter_fd,16)=type; - memcpy(WFIFOP(inter_fd,18),data,len); - WFIFOSET(inter_fd,len+18); - return 0; -} -// ギルド役職変更要求 -int intif_guild_position(int guild_id,int idx,struct guild_position *p) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, sizeof(struct guild_position)+12); - WFIFOW(inter_fd,0)=0x303b; - WFIFOW(inter_fd,2)=sizeof(struct guild_position)+12; - WFIFOL(inter_fd,4)=guild_id; - WFIFOL(inter_fd,8)=idx; - memcpy(WFIFOP(inter_fd,12),p,sizeof(struct guild_position)); - WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); - return 0; -} -// ギルドスキルアップ要求 -int intif_guild_skillup(int guild_id,int skill_num,int account_id,int flag) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,14); - WFIFOW(inter_fd, 0)=0x303c; - WFIFOL(inter_fd, 2)=guild_id; - WFIFOL(inter_fd, 6)=skill_num; - WFIFOL(inter_fd,10)=account_id; - //WFIFOL(inter_fd,14)=flag; - WFIFOSET(inter_fd,14); - return 0; -} -// ギルド同盟/敵対要求 -int intif_guild_alliance(int guild_id1,int guild_id2,int account_id1,int account_id2,int flag) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,19); - WFIFOW(inter_fd, 0)=0x303d; - WFIFOL(inter_fd, 2)=guild_id1; - WFIFOL(inter_fd, 6)=guild_id2; - WFIFOL(inter_fd,10)=account_id1; - WFIFOL(inter_fd,14)=account_id2; - WFIFOB(inter_fd,18)=flag; - WFIFOSET(inter_fd,19); - return 0; -} -// ギルド告知変更要求 -int intif_guild_notice(int guild_id,const char *mes1,const char *mes2) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,186); - WFIFOW(inter_fd,0)=0x303e; - WFIFOL(inter_fd,2)=guild_id; - memcpy(WFIFOP(inter_fd,6),mes1,60); - memcpy(WFIFOP(inter_fd,66),mes2,120); - WFIFOSET(inter_fd,186); - return 0; -} -// ギルドエンブレム変更要求 -int intif_guild_emblem(int guild_id,int len,const char *data) -{ - if (CheckForCharServer()) - return 0; - if(guild_id<=0 || len<0 || len>2000) - return 0; - WFIFOHEAD(inter_fd,len + 12); - WFIFOW(inter_fd,0)=0x303f; - WFIFOW(inter_fd,2)=len+12; - WFIFOL(inter_fd,4)=guild_id; - WFIFOL(inter_fd,8)=0; - memcpy(WFIFOP(inter_fd,12),data,len); - WFIFOSET(inter_fd,len+12); - return 0; -} -//現在のギルド城占領ギルドを調べる -int intif_guild_castle_dataload(int castle_id,int index) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,5); - WFIFOW(inter_fd,0)=0x3040; - WFIFOW(inter_fd,2)=castle_id; - WFIFOB(inter_fd,4)=index; - WFIFOSET(inter_fd,5); - return 0; -} - -//ギルド城占領ギルド変更要求 -int intif_guild_castle_datasave(int castle_id,int index, int value) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd,9); - WFIFOW(inter_fd,0)=0x3041; - WFIFOW(inter_fd,2)=castle_id; - WFIFOB(inter_fd,4)=index; - WFIFOL(inter_fd,5)=value; - WFIFOSET(inter_fd,9); - return 0; -} - -//----------------------------------------------------------------- -// Homunculus Packets send to Inter server [albator] -//----------------------------------------------------------------- - -int intif_homunculus_create(int account_id, struct s_homunculus *sh) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, sizeof(struct s_homunculus)+8); - WFIFOW(inter_fd,0) = 0x3090; - WFIFOW(inter_fd,2) = sizeof(struct s_homunculus)+8; - WFIFOL(inter_fd,4) = account_id; - memcpy(WFIFOP(inter_fd,8),sh,sizeof(struct s_homunculus)); - WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); - return 0; -} - -int intif_homunculus_requestload(int account_id, int homun_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 10); - WFIFOW(inter_fd,0) = 0x3091; - WFIFOL(inter_fd,2) = account_id; - WFIFOL(inter_fd,6) = homun_id; - WFIFOSET(inter_fd, 10); - return 1; -} - -int intif_homunculus_requestsave(int account_id, struct s_homunculus* sh) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, sizeof(struct s_homunculus)+8); - WFIFOW(inter_fd,0) = 0x3092; - WFIFOW(inter_fd,2) = sizeof(struct s_homunculus)+8; - WFIFOL(inter_fd,4) = account_id; - memcpy(WFIFOP(inter_fd,8),sh,sizeof(struct s_homunculus)); - WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); - return 0; - -} - -int intif_homunculus_requestdelete(int homun_id) -{ - if (CheckForCharServer()) - return 0; - WFIFOHEAD(inter_fd, 6); - WFIFOW(inter_fd, 0) = 0x3093; - WFIFOL(inter_fd,2) = homun_id; - WFIFOSET(inter_fd,6); - return 0; - -} - - -//----------------------------------------------------------------- -// Packets receive from inter server - -// Wisp/Page reception -int intif_parse_WisMessage(int fd) { // rewritten by [Yor] - struct map_session_data* sd; - char *wisp_source; - char name[NAME_LENGTH]; - int id, i; - RFIFOHEAD(fd); - id=RFIFOL(fd,4); - - memcpy(name, RFIFOP(fd,32), NAME_LENGTH); - name[NAME_LENGTH-1] = '\0'; //In case name arrived without it's terminator. [Skotlex] - sd = map_nick2sd(name); - if(sd == NULL || strcmp(sd->status.name, name) != 0) - { //Not found - intif_wis_replay(id,1); - return 0; - } - if(sd->state.ignoreAll) { - intif_wis_replay(id, 2); - return 0; - } - wisp_source = (char *) RFIFOP(fd,8); // speed up [Yor] - for(i=0; i < MAX_IGNORE_LIST && - sd->ignore[i].name[0] != '\0' && - strcmp(sd->ignore[i].name, wisp_source) != 0 - ; i++); - - if (i < MAX_IGNORE_LIST && sd->ignore[i].name[0] != '\0') - { //Ignored - intif_wis_replay(id, 2); - return 0; - } - //Success to send whisper. - clif_wis_message(sd->fd, wisp_source, (char*)RFIFOP(fd,56),RFIFOW(fd,2)-56); - intif_wis_replay(id,0); // 送信成功 - return 0; -} - -// Wisp/page transmission result reception -int intif_parse_WisEnd(int fd) { - struct map_session_data* sd; - RFIFOHEAD(fd); - - if (battle_config.etc_log) - ShowInfo("intif_parse_wisend: player: %s, flag: %d\n", RFIFOP(fd,2), RFIFOB(fd,26)); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target - sd = (struct map_session_data *)map_nick2sd((char *) RFIFOP(fd,2)); - if (sd != NULL) - clif_wis_end(sd->fd, RFIFOB(fd,26)); - - return 0; -} - -static int mapif_parse_WisToGM_sub(struct map_session_data* sd,va_list va) { - int min_gm_level = va_arg(va, int); - char *wisp_name; - char *message; - int len; - if (pc_isGM(sd) < min_gm_level) return 0; - wisp_name = va_arg(va, char*); - message = va_arg(va, char*); - len = va_arg(va, int); - clif_wis_message(sd->fd, wisp_name, message, len); - return 1; -} - -// Received wisp message from map-server via char-server for ALL gm -int mapif_parse_WisToGM(int fd) { // 0x3003/0x3803 .w .24B .w .?B - int min_gm_level, mes_len; - char Wisp_name[NAME_LENGTH]; - char mbuf[255]; - char *message; - RFIFOHEAD(fd); - - mes_len = RFIFOW(fd,2) - 30; - message = (char *) (mes_len >= 255 ? (char *) aMallocA(mes_len) : mbuf); - - min_gm_level = (int)RFIFOW(fd,28); - memcpy(Wisp_name, RFIFOP(fd,4), NAME_LENGTH); - Wisp_name[NAME_LENGTH-1] = '\0'; - memcpy(message, RFIFOP(fd,30), mes_len); - message[mes_len-1] = '\0'; - // information is sended to all online GM - clif_foreachclient(mapif_parse_WisToGM_sub, min_gm_level, Wisp_name, message, mes_len); - - if (message != mbuf) - aFree(message); - return 0; -} - -// アカウント変数通知 -int intif_parse_Registers(int fd) { - int j,p,len,max, flag; - struct map_session_data *sd; - struct global_reg *reg; - int *qty; - RFIFOHEAD(fd); - - if( (sd=map_id2sd(RFIFOL(fd,4)))==NULL) - return 1; - - if (RFIFOB(fd,12) == 3 && sd->status.char_id != RFIFOL(fd,8)) - return 1; //Character registry from another character. - - flag = (sd->save_reg.global_num == -1 || sd->save_reg.account_num == -1 || sd->save_reg.account2_num == -1); - - switch (RFIFOB(fd,12)) { - case 3: //Character Registry - reg = sd->save_reg.global; - qty = &sd->save_reg.global_num; - max = GLOBAL_REG_NUM; - break; - case 2: //Account Registry - reg = sd->save_reg.account; - qty = &sd->save_reg.account_num; - max = ACCOUNT_REG_NUM; - break; - case 1: //Account2 Registry - reg = sd->save_reg.account2; - qty = &sd->save_reg.account2_num; - max = ACCOUNT_REG2_NUM; - break; - default: - if (battle_config.error_log) - ShowError("intif_parse_Registers: Unrecognized type %d\n",RFIFOB(fd,12)); - return 0; - } - for(j=0,p=13;jsave_reg.global_num > -1 && sd->save_reg.account_num > -1 && sd->save_reg.account2_num > -1) - pc_reg_received(sd); //Received all registry values, execute init scripts and what-not. [Skotlex] - return 1; -} - -// 倉庫データ受信 -int intif_parse_LoadStorage(int fd) { - struct storage *stor; - struct map_session_data *sd; - RFIFOHEAD(fd); - - sd=map_id2sd( RFIFOL(fd,4) ); - if(sd==NULL){ - if(battle_config.error_log) - ShowError("intif_parse_LoadStorage: user not found %d\n",RFIFOL(fd,4)); - return 1; - } - - if (sd->state.finalsave) - return 1; //Player is already scheduled to leave the server. - - stor = account2storage( RFIFOL(fd,4)); - - if (stor->storage_status == 1) { // Already open.. lets ignore this update - if (battle_config.error_log) - ShowWarning("intif_parse_LoadStorage: storage received for a client already open (User %d:%d)\n", sd->status.account_id, sd->status.char_id); - return 1; - } - if (stor->dirty) { // Already have storage, and it has been modified and not saved yet! Exploit! [Skotlex] - if (battle_config.error_log) - ShowWarning("intif_parse_LoadStorage: received storage for an already modified non-saved storage! (User %d:%d)\n", sd->status.account_id, sd->status.char_id); - return 1; - } - if (RFIFOW(fd,2)-8 != sizeof(struct storage)) { - if (battle_config.error_log) - ShowError("intif_parse_LoadStorage: data size error %d %d\n", RFIFOW(fd,2)-8, sizeof(struct storage)); - return 1; - } - if(battle_config.save_log) - ShowInfo("intif_openstorage: %d\n",RFIFOL(fd,4) ); - memcpy(stor,RFIFOP(fd,8),sizeof(struct storage)); - stor->dirty=0; - stor->storage_status=1; - sd->state.storage_flag = 1; - clif_storagelist(sd,stor); - clif_updatestorageamount(sd,stor); - - return 0; -} - -// 倉庫データ送信成功 -int intif_parse_SaveStorage(int fd) -{ - RFIFOHEAD(fd); - if(battle_config.save_log) - ShowInfo("intif_savestorage: done %d %d\n",RFIFOL(fd,2),RFIFOB(fd,6) ); - storage_storage_saved(RFIFOL(fd,2)); - return 0; -} - -int intif_parse_LoadGuildStorage(int fd) -{ - struct guild_storage *gstor; - struct map_session_data *sd; - int guild_id; - RFIFOHEAD(fd); - guild_id = RFIFOL(fd,8); - if(guild_id <= 0) - return 1; - sd=map_id2sd( RFIFOL(fd,4) ); - if(sd==NULL){ - if(battle_config.error_log) - ShowError("intif_parse_LoadGuildStorage: user not found %d\n",RFIFOL(fd,4)); - return 1; - } - gstor=guild2storage(guild_id); - if(!gstor) { - if(battle_config.error_log) - ShowWarning("intif_parse_LoadGuildStorage: error guild_id %d not exist\n",guild_id); - return 1; - } - if (gstor->storage_status == 1) { // Already open.. lets ignore this update - if (battle_config.error_log) - ShowWarning("intif_parse_LoadGuildStorage: storage received for a client already open (User %d:%d)\n", sd->status.account_id, sd->status.char_id); - return 1; - } - if (gstor->dirty) { // Already have storage, and it has been modified and not saved yet! Exploit! [Skotlex] - if (battle_config.error_log) - ShowWarning("intif_parse_LoadGuildStorage: received storage for an already modified non-saved storage! (User %d:%d)\n", sd->status.account_id, sd->status.char_id); - return 1; - } - if( RFIFOW(fd,2)-12 != sizeof(struct guild_storage) ){ - gstor->storage_status = 0; - if(battle_config.error_log) - ShowError("intif_parse_LoadGuildStorage: data size error %d %d\n",RFIFOW(fd,2)-12 , sizeof(struct guild_storage)); - return 1; - } - if(battle_config.save_log) - ShowInfo("intif_open_guild_storage: %d\n",RFIFOL(fd,4) ); - memcpy(gstor,RFIFOP(fd,12),sizeof(struct guild_storage)); - gstor->storage_status = 1; - sd->state.storage_flag = 2; - clif_guildstoragelist(sd,gstor); - clif_updateguildstorageamount(sd,gstor); - return 0; -} -int intif_parse_SaveGuildStorage(int fd) -{ - RFIFOHEAD(fd); - if(battle_config.save_log) { - ShowInfo("intif_save_guild_storage: done %d %d %d\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOB(fd,10) ); - } - storage_guild_storagesaved(/*RFIFOL(fd,2), */RFIFOL(fd,6)); - return 0; -} - -// パーティ作成可否 -int intif_parse_PartyCreated(int fd) -{ - RFIFOHEAD(fd); - if(battle_config.etc_log) - ShowInfo("intif: party created by account %d\n\n", RFIFOL(fd,2)); - party_created(RFIFOL(fd,2), RFIFOL(fd,6),RFIFOB(fd,10),RFIFOL(fd,11), (char *)RFIFOP(fd,15)); - return 0; -} -// パーティ情報 -int intif_parse_PartyInfo(int fd) -{ - RFIFOHEAD(fd); - if( RFIFOW(fd,2)==8){ - if(battle_config.error_log) - ShowWarning("intif: party noinfo %d\n",RFIFOL(fd,4)); - party_recv_noinfo(RFIFOL(fd,4)); - return 0; - } - -// printf("intif: party info %d\n",RFIFOL(fd,4)); - if( RFIFOW(fd,2)!=sizeof(struct party)+4 ){ - if(battle_config.error_log) - ShowError("intif: party info : data size error %d %d %d\n",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct party)+4); - } - party_recv_info((struct party *)RFIFOP(fd,4)); - return 0; -} -// パーティ追加通知 -int intif_parse_PartyMemberAdded(int fd) -{ - RFIFOHEAD(fd); - if(battle_config.etc_log) - ShowInfo("intif: party member added Party (%d), Account(%d), Char(%d)\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); - party_member_added(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10), RFIFOB(fd, 14)); - return 0; -} -// パーティ設定変更通知 -int intif_parse_PartyOptionChanged(int fd) -{ - RFIFOHEAD(fd); - party_optionchanged(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOW(fd,10),RFIFOW(fd,12),RFIFOB(fd,14)); - return 0; -} -// パーティ脱退通知 -int intif_parse_PartyMemberLeaved(int fd) -{ - RFIFOHEAD(fd); - if(battle_config.etc_log) - ShowInfo("intif: party member leaved: Party(%d), Account(%d), Char(%d)\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); - party_member_leaved(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); - return 0; -} -// パーティ解散通知 -int intif_parse_PartyBroken(int fd) -{ - RFIFOHEAD(fd); - party_broken(RFIFOL(fd,2)); - return 0; -} -// パーティ移動通知 -int intif_parse_PartyMove(int fd) -{ - RFIFOHEAD(fd); - party_recv_movemap(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOW(fd,14),RFIFOB(fd,16),RFIFOW(fd,17)); - return 0; -} -// パーティメッセージ -int intif_parse_PartyMessage(int fd) -{ - RFIFOHEAD(fd); -// if(battle_config.etc_log) -// printf("intif_parse_PartyMessage: %s\n",RFIFOP(fd,12)); - party_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),(char *) RFIFOP(fd,12),RFIFOW(fd,2)-12); - return 0; -} - -// ギルド作成可否 -int intif_parse_GuildCreated(int fd) -{ - RFIFOHEAD(fd); - guild_created(RFIFOL(fd,2),RFIFOL(fd,6)); - return 0; -} -// ギルド情報 -int intif_parse_GuildInfo(int fd) -{ - RFIFOHEAD(fd); - if( RFIFOW(fd,2)==8){ - if(battle_config.error_log) - ShowWarning("intif: guild noinfo %d\n",RFIFOL(fd,4)); - guild_recv_noinfo(RFIFOL(fd,4)); - return 0; - } - -// if(battle_config.etc_log) -// printf("intif: guild info %d\n",RFIFOL(fd,4)); - if( RFIFOW(fd,2)!=sizeof(struct guild)+4 ){ - if(battle_config.error_log) - ShowError("intif: guild info : data size error Gid: %d recv size: %d Expected size: %d\n",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct guild)+4); - } - guild_recv_info((struct guild *)RFIFOP(fd,4)); - return 0; -} -// ギルドメンバ追加通知 -int intif_parse_GuildMemberAdded(int fd) -{ - RFIFOHEAD(fd); - if(battle_config.etc_log) - ShowInfo("intif: guild member added %d %d %d %d\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14)); - guild_member_added(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14)); - return 0; -} -// ギルドメンバ脱退/追放通知 -int intif_parse_GuildMemberLeaved(int fd) -{ - RFIFOHEAD(fd); - guild_member_leaved(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14), - (char *) RFIFOP(fd,55), (char *) RFIFOP(fd,15)); - return 0; -} - -// ギルドメンバオンライン状態/Lv変更通知 -int intif_parse_GuildMemberInfoShort(int fd) -{ - RFIFOHEAD(fd); - guild_recv_memberinfoshort(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOW(fd,15),RFIFOW(fd,17)); - return 0; -} -// ギルド解散通知 -int intif_parse_GuildBroken(int fd) -{ - RFIFOHEAD(fd); - guild_broken(RFIFOL(fd,2),RFIFOB(fd,6)); - return 0; -} - -// ギルド基本情報変更通知 -int intif_parse_GuildBasicInfoChanged(int fd) -{ - int type, guild_id; - unsigned int dd; - void *data; - struct guild *g; - short dw; - RFIFOHEAD(fd); - type=RFIFOW(fd,8); - guild_id=RFIFOL(fd,4); - data=RFIFOP(fd,10); - g=guild_search(guild_id); - dw=*((short *)data); - dd=*((unsigned int *)data); - if( g==NULL ) - return 0; - switch(type){ - case GBI_EXP: g->exp=dd; break; - case GBI_GUILDLV: g->guild_lv=dw; break; - case GBI_SKILLPOINT: g->skill_point=dd; break; - } - return 0; -} -// ギルドメンバ情報変更通知 -int intif_parse_GuildMemberInfoChanged(int fd) -{ - int type, guild_id, account_id, char_id, idx, dd; - void* data; - struct guild *g; - RFIFOHEAD(fd); - type=RFIFOW(fd,16); - guild_id=RFIFOL(fd,4); - account_id=RFIFOL(fd,8); - char_id=RFIFOL(fd,12); - data=RFIFOP(fd,18); - g=guild_search(guild_id); - dd=*((int *)data); - if( g==NULL ) - return 0; - idx=guild_getindex(g,account_id,char_id); - switch(type){ - case GMI_POSITION: - g->member[idx].position=dd; - guild_memberposition_changed(g,idx,dd); - break; - case GMI_EXP: - g->member[idx].exp=dd; - break; - case GMI_HAIR: - g->member[idx].hair=dd; - break; - case GMI_HAIR_COLOR: - g->member[idx].hair_color=dd; - break; - case GMI_GENDER: - g->member[idx].gender=dd; - break; - case GMI_CLASS: - g->member[idx].class_=dd; - break; - case GMI_LEVEL: - g->member[idx].lv=dd; - break; - } - return 0; -} - -// ギルド役職変更通知 -int intif_parse_GuildPosition(int fd) -{ - RFIFOHEAD(fd); - if( RFIFOW(fd,2)!=sizeof(struct guild_position)+12 ){ - if(battle_config.error_log) - ShowError("intif: guild info : data size error\n %d %d %d",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct guild_position)+12); - } - guild_position_changed(RFIFOL(fd,4),RFIFOL(fd,8),(struct guild_position *)RFIFOP(fd,12)); - return 0; -} -// ギルドスキル割り振り通知 -int intif_parse_GuildSkillUp(int fd) -{ - RFIFOHEAD(fd); - guild_skillupack(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); - return 0; -} -// ギルド同盟/敵対通知 -int intif_parse_GuildAlliance(int fd) -{ - RFIFOHEAD(fd); - guild_allianceack(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14), - RFIFOB(fd,18),(char *) RFIFOP(fd,19),(char *) RFIFOP(fd,43)); - return 0; -} -// ギルド告知変更通知 -int intif_parse_GuildNotice(int fd) -{ - RFIFOHEAD(fd); - guild_notice_changed(RFIFOL(fd,2),(char *) RFIFOP(fd,6),(char *) RFIFOP(fd,66)); - return 0; -} -// ギルドエンブレム変更通知 -int intif_parse_GuildEmblem(int fd) -{ - RFIFOHEAD(fd); - guild_emblem_changed(RFIFOW(fd,2)-12,RFIFOL(fd,4),RFIFOL(fd,8), (char *)RFIFOP(fd,12)); - return 0; -} -// ギルド会話受信 -int intif_parse_GuildMessage(int fd) -{ - RFIFOHEAD(fd); - guild_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),(char *) RFIFOP(fd,12),RFIFOW(fd,2)-12); - return 0; -} -// ギルド城データ要求返信 -int intif_parse_GuildCastleDataLoad(int fd) -{ - RFIFOHEAD(fd); - return guild_castledataloadack(RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); -} -// ギルド城データ変更通知 -int intif_parse_GuildCastleDataSave(int fd) -{ - RFIFOHEAD(fd); - return guild_castledatasaveack(RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); -} - -// ギルド城データ一括受信(初期化時) -int intif_parse_GuildCastleAllDataLoad(int fd) -{ - RFIFOHEAD(fd); - return guild_castlealldataload(RFIFOW(fd,2),(struct guild_castle *)RFIFOP(fd,4)); -} - -int intif_parse_GuildMasterChanged(int fd) -{ - RFIFOHEAD(fd); - return guild_gm_changed(RFIFOL(fd,2),RFIFOL(fd,6)); -} - -// pet -int intif_parse_CreatePet(int fd) -{ - RFIFOHEAD(fd); - pet_get_egg(RFIFOL(fd,2),RFIFOL(fd,7),RFIFOB(fd,6)); - - return 0; -} - -int intif_parse_RecvPetData(int fd) -{ - struct s_pet p; - int len; - RFIFOHEAD(fd); - len=RFIFOW(fd,2); - if(sizeof(struct s_pet)!=len-9) { - if(battle_config.etc_log) - ShowError("intif: pet data: data size error %d %d\n",sizeof(struct s_pet),len-9); - } - else{ - memcpy(&p,RFIFOP(fd,9),sizeof(struct s_pet)); - pet_recv_petdata(RFIFOL(fd,4),&p,RFIFOB(fd,8)); - } - - return 0; -} -int intif_parse_SavePetOk(int fd) -{ - RFIFOHEAD(fd); - if(RFIFOB(fd,6) == 1) { - if(battle_config.error_log) - ShowError("pet data save failure\n"); - } - - return 0; -} - -int intif_parse_DeletePetOk(int fd) -{ - RFIFOHEAD(fd); - if(RFIFOB(fd,2) == 1) { - if(battle_config.error_log) - ShowError("pet data delete failure\n"); - } - - return 0; -} - -int intif_parse_RenamePetOk(int fd) -{ - struct map_session_data *sd = NULL; - RFIFOHEAD(fd); - if((sd=map_id2sd(RFIFOL(fd,2)))==NULL || - sd->status.char_id != RFIFOL(fd,6)) - return 0; - if (RFIFOB(fd,10) == 0) { - clif_displaymessage(sd->fd, msg_txt(280)); // You cannot use this name for your pet. - clif_send_petstatus(sd); //Send status so client knows oet name change got rejected. - return 0; - } - pet_change_name(sd, RFIFOP(fd,11),1); - return 0; -} - -//---------------------------------------------------------------- -// Homunculus recv packets [albator] - -int intif_parse_CreateHomunculus(int fd) -{ - int len; - RFIFOHEAD(fd); - len=RFIFOW(fd,2)-9; - if(sizeof(struct s_homunculus)!=len) { - if(battle_config.etc_log) - ShowError("intif: create homun data: data size error %d != %d\n",sizeof(struct s_homunculus),len); - return 0; - } - merc_hom_recv_data(RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,9), RFIFOB(fd,8)) ; - return 0; -} - -int intif_parse_RecvHomunculusData(int fd) -{ - int len; - - RFIFOHEAD(fd); - len=RFIFOW(fd,2)-9; - - if(sizeof(struct s_homunculus)!=len) { - if(battle_config.etc_log) - ShowError("intif: homun data: data size error %d %d\n",sizeof(struct s_homunculus),len); - return 0; - } - merc_hom_recv_data(RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,9), RFIFOB(fd,8)); - return 0; -} - -int intif_parse_SaveHomunculusOk(int fd) -{ - RFIFOHEAD(fd); - if(RFIFOB(fd,6) != 1) { - if(battle_config.error_log) - ShowError("homunculus data save failure for account %d\n", RFIFOL(fd,2)); - } - return 0; -} - -int intif_parse_DeleteHomunculusOk(int fd) -{ - RFIFOHEAD(fd); - if(RFIFOB(fd,2) != 1) { - if(battle_config.error_log) - ShowError("Homunculus data delete failure\n"); - } - - return 0; -} -//----------------------------------------------------------------- -// inter serverからの通信 -// エラーがあれば0(false)を返すこと -// パケットが処理できれば1,パケット長が足りなければ2を返すこと -int intif_parse(int fd) -{ - int packet_len, cmd; - RFIFOHEAD(fd); - cmd = RFIFOW(fd,0); - // パケットのID確認 - if(cmd<0x3800 || cmd>=0x3800+(sizeof(packet_len_table)/sizeof(packet_len_table[0])) || - packet_len_table[cmd-0x3800]==0){ - return 0; - } - // パケットの長さ確認 - packet_len = packet_len_table[cmd-0x3800]; - if(packet_len==-1){ - if(RFIFOREST(fd)<4) - return 2; - packet_len = RFIFOW(fd,2); - } -// if(battle_config.etc_log) -// printf("intif_parse %d %x %d %d\n",fd,cmd,packet_len,RFIFOREST(fd)); - if((int)RFIFOREST(fd) +#include +#include +#include +#include +#include + +#include "../common/showmsg.h" +#include "../common/socket.h" +#include "../common/timer.h" +#include "../common/nullpo.h" +#include "../common/malloc.h" +#include "map.h" +#include "battle.h" +#include "chrif.h" +#include "clif.h" +#include "pc.h" +#include "intif.h" +#include "storage.h" +#include "party.h" +#include "guild.h" +#include "pet.h" +#include "atcommand.h" +#include "mercenary.h" //albator + +static const int packet_len_table[]={ + -1,-1,27,-1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3800-0x380f + -1, 7, 0, 0, 0, 0, 0, 0, -1,11, 0, 0, 0, 0, 0, 0, //0x3810 + 39,-1,15,15, 14,19, 7,-1, 0, 0, 0, 0, 0, 0, 0, 0, //0x3820 + 10,-1,15, 0, 79,19, 7,-1, 0,-1,-1,-1, 14,67,186,-1, //0x3830 + 9, 9,-1,10, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3840 + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 11,-1, 7, 3, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3880 + -1,-1, 7, 3, 36, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, //0x3890 Homunculus [albator] +}; + +extern int char_fd; // inter serverのfdはchar_fdを使う +#define inter_fd (char_fd) // エイリアス + +//----------------------------------------------------------------- +// inter serverへの送信 + +int CheckForCharServer(void) { + return ((char_fd <= 0) || session[char_fd] == NULL || session[char_fd]->wdata == NULL); +} + +// pet +int intif_create_pet(int account_id,int char_id,short pet_class,short pet_lv,short pet_egg_id, + short pet_equip,short intimate,short hungry,char rename_flag,char incuvate,char *pet_name) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 24 + NAME_LENGTH); + WFIFOW(inter_fd,0) = 0x3080; + WFIFOL(inter_fd,2) = account_id; + WFIFOL(inter_fd,6) = char_id; + WFIFOW(inter_fd,10) = pet_class; + WFIFOW(inter_fd,12) = pet_lv; + WFIFOW(inter_fd,14) = pet_egg_id; + WFIFOW(inter_fd,16) = pet_equip; + WFIFOW(inter_fd,18) = intimate; + WFIFOW(inter_fd,20) = hungry; + WFIFOB(inter_fd,22) = rename_flag; + WFIFOB(inter_fd,23) = incuvate; + memcpy(WFIFOP(inter_fd,24),pet_name,NAME_LENGTH); + WFIFOSET(inter_fd,24+NAME_LENGTH); + + return 0; +} + +int intif_request_petdata(int account_id,int char_id,int pet_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 14); + WFIFOW(inter_fd,0) = 0x3081; + WFIFOL(inter_fd,2) = account_id; + WFIFOL(inter_fd,6) = char_id; + WFIFOL(inter_fd,10) = pet_id; + WFIFOSET(inter_fd,14); + + return 0; +} + +int intif_save_petdata(int account_id,struct s_pet *p) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, sizeof(struct s_pet) + 8); + WFIFOW(inter_fd,0) = 0x3082; + WFIFOW(inter_fd,2) = sizeof(struct s_pet) + 8; + WFIFOL(inter_fd,4) = account_id; + memcpy(WFIFOP(inter_fd,8),p,sizeof(struct s_pet)); + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + + return 0; +} + +int intif_delete_petdata(int pet_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,6); + WFIFOW(inter_fd,0) = 0x3083; + WFIFOL(inter_fd,2) = pet_id; + WFIFOSET(inter_fd,6); + + return 1; +} +int intif_rename_pet(struct map_session_data *sd,char *name) +{ + if (CheckForCharServer()) + return 1; + + WFIFOHEAD(inter_fd,NAME_LENGTH+11); + WFIFOW(inter_fd,0) = 0x3084; + WFIFOL(inter_fd,2) = sd->status.account_id; + WFIFOL(inter_fd,6) = sd->status.char_id; + memcpy(WFIFOP(inter_fd,10),name, NAME_LENGTH); + WFIFOSET(inter_fd,NAME_LENGTH+11); + return 0; +} + + +// GMメッセージを送信 +int intif_GMmessage(char* mes,int len,int flag) +{ + int lp = (flag&0x10) ? 8 : 4; + + // Send to the local players + clif_GMmessage(NULL, mes, len, flag); + + if (CheckForCharServer()) + return 0; + + if (other_mapserver_count < 1) + return 0; //No need to send. + + WFIFOHEAD(inter_fd,lp + len + 4); + WFIFOW(inter_fd,0) = 0x3000; + WFIFOW(inter_fd,2) = lp + len + 4; + WFIFOL(inter_fd,4) = 0xFF000000; //"invalid" color signals standard broadcast. + WFIFOL(inter_fd,8) = 0x65756c62; + memcpy(WFIFOP(inter_fd,4+lp), mes, len); + WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); + return 0; +} + +int intif_announce(char* mes,int len, unsigned long color, int flag) +{ + // Send to the local players + if(color == 0xFE000000) // This is main chat message [LuzZza] + clif_MainChatMessage(mes); + else + clif_announce(NULL, mes, len, color, flag); + + if (CheckForCharServer()) + return 0; + + if (other_mapserver_count < 1) + return 0; //No need to send. + + WFIFOHEAD(inter_fd, 8 + len); + WFIFOW(inter_fd,0) = 0x3000; + WFIFOW(inter_fd,2) = 8 + len; + WFIFOL(inter_fd,4) = color; + memcpy(WFIFOP(inter_fd,8), mes, len); + WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); + return 0; +} + +// The transmission of Wisp/Page to inter-server (player not found on this server) +int intif_wis_message(struct map_session_data *sd, char *nick, char *mes, int mes_len) { + nullpo_retr(0, sd); + if (CheckForCharServer()) + return 0; + + if (other_mapserver_count < 1) + { //Character not found. + clif_wis_end(sd->fd, 1); + return 0; + } + + WFIFOHEAD(inter_fd,mes_len + 52); + WFIFOW(inter_fd,0) = 0x3001; + WFIFOW(inter_fd,2) = mes_len + 52; + memcpy(WFIFOP(inter_fd,4), sd->status.name, NAME_LENGTH); + memcpy(WFIFOP(inter_fd,4+NAME_LENGTH), nick, NAME_LENGTH); + memcpy(WFIFOP(inter_fd,4+2*NAME_LENGTH), mes, mes_len); + WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); + + if (battle_config.etc_log) + ShowInfo("intif_wis_message from %s to %s (message: '%s')\n", sd->status.name, nick, mes); + + return 0; +} + +// The reply of Wisp/page +int intif_wis_replay(int id, int flag) { + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,7); + WFIFOW(inter_fd,0) = 0x3002; + WFIFOL(inter_fd,2) = id; + WFIFOB(inter_fd,6) = flag; // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target + WFIFOSET(inter_fd,7); + + if (battle_config.etc_log) + ShowInfo("intif_wis_replay: id: %d, flag:%d\n", id, flag); + + return 0; +} + +// The transmission of GM only Wisp/Page from server to inter-server +int intif_wis_message_to_gm(char *Wisp_name, int min_gm_level, char *mes) { + int mes_len; + if (CheckForCharServer()) + return 0; + mes_len = strlen(mes) + 1; // + null + WFIFOHEAD(inter_fd, mes_len + 30); + WFIFOW(inter_fd,0) = 0x3003; + WFIFOW(inter_fd,2) = mes_len + 30; + memcpy(WFIFOP(inter_fd,4), Wisp_name, NAME_LENGTH); + WFIFOW(inter_fd,4+NAME_LENGTH) = (short)min_gm_level; + memcpy(WFIFOP(inter_fd,6+NAME_LENGTH), mes, mes_len); + WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); + + if (battle_config.etc_log) + ShowNotice("intif_wis_message_to_gm: from: '%s', min level: %d, message: '%s'.\n", Wisp_name, min_gm_level, mes); + + return 0; +} + +int intif_regtostr(char* str, struct global_reg *reg, int qty) { + int len =0, i; + + for (i = 0; i < qty; i++) { + len+= sprintf(str+len, "%s", reg[i].str)+1; //We add 1 to consider the '\0' in place. + len+= sprintf(str+len, "%s", reg[i].value)+1; + } + return len; +} + +//Request for saving registry values. +int intif_saveregistry(struct map_session_data *sd, int type) +{ + struct global_reg *reg; + int count; + + if (CheckForCharServer()) + return -1; + + switch (type) { + case 3: //Character reg + reg = sd->save_reg.global; + count = sd->save_reg.global_num; + sd->state.reg_dirty &= ~0x4; + break; + case 2: //Account reg + reg = sd->save_reg.account; + count = sd->save_reg.account_num; + sd->state.reg_dirty &= ~0x2; + break; + case 1: //Account2 reg + reg = sd->save_reg.account2; + count = sd->save_reg.account2_num; + sd->state.reg_dirty &= ~0x1; + break; + default: //Broken code? + if (battle_config.error_log) + ShowError("intif_saveregistry: Invalid type %d\n", type); + return -1; + } + WFIFOHEAD(inter_fd, 288 * MAX_REG_NUM+13); + WFIFOW(inter_fd,0)=0x3004; + WFIFOL(inter_fd,4)=sd->status.account_id; + WFIFOL(inter_fd,8)=sd->status.char_id; + WFIFOB(inter_fd,12)=type; + if(count ==0){ + WFIFOW(inter_fd,2)=13; + }else{ + int i,p; + for (p=13,i = 0; i < count; i++) { + if (reg[i].str[0] && reg[i].value != 0) { + p+= sprintf(WFIFOP(inter_fd,p), "%s", reg[i].str)+1; //We add 1 to consider the '\0' in place. + p+= sprintf(WFIFOP(inter_fd,p), "%s", reg[i].value)+1; + } + } + WFIFOW(inter_fd,2)=p; + } + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + return 0; +} + +//Request the registries for this player. +int intif_request_registry(struct map_session_data *sd, int flag) +{ + nullpo_retr(0, sd); + + sd->save_reg.account2_num = -1; + sd->save_reg.account_num = -1; + sd->save_reg.global_num = -1; + + if (CheckForCharServer()) + return 0; + + WFIFOHEAD(inter_fd,6); + WFIFOW(inter_fd,0) = 0x3005; + WFIFOL(inter_fd,2) = sd->status.account_id; + WFIFOL(inter_fd,6) = sd->status.char_id; + WFIFOB(inter_fd,10) = (flag&1?1:0); //Request Acc Reg 2 + WFIFOB(inter_fd,11) = (flag&2?1:0); //Request Acc Reg + WFIFOB(inter_fd,12) = (flag&4?1:0); //Request Char Reg + WFIFOSET(inter_fd,13); + + return 0; +} + +// 倉庫データ要求 +int intif_request_storage(int account_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,6); + WFIFOW(inter_fd,0) = 0x3010; + WFIFOL(inter_fd,2) = account_id; + WFIFOSET(inter_fd,6); + return 0; +} +// 倉庫データ送信 +int intif_send_storage(struct storage *stor) +{ + if (CheckForCharServer()) + return 0; + nullpo_retr(0, stor); + WFIFOHEAD(inter_fd,sizeof(struct storage)+8); + WFIFOW(inter_fd,0) = 0x3011; + WFIFOW(inter_fd,2) = sizeof(struct storage)+8; + WFIFOL(inter_fd,4) = stor->account_id; + memcpy( WFIFOP(inter_fd,8),stor, sizeof(struct storage) ); + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + return 0; +} + +int intif_request_guild_storage(int account_id,int guild_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,10); + WFIFOW(inter_fd,0) = 0x3018; + WFIFOL(inter_fd,2) = account_id; + WFIFOL(inter_fd,6) = guild_id; + WFIFOSET(inter_fd,10); + return 0; +} +int intif_send_guild_storage(int account_id,struct guild_storage *gstor) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,sizeof(struct guild_storage)+12); + WFIFOW(inter_fd,0) = 0x3019; + WFIFOW(inter_fd,2) = sizeof(struct guild_storage)+12; + WFIFOL(inter_fd,4) = account_id; + WFIFOL(inter_fd,8) = gstor->guild_id; + memcpy( WFIFOP(inter_fd,12),gstor, sizeof(struct guild_storage) ); + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + return 0; +} + +// パーティ作成要求 +int intif_create_party(struct party_member *member,char *name,int item,int item2) +{ + if (CheckForCharServer()) + return 0; + nullpo_retr(0, member); + + WFIFOHEAD(inter_fd,64); + WFIFOW(inter_fd,0) = 0x3020; + WFIFOW(inter_fd,2) = 30+sizeof(struct party_member); + memcpy(WFIFOP(inter_fd,4),name, NAME_LENGTH); + WFIFOB(inter_fd,28)= item; + WFIFOB(inter_fd,29)= item2; + memcpy(WFIFOP(inter_fd,30), member, sizeof(struct party_member)); + WFIFOSET(inter_fd,WFIFOW(inter_fd, 2)); + return 0; +} +// パーティ情報要求 +int intif_request_partyinfo(int party_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,6); + WFIFOW(inter_fd,0) = 0x3021; + WFIFOL(inter_fd,2) = party_id; + WFIFOSET(inter_fd,6); +// if(battle_config.etc_log) +// printf("intif: request party info\n"); + return 0; +} +// パーティ追加要求 +int intif_party_addmember(int party_id,struct party_member *member) +{ + if (CheckForCharServer()) + return 0; + + WFIFOHEAD(inter_fd,42); + WFIFOW(inter_fd,0)=0x3022; + WFIFOW(inter_fd,2)=8+sizeof(struct party_member); + WFIFOL(inter_fd,4)=party_id; + memcpy(WFIFOP(inter_fd,8),member,sizeof(struct party_member)); + WFIFOSET(inter_fd,WFIFOW(inter_fd, 2)); + return 1; +} +// パーティ設定変更 +int intif_party_changeoption(int party_id,int account_id,int exp,int item) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,14); + WFIFOW(inter_fd,0)=0x3023; + WFIFOL(inter_fd,2)=party_id; + WFIFOL(inter_fd,6)=account_id; + WFIFOW(inter_fd,10)=exp; + WFIFOW(inter_fd,12)=item; + WFIFOSET(inter_fd,14); + return 0; +} +// パーティ脱退要求 +int intif_party_leave(int party_id,int account_id, int char_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,14); + WFIFOW(inter_fd,0)=0x3024; + WFIFOL(inter_fd,2)=party_id; + WFIFOL(inter_fd,6)=account_id; + WFIFOL(inter_fd,10)=char_id; + WFIFOSET(inter_fd,14); + return 0; +} +// パーティ移動要求 +int intif_party_changemap(struct map_session_data *sd,int online) +{ + if (CheckForCharServer()) + return 0; + if(!sd) + return 0; + + WFIFOHEAD(inter_fd,19); + WFIFOW(inter_fd,0)=0x3025; + WFIFOL(inter_fd,2)=sd->status.party_id; + WFIFOL(inter_fd,6)=sd->status.account_id; + WFIFOL(inter_fd,10)=sd->status.char_id; + WFIFOW(inter_fd,14)=sd->mapindex; + WFIFOB(inter_fd,16)=online; + WFIFOW(inter_fd,17)=sd->status.base_level; + WFIFOSET(inter_fd,19); + return 1; +} +// パーティー解散要求 +int intif_break_party(int party_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,6); + WFIFOW(inter_fd,0)=0x3026; + WFIFOL(inter_fd,2)=party_id; + WFIFOSET(inter_fd,6); + return 0; +} +// パーティ会話送信 +int intif_party_message(int party_id,int account_id,char *mes,int len) +{ + if (CheckForCharServer()) + return 0; + + if (other_mapserver_count < 1) + return 0; //No need to send. + + WFIFOHEAD(inter_fd,len + 12); + WFIFOW(inter_fd,0)=0x3027; + WFIFOW(inter_fd,2)=len+12; + WFIFOL(inter_fd,4)=party_id; + WFIFOL(inter_fd,8)=account_id; + memcpy(WFIFOP(inter_fd,12),mes,len); + WFIFOSET(inter_fd,len+12); + return 0; +} +// パーティ競合チェック要求 +int intif_party_checkconflict(int party_id,int account_id,int char_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,10 + NAME_LENGTH); + WFIFOW(inter_fd,0)=0x3028; + WFIFOL(inter_fd,2)=party_id; + WFIFOL(inter_fd,6)=account_id; + WFIFOL(inter_fd,10)=char_id; + WFIFOSET(inter_fd,14); + return 0; +} + +int intif_party_leaderchange(int party_id,int account_id,int char_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,14); + WFIFOW(inter_fd,0)=0x3029; + WFIFOL(inter_fd,2)=party_id; + WFIFOL(inter_fd,6)=account_id; + WFIFOL(inter_fd,10)=char_id; + WFIFOSET(inter_fd,14); + return 0; +} + + +// ギルド作成要求 +int intif_guild_create(const char *name,const struct guild_member *master) +{ + if (CheckForCharServer()) + return 0; + nullpo_retr(0, master); + + WFIFOHEAD(inter_fd,sizeof(struct guild_member)+(8+NAME_LENGTH)); + WFIFOW(inter_fd,0)=0x3030; + WFIFOW(inter_fd,2)=sizeof(struct guild_member)+(8+NAME_LENGTH); + WFIFOL(inter_fd,4)=master->account_id; + memcpy(WFIFOP(inter_fd,8),name,NAME_LENGTH); + memcpy(WFIFOP(inter_fd,8+NAME_LENGTH),master,sizeof(struct guild_member)); + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + return 0; +} +// ギルド情報要求 +int intif_guild_request_info(int guild_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,6); + WFIFOW(inter_fd,0) = 0x3031; + WFIFOL(inter_fd,2) = guild_id; + WFIFOSET(inter_fd,6); + return 0; +} +// ギルドメンバ追加要求 +int intif_guild_addmember(int guild_id,struct guild_member *m) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,sizeof(struct guild_member)+8); + WFIFOW(inter_fd,0) = 0x3032; + WFIFOW(inter_fd,2) = sizeof(struct guild_member)+8; + WFIFOL(inter_fd,4) = guild_id; + memcpy(WFIFOP(inter_fd,8),m,sizeof(struct guild_member)); + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + return 0; +} + +int intif_guild_change_gm(int guild_id, const char* name, int len) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, len + 8); + WFIFOW(inter_fd, 0)=0x3033; + WFIFOW(inter_fd, 2)=len+8; + WFIFOL(inter_fd, 4)=guild_id; + memcpy(WFIFOP(inter_fd,8),name,len); + WFIFOSET(inter_fd,len+8); + return 0; +} + +// ギルドメンバ脱退/追放要求 +int intif_guild_leave(int guild_id,int account_id,int char_id,int flag,const char *mes) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 55); + WFIFOW(inter_fd, 0) = 0x3034; + WFIFOL(inter_fd, 2) = guild_id; + WFIFOL(inter_fd, 6) = account_id; + WFIFOL(inter_fd,10) = char_id; + WFIFOB(inter_fd,14) = flag; + memcpy(WFIFOP(inter_fd,15),mes,40); + WFIFOSET(inter_fd,55); + return 0; +} +// ギルドメンバのオンライン状況/Lv更新要求 +int intif_guild_memberinfoshort(int guild_id, + int account_id,int char_id,int online,int lv,int class_) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 19); + WFIFOW(inter_fd, 0) = 0x3035; + WFIFOL(inter_fd, 2) = guild_id; + WFIFOL(inter_fd, 6) = account_id; + WFIFOL(inter_fd,10) = char_id; + WFIFOB(inter_fd,14) = online; + WFIFOW(inter_fd,15) = lv; + WFIFOW(inter_fd,17) = class_; + WFIFOSET(inter_fd,19); + return 0; +} +// ギルド解散通知 +int intif_guild_break(int guild_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 6); + WFIFOW(inter_fd, 0) = 0x3036; + WFIFOL(inter_fd, 2) = guild_id; + WFIFOSET(inter_fd,6); + return 0; +} +// ギルド会話送信 +int intif_guild_message(int guild_id,int account_id,char *mes,int len) +{ + if (CheckForCharServer()) + return 0; + + if (other_mapserver_count < 1) + return 0; //No need to send. + + WFIFOHEAD(inter_fd, len + 12); + WFIFOW(inter_fd,0)=0x3037; + WFIFOW(inter_fd,2)=len+12; + WFIFOL(inter_fd,4)=guild_id; + WFIFOL(inter_fd,8)=account_id; + memcpy(WFIFOP(inter_fd,12),mes,len); + WFIFOSET(inter_fd,len+12); + + return 0; +} +// ギルド競合チェック要求 +int intif_guild_checkconflict(int guild_id,int account_id,int char_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 14); + WFIFOW(inter_fd, 0)=0x3038; + WFIFOL(inter_fd, 2)=guild_id; + WFIFOL(inter_fd, 6)=account_id; + WFIFOL(inter_fd,10)=char_id; + WFIFOSET(inter_fd,14); + return 0; +} +// ギルド基本情報変更要求 +int intif_guild_change_basicinfo(int guild_id,int type,const void *data,int len) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, len + 10); + WFIFOW(inter_fd,0)=0x3039; + WFIFOW(inter_fd,2)=len+10; + WFIFOL(inter_fd,4)=guild_id; + WFIFOW(inter_fd,8)=type; + memcpy(WFIFOP(inter_fd,10),data,len); + WFIFOSET(inter_fd,len+10); + return 0; +} +// ギルドメンバ情報変更要求 +int intif_guild_change_memberinfo(int guild_id,int account_id,int char_id, + int type,const void *data,int len) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, len + 18); + WFIFOW(inter_fd, 0)=0x303a; + WFIFOW(inter_fd, 2)=len+18; + WFIFOL(inter_fd, 4)=guild_id; + WFIFOL(inter_fd, 8)=account_id; + WFIFOL(inter_fd,12)=char_id; + WFIFOW(inter_fd,16)=type; + memcpy(WFIFOP(inter_fd,18),data,len); + WFIFOSET(inter_fd,len+18); + return 0; +} +// ギルド役職変更要求 +int intif_guild_position(int guild_id,int idx,struct guild_position *p) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, sizeof(struct guild_position)+12); + WFIFOW(inter_fd,0)=0x303b; + WFIFOW(inter_fd,2)=sizeof(struct guild_position)+12; + WFIFOL(inter_fd,4)=guild_id; + WFIFOL(inter_fd,8)=idx; + memcpy(WFIFOP(inter_fd,12),p,sizeof(struct guild_position)); + WFIFOSET(inter_fd,WFIFOW(inter_fd,2)); + return 0; +} +// ギルドスキルアップ要求 +int intif_guild_skillup(int guild_id,int skill_num,int account_id,int flag) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,14); + WFIFOW(inter_fd, 0)=0x303c; + WFIFOL(inter_fd, 2)=guild_id; + WFIFOL(inter_fd, 6)=skill_num; + WFIFOL(inter_fd,10)=account_id; + //WFIFOL(inter_fd,14)=flag; + WFIFOSET(inter_fd,14); + return 0; +} +// ギルド同盟/敵対要求 +int intif_guild_alliance(int guild_id1,int guild_id2,int account_id1,int account_id2,int flag) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,19); + WFIFOW(inter_fd, 0)=0x303d; + WFIFOL(inter_fd, 2)=guild_id1; + WFIFOL(inter_fd, 6)=guild_id2; + WFIFOL(inter_fd,10)=account_id1; + WFIFOL(inter_fd,14)=account_id2; + WFIFOB(inter_fd,18)=flag; + WFIFOSET(inter_fd,19); + return 0; +} +// ギルド告知変更要求 +int intif_guild_notice(int guild_id,const char *mes1,const char *mes2) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,186); + WFIFOW(inter_fd,0)=0x303e; + WFIFOL(inter_fd,2)=guild_id; + memcpy(WFIFOP(inter_fd,6),mes1,60); + memcpy(WFIFOP(inter_fd,66),mes2,120); + WFIFOSET(inter_fd,186); + return 0; +} +// ギルドエンブレム変更要求 +int intif_guild_emblem(int guild_id,int len,const char *data) +{ + if (CheckForCharServer()) + return 0; + if(guild_id<=0 || len<0 || len>2000) + return 0; + WFIFOHEAD(inter_fd,len + 12); + WFIFOW(inter_fd,0)=0x303f; + WFIFOW(inter_fd,2)=len+12; + WFIFOL(inter_fd,4)=guild_id; + WFIFOL(inter_fd,8)=0; + memcpy(WFIFOP(inter_fd,12),data,len); + WFIFOSET(inter_fd,len+12); + return 0; +} +//現在のギルド城占領ギルドを調べる +int intif_guild_castle_dataload(int castle_id,int index) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,5); + WFIFOW(inter_fd,0)=0x3040; + WFIFOW(inter_fd,2)=castle_id; + WFIFOB(inter_fd,4)=index; + WFIFOSET(inter_fd,5); + return 0; +} + +//ギルド城占領ギルド変更要求 +int intif_guild_castle_datasave(int castle_id,int index, int value) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd,9); + WFIFOW(inter_fd,0)=0x3041; + WFIFOW(inter_fd,2)=castle_id; + WFIFOB(inter_fd,4)=index; + WFIFOL(inter_fd,5)=value; + WFIFOSET(inter_fd,9); + return 0; +} + +//----------------------------------------------------------------- +// Homunculus Packets send to Inter server [albator] +//----------------------------------------------------------------- + +int intif_homunculus_create(int account_id, struct s_homunculus *sh) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, sizeof(struct s_homunculus)+8); + WFIFOW(inter_fd,0) = 0x3090; + WFIFOW(inter_fd,2) = sizeof(struct s_homunculus)+8; + WFIFOL(inter_fd,4) = account_id; + memcpy(WFIFOP(inter_fd,8),sh,sizeof(struct s_homunculus)); + WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); + return 0; +} + +int intif_homunculus_requestload(int account_id, int homun_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 10); + WFIFOW(inter_fd,0) = 0x3091; + WFIFOL(inter_fd,2) = account_id; + WFIFOL(inter_fd,6) = homun_id; + WFIFOSET(inter_fd, 10); + return 1; +} + +int intif_homunculus_requestsave(int account_id, struct s_homunculus* sh) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, sizeof(struct s_homunculus)+8); + WFIFOW(inter_fd,0) = 0x3092; + WFIFOW(inter_fd,2) = sizeof(struct s_homunculus)+8; + WFIFOL(inter_fd,4) = account_id; + memcpy(WFIFOP(inter_fd,8),sh,sizeof(struct s_homunculus)); + WFIFOSET(inter_fd, WFIFOW(inter_fd,2)); + return 0; + +} + +int intif_homunculus_requestdelete(int homun_id) +{ + if (CheckForCharServer()) + return 0; + WFIFOHEAD(inter_fd, 6); + WFIFOW(inter_fd, 0) = 0x3093; + WFIFOL(inter_fd,2) = homun_id; + WFIFOSET(inter_fd,6); + return 0; + +} + + +//----------------------------------------------------------------- +// Packets receive from inter server + +// Wisp/Page reception +int intif_parse_WisMessage(int fd) { // rewritten by [Yor] + struct map_session_data* sd; + char *wisp_source; + char name[NAME_LENGTH]; + int id, i; + RFIFOHEAD(fd); + id=RFIFOL(fd,4); + + memcpy(name, RFIFOP(fd,32), NAME_LENGTH); + name[NAME_LENGTH-1] = '\0'; //In case name arrived without it's terminator. [Skotlex] + sd = map_nick2sd(name); + if(sd == NULL || strcmp(sd->status.name, name) != 0) + { //Not found + intif_wis_replay(id,1); + return 0; + } + if(sd->state.ignoreAll) { + intif_wis_replay(id, 2); + return 0; + } + wisp_source = (char *) RFIFOP(fd,8); // speed up [Yor] + for(i=0; i < MAX_IGNORE_LIST && + sd->ignore[i].name[0] != '\0' && + strcmp(sd->ignore[i].name, wisp_source) != 0 + ; i++); + + if (i < MAX_IGNORE_LIST && sd->ignore[i].name[0] != '\0') + { //Ignored + intif_wis_replay(id, 2); + return 0; + } + //Success to send whisper. + clif_wis_message(sd->fd, wisp_source, (char*)RFIFOP(fd,56),RFIFOW(fd,2)-56); + intif_wis_replay(id,0); // 送信成功 + return 0; +} + +// Wisp/page transmission result reception +int intif_parse_WisEnd(int fd) { + struct map_session_data* sd; + RFIFOHEAD(fd); + + if (battle_config.etc_log) + ShowInfo("intif_parse_wisend: player: %s, flag: %d\n", RFIFOP(fd,2), RFIFOB(fd,26)); // flag: 0: success to send wisper, 1: target character is not loged in?, 2: ignored by target + sd = (struct map_session_data *)map_nick2sd((char *) RFIFOP(fd,2)); + if (sd != NULL) + clif_wis_end(sd->fd, RFIFOB(fd,26)); + + return 0; +} + +static int mapif_parse_WisToGM_sub(struct map_session_data* sd,va_list va) { + int min_gm_level = va_arg(va, int); + char *wisp_name; + char *message; + int len; + if (pc_isGM(sd) < min_gm_level) return 0; + wisp_name = va_arg(va, char*); + message = va_arg(va, char*); + len = va_arg(va, int); + clif_wis_message(sd->fd, wisp_name, message, len); + return 1; +} + +// Received wisp message from map-server via char-server for ALL gm +int mapif_parse_WisToGM(int fd) { // 0x3003/0x3803 .w .24B .w .?B + int min_gm_level, mes_len; + char Wisp_name[NAME_LENGTH]; + char mbuf[255]; + char *message; + RFIFOHEAD(fd); + + mes_len = RFIFOW(fd,2) - 30; + message = (char *) (mes_len >= 255 ? (char *) aMallocA(mes_len) : mbuf); + + min_gm_level = (int)RFIFOW(fd,28); + memcpy(Wisp_name, RFIFOP(fd,4), NAME_LENGTH); + Wisp_name[NAME_LENGTH-1] = '\0'; + memcpy(message, RFIFOP(fd,30), mes_len); + message[mes_len-1] = '\0'; + // information is sended to all online GM + clif_foreachclient(mapif_parse_WisToGM_sub, min_gm_level, Wisp_name, message, mes_len); + + if (message != mbuf) + aFree(message); + return 0; +} + +// アカウント変数通知 +int intif_parse_Registers(int fd) { + int j,p,len,max, flag; + struct map_session_data *sd; + struct global_reg *reg; + int *qty; + RFIFOHEAD(fd); + + if( (sd=map_id2sd(RFIFOL(fd,4)))==NULL || + !session_isValid(sd->fd)) // Invalid session + return 1; + + if (RFIFOB(fd,12) == 3 && sd->status.char_id != RFIFOL(fd,8)) + return 1; //Character registry from another character. + + flag = (sd->save_reg.global_num == -1 || sd->save_reg.account_num == -1 || sd->save_reg.account2_num == -1); + + switch (RFIFOB(fd,12)) { + case 3: //Character Registry + reg = sd->save_reg.global; + qty = &sd->save_reg.global_num; + max = GLOBAL_REG_NUM; + break; + case 2: //Account Registry + reg = sd->save_reg.account; + qty = &sd->save_reg.account_num; + max = ACCOUNT_REG_NUM; + break; + case 1: //Account2 Registry + reg = sd->save_reg.account2; + qty = &sd->save_reg.account2_num; + max = ACCOUNT_REG2_NUM; + break; + default: + if (battle_config.error_log) + ShowError("intif_parse_Registers: Unrecognized type %d\n",RFIFOB(fd,12)); + return 0; + } + for(j=0,p=13;jsave_reg.global_num > -1 && sd->save_reg.account_num > -1 && sd->save_reg.account2_num > -1) + pc_reg_received(sd); //Received all registry values, execute init scripts and what-not. [Skotlex] + return 1; +} + +// 倉庫データ受信 +int intif_parse_LoadStorage(int fd) { + struct storage *stor; + struct map_session_data *sd; + RFIFOHEAD(fd); + + sd=map_id2sd( RFIFOL(fd,4) ); + if(sd==NULL){ + if(battle_config.error_log) + ShowError("intif_parse_LoadStorage: user not found %d\n",RFIFOL(fd,4)); + return 1; + } + + if (sd->state.finalsave || //Player is already scheduled to leave the server. + !session_isValid(sd->fd)) // Invalid session + return 1; + + stor = account2storage( RFIFOL(fd,4)); + + if (stor->storage_status == 1) { // Already open.. lets ignore this update + if (battle_config.error_log) + ShowWarning("intif_parse_LoadStorage: storage received for a client already open (User %d:%d)\n", sd->status.account_id, sd->status.char_id); + return 1; + } + if (stor->dirty) { // Already have storage, and it has been modified and not saved yet! Exploit! [Skotlex] + if (battle_config.error_log) + ShowWarning("intif_parse_LoadStorage: received storage for an already modified non-saved storage! (User %d:%d)\n", sd->status.account_id, sd->status.char_id); + return 1; + } + if (RFIFOW(fd,2)-8 != sizeof(struct storage)) { + if (battle_config.error_log) + ShowError("intif_parse_LoadStorage: data size error %d %d\n", RFIFOW(fd,2)-8, sizeof(struct storage)); + return 1; + } + if(battle_config.save_log) + ShowInfo("intif_openstorage: %d\n",RFIFOL(fd,4) ); + memcpy(stor,RFIFOP(fd,8),sizeof(struct storage)); + stor->dirty=0; + stor->storage_status=1; + sd->state.storage_flag = 1; + clif_storagelist(sd,stor); + clif_updatestorageamount(sd,stor); + + return 0; +} + +// 倉庫データ送信成功 +int intif_parse_SaveStorage(int fd) +{ + RFIFOHEAD(fd); + if(battle_config.save_log) + ShowInfo("intif_savestorage: done %d %d\n",RFIFOL(fd,2),RFIFOB(fd,6) ); + storage_storage_saved(RFIFOL(fd,2)); + return 0; +} + +int intif_parse_LoadGuildStorage(int fd) +{ + struct guild_storage *gstor; + struct map_session_data *sd; + int guild_id; + RFIFOHEAD(fd); + guild_id = RFIFOL(fd,8); + if(guild_id <= 0) + return 1; + sd=map_id2sd( RFIFOL(fd,4) ); + if(sd==NULL){ + if(battle_config.error_log) + ShowError("intif_parse_LoadGuildStorage: user not found %d\n",RFIFOL(fd,4)); + return 1; + } else if(!session_isValid(sd->fd)) + return 1; // Invalid session + + gstor=guild2storage(guild_id); + if(!gstor) { + if(battle_config.error_log) + ShowWarning("intif_parse_LoadGuildStorage: error guild_id %d not exist\n",guild_id); + return 1; + } + if (gstor->storage_status == 1) { // Already open.. lets ignore this update + if (battle_config.error_log) + ShowWarning("intif_parse_LoadGuildStorage: storage received for a client already open (User %d:%d)\n", sd->status.account_id, sd->status.char_id); + return 1; + } + if (gstor->dirty) { // Already have storage, and it has been modified and not saved yet! Exploit! [Skotlex] + if (battle_config.error_log) + ShowWarning("intif_parse_LoadGuildStorage: received storage for an already modified non-saved storage! (User %d:%d)\n", sd->status.account_id, sd->status.char_id); + return 1; + } + if( RFIFOW(fd,2)-12 != sizeof(struct guild_storage) ){ + gstor->storage_status = 0; + if(battle_config.error_log) + ShowError("intif_parse_LoadGuildStorage: data size error %d %d\n",RFIFOW(fd,2)-12 , sizeof(struct guild_storage)); + return 1; + } + if(battle_config.save_log) + ShowInfo("intif_open_guild_storage: %d\n",RFIFOL(fd,4) ); + memcpy(gstor,RFIFOP(fd,12),sizeof(struct guild_storage)); + gstor->storage_status = 1; + sd->state.storage_flag = 2; + clif_guildstoragelist(sd,gstor); + clif_updateguildstorageamount(sd,gstor); + return 0; +} +int intif_parse_SaveGuildStorage(int fd) +{ + RFIFOHEAD(fd); + if(battle_config.save_log) { + ShowInfo("intif_save_guild_storage: done %d %d %d\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOB(fd,10) ); + } + storage_guild_storagesaved(/*RFIFOL(fd,2), */RFIFOL(fd,6)); + return 0; +} + +// パーティ作成可否 +int intif_parse_PartyCreated(int fd) +{ + RFIFOHEAD(fd); + if(battle_config.etc_log) + ShowInfo("intif: party created by account %d\n\n", RFIFOL(fd,2)); + party_created(RFIFOL(fd,2), RFIFOL(fd,6),RFIFOB(fd,10),RFIFOL(fd,11), (char *)RFIFOP(fd,15)); + return 0; +} +// パーティ情報 +int intif_parse_PartyInfo(int fd) +{ + RFIFOHEAD(fd); + if( RFIFOW(fd,2)==8){ + if(battle_config.error_log) + ShowWarning("intif: party noinfo %d\n",RFIFOL(fd,4)); + party_recv_noinfo(RFIFOL(fd,4)); + return 0; + } + +// printf("intif: party info %d\n",RFIFOL(fd,4)); + if( RFIFOW(fd,2)!=sizeof(struct party)+4 ){ + if(battle_config.error_log) + ShowError("intif: party info : data size error %d %d %d\n",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct party)+4); + } + party_recv_info((struct party *)RFIFOP(fd,4)); + return 0; +} +// パーティ追加通知 +int intif_parse_PartyMemberAdded(int fd) +{ + RFIFOHEAD(fd); + if(battle_config.etc_log) + ShowInfo("intif: party member added Party (%d), Account(%d), Char(%d)\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); + party_member_added(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10), RFIFOB(fd, 14)); + return 0; +} +// パーティ設定変更通知 +int intif_parse_PartyOptionChanged(int fd) +{ + RFIFOHEAD(fd); + party_optionchanged(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOW(fd,10),RFIFOW(fd,12),RFIFOB(fd,14)); + return 0; +} +// パーティ脱退通知 +int intif_parse_PartyMemberLeaved(int fd) +{ + RFIFOHEAD(fd); + if(battle_config.etc_log) + ShowInfo("intif: party member leaved: Party(%d), Account(%d), Char(%d)\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); + party_member_leaved(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); + return 0; +} +// パーティ解散通知 +int intif_parse_PartyBroken(int fd) +{ + RFIFOHEAD(fd); + party_broken(RFIFOL(fd,2)); + return 0; +} +// パーティ移動通知 +int intif_parse_PartyMove(int fd) +{ + RFIFOHEAD(fd); + party_recv_movemap(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOW(fd,14),RFIFOB(fd,16),RFIFOW(fd,17)); + return 0; +} +// パーティメッセージ +int intif_parse_PartyMessage(int fd) +{ + RFIFOHEAD(fd); +// if(battle_config.etc_log) +// printf("intif_parse_PartyMessage: %s\n",RFIFOP(fd,12)); + party_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),(char *) RFIFOP(fd,12),RFIFOW(fd,2)-12); + return 0; +} + +// ギルド作成可否 +int intif_parse_GuildCreated(int fd) +{ + RFIFOHEAD(fd); + guild_created(RFIFOL(fd,2),RFIFOL(fd,6)); + return 0; +} +// ギルド情報 +int intif_parse_GuildInfo(int fd) +{ + RFIFOHEAD(fd); + if( RFIFOW(fd,2)==8){ + if(battle_config.error_log) + ShowWarning("intif: guild noinfo %d\n",RFIFOL(fd,4)); + guild_recv_noinfo(RFIFOL(fd,4)); + return 0; + } + +// if(battle_config.etc_log) +// printf("intif: guild info %d\n",RFIFOL(fd,4)); + if( RFIFOW(fd,2)!=sizeof(struct guild)+4 ){ + if(battle_config.error_log) + ShowError("intif: guild info : data size error Gid: %d recv size: %d Expected size: %d\n",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct guild)+4); + } + guild_recv_info((struct guild *)RFIFOP(fd,4)); + return 0; +} +// ギルドメンバ追加通知 +int intif_parse_GuildMemberAdded(int fd) +{ + RFIFOHEAD(fd); + if(battle_config.etc_log) + ShowInfo("intif: guild member added %d %d %d %d\n",RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14)); + guild_member_added(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14)); + return 0; +} +// ギルドメンバ脱退/追放通知 +int intif_parse_GuildMemberLeaved(int fd) +{ + RFIFOHEAD(fd); + guild_member_leaved(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14), + (char *) RFIFOP(fd,55), (char *) RFIFOP(fd,15)); + return 0; +} + +// ギルドメンバオンライン状態/Lv変更通知 +int intif_parse_GuildMemberInfoShort(int fd) +{ + RFIFOHEAD(fd); + guild_recv_memberinfoshort(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOB(fd,14),RFIFOW(fd,15),RFIFOW(fd,17)); + return 0; +} +// ギルド解散通知 +int intif_parse_GuildBroken(int fd) +{ + RFIFOHEAD(fd); + guild_broken(RFIFOL(fd,2),RFIFOB(fd,6)); + return 0; +} + +// ギルド基本情報変更通知 +int intif_parse_GuildBasicInfoChanged(int fd) +{ + int type, guild_id; + unsigned int dd; + void *data; + struct guild *g; + short dw; + RFIFOHEAD(fd); + type=RFIFOW(fd,8); + guild_id=RFIFOL(fd,4); + data=RFIFOP(fd,10); + g=guild_search(guild_id); + dw=*((short *)data); + dd=*((unsigned int *)data); + if( g==NULL ) + return 0; + switch(type){ + case GBI_EXP: g->exp=dd; break; + case GBI_GUILDLV: g->guild_lv=dw; break; + case GBI_SKILLPOINT: g->skill_point=dd; break; + } + return 0; +} +// ギルドメンバ情報変更通知 +int intif_parse_GuildMemberInfoChanged(int fd) +{ + int type, guild_id, account_id, char_id, idx, dd; + void* data; + struct guild *g; + RFIFOHEAD(fd); + type=RFIFOW(fd,16); + guild_id=RFIFOL(fd,4); + account_id=RFIFOL(fd,8); + char_id=RFIFOL(fd,12); + data=RFIFOP(fd,18); + g=guild_search(guild_id); + dd=*((int *)data); + if( g==NULL ) + return 0; + idx=guild_getindex(g,account_id,char_id); + switch(type){ + case GMI_POSITION: + g->member[idx].position=dd; + guild_memberposition_changed(g,idx,dd); + break; + case GMI_EXP: + g->member[idx].exp=dd; + break; + case GMI_HAIR: + g->member[idx].hair=dd; + break; + case GMI_HAIR_COLOR: + g->member[idx].hair_color=dd; + break; + case GMI_GENDER: + g->member[idx].gender=dd; + break; + case GMI_CLASS: + g->member[idx].class_=dd; + break; + case GMI_LEVEL: + g->member[idx].lv=dd; + break; + } + return 0; +} + +// ギルド役職変更通知 +int intif_parse_GuildPosition(int fd) +{ + RFIFOHEAD(fd); + if( RFIFOW(fd,2)!=sizeof(struct guild_position)+12 ){ + if(battle_config.error_log) + ShowError("intif: guild info : data size error\n %d %d %d",RFIFOL(fd,4),RFIFOW(fd,2),sizeof(struct guild_position)+12); + } + guild_position_changed(RFIFOL(fd,4),RFIFOL(fd,8),(struct guild_position *)RFIFOP(fd,12)); + return 0; +} +// ギルドスキル割り振り通知 +int intif_parse_GuildSkillUp(int fd) +{ + RFIFOHEAD(fd); + guild_skillupack(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10)); + return 0; +} +// ギルド同盟/敵対通知 +int intif_parse_GuildAlliance(int fd) +{ + RFIFOHEAD(fd); + guild_allianceack(RFIFOL(fd,2),RFIFOL(fd,6),RFIFOL(fd,10),RFIFOL(fd,14), + RFIFOB(fd,18),(char *) RFIFOP(fd,19),(char *) RFIFOP(fd,43)); + return 0; +} +// ギルド告知変更通知 +int intif_parse_GuildNotice(int fd) +{ + RFIFOHEAD(fd); + guild_notice_changed(RFIFOL(fd,2),(char *) RFIFOP(fd,6),(char *) RFIFOP(fd,66)); + return 0; +} +// ギルドエンブレム変更通知 +int intif_parse_GuildEmblem(int fd) +{ + RFIFOHEAD(fd); + guild_emblem_changed(RFIFOW(fd,2)-12,RFIFOL(fd,4),RFIFOL(fd,8), (char *)RFIFOP(fd,12)); + return 0; +} +// ギルド会話受信 +int intif_parse_GuildMessage(int fd) +{ + RFIFOHEAD(fd); + guild_recv_message(RFIFOL(fd,4),RFIFOL(fd,8),(char *) RFIFOP(fd,12),RFIFOW(fd,2)-12); + return 0; +} +// ギルド城データ要求返信 +int intif_parse_GuildCastleDataLoad(int fd) +{ + RFIFOHEAD(fd); + return guild_castledataloadack(RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); +} +// ギルド城データ変更通知 +int intif_parse_GuildCastleDataSave(int fd) +{ + RFIFOHEAD(fd); + return guild_castledatasaveack(RFIFOW(fd,2),RFIFOB(fd,4),RFIFOL(fd,5)); +} + +// ギルド城データ一括受信(初期化時) +int intif_parse_GuildCastleAllDataLoad(int fd) +{ + RFIFOHEAD(fd); + return guild_castlealldataload(RFIFOW(fd,2),(struct guild_castle *)RFIFOP(fd,4)); +} + +int intif_parse_GuildMasterChanged(int fd) +{ + RFIFOHEAD(fd); + return guild_gm_changed(RFIFOL(fd,2),RFIFOL(fd,6)); +} + +// pet +int intif_parse_CreatePet(int fd) +{ + RFIFOHEAD(fd); + pet_get_egg(RFIFOL(fd,2),RFIFOL(fd,7),RFIFOB(fd,6)); + + return 0; +} + +int intif_parse_RecvPetData(int fd) +{ + struct s_pet p; + int len; + RFIFOHEAD(fd); + len=RFIFOW(fd,2); + if(sizeof(struct s_pet)!=len-9) { + if(battle_config.etc_log) + ShowError("intif: pet data: data size error %d %d\n",sizeof(struct s_pet),len-9); + } + else{ + memcpy(&p,RFIFOP(fd,9),sizeof(struct s_pet)); + pet_recv_petdata(RFIFOL(fd,4),&p,RFIFOB(fd,8)); + } + + return 0; +} +int intif_parse_SavePetOk(int fd) +{ + RFIFOHEAD(fd); + if(RFIFOB(fd,6) == 1) { + if(battle_config.error_log) + ShowError("pet data save failure\n"); + } + + return 0; +} + +int intif_parse_DeletePetOk(int fd) +{ + RFIFOHEAD(fd); + if(RFIFOB(fd,2) == 1) { + if(battle_config.error_log) + ShowError("pet data delete failure\n"); + } + + return 0; +} + +int intif_parse_RenamePetOk(int fd) +{ + struct map_session_data *sd = NULL; + RFIFOHEAD(fd); + if((sd=map_id2sd(RFIFOL(fd,2)))==NULL || + sd->status.char_id != RFIFOL(fd,6) || + !session_isValid(sd->fd)) // Invalid session + return 0; + if (RFIFOB(fd,10) == 0) { + clif_displaymessage(sd->fd, msg_txt(280)); // You cannot use this name for your pet. + clif_send_petstatus(sd); //Send status so client knows oet name change got rejected. + return 0; + } + pet_change_name(sd, RFIFOP(fd,11),1); + return 0; +} + +//---------------------------------------------------------------- +// Homunculus recv packets [albator] + +int intif_parse_CreateHomunculus(int fd) +{ + int len; + RFIFOHEAD(fd); + len=RFIFOW(fd,2)-9; + if(sizeof(struct s_homunculus)!=len) { + if(battle_config.etc_log) + ShowError("intif: create homun data: data size error %d != %d\n",sizeof(struct s_homunculus),len); + return 0; + } + merc_hom_recv_data(RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,9), RFIFOB(fd,8)) ; + return 0; +} + +int intif_parse_RecvHomunculusData(int fd) +{ + int len; + + RFIFOHEAD(fd); + len=RFIFOW(fd,2)-9; + + if(sizeof(struct s_homunculus)!=len) { + if(battle_config.etc_log) + ShowError("intif: homun data: data size error %d %d\n",sizeof(struct s_homunculus),len); + return 0; + } + merc_hom_recv_data(RFIFOL(fd,4), (struct s_homunculus*)RFIFOP(fd,9), RFIFOB(fd,8)); + return 0; +} + +int intif_parse_SaveHomunculusOk(int fd) +{ + RFIFOHEAD(fd); + if(RFIFOB(fd,6) != 1) { + if(battle_config.error_log) + ShowError("homunculus data save failure for account %d\n", RFIFOL(fd,2)); + } + return 0; +} + +int intif_parse_DeleteHomunculusOk(int fd) +{ + RFIFOHEAD(fd); + if(RFIFOB(fd,2) != 1) { + if(battle_config.error_log) + ShowError("Homunculus data delete failure\n"); + } + + return 0; +} +//----------------------------------------------------------------- +// inter serverからの通信 +// エラーがあれば0(false)を返すこと +// パケットが処理できれば1,パケット長が足りなければ2を返すこと +int intif_parse(int fd) +{ + int packet_len, cmd; + RFIFOHEAD(fd); + cmd = RFIFOW(fd,0); + // パケットのID確認 + if(cmd<0x3800 || cmd>=0x3800+(sizeof(packet_len_table)/sizeof(packet_len_table[0])) || + packet_len_table[cmd-0x3800]==0){ + return 0; + } + // パケットの長さ確認 + packet_len = packet_len_table[cmd-0x3800]; + if(packet_len==-1){ + if(RFIFOREST(fd)<4) + return 2; + packet_len = RFIFOW(fd,2); + } +// if(battle_config.etc_log) +// printf("intif_parse %d %x %d %d\n",fd,cmd,packet_len,RFIFOREST(fd)); + if((int)RFIFOREST(fd)