socket.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. #ifndef _CBASETYPES_H_
  6. #include "../common/cbasetypes.h"
  7. #endif
  8. #ifdef WIN32
  9. #include <winsock2.h>
  10. typedef long in_addr_t;
  11. #else
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <netinet/in.h>
  15. #endif
  16. #include <time.h>
  17. // socket I/O macros
  18. #ifdef TURBO
  19. #define RFIFOVAR(fd) rbPtr ## fd
  20. #define WFIFOVAR(fd) wbPtr ## fd
  21. #define RFIFOHEAD(fd) uint8 *RFIFOVAR(fd) = session[fd]->rdata+session[fd]->rdata_pos
  22. #define WFIFOHEAD(fd, x) uint8 *WFIFOVAR(fd) = ( (fd) > 0 && session[fd] ? session[fd]->wdata+session[fd]->wdata_size : NULL )
  23. #define RFIFOP(fd,pos) ( &RFIFOVAR(fd) + (pos) )
  24. #define WFIFOP(fd,pos) ( &WFIFOVAR(fd) + (pos) )
  25. #else
  26. #define RFIFOHEAD(fd)
  27. #define WFIFOHEAD(fd, size) do{ if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); }while(0)
  28. #define RFIFOP(fd,pos) (session[fd]->rdata + session[fd]->rdata_pos + (pos))
  29. #define WFIFOP(fd,pos) (session[fd]->wdata + session[fd]->wdata_size + (pos))
  30. #endif
  31. #define RFIFOB(fd,pos) (*(uint8*)RFIFOP(fd,pos))
  32. #define WFIFOB(fd,pos) (*(uint8*)WFIFOP(fd,pos))
  33. #define RFIFOW(fd,pos) (*(uint16*)RFIFOP(fd,pos))
  34. #define WFIFOW(fd,pos) (*(uint16*)WFIFOP(fd,pos))
  35. #define RFIFOL(fd,pos) (*(uint32*)RFIFOP(fd,pos))
  36. #define WFIFOL(fd,pos) (*(uint32*)WFIFOP(fd,pos))
  37. #define RFIFOSPACE(fd) (session[fd]->max_rdata - session[fd]->rdata_size)
  38. #define WFIFOSPACE(fd) (session[fd]->max_wdata - session[fd]->wdata_size)
  39. #define RFIFOREST(fd) (session[fd]->rdata_size - session[fd]->rdata_pos)
  40. #define RFIFOFLUSH(fd) \
  41. do { \
  42. if(session[fd]->rdata_size == session[fd]->rdata_pos){ \
  43. session[fd]->rdata_size = session[fd]->rdata_pos = 0; \
  44. } else { \
  45. session[fd]->rdata_size -= session[fd]->rdata_pos; \
  46. memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \
  47. session[fd]->rdata_pos = 0; \
  48. } \
  49. } while(0)
  50. // buffer I/O macros
  51. #define RBUFP(p,pos) (((uint8*)(p)) + (pos))
  52. #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos)))
  53. #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos)))
  54. #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos)))
  55. #define WBUFP(p,pos) (((uint8*)(p)) + (pos))
  56. #define WBUFB(p,pos) (*(uint8*)WBUFP((p),(pos)))
  57. #define WBUFW(p,pos) (*(uint16*)WBUFP((p),(pos)))
  58. #define WBUFL(p,pos) (*(uint32*)WBUFP((p),(pos)))
  59. #define TOB(n) ((uint8)((n)&UINT8_MAX))
  60. #define TOW(n) ((uint16)((n)&UINT16_MAX))
  61. #define TOL(n) ((uint32)((n)&UINT32_MAX))
  62. // Struct declaration
  63. typedef int (*RecvFunc)(int fd);
  64. typedef int (*SendFunc)(int fd);
  65. typedef int (*ParseFunc)(int fd);
  66. struct socket_data {
  67. unsigned char eof;
  68. unsigned char *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 receive (for detecting timeouts)
  73. uint32 client_addr; // remote client address (zero for s2s connections)
  74. void* session_data;
  75. RecvFunc func_recv;
  76. SendFunc func_send;
  77. ParseFunc func_parse;
  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 int session_isValid(int fd);
  87. extern int 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);
  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, int len);
  95. int RFIFOSKIP(int fd, int len);
  96. int do_sendrecv(int next);
  97. int do_parsepacket(void);
  98. void do_close(int fd);
  99. void socket_init(void);
  100. void socket_final(void);
  101. extern void flush_fifo(int fd);
  102. extern void flush_fifos(void);
  103. extern void set_nonblocking(int fd, unsigned long yes);
  104. void set_defaultparse(ParseFunc defaultparse);
  105. // hostname/ip conversion functions
  106. uint32 host2ip(const char* hostname);
  107. const char* ip2str(uint32 ip, char ip_str[16]);
  108. uint32 str2ip(const char* ip_str);
  109. #define CONVIP(ip) (ip>>24)&0xFF,(ip>>16)&0xFF,(ip>>8)&0xFF,(ip>>0)&0xFF
  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 processing.
  117. ///
  118. /// @author Buuyo-tama
  119. //#define SEND_SHORTLIST
  120. #ifdef SEND_SHORTLIST
  121. struct send_shortlist_node {
  122. struct send_shortlist_node *next; // Next node in the linked list
  123. struct send_shortlist_node *prev; // Previous node in the linked list
  124. int fd; // FD that needs sending.
  125. };
  126. // Add a fd to the shortlist so that it'll be recognized as a fd that needs
  127. // sending done on it.
  128. void send_shortlist_add_fd(int fd);
  129. // Do pending network sends (and eof handling) from the shortlist.
  130. void send_shortlist_do_sends();
  131. #endif
  132. #endif /* _SOCKET_H_ */