socket.hpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright (c) rAthena Dev Teams - Licensed under GNU GPL
  2. // For more information, see LICENCE in the main folder
  3. #ifndef SOCKET_HPP
  4. #define SOCKET_HPP
  5. #include "../config/core.hpp"
  6. #ifdef WIN32
  7. #include "winapi.hpp"
  8. typedef long in_addr_t;
  9. #else
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #endif
  14. #include <time.h>
  15. #include "cbasetypes.hpp"
  16. #include "timer.hpp" // t_tick
  17. #ifndef MAXCONN
  18. #define MAXCONN FD_SETSIZE
  19. #endif
  20. #define FIFOSIZE_SERVERLINK 256*1024
  21. // socket I/O macros
  22. #define RFIFOHEAD(fd)
  23. #define WFIFOHEAD(fd, size) do{ if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); }while(0)
  24. #define RFIFOP(fd,pos) (session[fd]->rdata + session[fd]->rdata_pos + (pos))
  25. #define WFIFOP(fd,pos) (session[fd]->wdata + session[fd]->wdata_size + (pos))
  26. #define RFIFOCP(fd,pos) ((char*)RFIFOP(fd,pos))
  27. #define WFIFOCP(fd,pos) ((char*)WFIFOP(fd,pos))
  28. #define RFIFOB(fd,pos) (*(uint8*)RFIFOP(fd,pos))
  29. #define WFIFOB(fd,pos) (*(uint8*)WFIFOP(fd,pos))
  30. #define RFIFOW(fd,pos) (*(uint16*)RFIFOP(fd,pos))
  31. #define WFIFOW(fd,pos) (*(uint16*)WFIFOP(fd,pos))
  32. #define RFIFOL(fd,pos) (*(uint32*)RFIFOP(fd,pos))
  33. #define WFIFOL(fd,pos) (*(uint32*)WFIFOP(fd,pos))
  34. #define RFIFOF(fd,pos) (*(float*)RFIFOP(fd,pos))
  35. #define WFIFOF(fd,pos) (*(float*)WFIFOP(fd,pos))
  36. #define RFIFOQ(fd,pos) (*(uint64*)RFIFOP(fd,pos))
  37. #define WFIFOQ(fd,pos) (*(uint64*)WFIFOP(fd,pos))
  38. #define RFIFOSPACE(fd) (session[fd]->max_rdata - session[fd]->rdata_size)
  39. #define WFIFOSPACE(fd) (session[fd]->max_wdata - session[fd]->wdata_size)
  40. #define RFIFOREST(fd) (session[fd]->flag.eof ? 0 : session[fd]->rdata_size - session[fd]->rdata_pos)
  41. #define RFIFOFLUSH(fd) \
  42. do { \
  43. if(session[fd]->rdata_size == session[fd]->rdata_pos){ \
  44. session[fd]->rdata_size = session[fd]->rdata_pos = 0; \
  45. } else { \
  46. session[fd]->rdata_size -= session[fd]->rdata_pos; \
  47. memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \
  48. session[fd]->rdata_pos = 0; \
  49. } \
  50. } while(0)
  51. // buffer I/O macros
  52. #define RBUFP(p,pos) (((uint8*)(p)) + (pos))
  53. #define RBUFCP(p,pos) ((char*)RBUFP((p),(pos)))
  54. #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos)))
  55. #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos)))
  56. #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos)))
  57. #define RBUFQ(p,pos) (*(uint64*)RBUFP((p),(pos)))
  58. #define WBUFP(p,pos) (((uint8*)(p)) + (pos))
  59. #define WBUFCP(p,pos) ((char*)WBUFP((p),(pos)))
  60. #define WBUFB(p,pos) (*(uint8*)WBUFP((p),(pos)))
  61. #define WBUFW(p,pos) (*(uint16*)WBUFP((p),(pos)))
  62. #define WBUFL(p,pos) (*(uint32*)WBUFP((p),(pos)))
  63. #define WBUFQ(p,pos) (*(uint64*)WBUFP((p),(pos)))
  64. #define TOB(n) ((uint8)((n)&UINT8_MAX))
  65. #define TOW(n) ((uint16)((n)&UINT16_MAX))
  66. #define TOL(n) ((uint32)((n)&UINT32_MAX))
  67. // Struct declaration
  68. typedef int (*RecvFunc)(int fd);
  69. typedef int (*SendFunc)(int fd);
  70. typedef int (*ParseFunc)(int fd);
  71. struct socket_data
  72. {
  73. struct {
  74. unsigned char eof : 1;
  75. unsigned char server : 1;
  76. unsigned char ping : 2;
  77. } flag;
  78. uint32 client_addr; // remote client address
  79. uint8 *rdata, *wdata;
  80. size_t max_rdata, max_wdata;
  81. size_t rdata_size, wdata_size;
  82. size_t rdata_pos;
  83. time_t rdata_tick; // time of last recv (for detecting timeouts); zero when timeout is disabled
  84. RecvFunc func_recv;
  85. SendFunc func_send;
  86. ParseFunc func_parse;
  87. void* session_data; // stores application-specific data related to the session
  88. };
  89. // Data prototype declaration
  90. extern struct socket_data* session[MAXCONN];
  91. extern int fd_max;
  92. extern time_t last_tick;
  93. extern time_t stall_time;
  94. //////////////////////////////////
  95. // some checking on sockets
  96. extern bool session_isValid(int fd);
  97. extern bool session_isActive(int fd);
  98. //////////////////////////////////
  99. // Function prototype declaration
  100. int make_listen_bind(uint32 ip, uint16 port);
  101. int make_connection(uint32 ip, uint16 port, bool silent, int timeout);
  102. int realloc_fifo(int fd, unsigned int rfifo_size, unsigned int wfifo_size);
  103. int realloc_writefifo(int fd, size_t addition);
  104. int WFIFOSET(int fd, size_t len);
  105. int RFIFOSKIP(int fd, size_t len);
  106. int do_sockets(t_tick next);
  107. void do_close(int fd);
  108. void socket_init(void);
  109. void socket_final(void);
  110. extern void flush_fifo(int fd);
  111. extern void flush_fifos(void);
  112. extern void set_nonblocking(int fd, unsigned long yes);
  113. void set_defaultparse(ParseFunc defaultparse);
  114. /// Server operation request
  115. enum chrif_req_op {
  116. // Char-server <-> login-server oepration
  117. CHRIF_OP_LOGIN_BLOCK = 1,
  118. CHRIF_OP_LOGIN_BAN,
  119. CHRIF_OP_LOGIN_UNBLOCK,
  120. CHRIF_OP_LOGIN_UNBAN,
  121. CHRIF_OP_LOGIN_CHANGESEX,
  122. CHRIF_OP_LOGIN_VIP,
  123. // Char-server operation
  124. CHRIF_OP_BAN,
  125. CHRIF_OP_UNBAN,
  126. CHRIF_OP_CHANGECHARSEX,
  127. };
  128. // hostname/ip conversion functions
  129. uint32 host2ip(const char* hostname);
  130. const char* ip2str(uint32 ip, char ip_str[16]);
  131. uint32 str2ip(const char* ip_str);
  132. #define CONVIP(ip) ((ip)>>24)&0xFF,((ip)>>16)&0xFF,((ip)>>8)&0xFF,((ip)>>0)&0xFF
  133. #define MAKEIP(a,b,c,d) (uint32)( ( ( (a)&0xFF ) << 24 ) | ( ( (b)&0xFF ) << 16 ) | ( ( (c)&0xFF ) << 8 ) | ( ( (d)&0xFF ) << 0 ) )
  134. uint16 ntows(uint16 netshort);
  135. int socket_getips(uint32* ips, int max);
  136. extern uint32 addr_[16]; // ip addresses of local host (host byte order)
  137. extern int naddr_; // # of ip addresses
  138. void set_eof(int fd);
  139. /// Use a shortlist of sockets instead of iterating all sessions for sockets
  140. /// that have data to send or need eof handling.
  141. /// Adapted to use a static array instead of a linked list.
  142. ///
  143. /// @author Buuyo-tama
  144. #define SEND_SHORTLIST
  145. #ifdef SEND_SHORTLIST
  146. // Add a fd to the shortlist so that it'll be recognized as a fd that needs
  147. // sending done on it.
  148. void send_shortlist_add_fd(int fd);
  149. // Do pending network sends (and eof handling) from the shortlist.
  150. void send_shortlist_do_sends();
  151. #endif
  152. #endif /* SOCKET_HPP */