conn-func.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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.12 2008/03/11 14:05:27 alex Exp $";
  16. #include "imp.h"
  17. #include <assert.h>
  18. #include <string.h>
  19. #include "log.h"
  20. #include "conn.h"
  21. #include "client.h"
  22. #include "exp.h"
  23. #include "conn-func.h"
  24. GLOBAL void
  25. Conn_UpdateIdle( CONN_ID Idx )
  26. {
  27. /* Idle-Timer zuruecksetzen */
  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. /* Idle-Time einer Verbindung liefern (in Sekunden) */
  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. /* Zeitpunkt des letzten PING liefern */
  51. assert( Idx > NONE );
  52. return My_Connections[Idx].lastping;
  53. } /* Conn_LastPing */
  54. GLOBAL void
  55. Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
  56. {
  57. /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
  58. * waehrend dieser Zeit wird der entsprechende Socket vom Server
  59. * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
  60. * dieser Funktion nur erhoeht, nicht aber verringert werden. */
  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. /* Alle Connection auf "nicht-markiert" setzen */
  82. CONN_ID i;
  83. for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
  84. } /* Conn_ClearFlags */
  85. GLOBAL int
  86. Conn_Flag( CONN_ID Idx )
  87. {
  88. /* Ist eine Connection markiert (true) oder nicht? */
  89. assert( Idx > NONE );
  90. return My_Connections[Idx].flag;
  91. } /* Conn_Flag */
  92. GLOBAL void
  93. Conn_SetFlag( CONN_ID Idx, int Flag )
  94. {
  95. /* Connection markieren */
  96. assert( Idx > NONE );
  97. My_Connections[Idx].flag = Flag;
  98. } /* Conn_SetFlag */
  99. GLOBAL CONN_ID
  100. Conn_First( void )
  101. {
  102. /* Connection-Struktur der ersten Verbindung liefern;
  103. * Ist keine Verbindung vorhanden, wird NONE geliefert. */
  104. CONN_ID i;
  105. for( i = 0; i < Pool_Size; i++ )
  106. {
  107. if( My_Connections[i].sock != NONE ) return i;
  108. }
  109. return NONE;
  110. } /* Conn_First */
  111. GLOBAL CONN_ID
  112. Conn_Next( CONN_ID Idx )
  113. {
  114. /* Naechste Verbindungs-Struktur liefern; existiert keine
  115. * weitere, so wird NONE geliefert. */
  116. CONN_ID i = NONE;
  117. assert( Idx > NONE );
  118. for( i = Idx + 1; i < Pool_Size; i++ )
  119. {
  120. if( My_Connections[i].sock != NONE ) return i;
  121. }
  122. return NONE;
  123. } /* Conn_Next */
  124. GLOBAL UINT16
  125. Conn_Options( CONN_ID Idx )
  126. {
  127. assert( Idx > NONE );
  128. return My_Connections[Idx].options;
  129. } /* Conn_Options */
  130. /**
  131. * Get the start time of the connection.
  132. * The result is the start time in seconds since 1970-01-01, as reported
  133. * by the C function time(NULL).
  134. */
  135. GLOBAL time_t
  136. Conn_StartTime( CONN_ID Idx )
  137. {
  138. CLIENT *c;
  139. assert(Idx > NONE);
  140. /* Search client structure for this link ... */
  141. c = Conn_GetClient(Idx);
  142. if(c != NULL)
  143. return Client_StartTime(c);
  144. return 0;
  145. } /* Conn_StartTime */
  146. GLOBAL size_t
  147. Conn_SendQ( CONN_ID Idx )
  148. {
  149. /* Laenge der Daten im Schreibbuffer liefern */
  150. assert( Idx > NONE );
  151. #ifdef ZLIB
  152. if( My_Connections[Idx].options & CONN_ZIP )
  153. return array_bytes(&My_Connections[Idx].zip.wbuf);
  154. else
  155. #endif
  156. return array_bytes(&My_Connections[Idx].wbuf);
  157. } /* Conn_SendQ */
  158. GLOBAL long
  159. Conn_SendMsg( CONN_ID Idx )
  160. {
  161. /* Anzahl gesendeter Nachrichten liefern */
  162. assert( Idx > NONE );
  163. return My_Connections[Idx].msg_out;
  164. } /* Conn_SendMsg */
  165. GLOBAL long
  166. Conn_SendBytes( CONN_ID Idx )
  167. {
  168. /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
  169. assert( Idx > NONE );
  170. return My_Connections[Idx].bytes_out;
  171. } /* Conn_SendBytes */
  172. GLOBAL size_t
  173. Conn_RecvQ( CONN_ID Idx )
  174. {
  175. /* Laenge der Daten im Lesebuffer liefern */
  176. assert( Idx > NONE );
  177. #ifdef ZLIB
  178. if( My_Connections[Idx].options & CONN_ZIP )
  179. return array_bytes(&My_Connections[Idx].zip.rbuf);
  180. else
  181. #endif
  182. return array_bytes(&My_Connections[Idx].rbuf);
  183. } /* Conn_RecvQ */
  184. GLOBAL long
  185. Conn_RecvMsg( CONN_ID Idx )
  186. {
  187. /* Anzahl empfangener Nachrichten liefern */
  188. assert( Idx > NONE );
  189. return My_Connections[Idx].msg_in;
  190. } /* Conn_RecvMsg */
  191. GLOBAL long
  192. Conn_RecvBytes( CONN_ID Idx )
  193. {
  194. /* Anzahl empfangener Bytes (unkomprimiert) liefern */
  195. assert( Idx > NONE );
  196. return My_Connections[Idx].bytes_in;
  197. } /* Conn_RecvBytes */
  198. GLOBAL void
  199. Conn_ResetWCounter( void )
  200. {
  201. WCounter = 0;
  202. } /* Conn_ResetWCounter */
  203. GLOBAL long
  204. Conn_WCounter( void )
  205. {
  206. return WCounter;
  207. } /* Conn_WCounter */
  208. /* -eof- */