conn-func.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * ngIRCd -- The Next Generation IRC Daemon
  3. * Copyright (c)2001,2002 by 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. * Connection management: Global functions
  12. */
  13. #define CONN_MODULE
  14. #include "portab.h"
  15. static char UNUSED id[] = "$Id: conn-func.c,v 1.10 2006/05/10 21:24:01 alex Exp $";
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <log.h>
  19. #include "conn.h"
  20. #include "client.h"
  21. #include "exp.h"
  22. #include "conn-func.h"
  23. GLOBAL void
  24. Conn_UpdateIdle( CONN_ID Idx )
  25. {
  26. /* Idle-Timer zuruecksetzen */
  27. assert( Idx > NONE );
  28. My_Connections[Idx].lastprivmsg = time( NULL );
  29. }
  30. GLOBAL time_t
  31. Conn_GetIdle( CONN_ID Idx )
  32. {
  33. /* Idle-Time einer Verbindung liefern (in Sekunden) */
  34. assert( Idx > NONE );
  35. return time( NULL ) - My_Connections[Idx].lastprivmsg;
  36. } /* Conn_GetIdle */
  37. GLOBAL time_t
  38. Conn_LastPing( CONN_ID Idx )
  39. {
  40. /* Zeitpunkt des letzten PING liefern */
  41. assert( Idx > NONE );
  42. return My_Connections[Idx].lastping;
  43. } /* Conn_LastPing */
  44. GLOBAL void
  45. Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
  46. {
  47. /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
  48. * waehrend dieser Zeit wird der entsprechende Socket vom Server
  49. * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
  50. * dieser Funktion nur erhoeht, nicht aber verringert werden. */
  51. time_t t;
  52. assert( Idx > NONE );
  53. assert( Seconds >= 0 );
  54. t = time( NULL ) + Seconds;
  55. if (t > My_Connections[Idx].delaytime)
  56. My_Connections[Idx].delaytime = t;
  57. #ifdef DEBUG
  58. Log(LOG_DEBUG, "Add penalty time on connection %d: %ld second(s).",
  59. Idx, (long)Seconds);
  60. #endif
  61. } /* Conn_SetPenalty */
  62. GLOBAL void
  63. Conn_ResetPenalty( CONN_ID Idx )
  64. {
  65. assert( Idx > NONE );
  66. My_Connections[Idx].delaytime = 0;
  67. } /* Conn_ResetPenalty */
  68. GLOBAL void
  69. Conn_ClearFlags( void )
  70. {
  71. /* Alle Connection auf "nicht-markiert" setzen */
  72. CONN_ID i;
  73. for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
  74. } /* Conn_ClearFlags */
  75. GLOBAL int
  76. Conn_Flag( CONN_ID Idx )
  77. {
  78. /* Ist eine Connection markiert (true) oder nicht? */
  79. assert( Idx > NONE );
  80. return My_Connections[Idx].flag;
  81. } /* Conn_Flag */
  82. GLOBAL void
  83. Conn_SetFlag( CONN_ID Idx, int Flag )
  84. {
  85. /* Connection markieren */
  86. assert( Idx > NONE );
  87. My_Connections[Idx].flag = Flag;
  88. } /* Conn_SetFlag */
  89. GLOBAL CONN_ID
  90. Conn_First( void )
  91. {
  92. /* Connection-Struktur der ersten Verbindung liefern;
  93. * Ist keine Verbindung vorhanden, wird NONE geliefert. */
  94. CONN_ID i;
  95. for( i = 0; i < Pool_Size; i++ )
  96. {
  97. if( My_Connections[i].sock != NONE ) return i;
  98. }
  99. return NONE;
  100. } /* Conn_First */
  101. GLOBAL CONN_ID
  102. Conn_Next( CONN_ID Idx )
  103. {
  104. /* Naechste Verbindungs-Struktur liefern; existiert keine
  105. * weitere, so wird NONE geliefert. */
  106. CONN_ID i = NONE;
  107. assert( Idx > NONE );
  108. for( i = Idx + 1; i < Pool_Size; i++ )
  109. {
  110. if( My_Connections[i].sock != NONE ) return i;
  111. }
  112. return NONE;
  113. } /* Conn_Next */
  114. GLOBAL UINT16
  115. Conn_Options( CONN_ID Idx )
  116. {
  117. assert( Idx > NONE );
  118. return My_Connections[Idx].options;
  119. } /* Conn_Options */
  120. /**
  121. * Get the start time of the connection.
  122. * The result is the start time in seconds since 1970-01-01, as reported
  123. * by the C function time(NULL).
  124. */
  125. GLOBAL time_t
  126. Conn_StartTime( CONN_ID Idx )
  127. {
  128. CLIENT *c;
  129. assert(Idx > NONE);
  130. /* Search client structure for this link ... */
  131. c = Conn_GetClient(Idx);
  132. if(c != NULL)
  133. return Client_StartTime(c);
  134. return 0;
  135. } /* Conn_StartTime */
  136. GLOBAL size_t
  137. Conn_SendQ( CONN_ID Idx )
  138. {
  139. /* Laenge der Daten im Schreibbuffer liefern */
  140. assert( Idx > NONE );
  141. #ifdef ZLIB
  142. if( My_Connections[Idx].options & CONN_ZIP )
  143. return array_bytes(&My_Connections[Idx].zip.wbuf);
  144. else
  145. #endif
  146. return array_bytes(&My_Connections[Idx].wbuf);
  147. } /* Conn_SendQ */
  148. GLOBAL long
  149. Conn_SendMsg( CONN_ID Idx )
  150. {
  151. /* Anzahl gesendeter Nachrichten liefern */
  152. assert( Idx > NONE );
  153. return My_Connections[Idx].msg_out;
  154. } /* Conn_SendMsg */
  155. GLOBAL long
  156. Conn_SendBytes( CONN_ID Idx )
  157. {
  158. /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
  159. assert( Idx > NONE );
  160. return My_Connections[Idx].bytes_out;
  161. } /* Conn_SendBytes */
  162. GLOBAL size_t
  163. Conn_RecvQ( CONN_ID Idx )
  164. {
  165. /* Laenge der Daten im Lesebuffer liefern */
  166. assert( Idx > NONE );
  167. #ifdef ZLIB
  168. if( My_Connections[Idx].options & CONN_ZIP )
  169. return array_bytes(&My_Connections[Idx].zip.rbuf);
  170. else
  171. #endif
  172. return array_bytes(&My_Connections[Idx].rbuf);
  173. } /* Conn_RecvQ */
  174. GLOBAL long
  175. Conn_RecvMsg( CONN_ID Idx )
  176. {
  177. /* Anzahl empfangener Nachrichten liefern */
  178. assert( Idx > NONE );
  179. return My_Connections[Idx].msg_in;
  180. } /* Conn_RecvMsg */
  181. GLOBAL long
  182. Conn_RecvBytes( CONN_ID Idx )
  183. {
  184. /* Anzahl empfangener Bytes (unkomprimiert) liefern */
  185. assert( Idx > NONE );
  186. return My_Connections[Idx].bytes_in;
  187. } /* Conn_RecvBytes */
  188. GLOBAL void
  189. Conn_ResetWCounter( void )
  190. {
  191. WCounter = 0;
  192. } /* Conn_ResetWCounter */
  193. GLOBAL long
  194. Conn_WCounter( void )
  195. {
  196. return WCounter;
  197. } /* Conn_WCounter */
  198. /* -eof- */