conn-func.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. /* Connection markieren */
  115. assert( Idx > NONE );
  116. My_Connections[Idx].flag = Flag;
  117. } /* Conn_SetFlag */
  118. GLOBAL CONN_ID
  119. Conn_First( void )
  120. {
  121. /* Connection-Struktur der ersten Verbindung liefern;
  122. * Ist keine Verbindung vorhanden, wird NONE geliefert. */
  123. CONN_ID i;
  124. for( i = 0; i < Pool_Size; i++ )
  125. {
  126. if( My_Connections[i].sock != NONE ) return i;
  127. }
  128. return NONE;
  129. } /* Conn_First */
  130. GLOBAL CONN_ID
  131. Conn_Next( CONN_ID Idx )
  132. {
  133. /* Naechste Verbindungs-Struktur liefern; existiert keine
  134. * weitere, so wird NONE geliefert. */
  135. CONN_ID i = NONE;
  136. assert( Idx > NONE );
  137. for( i = Idx + 1; i < Pool_Size; i++ )
  138. {
  139. if( My_Connections[i].sock != NONE ) return i;
  140. }
  141. return NONE;
  142. } /* Conn_Next */
  143. GLOBAL UINT16
  144. Conn_Options( CONN_ID Idx )
  145. {
  146. assert( Idx > NONE );
  147. return My_Connections[Idx].options;
  148. } /* Conn_Options */
  149. /**
  150. * Set connection option.
  151. */
  152. GLOBAL void
  153. Conn_SetOption(CONN_ID Idx, int Option)
  154. {
  155. assert(Idx > NONE);
  156. Conn_OPTION_ADD(&My_Connections[Idx], Option);
  157. } /* Conn_SetOption */
  158. /**
  159. * Get the start time of the connection.
  160. * The result is the start time in seconds since 1970-01-01, as reported
  161. * by the C function time(NULL).
  162. */
  163. GLOBAL time_t
  164. Conn_StartTime( CONN_ID Idx )
  165. {
  166. CLIENT *c;
  167. assert(Idx > NONE);
  168. /* Search client structure for this link ... */
  169. c = Conn_GetClient(Idx);
  170. if(c != NULL)
  171. return Client_StartTime(c);
  172. return 0;
  173. } /* Conn_StartTime */
  174. /**
  175. * return number of bytes queued for writing
  176. */
  177. GLOBAL size_t
  178. Conn_SendQ( CONN_ID Idx )
  179. {
  180. assert( Idx > NONE );
  181. #ifdef ZLIB
  182. if( My_Connections[Idx].options & CONN_ZIP )
  183. return array_bytes(&My_Connections[Idx].zip.wbuf);
  184. else
  185. #endif
  186. return array_bytes(&My_Connections[Idx].wbuf);
  187. } /* Conn_SendQ */
  188. /**
  189. * return number of messages sent on this connection so far
  190. */
  191. GLOBAL long
  192. Conn_SendMsg( CONN_ID Idx )
  193. {
  194. assert( Idx > NONE );
  195. return My_Connections[Idx].msg_out;
  196. } /* Conn_SendMsg */
  197. /**
  198. * return number of (uncompressed) bytes sent
  199. * on this connection so far
  200. */
  201. GLOBAL long
  202. Conn_SendBytes( CONN_ID Idx )
  203. {
  204. assert( Idx > NONE );
  205. return My_Connections[Idx].bytes_out;
  206. } /* Conn_SendBytes */
  207. /**
  208. * return number of bytes pending in read buffer
  209. */
  210. GLOBAL size_t
  211. Conn_RecvQ( CONN_ID Idx )
  212. {
  213. assert( Idx > NONE );
  214. #ifdef ZLIB
  215. if( My_Connections[Idx].options & CONN_ZIP )
  216. return array_bytes(&My_Connections[Idx].zip.rbuf);
  217. else
  218. #endif
  219. return array_bytes(&My_Connections[Idx].rbuf);
  220. } /* Conn_RecvQ */
  221. /**
  222. * return number of messages received on this connection so far
  223. */
  224. GLOBAL long
  225. Conn_RecvMsg( CONN_ID Idx )
  226. {
  227. assert( Idx > NONE );
  228. return My_Connections[Idx].msg_in;
  229. } /* Conn_RecvMsg */
  230. /**
  231. * return number of (uncompressed) bytes received on this
  232. * connection so far
  233. */
  234. GLOBAL long
  235. Conn_RecvBytes( CONN_ID Idx )
  236. {
  237. assert( Idx > NONE );
  238. return My_Connections[Idx].bytes_in;
  239. } /* Conn_RecvBytes */
  240. /**
  241. * Return the remote IP address of this connection as string.
  242. */
  243. GLOBAL const char *
  244. Conn_IPA(CONN_ID Idx)
  245. {
  246. assert (Idx > NONE);
  247. return ng_ipaddr_tostr(&My_Connections[Idx].addr);
  248. }
  249. GLOBAL void
  250. Conn_ResetWCounter( void )
  251. {
  252. WCounter = 0;
  253. } /* Conn_ResetWCounter */
  254. GLOBAL long
  255. Conn_WCounter( void )
  256. {
  257. return WCounter;
  258. } /* Conn_WCounter */
  259. /* -eof- */