net.h 4.8 KB

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