net.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #ifndef _NET_H
  2. #define _NET_H
  3. #ifdef HAVE_CONFIG_H
  4. # include "config.h"
  5. #endif
  6. #include "types.h"
  7. #include <netinet/in.h>
  8. #include <sys/socket.h>
  9. #include <sys/un.h>
  10. #include <setjmp.h>
  11. #include <bdlib/src/String.h>
  12. namespace bd {
  13. class Stream;
  14. }
  15. #define ENC_KEY_LEN 32
  16. #define SGRAB 2010 /* How much data to allow through sockets. */
  17. enum {
  18. EGG_OPTION_SET = 1, /* Set option(s). */
  19. EGG_OPTION_UNSET = 2 /* Unset option(s). */
  20. };
  21. /* Socket flags:
  22. */
  23. #define SOCK_UNUSED BIT0 /* empty socket */
  24. #define SOCK_BINARY BIT1 /* do not buffer input */
  25. #define SOCK_LISTEN BIT2 /* listening port */
  26. #define SOCK_CONNECT BIT3 /* connection attempt */
  27. #define SOCK_NONSOCK BIT4 /* used for file i/o on debug */
  28. #define SOCK_STRONGCONN BIT5 /* don't report success until sure */
  29. #define SOCK_EOFD BIT6 /* it EOF'd recently during a write */
  30. #define SOCK_PROXYWAIT BIT7 /* waiting for SOCKS traversal */
  31. #define SOCK_PASS BIT8 /* passed on; only notify in case of traffic */
  32. #define SOCK_VIRTUAL BIT9 /* not-connected socket (dont read it!) */
  33. #define SOCK_BUFFER BIT10 /* buffer data; don't notify dcc funcs */
  34. /* Flags to sock_has_data
  35. */
  36. enum {
  37. SOCK_DATA_OUTGOING, /* Data in out-queue? */
  38. SOCK_DATA_INCOMING /* Data in in-queue? */
  39. };
  40. #define iptolong(a) (0xffffffff & \
  41. (long) (htonl((unsigned long) a)))
  42. #ifdef USE_IPV6
  43. #define SIZEOF_SOCKADDR(so) ((so).sa.sa_family == AF_INET6 ? sizeof(so.sin6) : sizeof(so.sin))
  44. #else
  45. #define SIZEOF_SOCKADDR(so) (sizeof(so.sin))
  46. #endif /* USE_IPV6 */
  47. #if !defined(IN6_IS_ADDR_V4MAPPED)
  48. # define IN6_IS_ADDR_V4MAPPED(a) \
  49. ((((u_int32_t *) (a))[0] == 0) && (((u_int32_t *) (a))[1] == 0) && \
  50. (((u_int32_t *) (a))[2] == htonl (0xffff)))
  51. #endif /* !defined(IN6_IS_ADDR_V4MAPPED) */
  52. union sockaddr_union { /* replaced by sockname_t */
  53. struct sockaddr sa;
  54. struct sockaddr_in sin;
  55. #ifdef USE_IPV6
  56. struct sockaddr_in6 sin6;
  57. #endif /* USE_IPV6 */
  58. struct sockaddr_un sock_un;
  59. };
  60. /* This is used by the net module to keep track of sockets and what's
  61. * queued on them
  62. */
  63. typedef struct {
  64. #ifdef USE_IPV6
  65. unsigned int af;
  66. #endif /* USE_IPV6 */
  67. int sock;
  68. int encstatus; /* encrypted botlink */
  69. int oseed; /* botlink out seed */
  70. int iseed; /* botlink in seed */
  71. int gz; /* gzip compression */
  72. int enclink; /* new encrypted botlink */
  73. bd::String* inbuf;
  74. bd::String* outbuf;
  75. char *host;
  76. port_t port;
  77. short flags;
  78. char okey[ENC_KEY_LEN + 1]; /* botlink enckey: out */
  79. char ikey[ENC_KEY_LEN + 1]; /* botlink enckey: in */
  80. } sock_list;
  81. # define killsock(x) real_killsock((x),__FILE__,__LINE__)
  82. unsigned long my_atoul(const char *);
  83. char *myipstr(int);
  84. in_addr_t getmyip();
  85. void cache_my_ip();
  86. void setsock(int, int);
  87. int allocsock(int, int);
  88. #ifdef USE_IPV6
  89. #define getsock(opt, af) real_getsock(opt, af, __FILE__, __LINE__)
  90. int real_getsock(int, int, const char *, int);
  91. #else
  92. #define getsock(opt) real_getsock(opt, __FILE__, __LINE__)
  93. int real_getsock(int, const char *, int);
  94. #endif /* USE_IPV6 */
  95. int sockprotocol(int);
  96. void real_killsock(int, const char *, int);
  97. int answer(int, char *, in_addr_t *, port_t *, int);
  98. int findanysnum(register int);
  99. int findanyidx(register int sock);
  100. int open_listen(port_t *);
  101. int open_listen_by_af(port_t *, int);
  102. int open_listen_addr_by_af(const char*, port_t *, int);
  103. #ifdef USE_IPV6
  104. int open_address_listen(const char*, int, port_t *);
  105. #else
  106. int open_address_listen(const char*, port_t *);
  107. #endif /* USE_IPV6 */
  108. int open_telnet(const char *, port_t, bool proxy = 0, int identd = 0);
  109. int open_telnet_dcc(int, char *, char *);
  110. int open_telnet_raw(int, const char *, port_t, bool, int = 0);
  111. void tputs(int, const char *, size_t);
  112. void dequeue_sockets();
  113. int sockgets(char *, int *);
  114. void tell_netdebug(int);
  115. char *iptostr(in_addr_t);
  116. bool sock_has_data(int, int);
  117. int sockoptions(int sock, int operation, int sock_options);
  118. void init_net(void);
  119. int sock_read(bd::Stream&);
  120. void sock_write(bd::Stream&, int);
  121. bool socket_run();
  122. extern union sockaddr_union cached_myip4_so;
  123. #ifdef USE_IPV6
  124. extern union sockaddr_union cached_myip6_so;
  125. extern unsigned long notalloc;
  126. #endif /* USE_IPV6 */
  127. extern char firewall[], botuser[21];
  128. extern int MAXSOCKS, socks_total;
  129. extern bool identd_hack, cached_ip;
  130. extern port_t firewallport;
  131. extern jmp_buf alarmret;
  132. extern sock_list *socklist;
  133. #endif /* !_NET_H */