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