conn-func.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2018 Alexander Barton (alex@barton.de) and Contributors.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. * Please read the file COPYING, README and AUTHORS for more information.
  10. */
  11. #define CONN_MODULE
  12. #include "portab.h"
  13. /**
  14. * @file
  15. * Connection management: Global functions
  16. */
  17. #include <assert.h>
  18. #include <time.h>
  19. #ifdef DEBUG
  20. # include "log.h"
  21. #endif
  22. #include "conn.h"
  23. #include "conf.h"
  24. #include "conn-func.h"
  25. /**
  26. * Update "idle timestamp", the time of the last visible user action
  27. * (e. g. like sending messages, joining or leaving channels).
  28. *
  29. * @param Idx Connection index.
  30. */
  31. GLOBAL void
  32. Conn_UpdateIdle(CONN_ID Idx)
  33. {
  34. assert(Idx > NONE);
  35. My_Connections[Idx].lastprivmsg = time(NULL);
  36. }
  37. /**
  38. * Update "ping timestamp", the time of the last outgoing PING request.
  39. *
  40. * @param Idx Connection index.
  41. */
  42. GLOBAL void
  43. Conn_UpdatePing(CONN_ID Idx)
  44. {
  45. assert(Idx > NONE);
  46. My_Connections[Idx].lastping = time(NULL);
  47. }
  48. /*
  49. * Get signon time of a connection.
  50. */
  51. GLOBAL time_t
  52. Conn_GetSignon(CONN_ID Idx)
  53. {
  54. assert(Idx > NONE);
  55. return My_Connections[Idx].signon;
  56. }
  57. GLOBAL time_t
  58. Conn_GetIdle( CONN_ID Idx )
  59. {
  60. /* Return Idle-Timer of a connetion */
  61. assert( Idx > NONE );
  62. return time( NULL ) - My_Connections[Idx].lastprivmsg;
  63. } /* Conn_GetIdle */
  64. GLOBAL time_t
  65. Conn_LastPing( CONN_ID Idx )
  66. {
  67. assert( Idx > NONE );
  68. return My_Connections[Idx].lastping;
  69. } /* Conn_LastPing */
  70. /**
  71. * Add "penalty time" for a connection.
  72. *
  73. * During the "penalty time" the socket is ignored completely, no new data
  74. * is read. This function only increases the penalty, it is not possible to
  75. * decrease the penalty time.
  76. *
  77. * @param Idx Connection index.
  78. * @param Seconds Seconds to add.
  79. * @see Conn_ResetPenalty
  80. */
  81. GLOBAL void
  82. Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
  83. {
  84. time_t t;
  85. assert(Idx > NONE);
  86. assert(Seconds >= 0);
  87. /* Limit new penalty to maximum configured, when less than 10 seconds. *
  88. The latter is used to limit brute force attacks, therefore we don't *
  89. want to limit that! */
  90. if (Conf_MaxPenaltyTime >= 0
  91. && Seconds > Conf_MaxPenaltyTime
  92. && Seconds < 10)
  93. Seconds = Conf_MaxPenaltyTime;
  94. t = time(NULL);
  95. if (My_Connections[Idx].delaytime < t)
  96. My_Connections[Idx].delaytime = t;
  97. My_Connections[Idx].delaytime += Seconds;
  98. #ifdef DEBUG
  99. Log(LOG_DEBUG,
  100. "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
  101. Idx, (long)Seconds, Seconds != 1 ? "s" : "",
  102. My_Connections[Idx].delaytime - t,
  103. My_Connections[Idx].delaytime - t != 1 ? "s" : "");
  104. #endif
  105. } /* Conn_SetPenalty */
  106. GLOBAL void
  107. Conn_ClearFlags( void )
  108. {
  109. CONN_ID i;
  110. for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
  111. } /* Conn_ClearFlags */
  112. GLOBAL int
  113. Conn_Flag( CONN_ID Idx )
  114. {
  115. assert( Idx > NONE );
  116. return My_Connections[Idx].flag;
  117. } /* Conn_Flag */
  118. GLOBAL void
  119. Conn_SetFlag( CONN_ID Idx, int Flag )
  120. {
  121. assert( Idx > NONE );
  122. My_Connections[Idx].flag = Flag;
  123. } /* Conn_SetFlag */
  124. GLOBAL CONN_ID
  125. Conn_First( void )
  126. {
  127. CONN_ID i;
  128. for( i = 0; i < Pool_Size; i++ )
  129. {
  130. if( My_Connections[i].sock != NONE ) return i;
  131. }
  132. return NONE;
  133. } /* Conn_First */
  134. GLOBAL CONN_ID
  135. Conn_Next( CONN_ID Idx )
  136. {
  137. CONN_ID i = NONE;
  138. assert( Idx > NONE );
  139. for( i = Idx + 1; i < Pool_Size; i++ )
  140. {
  141. if( My_Connections[i].sock != NONE ) return i;
  142. }
  143. return NONE;
  144. } /* Conn_Next */
  145. GLOBAL UINT16
  146. Conn_Options( CONN_ID Idx )
  147. {
  148. assert( Idx > NONE );
  149. return My_Connections[Idx].options;
  150. } /* Conn_Options */
  151. /**
  152. * Set connection option.
  153. */
  154. GLOBAL void
  155. Conn_SetOption(CONN_ID Idx, int Option)
  156. {
  157. assert(Idx > NONE);
  158. Conn_OPTION_ADD(&My_Connections[Idx], Option);
  159. } /* Conn_SetOption */
  160. /**
  161. * Get the start time of the connection.
  162. * The result is the start time in seconds since 1970-01-01, as reported
  163. * by the C function time(NULL).
  164. */
  165. GLOBAL time_t
  166. Conn_StartTime( CONN_ID Idx )
  167. {
  168. CLIENT *c;
  169. assert(Idx > NONE);
  170. /* Search client structure for this link ... */
  171. c = Conn_GetClient(Idx);
  172. if(c != NULL)
  173. return Client_StartTime(c);
  174. return 0;
  175. } /* Conn_StartTime */
  176. /**
  177. * return number of bytes queued for writing
  178. */
  179. GLOBAL size_t
  180. Conn_SendQ( CONN_ID Idx )
  181. {
  182. assert( Idx > NONE );
  183. #ifdef ZLIB
  184. if( My_Connections[Idx].options & CONN_ZIP )
  185. return array_bytes(&My_Connections[Idx].zip.wbuf);
  186. else
  187. #endif
  188. return array_bytes(&My_Connections[Idx].wbuf);
  189. } /* Conn_SendQ */
  190. /**
  191. * return number of messages sent on this connection so far
  192. */
  193. GLOBAL long
  194. Conn_SendMsg( CONN_ID Idx )
  195. {
  196. assert( Idx > NONE );
  197. return My_Connections[Idx].msg_out;
  198. } /* Conn_SendMsg */
  199. /**
  200. * return number of (uncompressed) bytes sent
  201. * on this connection so far
  202. */
  203. GLOBAL long
  204. Conn_SendBytes( CONN_ID Idx )
  205. {
  206. assert( Idx > NONE );
  207. return My_Connections[Idx].bytes_out;
  208. } /* Conn_SendBytes */
  209. /**
  210. * return number of bytes pending in read buffer
  211. */
  212. GLOBAL size_t
  213. Conn_RecvQ( CONN_ID Idx )
  214. {
  215. assert( Idx > NONE );
  216. #ifdef ZLIB
  217. if( My_Connections[Idx].options & CONN_ZIP )
  218. return array_bytes(&My_Connections[Idx].zip.rbuf);
  219. else
  220. #endif
  221. return array_bytes(&My_Connections[Idx].rbuf);
  222. } /* Conn_RecvQ */
  223. /**
  224. * return number of messages received on this connection so far
  225. */
  226. GLOBAL long
  227. Conn_RecvMsg( CONN_ID Idx )
  228. {
  229. assert( Idx > NONE );
  230. return My_Connections[Idx].msg_in;
  231. } /* Conn_RecvMsg */
  232. /**
  233. * return number of (uncompressed) bytes received on this
  234. * connection so far
  235. */
  236. GLOBAL long
  237. Conn_RecvBytes( CONN_ID Idx )
  238. {
  239. assert( Idx > NONE );
  240. return My_Connections[Idx].bytes_in;
  241. } /* Conn_RecvBytes */
  242. /**
  243. * Return the remote IP address of this connection as string.
  244. */
  245. GLOBAL const char *
  246. Conn_IPA(CONN_ID Idx)
  247. {
  248. assert (Idx > NONE);
  249. return ng_ipaddr_tostr(&My_Connections[Idx].addr);
  250. }
  251. GLOBAL void
  252. Conn_ResetWCounter( void )
  253. {
  254. WCounter = 0;
  255. } /* Conn_ResetWCounter */
  256. GLOBAL long
  257. Conn_WCounter( void )
  258. {
  259. return WCounter;
  260. } /* Conn_WCounter */
  261. /* -eof- */