net.h 5.0 KB

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