socket.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <stdio.h>
  6. #ifdef __WIN32
  7. #define __USE_W32_SOCKETS
  8. #include <windows.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. #include "../common/malloc.h"
  17. #include "cbasetypes.h"
  18. extern time_t last_tick;
  19. extern time_t stall_time;
  20. // define declaration
  21. #define RFIFOSPACE(fd) (session[fd]->max_rdata-session[fd]->rdata_size)
  22. #ifdef TURBO
  23. #define RFIFOHEAD(fd) char *rbPtr ## fd = session[fd]->rdata+session[fd]->rdata_pos
  24. #define RFIFOP(fd,pos) (&rbPtr ## fd[pos])
  25. #else
  26. //Make it a comment so it does not disrupts the rest of code.
  27. #define RFIFOHEAD(fd) //
  28. #define RFIFOP(fd,pos) (session[fd]->rdata+session[fd]->rdata_pos+(pos))
  29. #endif
  30. // use function instead of macro.
  31. #define RFIFOB(fd,pos) (*(uint8*)RFIFOP(fd,pos))
  32. #define RFIFOW(fd,pos) (*(uint16*)RFIFOP(fd,pos))
  33. #define RFIFOL(fd,pos) (*(uint32*)RFIFOP(fd,pos))
  34. #define RFIFOREST(fd) (session[fd]->rdata_size-session[fd]->rdata_pos)
  35. #define RFIFOFLUSH(fd) \
  36. do { \
  37. if(session[fd]->rdata_size == session[fd]->rdata_pos){ \
  38. session[fd]->rdata_size = session[fd]->rdata_pos = 0; \
  39. } else { \
  40. session[fd]->rdata_size -= session[fd]->rdata_pos; \
  41. memmove(session[fd]->rdata, session[fd]->rdata+session[fd]->rdata_pos, session[fd]->rdata_size); \
  42. session[fd]->rdata_pos=0; \
  43. } \
  44. } while(0)
  45. //#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)))
  46. #define RBUFP(p,pos) (((uint8*)(p))+(pos))
  47. #define RBUFB(p,pos) (*(uint8*)RBUFP((p),(pos)))
  48. #define RBUFW(p,pos) (*(uint16*)RBUFP((p),(pos)))
  49. #define RBUFL(p,pos) (*(uint32*)RBUFP((p),(pos)))
  50. #define WFIFOSPACE(fd) (session[fd]->max_wdata-session[fd]->wdata_size)
  51. #ifdef TURBO
  52. #define WFIFOHEAD(fd, x) uint8 *wbPtr ## fd = (fd>0&&session[fd])?(session[fd]->wdata+session[fd]->wdata_size):NULL;
  53. #define WFIFOP(fd,pos) (&wbPtr ## fd[pos])
  54. #else
  55. #define WFIFOHEAD(fd, size) do{ if((fd) && session[fd]->wdata_size + (size) > session[fd]->max_wdata ) realloc_writefifo(fd, size); }while(0)
  56. #define WFIFOP(fd,pos) (session[fd]->wdata+session[fd]->wdata_size+(pos))
  57. #endif
  58. #define WFIFOB(fd,pos) (*(uint8*)WFIFOP(fd,pos))
  59. #define WFIFOW(fd,pos) (*(uint16*)WFIFOP(fd,pos))
  60. #define WFIFOL(fd,pos) (*(uint32*)WFIFOP(fd,pos))
  61. // use function instead of macro.
  62. //#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)
  63. #define WBUFP(p,pos) (((uint8*)(p)) + (pos))
  64. #define WBUFB(p,pos) (*(uint8*)((p) + (pos)))
  65. #define WBUFW(p,pos) (*(uint16*)((p) + (pos)))
  66. #define WBUFL(p,pos) (*(uint32*)((p) + (pos)))
  67. #define TOB(n) ((uint8)((n)&UINT8_MAX))
  68. #define TOW(n) ((uint16)((n)&UINT16_MAX))
  69. #define TOL(n) ((uint32)((n)&UINT32_MAX))
  70. //FD_SETSIZE must be modified on the project files/Makefile, since a change here won't affect
  71. // dependant windows libraries.
  72. /*
  73. #ifdef __WIN32
  74. //The default FD_SETSIZE is kinda small for windows systems.
  75. #ifdef FD_SETSIZE
  76. #undef FD_SETSIZE
  77. #endif
  78. #define FD_SETSIZE 4096
  79. #endif
  80. */
  81. #ifdef __INTERIX
  82. #define FD_SETSIZE 4096
  83. #endif // __INTERIX
  84. /* Removed Cygwin FD_SETSIZE declarations, now are directly passed on to the compiler through Makefile [Valaris] */
  85. // Session type
  86. enum SessionType {
  87. SESSION_UNKNOWN = -1,
  88. SESSION_RAW = 0,
  89. SESSION_HTTP = 1,
  90. //-----------------
  91. SESSION_MAX = 2
  92. };
  93. // Struct declaration
  94. struct socket_data{
  95. unsigned char eof;
  96. unsigned char *rdata, *wdata;
  97. size_t max_rdata, max_wdata;
  98. size_t rdata_size, wdata_size;
  99. size_t rdata_pos;
  100. time_t rdata_tick;
  101. struct sockaddr_in client_addr;
  102. int (*func_recv)(int);
  103. int (*func_send)(int);
  104. int (*func_parse)(int);
  105. int (*func_console)(char*);
  106. void* session_data;
  107. void* session_data2;
  108. enum SessionType type;
  109. };
  110. // Parse functions table
  111. struct func_parse_table {
  112. int (*func)(int);
  113. int (*check)(struct socket_data *);
  114. };
  115. extern struct func_parse_table func_parse_table[SESSION_MAX];
  116. // Data prototype declaration
  117. extern struct socket_data *session[FD_SETSIZE];
  118. extern int fd_max;
  119. //////////////////////////////////
  120. // some checking on sockets
  121. extern bool session_isValid(int fd);
  122. extern bool session_isActive(int fd);
  123. //////////////////////////////////
  124. // Function prototype declaration
  125. int make_listen_port(int);
  126. int make_listen_bind(long,int);
  127. int make_connection(long,int);
  128. int delete_session(int);
  129. int realloc_fifo(int fd,unsigned int rfifo_size,unsigned int wfifo_size);
  130. int realloc_writefifo(int fd, size_t addition);
  131. int WFIFOSET(int fd,int len);
  132. int RFIFOSKIP(int fd,int len);
  133. int do_sendrecv(int next);
  134. int do_parsepacket(void);
  135. void do_close(int fd);
  136. void socket_init(void);
  137. void socket_final(void);
  138. extern void flush_fifo(int fd);
  139. extern void flush_fifos(void);
  140. extern void set_nonblocking(int fd, int yes);
  141. int start_console(void);
  142. void set_defaultparse(int (*defaultparse)(int));
  143. void set_defaultconsoleparse(int (*defaultparse)(char*));
  144. //Resolves the hostname and stores the string representation of the string in ip.
  145. //Meant to simplify calls to gethostbyname without the need of all the
  146. //required network includes.
  147. //hostname is the name to resolve.
  148. //ip is an array of char[4] where the individual parts of the ip are stored (optional)
  149. //ip_str is a char[16] where the whole ip is stored in string notation (optional)
  150. in_addr_t resolve_hostbyname(char* hostname, unsigned char *ip, char *ip_str);
  151. extern unsigned int addr_[16]; // ip addresses of local host (host byte order)
  152. extern unsigned int naddr_; // # of ip addresses
  153. #endif // _SOCKET_H_