conn-func.c 5.4 KB

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