socket.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. #ifdef WIN32
  6. #include <windows.h>
  7. typedef long in_addr_t;
  8. #else
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #endif
  13. #include "../common/cbasetypes.h"
  14. #include <time.h>
  15. // define declaration
  16. #ifdef TURBO
  17. #define RFIFOVAR(fd) rbPtr ## fd
  18. #define RFIFOHEAD(fd) uint8 *RFIFOVAR(fd) = session[fd]->rdata+session[fd]->rdata_pos
  19. #define RFIFOP(fd,pos) ( &RFIFOVAR(fd) + (pos) )
  20. #else
  21. #define RFIFOHEAD(fd)
  22. #define RFIFOP(fd,pos) (session[fd]->rdata + session[fd]->rdata_pos + (pos))
  23. #endif
  24. #define RFIFOB(fd,pos) (*(uint8*)RFIFOP(fd,pos))
  25. #define RFIFOW(fd,pos) (*(uint16*)RFIFOP(fd,pos))
  26. #define RFIFOL(fd,pos) (*(uint32*)RFIFOP(fd,pos))
  27. #define RFIFOSPACE(fd) (session[fd]->max_rdata - session[fd]->rdata_size)
  28. #define RFIFOREST(fd) (session[fd]->rdata_size - session[fd]->rdata_pos)
  29. //#define RFIFOSKIP(fd,len) ((session[fd]->rdata_size - session[fd]->rdata_pos - (len) < 0) ? (fprintf(stderr,"too many skip\n"),exit(1)) : (session[fd]->rdata_pos += (len)))
  30. #define RFIFOFLUSH(fd) \
  31. do { \
  32. if(session[fd]->rdata_size == session[fd]->rdata_pos){ \
  33. session[fd]->rdata_size = session[fd]->rdata_pos = 0; \
  34. } else { \
  35. session[fd]->rdata_size -= session[fd]->rdata_pos; \
  36. memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \
  37. session[fd]->rdata_pos = 0; \
  38. } \
  39. } while(0)
  40. #define RBUFP(p,pos) (((uint8*)(p)) + (pos))
  41. #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos)))
  42. #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos)))
  43. #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos)))
  44. #ifdef TURBO
  45. #define WFIFOVAR(fd) wbPtr ## fd
  46. #define WFIFOHEAD(fd, x) uint8 *WFIFOVAR(fd) = ( (fd) > 0 && session[fd] ? session[fd]->wdata+session[fd]->wdata_size : NULL )
  47. #define WFIFOP(fd,pos) ( &WFIFOVAR(fd) + (pos) )
  48. #else
  49. #define WFIFOHEAD(fd, size) do{ if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); }while(0)
  50. #define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos))
  51. #endif
  52. #define WFIFOB(fd,pos) (*(uint8*)WFIFOP(fd,pos))
  53. #define WFIFOW(fd,pos) (*(uint16*)WFIFOP(fd,pos))
  54. #define WFIFOL(fd,pos) (*(uint32*)WFIFOP(fd,pos))
  55. #define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size)
  56. //#define WFIFOSET(fd,len) (session[fd]->wdata_size = (session[fd]->wdata_size + (len) + 2048 < session[fd]->max_wdata) ? session[fd]->wdata_size + len : session[fd]->wdata_size)
  57. #define WBUFP(p,pos) (((uint8*)(p)) + (pos))
  58. #define WBUFB(p,pos) (*(uint8*)((p) + (pos)))
  59. #define WBUFW(p,pos) (*(uint16*)((p) + (pos)))
  60. #define WBUFL(p,pos) (*(uint32*)((p) + (pos)))
  61. #define TOB(n) ((uint8)((n)&UINT8_MAX))
  62. #define TOW(n) ((uint16)((n)&UINT16_MAX))
  63. #define TOL(n) ((uint32)((n)&UINT32_MAX))
  64. // Struct declaration
  65. typedef int (*RecvFunc)(int fd);
  66. typedef int (*SendFunc)(int fd);
  67. typedef int (*ParseFunc)(int fd);
  68. struct socket_data {
  69. unsigned char eof;
  70. unsigned char *rdata, *wdata;
  71. size_t max_rdata, max_wdata;
  72. size_t rdata_size, wdata_size;
  73. size_t rdata_pos;
  74. time_t rdata_tick; // time of last receive (for detecting timeouts)
  75. struct sockaddr_in client_addr; // remote client address (zero for s2s connections)
  76. void* session_data;
  77. RecvFunc func_recv;
  78. SendFunc func_send;
  79. ParseFunc func_parse;
  80. };
  81. // Data prototype declaration
  82. extern struct socket_data *session[FD_SETSIZE];
  83. extern int fd_max;
  84. extern time_t last_tick;
  85. extern time_t stall_time;
  86. //////////////////////////////////
  87. // some checking on sockets
  88. extern int session_isValid(int fd);
  89. extern int session_isActive(int fd);
  90. //////////////////////////////////
  91. // Function prototype declaration
  92. int make_listen_port(int);
  93. int make_listen_bind(long,int);
  94. int make_connection(long,int);
  95. int delete_session(int fd);
  96. int realloc_fifo(int fd,unsigned int rfifo_size,unsigned int wfifo_size);
  97. int realloc_writefifo(int fd, size_t addition);
  98. int WFIFOSET(int fd,int len);
  99. int RFIFOSKIP(int fd,int len);
  100. int do_sendrecv(int next);
  101. int do_parsepacket(void);
  102. void do_close(int fd);
  103. void socket_init(void);
  104. void socket_final(void);
  105. extern void flush_fifo(int fd);
  106. extern void flush_fifos(void);
  107. extern void set_nonblocking(int fd, int yes);
  108. void set_defaultparse(int (*defaultparse)(int));
  109. //Resolves the hostname and stores the string representation of the string in ip.
  110. //Meant to simplify calls to gethostbyname without the need of all the
  111. //required network includes.
  112. //hostname is the name to resolve.
  113. //ip is an array of char[4] where the individual parts of the ip are stored (optional)
  114. //ip_str is a char[16] where the whole ip is stored in string notation (optional)
  115. in_addr_t resolve_hostbyname(char* hostname, unsigned char* ip, char* ip_str);
  116. int socket_getips(uint32* ips, int max);
  117. extern uint32 addr_[16]; // ip addresses of local host (host byte order)
  118. extern int naddr_; // # of ip addresses
  119. #endif // _SOCKET_H_