conn-func.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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.6 2005/06/12 16:32:17 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 ) My_Connections[Idx].delaytime = t;
  56. } /* Conn_SetPenalty */
  57. GLOBAL void
  58. Conn_ResetPenalty( CONN_ID Idx )
  59. {
  60. assert( Idx > NONE );
  61. My_Connections[Idx].delaytime = 0;
  62. } /* Conn_ResetPenalty */
  63. GLOBAL void
  64. Conn_ClearFlags( void )
  65. {
  66. /* Alle Connection auf "nicht-markiert" setzen */
  67. CONN_ID i;
  68. for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
  69. } /* Conn_ClearFlags */
  70. GLOBAL int
  71. Conn_Flag( CONN_ID Idx )
  72. {
  73. /* Ist eine Connection markiert (true) oder nicht? */
  74. assert( Idx > NONE );
  75. return My_Connections[Idx].flag;
  76. } /* Conn_Flag */
  77. GLOBAL void
  78. Conn_SetFlag( CONN_ID Idx, int Flag )
  79. {
  80. /* Connection markieren */
  81. assert( Idx > NONE );
  82. My_Connections[Idx].flag = Flag;
  83. } /* Conn_SetFlag */
  84. GLOBAL CONN_ID
  85. Conn_First( void )
  86. {
  87. /* Connection-Struktur der ersten Verbindung liefern;
  88. * Ist keine Verbindung vorhanden, wird NONE geliefert. */
  89. CONN_ID i;
  90. for( i = 0; i < Pool_Size; i++ )
  91. {
  92. if( My_Connections[i].sock != NONE ) return i;
  93. }
  94. return NONE;
  95. } /* Conn_First */
  96. GLOBAL CONN_ID
  97. Conn_Next( CONN_ID Idx )
  98. {
  99. /* Naechste Verbindungs-Struktur liefern; existiert keine
  100. * weitere, so wird NONE geliefert. */
  101. CONN_ID i = NONE;
  102. assert( Idx > NONE );
  103. for( i = Idx + 1; i < Pool_Size; i++ )
  104. {
  105. if( My_Connections[i].sock != NONE ) return i;
  106. }
  107. return NONE;
  108. } /* Conn_Next */
  109. GLOBAL int
  110. Conn_Options( CONN_ID Idx )
  111. {
  112. assert( Idx > NONE );
  113. return My_Connections[Idx].options;
  114. } /* Conn_Options */
  115. /**
  116. * Get the start time of the connection.
  117. * The result is the start time in seconds since 1970-01-01, as reported
  118. * by the C function time(NULL).
  119. */
  120. GLOBAL time_t
  121. Conn_StartTime( CONN_ID Idx )
  122. {
  123. CLIENT *c;
  124. assert(Idx > NONE);
  125. /* Search client structure for this link ... */
  126. c = Client_GetFromConn(Idx);
  127. if(c != NULL)
  128. return Client_StartTime(c);
  129. return 0;
  130. } /* Conn_StartTime */
  131. GLOBAL int
  132. Conn_SendQ( CONN_ID Idx )
  133. {
  134. /* Laenge der Daten im Schreibbuffer liefern */
  135. assert( Idx > NONE );
  136. #ifdef ZLIB
  137. if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.wdatalen;
  138. else
  139. #endif
  140. return My_Connections[Idx].wdatalen;
  141. } /* Conn_SendQ */
  142. GLOBAL long
  143. Conn_SendMsg( CONN_ID Idx )
  144. {
  145. /* Anzahl gesendeter Nachrichten liefern */
  146. assert( Idx > NONE );
  147. return My_Connections[Idx].msg_out;
  148. } /* Conn_SendMsg */
  149. GLOBAL long
  150. Conn_SendBytes( CONN_ID Idx )
  151. {
  152. /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
  153. assert( Idx > NONE );
  154. return My_Connections[Idx].bytes_out;
  155. } /* Conn_SendBytes */
  156. GLOBAL int
  157. Conn_RecvQ( CONN_ID Idx )
  158. {
  159. /* Laenge der Daten im Lesebuffer liefern */
  160. assert( Idx > NONE );
  161. #ifdef ZLIB
  162. if( My_Connections[Idx].options & CONN_ZIP ) return My_Connections[Idx].zip.rdatalen;
  163. else
  164. #endif
  165. return My_Connections[Idx].rdatalen;
  166. } /* Conn_RecvQ */
  167. GLOBAL long
  168. Conn_RecvMsg( CONN_ID Idx )
  169. {
  170. /* Anzahl empfangener Nachrichten liefern */
  171. assert( Idx > NONE );
  172. return My_Connections[Idx].msg_in;
  173. } /* Conn_RecvMsg */
  174. GLOBAL long
  175. Conn_RecvBytes( CONN_ID Idx )
  176. {
  177. /* Anzahl empfangener Bytes (unkomprimiert) liefern */
  178. assert( Idx > NONE );
  179. return My_Connections[Idx].bytes_in;
  180. } /* Conn_RecvBytes */
  181. GLOBAL void
  182. Conn_ResetWCounter( void )
  183. {
  184. WCounter = 0;
  185. } /* Conn_ResetWCounter */
  186. GLOBAL long
  187. Conn_WCounter( void )
  188. {
  189. return WCounter;
  190. } /* Conn_WCounter */
  191. /* -eof- */