conn-func.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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. * the value 0 signals a newly connected client including servers during the
  41. * initial "server burst"; and 1 means that no PONG is pending for a PING.
  42. *
  43. * @param Idx Connection index.
  44. * @param TimeStamp 0, 1, or time stamp.
  45. */
  46. GLOBAL void
  47. Conn_UpdatePing(CONN_ID Idx, time_t TimeStamp)
  48. {
  49. assert(Idx > NONE);
  50. My_Connections[Idx].lastping = TimeStamp;
  51. }
  52. /*
  53. * Get signon time of a connection.
  54. */
  55. GLOBAL time_t
  56. Conn_GetSignon(CONN_ID Idx)
  57. {
  58. assert(Idx > NONE);
  59. return My_Connections[Idx].signon;
  60. }
  61. GLOBAL time_t
  62. Conn_GetIdle( CONN_ID Idx )
  63. {
  64. /* Return Idle-Timer of a connetion */
  65. assert( Idx > NONE );
  66. return time( NULL ) - My_Connections[Idx].lastprivmsg;
  67. } /* Conn_GetIdle */
  68. GLOBAL time_t
  69. Conn_LastPing( CONN_ID Idx )
  70. {
  71. assert( Idx > NONE );
  72. return My_Connections[Idx].lastping;
  73. } /* Conn_LastPing */
  74. /**
  75. * Add "penalty time" for a connection.
  76. *
  77. * During the "penalty time" the socket is ignored completely, no new data
  78. * is read. This function only increases the penalty, it is not possible to
  79. * decrease the penalty time.
  80. *
  81. * @param Idx Connection index.
  82. * @param Seconds Seconds to add.
  83. * @see Conn_ResetPenalty
  84. */
  85. GLOBAL void
  86. Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
  87. {
  88. time_t t;
  89. assert(Idx > NONE);
  90. assert(Seconds >= 0);
  91. /* Limit new penalty to maximum configured, when less than 10 seconds. *
  92. The latter is used to limit brute force attacks, therefore we don't *
  93. want to limit that! */
  94. if (Conf_MaxPenaltyTime >= 0
  95. && Seconds > Conf_MaxPenaltyTime
  96. && Seconds < 10)
  97. Seconds = Conf_MaxPenaltyTime;
  98. t = time(NULL);
  99. if (My_Connections[Idx].delaytime < t)
  100. My_Connections[Idx].delaytime = t;
  101. My_Connections[Idx].delaytime += Seconds;
  102. #ifdef DEBUG
  103. Log(LOG_DEBUG,
  104. "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
  105. Idx, (long)Seconds, Seconds != 1 ? "s" : "",
  106. My_Connections[Idx].delaytime - t,
  107. My_Connections[Idx].delaytime - t != 1 ? "s" : "");
  108. #endif
  109. } /* Conn_SetPenalty */
  110. GLOBAL void
  111. Conn_ClearFlags( void )
  112. {
  113. CONN_ID i;
  114. for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
  115. } /* Conn_ClearFlags */
  116. GLOBAL int
  117. Conn_Flag( CONN_ID Idx )
  118. {
  119. assert( Idx > NONE );
  120. return My_Connections[Idx].flag;
  121. } /* Conn_Flag */
  122. GLOBAL void
  123. Conn_SetFlag( CONN_ID Idx, int Flag )
  124. {
  125. assert( Idx > NONE );
  126. My_Connections[Idx].flag = Flag;
  127. } /* Conn_SetFlag */
  128. GLOBAL CONN_ID
  129. Conn_First( void )
  130. {
  131. CONN_ID i;
  132. for( i = 0; i < Pool_Size; i++ )
  133. {
  134. if( My_Connections[i].sock != NONE ) return i;
  135. }
  136. return NONE;
  137. } /* Conn_First */
  138. GLOBAL CONN_ID
  139. Conn_Next( CONN_ID Idx )
  140. {
  141. CONN_ID i = NONE;
  142. assert( Idx > NONE );
  143. for( i = Idx + 1; i < Pool_Size; i++ )
  144. {
  145. if( My_Connections[i].sock != NONE ) return i;
  146. }
  147. return NONE;
  148. } /* Conn_Next */
  149. GLOBAL UINT16
  150. Conn_Options( CONN_ID Idx )
  151. {
  152. assert( Idx > NONE );
  153. return My_Connections[Idx].options;
  154. } /* Conn_Options */
  155. /**
  156. * Set connection option.
  157. */
  158. GLOBAL void
  159. Conn_SetOption(CONN_ID Idx, int Option)
  160. {
  161. assert(Idx > NONE);
  162. Conn_OPTION_ADD(&My_Connections[Idx], Option);
  163. } /* Conn_SetOption */
  164. /**
  165. * Get the start time of the connection.
  166. * The result is the start time in seconds since 1970-01-01, as reported
  167. * by the C function time(NULL).
  168. */
  169. GLOBAL time_t
  170. Conn_StartTime( CONN_ID Idx )
  171. {
  172. CLIENT *c;
  173. assert(Idx > NONE);
  174. /* Search client structure for this link ... */
  175. c = Conn_GetClient(Idx);
  176. if(c != NULL)
  177. return Client_StartTime(c);
  178. return 0;
  179. } /* Conn_StartTime */
  180. /**
  181. * return number of bytes queued for writing
  182. */
  183. GLOBAL size_t
  184. Conn_SendQ( CONN_ID Idx )
  185. {
  186. assert( Idx > NONE );
  187. #ifdef ZLIB
  188. if( My_Connections[Idx].options & CONN_ZIP )
  189. return array_bytes(&My_Connections[Idx].zip.wbuf);
  190. else
  191. #endif
  192. return array_bytes(&My_Connections[Idx].wbuf);
  193. } /* Conn_SendQ */
  194. /**
  195. * return number of messages sent on this connection so far
  196. */
  197. GLOBAL long
  198. Conn_SendMsg( CONN_ID Idx )
  199. {
  200. assert( Idx > NONE );
  201. return My_Connections[Idx].msg_out;
  202. } /* Conn_SendMsg */
  203. /**
  204. * return number of (uncompressed) bytes sent
  205. * on this connection so far
  206. */
  207. GLOBAL long
  208. Conn_SendBytes( CONN_ID Idx )
  209. {
  210. assert( Idx > NONE );
  211. return My_Connections[Idx].bytes_out;
  212. } /* Conn_SendBytes */
  213. /**
  214. * return number of bytes pending in read buffer
  215. */
  216. GLOBAL size_t
  217. Conn_RecvQ( CONN_ID Idx )
  218. {
  219. assert( Idx > NONE );
  220. #ifdef ZLIB
  221. if( My_Connections[Idx].options & CONN_ZIP )
  222. return array_bytes(&My_Connections[Idx].zip.rbuf);
  223. else
  224. #endif
  225. return array_bytes(&My_Connections[Idx].rbuf);
  226. } /* Conn_RecvQ */
  227. /**
  228. * return number of messages received on this connection so far
  229. */
  230. GLOBAL long
  231. Conn_RecvMsg( CONN_ID Idx )
  232. {
  233. assert( Idx > NONE );
  234. return My_Connections[Idx].msg_in;
  235. } /* Conn_RecvMsg */
  236. /**
  237. * return number of (uncompressed) bytes received on this
  238. * connection so far
  239. */
  240. GLOBAL long
  241. Conn_RecvBytes( CONN_ID Idx )
  242. {
  243. assert( Idx > NONE );
  244. return My_Connections[Idx].bytes_in;
  245. } /* Conn_RecvBytes */
  246. /**
  247. * Return the remote IP address of this connection as string.
  248. */
  249. GLOBAL const char *
  250. Conn_IPA(CONN_ID Idx)
  251. {
  252. assert (Idx > NONE);
  253. return ng_ipaddr_tostr(&My_Connections[Idx].addr);
  254. }
  255. GLOBAL void
  256. Conn_ResetWCounter( void )
  257. {
  258. WCounter = 0;
  259. } /* Conn_ResetWCounter */
  260. GLOBAL long
  261. Conn_WCounter( void )
  262. {
  263. return WCounter;
  264. } /* Conn_WCounter */
  265. /* -eof- */