conn-func.c 5.7 KB

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