]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
Conn_UpdateIdle(): Code cleanup
[ngircd-alex.git] / src / ngircd / conn-func.c
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
12 #define CONN_MODULE
13
14 #include "portab.h"
15
16 /**
17  * @file
18  * Connection management: Global functions
19  */
20
21 #include "imp.h"
22 #include <assert.h>
23 #include <string.h>
24 #include "log.h"
25
26 #include "conn.h"
27 #include "client.h"
28
29 #include "exp.h"
30 #include "conn-func.h"
31
32
33 /**
34  * Update "idle timestamp", the time of the last visible user action
35  * (e. g. like sending messages, joining or leaving channels).
36  *
37  * @param Idx Connection index.
38  */
39 GLOBAL void
40 Conn_UpdateIdle(CONN_ID Idx)
41 {
42         assert(Idx > NONE);
43         My_Connections[Idx].lastprivmsg = time(NULL);
44 }
45
46
47 /*
48  * Get signon time of a connection.
49  */
50 GLOBAL time_t
51 Conn_GetSignon(CONN_ID Idx)
52 {
53         assert(Idx > NONE);
54         return My_Connections[Idx].signon;
55 }
56
57 GLOBAL time_t
58 Conn_GetIdle( CONN_ID Idx )
59 {
60         /* Return Idle-Timer of a connetion */
61         assert( Idx > NONE );
62         return time( NULL ) - My_Connections[Idx].lastprivmsg;
63 } /* Conn_GetIdle */
64
65
66 GLOBAL time_t
67 Conn_LastPing( CONN_ID Idx )
68 {
69         assert( Idx > NONE );
70         return My_Connections[Idx].lastping;
71 } /* Conn_LastPing */
72
73
74 /**
75  * Add "penalty time" for a connection.
76  *
77  * During the "penalty time" the socket is ignored completely, no new data
78  * is read. This function only increases the penalty, it is not possible to
79  * decrease the penalty time.
80  *
81  * @param Idex Connection index.
82  * @param Seconds Seconds to add.
83  * @see Conn_ResetPenalty
84  */
85 GLOBAL void
86 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
87 {
88         time_t t;
89
90         assert(Idx > NONE);
91         assert(Seconds >= 0);
92
93         t = time(NULL);
94         if (My_Connections[Idx].delaytime < t)
95                 My_Connections[Idx].delaytime = t;
96
97         My_Connections[Idx].delaytime += Seconds;
98
99 #ifdef DEBUG
100         Log(LOG_DEBUG,
101             "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
102             Idx, (long)Seconds, Seconds != 1 ? "s" : "",
103             My_Connections[Idx].delaytime - t,
104             My_Connections[Idx].delaytime - t != 1 ? "s" : "");
105 #endif
106 } /* Conn_SetPenalty */
107
108
109 /**
110  * Reset the "penalty time" for one connection.
111  *
112  * @param Idx Connection index.
113  * @see Conn_SetPenalty
114  */
115 GLOBAL void
116 Conn_ResetPenalty(CONN_ID Idx)
117 {
118         assert(Idx > NONE);
119
120         My_Connections[Idx].delaytime = 0;
121 #ifdef DEBUG
122         Log(LOG_DEBUG, "Penalty time on connection %d has been reset.");
123 #endif
124 } /* Conn_ResetPenalty */
125
126
127 GLOBAL void
128 Conn_ClearFlags( void )
129 {
130         CONN_ID i;
131
132         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
133 } /* Conn_ClearFlags */
134
135
136 GLOBAL int
137 Conn_Flag( CONN_ID Idx )
138 {
139         assert( Idx > NONE );
140         return My_Connections[Idx].flag;
141 } /* Conn_Flag */
142
143
144 GLOBAL void
145 Conn_SetFlag( CONN_ID Idx, int Flag )
146 {
147         /* Connection markieren */
148
149         assert( Idx > NONE );
150         My_Connections[Idx].flag = Flag;
151 } /* Conn_SetFlag */
152
153
154 GLOBAL CONN_ID
155 Conn_First( void )
156 {
157         /* Connection-Struktur der ersten Verbindung liefern;
158          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
159
160         CONN_ID i;
161         
162         for( i = 0; i < Pool_Size; i++ )
163         {
164                 if( My_Connections[i].sock != NONE ) return i;
165         }
166         return NONE;
167 } /* Conn_First */
168
169
170 GLOBAL CONN_ID
171 Conn_Next( CONN_ID Idx )
172 {
173         /* Naechste Verbindungs-Struktur liefern; existiert keine
174          * weitere, so wird NONE geliefert. */
175
176         CONN_ID i = NONE;
177
178         assert( Idx > NONE );
179         
180         for( i = Idx + 1; i < Pool_Size; i++ )
181         {
182                 if( My_Connections[i].sock != NONE ) return i;
183         }
184         return NONE;
185 } /* Conn_Next */
186
187
188 GLOBAL UINT16
189 Conn_Options( CONN_ID Idx )
190 {
191         assert( Idx > NONE );
192         return My_Connections[Idx].options;
193 } /* Conn_Options */
194
195
196 /**
197  * Set connection option.
198  */
199 GLOBAL void
200 Conn_SetOption(CONN_ID Idx, int Option)
201 {
202         assert(Idx > NONE);
203         Conn_OPTION_ADD(&My_Connections[Idx], Option);
204 } /* Conn_SetOption */
205
206
207 /**
208  * Get the start time of the connection.
209  * The result is the start time in seconds since 1970-01-01, as reported
210  * by the C function time(NULL).
211  */
212 GLOBAL time_t
213 Conn_StartTime( CONN_ID Idx )
214 {
215         CLIENT *c;
216
217         assert(Idx > NONE);
218
219         /* Search client structure for this link ... */
220         c = Conn_GetClient(Idx);
221         if(c != NULL)
222                 return Client_StartTime(c);
223
224         return 0;
225 } /* Conn_StartTime */
226
227 /**
228  * return number of bytes queued for writing
229  */
230 GLOBAL size_t
231 Conn_SendQ( CONN_ID Idx )
232 {
233         assert( Idx > NONE );
234 #ifdef ZLIB
235         if( My_Connections[Idx].options & CONN_ZIP )
236                 return array_bytes(&My_Connections[Idx].zip.wbuf);
237         else
238 #endif
239         return array_bytes(&My_Connections[Idx].wbuf);
240 } /* Conn_SendQ */
241
242
243 /**
244  * return number of messages sent on this connection so far
245  */
246 GLOBAL long
247 Conn_SendMsg( CONN_ID Idx )
248 {
249
250         assert( Idx > NONE );
251         return My_Connections[Idx].msg_out;
252 } /* Conn_SendMsg */
253
254
255 /**
256  * return number of (uncompressed) bytes sent
257  * on this connection so far
258  */
259 GLOBAL long
260 Conn_SendBytes( CONN_ID Idx )
261 {
262         assert( Idx > NONE );
263         return My_Connections[Idx].bytes_out;
264 } /* Conn_SendBytes */
265
266
267 /**
268  * return number of bytes pending in read buffer
269  */
270 GLOBAL size_t
271 Conn_RecvQ( CONN_ID Idx )
272 {
273         assert( Idx > NONE );
274 #ifdef ZLIB
275         if( My_Connections[Idx].options & CONN_ZIP )
276                 return array_bytes(&My_Connections[Idx].zip.rbuf);
277         else
278 #endif
279         return array_bytes(&My_Connections[Idx].rbuf);
280 } /* Conn_RecvQ */
281
282
283 /**
284  * return number of messages received on this connection so far
285  */
286 GLOBAL long
287 Conn_RecvMsg( CONN_ID Idx )
288 {
289         assert( Idx > NONE );
290         return My_Connections[Idx].msg_in;
291 } /* Conn_RecvMsg */
292
293
294 /**
295  * return number of (uncompressed) bytes received on this
296  * connection so far
297  */
298 GLOBAL long
299 Conn_RecvBytes( CONN_ID Idx )
300 {
301         assert( Idx > NONE );
302         return My_Connections[Idx].bytes_in;
303 } /* Conn_RecvBytes */
304
305 /**
306  * Return the remote IP address of this connection as string.
307  */
308 GLOBAL const char *
309 Conn_IPA(CONN_ID Idx)
310 {
311         assert (Idx > NONE);
312         return ng_ipaddr_tostr(&My_Connections[Idx].addr);
313 }
314
315
316 GLOBAL void
317 Conn_ResetWCounter( void )
318 {
319         WCounter = 0;
320 } /* Conn_ResetWCounter */
321
322
323 GLOBAL long
324 Conn_WCounter( void )
325 {
326         return WCounter;
327 } /* Conn_WCounter */
328
329
330 /* -eof- */