]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
Debian: require "telnet" or "telnet-ssl" for building
[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 /**
121  * Reset the "penalty time" for one connection.
122  *
123  * @param Idx Connection index.
124  * @see Conn_SetPenalty
125  */
126 GLOBAL void
127 Conn_ResetPenalty(CONN_ID Idx)
128 {
129         assert(Idx > NONE);
130
131         My_Connections[Idx].delaytime = 0;
132 #ifdef DEBUG
133         Log(LOG_DEBUG, "Penalty time on connection %d has been reset.");
134 #endif
135 } /* Conn_ResetPenalty */
136
137
138 GLOBAL void
139 Conn_ClearFlags( void )
140 {
141         CONN_ID i;
142
143         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
144 } /* Conn_ClearFlags */
145
146
147 GLOBAL int
148 Conn_Flag( CONN_ID Idx )
149 {
150         assert( Idx > NONE );
151         return My_Connections[Idx].flag;
152 } /* Conn_Flag */
153
154
155 GLOBAL void
156 Conn_SetFlag( CONN_ID Idx, int Flag )
157 {
158         /* Connection markieren */
159
160         assert( Idx > NONE );
161         My_Connections[Idx].flag = Flag;
162 } /* Conn_SetFlag */
163
164
165 GLOBAL CONN_ID
166 Conn_First( void )
167 {
168         /* Connection-Struktur der ersten Verbindung liefern;
169          * Ist keine Verbindung vorhanden, wird NONE geliefert. */
170
171         CONN_ID i;
172         
173         for( i = 0; i < Pool_Size; i++ )
174         {
175                 if( My_Connections[i].sock != NONE ) return i;
176         }
177         return NONE;
178 } /* Conn_First */
179
180
181 GLOBAL CONN_ID
182 Conn_Next( CONN_ID Idx )
183 {
184         /* Naechste Verbindungs-Struktur liefern; existiert keine
185          * weitere, so wird NONE geliefert. */
186
187         CONN_ID i = NONE;
188
189         assert( Idx > NONE );
190         
191         for( i = Idx + 1; i < Pool_Size; i++ )
192         {
193                 if( My_Connections[i].sock != NONE ) return i;
194         }
195         return NONE;
196 } /* Conn_Next */
197
198
199 GLOBAL UINT16
200 Conn_Options( CONN_ID Idx )
201 {
202         assert( Idx > NONE );
203         return My_Connections[Idx].options;
204 } /* Conn_Options */
205
206
207 /**
208  * Set connection option.
209  */
210 GLOBAL void
211 Conn_SetOption(CONN_ID Idx, int Option)
212 {
213         assert(Idx > NONE);
214         Conn_OPTION_ADD(&My_Connections[Idx], Option);
215 } /* Conn_SetOption */
216
217
218 /**
219  * Get the start time of the connection.
220  * The result is the start time in seconds since 1970-01-01, as reported
221  * by the C function time(NULL).
222  */
223 GLOBAL time_t
224 Conn_StartTime( CONN_ID Idx )
225 {
226         CLIENT *c;
227
228         assert(Idx > NONE);
229
230         /* Search client structure for this link ... */
231         c = Conn_GetClient(Idx);
232         if(c != NULL)
233                 return Client_StartTime(c);
234
235         return 0;
236 } /* Conn_StartTime */
237
238 /**
239  * return number of bytes queued for writing
240  */
241 GLOBAL size_t
242 Conn_SendQ( CONN_ID Idx )
243 {
244         assert( Idx > NONE );
245 #ifdef ZLIB
246         if( My_Connections[Idx].options & CONN_ZIP )
247                 return array_bytes(&My_Connections[Idx].zip.wbuf);
248         else
249 #endif
250         return array_bytes(&My_Connections[Idx].wbuf);
251 } /* Conn_SendQ */
252
253
254 /**
255  * return number of messages sent on this connection so far
256  */
257 GLOBAL long
258 Conn_SendMsg( CONN_ID Idx )
259 {
260
261         assert( Idx > NONE );
262         return My_Connections[Idx].msg_out;
263 } /* Conn_SendMsg */
264
265
266 /**
267  * return number of (uncompressed) bytes sent
268  * on this connection so far
269  */
270 GLOBAL long
271 Conn_SendBytes( CONN_ID Idx )
272 {
273         assert( Idx > NONE );
274         return My_Connections[Idx].bytes_out;
275 } /* Conn_SendBytes */
276
277
278 /**
279  * return number of bytes pending in read buffer
280  */
281 GLOBAL size_t
282 Conn_RecvQ( CONN_ID Idx )
283 {
284         assert( Idx > NONE );
285 #ifdef ZLIB
286         if( My_Connections[Idx].options & CONN_ZIP )
287                 return array_bytes(&My_Connections[Idx].zip.rbuf);
288         else
289 #endif
290         return array_bytes(&My_Connections[Idx].rbuf);
291 } /* Conn_RecvQ */
292
293
294 /**
295  * return number of messages received on this connection so far
296  */
297 GLOBAL long
298 Conn_RecvMsg( CONN_ID Idx )
299 {
300         assert( Idx > NONE );
301         return My_Connections[Idx].msg_in;
302 } /* Conn_RecvMsg */
303
304
305 /**
306  * return number of (uncompressed) bytes received on this
307  * connection so far
308  */
309 GLOBAL long
310 Conn_RecvBytes( CONN_ID Idx )
311 {
312         assert( Idx > NONE );
313         return My_Connections[Idx].bytes_in;
314 } /* Conn_RecvBytes */
315
316 /**
317  * Return the remote IP address of this connection as string.
318  */
319 GLOBAL const char *
320 Conn_IPA(CONN_ID Idx)
321 {
322         assert (Idx > NONE);
323         return ng_ipaddr_tostr(&My_Connections[Idx].addr);
324 }
325
326
327 GLOBAL void
328 Conn_ResetWCounter( void )
329 {
330         WCounter = 0;
331 } /* Conn_ResetWCounter */
332
333
334 GLOBAL long
335 Conn_WCounter( void )
336 {
337         return WCounter;
338 } /* Conn_WCounter */
339
340
341 /* -eof- */