socket.h 5.0 KB

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