net.h 5.0 KB

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