conn-func.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
  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 "imp.h"
  18. #include <assert.h>
  19. #include <string.h>
  20. #include "log.h"
  21. #include "conn.h"
  22. #include "client.h"
  23. #include "exp.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 Idex 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. t = time(NULL);
  88. if (My_Connections[Idx].delaytime < t)
  89. My_Connections[Idx].delaytime = t;
  90. My_Connections[Idx].delaytime += Seconds;
  91. #ifdef DEBUG
  92. Log(LOG_DEBUG,
  93. "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
  94. Idx, (long)Seconds, Seconds != 1 ? "s" : "",
  95. My_Connections[Idx].delaytime - t,
  96. My_Connections[Idx].delaytime - t != 1 ? "s" : "");
  97. #endif
  98. } /* Conn_SetPenalty */
  99. GLOBAL void
  100. Conn_ClearFlags( void )
  101. {
  102. CONN_ID i;
  103. for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
  104. } /* Conn_ClearFlags */
  105. GLOBAL int
  106. Conn_Flag( CONN_ID Idx )
  107. {
  108. assert( Idx > NONE );
  109. return My_Connections[Idx].flag;
  110. } /* Conn_Flag */
  111. GLOBAL void
  112. Conn_SetFlag( CONN_ID Idx, int Flag )
  113. {
  114. assert( Idx > NONE );
  115. My_Connections[Idx].flag = Flag;
  116. } /* Conn_SetFlag */
  117. GLOBAL CONN_ID
  118. Conn_First( void )
  119. {
  120. CONN_ID i;
  121. for( i = 0; i < Pool_Size; i++ )
  122. {
  123. if( My_Connections[i].sock != NONE ) return i;
  124. }
  125. return NONE;
  126. } /* Conn_First */
  127. GLOBAL CONN_ID
  128. Conn_Next( CONN_ID Idx )
  129. {
  130. CONN_ID i = NONE;
  131. assert( Idx > NONE );
  132. for( i = Idx + 1; i < Pool_Size; i++ )
  133. {
  134. if( My_Connections[i].sock != NONE ) return i;
  135. }
  136. return NONE;
  137. } /* Conn_Next */
  138. GLOBAL UINT16
  139. Conn_Options( CONN_ID Idx )
  140. {
  141. assert( Idx > NONE );
  142. return My_Connections[Idx].options;
  143. } /* Conn_Options */
  144. /**
  145. * Set connection option.
  146. */
  147. GLOBAL void
  148. Conn_SetOption(CONN_ID Idx, int Option)
  149. {
  150. assert(Idx > NONE);
  151. Conn_OPTION_ADD(&My_Connections[Idx], Option);
  152. } /* Conn_SetOption */
  153. /**
  154. * Get the start time of the connection.
  155. * The result is the start time in seconds since 1970-01-01, as reported
  156. * by the C function time(NULL).
  157. */
  158. GLOBAL time_t
  159. Conn_StartTime( CONN_ID Idx )
  160. {
  161. CLIENT *c;
  162. assert(Idx > NONE);
  163. /* Search client structure for this link ... */
  164. c = Conn_GetClient(Idx);
  165. if(c != NULL)
  166. return Client_StartTime(c);
  167. return 0;
  168. } /* Conn_StartTime */
  169. /**
  170. * return number of bytes queued for writing
  171. */
  172. GLOBAL size_t
  173. Conn_SendQ( CONN_ID Idx )
  174. {
  175. assert( Idx > NONE );
  176. #ifdef ZLIB
  177. if( My_Connections[Idx].options & CONN_ZIP )
  178. return array_bytes(&My_Connections[Idx].zip.wbuf);
  179. else
  180. #endif
  181. return array_bytes(&My_Connections[Idx].wbuf);
  182. } /* Conn_SendQ */
  183. /**
  184. * return number of messages sent on this connection so far
  185. */
  186. GLOBAL long
  187. Conn_SendMsg( CONN_ID Idx )
  188. {
  189. assert( Idx > NONE );
  190. return My_Connections[Idx].msg_out;
  191. } /* Conn_SendMsg */
  192. /**
  193. * return number of (uncompressed) bytes sent
  194. * on this connection so far
  195. */
  196. GLOBAL long
  197. Conn_SendBytes( CONN_ID Idx )
  198. {
  199. assert( Idx > NONE );
  200. return My_Connections[Idx].bytes_out;
  201. } /* Conn_SendBytes */
  202. /**
  203. * return number of bytes pending in read buffer
  204. */
  205. GLOBAL size_t
  206. Conn_RecvQ( CONN_ID Idx )
  207. {
  208. assert( Idx > NONE );
  209. #ifdef ZLIB
  210. if( My_Connections[Idx].options & CONN_ZIP )
  211. return array_bytes(&My_Connections[Idx].zip.rbuf);
  212. else
  213. #endif
  214. return array_bytes(&My_Connections[Idx].rbuf);
  215. } /* Conn_RecvQ */
  216. /**
  217. * return number of messages received on this connection so far
  218. */
  219. GLOBAL long
  220. Conn_RecvMsg( CONN_ID Idx )
  221. {
  222. assert( Idx > NONE );
  223. return My_Connections[Idx].msg_in;
  224. } /* Conn_RecvMsg */
  225. /**
  226. * return number of (uncompressed) bytes received on this
  227. * connection so far
  228. */
  229. GLOBAL long
  230. Conn_RecvBytes( CONN_ID Idx )
  231. {
  232. assert( Idx > NONE );
  233. return My_Connections[Idx].bytes_in;
  234. } /* Conn_RecvBytes */
  235. /**
  236. * Return the remote IP address of this connection as string.
  237. */
  238. GLOBAL const char *
  239. Conn_IPA(CONN_ID Idx)
  240. {
  241. assert (Idx > NONE);
  242. return ng_ipaddr_tostr(&My_Connections[Idx].addr);
  243. }
  244. GLOBAL void
  245. Conn_ResetWCounter( void )
  246. {
  247. WCounter = 0;
  248. } /* Conn_ResetWCounter */
  249. GLOBAL long
  250. Conn_WCounter( void )
  251. {
  252. return WCounter;
  253. } /* Conn_WCounter */
  254. /* -eof- */