PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/common/socket.h

https://gitlab.com/evol/hercules
C Header | 222 lines | 139 code | 34 blank | 49 comment | 6 complexity | d4cc90656a66eef6523489cd504c6bc6 MD5 | raw file
Possible License(s): GPL-3.0, LGPL-2.0
  1. /**
  2. * This file is part of Hercules.
  3. * http://herc.ws - http://github.com/HerculesWS/Hercules
  4. *
  5. * Copyright (C) 2012-2015 Hercules Dev Team
  6. * Copyright (C) Athena Dev Teams
  7. *
  8. * Hercules is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. #ifndef COMMON_SOCKET_H
  22. #define COMMON_SOCKET_H
  23. #include "common/hercules.h"
  24. #include "common/conf.h"
  25. #include "common/db.h"
  26. #ifdef WIN32
  27. # include "common/winapi.h"
  28. typedef long in_addr_t;
  29. #else
  30. # include <netinet/in.h>
  31. # include <sys/socket.h>
  32. # include <sys/types.h>
  33. #endif
  34. struct hplugin_data_store;
  35. #define FIFOSIZE_SERVERLINK 256*1024
  36. // socket I/O macros
  37. #define RFIFOHEAD(fd)
  38. #define WFIFOHEAD(fd, size) \
  39. do{ \
  40. if ((fd) && sockt->session[fd]->wdata_size + (size) > sockt->session[fd]->max_wdata) \
  41. sockt->realloc_writefifo((fd), (size)); \
  42. } while(0)
  43. #define RFIFOP(fd,pos) (sockt->session[fd]->rdata + sockt->session[fd]->rdata_pos + (pos))
  44. #define WFIFOP(fd,pos) (sockt->session[fd]->wdata + sockt->session[fd]->wdata_size + (pos))
  45. #define RFIFOB(fd,pos) (*(uint8*)RFIFOP((fd),(pos)))
  46. #define WFIFOB(fd,pos) (*(uint8*)WFIFOP((fd),(pos)))
  47. #define RFIFOW(fd,pos) (*(uint16*)RFIFOP((fd),(pos)))
  48. #define WFIFOW(fd,pos) (*(uint16*)WFIFOP((fd),(pos)))
  49. #define RFIFOL(fd,pos) (*(uint32*)RFIFOP((fd),(pos)))
  50. #define WFIFOL(fd,pos) (*(uint32*)WFIFOP((fd),(pos)))
  51. #define RFIFOQ(fd,pos) (*(uint64*)RFIFOP((fd),(pos)))
  52. #define WFIFOQ(fd,pos) (*(uint64*)WFIFOP((fd),(pos)))
  53. #define RFIFOSPACE(fd) (sockt->session[fd]->max_rdata - sockt->session[fd]->rdata_size)
  54. #define WFIFOSPACE(fd) (sockt->session[fd]->max_wdata - sockt->session[fd]->wdata_size)
  55. #define RFIFOREST(fd) (sockt->session[fd]->flag.eof ? 0 : sockt->session[fd]->rdata_size - sockt->session[fd]->rdata_pos)
  56. #define RFIFOFLUSH(fd) \
  57. do { \
  58. if(sockt->session[fd]->rdata_size == sockt->session[fd]->rdata_pos){ \
  59. sockt->session[fd]->rdata_size = sockt->session[fd]->rdata_pos = 0; \
  60. } else { \
  61. sockt->session[fd]->rdata_size -= sockt->session[fd]->rdata_pos; \
  62. memmove(sockt->session[fd]->rdata, sockt->session[fd]->rdata+sockt->session[fd]->rdata_pos, sockt->session[fd]->rdata_size); \
  63. sockt->session[fd]->rdata_pos = 0; \
  64. } \
  65. } while(0)
  66. #define WFIFOSET(fd, len) (sockt->wfifoset(fd, len))
  67. #define RFIFOSKIP(fd, len) (sockt->rfifoskip(fd, len))
  68. /* [Ind/Hercules] */
  69. #define RFIFO2PTR(fd) (void*)(sockt->session[fd]->rdata + sockt->session[fd]->rdata_pos)
  70. // buffer I/O macros
  71. #define RBUFP(p,pos) (((uint8*)(p)) + (pos))
  72. #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos)))
  73. #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos)))
  74. #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos)))
  75. #define RBUFQ(p,pos) (*(uint64*)RBUFP((p),(pos)))
  76. #define WBUFP(p,pos) (((uint8*)(p)) + (pos))
  77. #define WBUFB(p,pos) (*(uint8*)WBUFP((p),(pos)))
  78. #define WBUFW(p,pos) (*(uint16*)WBUFP((p),(pos)))
  79. #define WBUFL(p,pos) (*(uint32*)WBUFP((p),(pos)))
  80. #define WBUFQ(p,pos) (*(uint64*)WBUFP((p),(pos)))
  81. #define TOB(n) ((uint8)((n)&UINT8_MAX))
  82. #define TOW(n) ((uint16)((n)&UINT16_MAX))
  83. #define TOL(n) ((uint32)((n)&UINT32_MAX))
  84. // Struct declaration
  85. typedef int (*RecvFunc)(int fd);
  86. typedef int (*SendFunc)(int fd);
  87. typedef int (*ParseFunc)(int fd);
  88. struct socket_data {
  89. struct {
  90. unsigned char eof : 1;
  91. unsigned char server : 1;
  92. unsigned char ping : 2;
  93. } flag;
  94. uint32 client_addr; // remote client address
  95. uint8 *rdata, *wdata;
  96. size_t max_rdata, max_wdata;
  97. size_t rdata_size, wdata_size;
  98. size_t rdata_pos;
  99. time_t rdata_tick; // time of last recv (for detecting timeouts); zero when timeout is disabled
  100. RecvFunc func_recv;
  101. SendFunc func_send;
  102. ParseFunc func_parse;
  103. void* session_data; // stores application-specific data related to the session
  104. struct hplugin_data_store *hdata; ///< HPM Plugin Data Store.
  105. };
  106. struct hSockOpt {
  107. unsigned int silent : 1;
  108. unsigned int setTimeo : 1;
  109. };
  110. /// Subnet/IP range in the IP/Mask format.
  111. struct s_subnet {
  112. uint32 ip;
  113. uint32 mask;
  114. };
  115. /// A vector of subnets/IP ranges.
  116. VECTOR_STRUCT_DECL(s_subnet_vector, struct s_subnet);
  117. /// Use a shortlist of sockets instead of iterating all sessions for sockets
  118. /// that have data to send or need eof handling.
  119. /// Adapted to use a static array instead of a linked list.
  120. ///
  121. /// @author Buuyo-tama
  122. #define SEND_SHORTLIST
  123. // Note: purposely returns four comma-separated arguments
  124. #define CONVIP(ip) ((ip)>>24)&0xFF,((ip)>>16)&0xFF,((ip)>>8)&0xFF,((ip)>>0)&0xFF
  125. #define MAKEIP(a,b,c,d) ((uint32)( ( ( (a)&0xFF ) << 24 ) | ( ( (b)&0xFF ) << 16 ) | ( ( (c)&0xFF ) << 8 ) | ( ( (d)&0xFF ) << 0 ) ))
  126. /// Applies a subnet mask to an IP
  127. #define APPLY_MASK(ip, mask) ((ip)&(mask))
  128. /// Verifies the match between two IPs, with a subnet mask applied
  129. #define SUBNET_MATCH(ip1, ip2, mask) (APPLY_MASK((ip1), (mask)) == APPLY_MASK((ip2), (mask)))
  130. /**
  131. * Socket.c interface, mostly for reading however.
  132. **/
  133. struct socket_interface {
  134. int fd_max;
  135. /* */
  136. time_t stall_time;
  137. time_t last_tick;
  138. /* */
  139. uint32 addr_[16]; // ip addresses of local host (host byte order)
  140. int naddr_; // # of ip addresses
  141. struct socket_data **session;
  142. struct s_subnet_vector lan_subnets; ///< LAN subnets.
  143. struct s_subnet_vector trusted_ips; ///< Trusted IP ranges
  144. struct s_subnet_vector allowed_ips; ///< Allowed server IP ranges
  145. /* */
  146. void (*init) (void);
  147. void (*final) (void);
  148. /* */
  149. int (*perform) (int next);
  150. /* [Ind/Hercules] - socket_datasync */
  151. void (*datasync) (int fd, bool send);
  152. /* */
  153. int (*make_listen_bind) (uint32 ip, uint16 port);
  154. int (*make_connection) (uint32 ip, uint16 port, struct hSockOpt *opt);
  155. int (*realloc_fifo) (int fd, unsigned int rfifo_size, unsigned int wfifo_size);
  156. int (*realloc_writefifo) (int fd, size_t addition);
  157. int (*wfifoset) (int fd, size_t len);
  158. int (*rfifoskip) (int fd, size_t len);
  159. void (*close) (int fd);
  160. /* */
  161. bool (*session_is_valid) (int fd);
  162. bool (*session_is_active) (int fd);
  163. /* */
  164. void (*flush) (int fd);
  165. void (*flush_fifos) (void);
  166. void (*set_nonblocking) (int fd, unsigned long yes);
  167. void (*set_defaultparse) (ParseFunc defaultparse);
  168. /* hostname/ip conversion functions */
  169. uint32 (*host2ip) (const char* hostname);
  170. const char * (*ip2str) (uint32 ip, char *ip_str);
  171. uint32 (*str2ip) (const char* ip_str);
  172. /* */
  173. uint16 (*ntows) (uint16 netshort);
  174. /* */
  175. int (*getips) (uint32* ips, int max);
  176. /* */
  177. void (*eof) (int fd);
  178. uint32 (*lan_subnet_check) (uint32 ip, struct s_subnet *info);
  179. bool (*allowed_ip_check) (uint32 ip);
  180. bool (*trusted_ip_check) (uint32 ip);
  181. int (*net_config_read_sub) (config_setting_t *t, struct s_subnet_vector *list, const char *filename, const char *groupname);
  182. void (*net_config_read) (const char *filename);
  183. };
  184. #ifdef HERCULES_CORE
  185. void socket_defaults(void);
  186. #endif // HERCULES_CORE
  187. HPShared struct socket_interface *sockt;
  188. #endif /* COMMON_SOCKET_H */