]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
92da1428c0a32ebe05353269bd0d7621758359f0
[ngircd-alex.git] / src / ngircd / conn-func.c
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
14
15 #define CONN_MODULE
16
17 #include "portab.h"
18
19 static char UNUSED id[] = "$Id: conn-func.c,v 1.11 2007/10/04 15:03:56 alex Exp $";
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <log.h>
24
25 #include "conn.h"
26 #include "client.h"
27
28 #include "exp.h"
29 #include "conn-func.h"
30
31
32 GLOBAL void
33 Conn_UpdateIdle( CONN_ID Idx )
34 {
35         /* Idle-Timer zuruecksetzen */
36
37         assert( Idx > NONE );
38         My_Connections[Idx].lastprivmsg = time( NULL );
39 }
40
41
42 /*
43  * Get signon time of a connection.
44  */
45 GLOBAL time_t
46 Conn_GetSignon(CONN_ID Idx)
47 {
48         assert(Idx > NONE);
49         return My_Connections[Idx].signon;
50 }
51
52 GLOBAL time_t
53 Conn_GetIdle( CONN_ID Idx )
54 {
55         /* Idle-Time einer Verbindung liefern (in Sekunden) */
56
57         assert( Idx > NONE );
58         return time( NULL ) - My_Connections[Idx].lastprivmsg;
59 } /* Conn_GetIdle */
60
61
62 GLOBAL time_t
63 Conn_LastPing( CONN_ID Idx )
64 {
65         /* Zeitpunkt des letzten PING liefern */
66
67         assert( Idx > NONE );
68         return My_Connections[Idx].lastping;
69 } /* Conn_LastPing */
70
71
72 GLOBAL void
73 Conn_SetPenalty( CONN_ID Idx, time_t Seconds )
74 {
75         /* Penalty-Delay fuer eine Verbindung (in Sekunden) setzen;
76          * waehrend dieser Zeit wird der entsprechende Socket vom Server
77          * bei Lese-Operationen komplett ignoriert. Der Delay kann mit
78          * dieser Funktion nur erhoeht, nicht aber verringert werden. */
79         
80         time_t t;
81         
82         assert( Idx > NONE );
83         assert( Seconds >= 0 );
84
85         t = time( NULL ) + Seconds;
86         if (t > My_Connections[Idx].delaytime)
87                 My_Connections[Idx].delaytime = t;
88
89 #ifdef DEBUG
90         Log(LOG_DEBUG, "Add penalty time on connection %d: %ld second(s).",
91                         Idx, (long)Seconds);
92 #endif
93 } /* Conn_SetPenalty */
94
95
96 GLOBAL void
97 Conn_ResetPenalty( CONN_ID Idx )
98 {
99         assert( Idx > NONE );
100         My_Connections[Idx].delaytime = 0;
101 } /* Conn_ResetPenalty */
102
103
104 GLOBAL void
105 Conn_ClearFlags( void )
106 {
107         /* Alle Connection auf "nicht-markiert" setzen */
108
109         CONN_ID i;
110
111         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
112 } /* Conn_ClearFlags */
113
114
115 GLOBAL int
116 Conn_Flag( CONN_ID Idx )
117 {
118         /* Ist eine Connection markiert (true) oder nicht? */
119
120         assert( Idx > NONE );
121         return My_Connections[Idx].flag;
122 } /* Conn_Flag */
123
124
125 GLOBAL void
126 Conn_SetFlag( CONN_ID Idx, int Flag )
127 {
128         /* Connection markieren */
129
130         assert( Idx > NONE );
131         My_Connections[Idx].flag = Flag;
132 } /* Conn_SetFlag */
133
134
135 GLOBAL CONN_ID
136 Conn_First( void )
137 {
138         /* Connection-Struktur der ersten Verbindung liefern;
139          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
140
141         CONN_ID i;
142         
143         for( i = 0; i < Pool_Size; i++ )
144         {
145                 if( My_Connections[i].sock != NONE ) return i;
146         }
147         return NONE;
148 } /* Conn_First */
149
150
151 GLOBAL CONN_ID
152 Conn_Next( CONN_ID Idx )
153 {
154         /* Naechste Verbindungs-Struktur liefern; existiert keine
155          * weitere, so wird NONE geliefert. */
156
157         CONN_ID i = NONE;
158
159         assert( Idx > NONE );
160         
161         for( i = Idx + 1; i < Pool_Size; i++ )
162         {
163                 if( My_Connections[i].sock != NONE ) return i;
164         }
165         return NONE;
166 } /* Conn_Next */
167
168
169 GLOBAL UINT16
170 Conn_Options( CONN_ID Idx )
171 {
172         assert( Idx > NONE );
173         return My_Connections[Idx].options;
174 } /* Conn_Options */
175
176
177 /**
178  * Get the start time of the connection.
179  * The result is the start time in seconds since 1970-01-01, as reported
180  * by the C function time(NULL).
181  */
182 GLOBAL time_t
183 Conn_StartTime( CONN_ID Idx )
184 {
185         CLIENT *c;
186
187         assert(Idx > NONE);
188
189         /* Search client structure for this link ... */
190         c = Conn_GetClient(Idx);
191         if(c != NULL)
192                 return Client_StartTime(c);
193
194         return 0;
195 } /* Conn_StartTime */
196
197
198 GLOBAL size_t
199 Conn_SendQ( CONN_ID Idx )
200 {
201         /* Laenge der Daten im Schreibbuffer liefern */
202
203         assert( Idx > NONE );
204 #ifdef ZLIB
205         if( My_Connections[Idx].options & CONN_ZIP )
206                 return array_bytes(&My_Connections[Idx].zip.wbuf);
207         else
208 #endif
209         return array_bytes(&My_Connections[Idx].wbuf);
210 } /* Conn_SendQ */
211
212
213 GLOBAL long
214 Conn_SendMsg( CONN_ID Idx )
215 {
216         /* Anzahl gesendeter Nachrichten liefern */
217
218         assert( Idx > NONE );
219         return My_Connections[Idx].msg_out;
220 } /* Conn_SendMsg */
221
222
223 GLOBAL long
224 Conn_SendBytes( CONN_ID Idx )
225 {
226         /* Anzahl gesendeter Bytes (unkomprimiert) liefern */
227
228         assert( Idx > NONE );
229         return My_Connections[Idx].bytes_out;
230 } /* Conn_SendBytes */
231
232
233 GLOBAL size_t
234 Conn_RecvQ( CONN_ID Idx )
235 {
236         /* Laenge der Daten im Lesebuffer liefern */
237
238         assert( Idx > NONE );
239 #ifdef ZLIB
240         if( My_Connections[Idx].options & CONN_ZIP )
241                 return array_bytes(&My_Connections[Idx].zip.rbuf);
242         else
243 #endif
244         return array_bytes(&My_Connections[Idx].rbuf);
245 } /* Conn_RecvQ */
246
247
248 GLOBAL long
249 Conn_RecvMsg( CONN_ID Idx )
250 {
251         /* Anzahl empfangener Nachrichten liefern */
252
253         assert( Idx > NONE );
254         return My_Connections[Idx].msg_in;
255 } /* Conn_RecvMsg */
256
257
258 GLOBAL long
259 Conn_RecvBytes( CONN_ID Idx )
260 {
261         /* Anzahl empfangener Bytes (unkomprimiert) liefern */
262
263         assert( Idx > NONE );
264         return My_Connections[Idx].bytes_in;
265 } /* Conn_RecvBytes */
266
267
268 GLOBAL void
269 Conn_ResetWCounter( void )
270 {
271         WCounter = 0;
272 } /* Conn_ResetWCounter */
273
274
275 GLOBAL long
276 Conn_WCounter( void )
277 {
278         return WCounter;
279 } /* Conn_WCounter */
280
281
282 /* -eof- */