conn-func.c 6.3 KB

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