Blob


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 */
15 #define CONN_MODULE
17 #include "portab.h"
19 static char UNUSED id[] = "$Id: conn-func.c,v 1.12 2008/03/11 14:05:27 alex Exp $";
21 #include "imp.h"
22 #include <assert.h>
23 #include <string.h>
24 #include "log.h"
26 #include "conn.h"
27 #include "client.h"
29 #include "exp.h"
30 #include "conn-func.h"
33 GLOBAL void
34 Conn_UpdateIdle( CONN_ID Idx )
35 {
36 /* Idle-Timer zuruecksetzen */
38 assert( Idx > NONE );
39 My_Connections[Idx].lastprivmsg = time( NULL );
40 }
43 /*
44 * Get signon time of a connection.
45 */
46 GLOBAL time_t
47 Conn_GetSignon(CONN_ID Idx)
48 {
49 assert(Idx > NONE);
50 return My_Connections[Idx].signon;
51 }
53 GLOBAL time_t
54 Conn_GetIdle( CONN_ID Idx )
55 {
56 /* Idle-Time einer Verbindung liefern (in Sekunden) */
58 assert( Idx > NONE );
59 return time( NULL ) - My_Connections[Idx].lastprivmsg;
60 } /* Conn_GetIdle */
63 GLOBAL time_t
64 Conn_LastPing( CONN_ID Idx )
65 {
66 /* Zeitpunkt des letzten PING liefern */
68 assert( Idx > NONE );
69 return My_Connections[Idx].lastping;
70 } /* Conn_LastPing */
73 GLOBAL void
74 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
75 {
76 /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
77 * waehrend dieser Zeit wird der entsprechende Socket vom Server
78 * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
79 * dieser Funktion nur erhoeht, nicht aber verringert werden. */
81 time_t t;
83 assert( Idx > NONE );
84 assert( Seconds >= 0 );
86 t = time( NULL ) + Seconds;
87 if (t > My_Connections[Idx].delaytime)
88 My_Connections[Idx].delaytime = t;
90 #ifdef DEBUG
91 Log(LOG_DEBUG, "Add penalty time on connection %d: %ld second(s).",
92 Idx, (long)Seconds);
93 #endif
94 } /* Conn_SetPenalty */
97 GLOBAL void
98 Conn_ResetPenalty( CONN_ID Idx )
99 {
100 assert( Idx > NONE );
101 My_Connections[Idx].delaytime = 0;
102 } /* Conn_ResetPenalty */
105 GLOBAL void
106 Conn_ClearFlags( void )
108 /* Alle Connection auf "nicht-markiert" setzen */
110 CONN_ID i;
112 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
113 } /* Conn_ClearFlags */
116 GLOBAL int
117 Conn_Flag( CONN_ID Idx )
119 /* Ist eine Connection markiert (true) oder nicht? */
121 assert( Idx > NONE );
122 return My_Connections[Idx].flag;
123 } /* Conn_Flag */
126 GLOBAL void
127 Conn_SetFlag( CONN_ID Idx, int Flag )
129 /* Connection markieren */
131 assert( Idx > NONE );
132 My_Connections[Idx].flag = Flag;
133 } /* Conn_SetFlag */
136 GLOBAL CONN_ID
137 Conn_First( void )
139 /* Connection-Struktur der ersten Verbindung liefern;
140 * Ist keine Verbindung vorhanden, wird NONE geliefert. */
142 CONN_ID i;
144 for( i = 0; i < Pool_Size; i++ )
146 if( My_Connections[i].sock != NONE ) return i;
148 return NONE;
149 } /* Conn_First */
152 GLOBAL CONN_ID
153 Conn_Next( CONN_ID Idx )
155 /* Naechste Verbindungs-Struktur liefern; existiert keine
156 * weitere, so wird NONE geliefert. */
158 CONN_ID i = NONE;
160 assert( Idx > NONE );
162 for( i = Idx + 1; i < Pool_Size; i++ )
164 if( My_Connections[i].sock != NONE ) return i;
166 return NONE;
167 } /* Conn_Next */
170 GLOBAL UINT16
171 Conn_Options( CONN_ID Idx )
173 assert( Idx > NONE );
174 return My_Connections[Idx].options;
175 } /* Conn_Options */
178 /**
179 * Set connection option.
180 */
181 GLOBAL void
182 Conn_SetOption(CONN_ID Idx, int Option)
184 assert(Idx > NONE);
185 Conn_OPTION_ADD(&My_Connections[Idx], Option);
186 } /* Conn_SetOption */
189 /**
190 * Get the start time of the connection.
191 * The result is the start time in seconds since 1970-01-01, as reported
192 * by the C function time(NULL).
193 */
194 GLOBAL time_t
195 Conn_StartTime( CONN_ID Idx )
197 CLIENT *c;
199 assert(Idx > NONE);
201 /* Search client structure for this link ... */
202 c = Conn_GetClient(Idx);
203 if(c != NULL)
204 return Client_StartTime(c);
206 return 0;
207 } /* Conn_StartTime */
210 GLOBAL size_t
211 Conn_SendQ( CONN_ID Idx )
213 /* Laenge der Daten im Schreibbuffer liefern */
215 assert( Idx > NONE );
216 #ifdef ZLIB
217 if( My_Connections[Idx].options & CONN_ZIP )
218 return array_bytes(&My_Connections[Idx].zip.wbuf);
219 else
220 #endif
221 return array_bytes(&My_Connections[Idx].wbuf);
222 } /* Conn_SendQ */
225 GLOBAL long
226 Conn_SendMsg( CONN_ID Idx )
228 /* Anzahl gesendeter Nachrichten liefern */
230 assert( Idx > NONE );
231 return My_Connections[Idx].msg_out;
232 } /* Conn_SendMsg */
235 GLOBAL long
236 Conn_SendBytes( CONN_ID Idx )
238 /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
240 assert( Idx > NONE );
241 return My_Connections[Idx].bytes_out;
242 } /* Conn_SendBytes */
245 GLOBAL size_t
246 Conn_RecvQ( CONN_ID Idx )
248 /* Laenge der Daten im Lesebuffer liefern */
250 assert( Idx > NONE );
251 #ifdef ZLIB
252 if( My_Connections[Idx].options & CONN_ZIP )
253 return array_bytes(&My_Connections[Idx].zip.rbuf);
254 else
255 #endif
256 return array_bytes(&My_Connections[Idx].rbuf);
257 } /* Conn_RecvQ */
260 GLOBAL long
261 Conn_RecvMsg( CONN_ID Idx )
263 /* Anzahl empfangener Nachrichten liefern */
265 assert( Idx > NONE );
266 return My_Connections[Idx].msg_in;
267 } /* Conn_RecvMsg */
270 GLOBAL long
271 Conn_RecvBytes( CONN_ID Idx )
273 /* Anzahl empfangener Bytes (unkomprimiert) liefern */
275 assert( Idx > NONE );
276 return My_Connections[Idx].bytes_in;
277 } /* Conn_RecvBytes */
280 GLOBAL void
281 Conn_ResetWCounter( void )
283 WCounter = 0;
284 } /* Conn_ResetWCounter */
287 GLOBAL long
288 Conn_WCounter( void )
290 return WCounter;
291 } /* Conn_WCounter */
294 /* -eof- */