conn-func.c 5.4 KB

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