socket.h 5.0 KB

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