]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
Implement new configuration option "MaxPenaltyTime" (#251)
[ngircd-alex.git] / src / ngircd / conn-func.c
1 /*
2  * ngIRCd -- The Next Generation IRC Daemon
3  * Copyright (c)2001-2018 Alexander Barton (alex@barton.de) and Contributors.
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 <assert.h>
22 #include <time.h>
23
24 #ifdef DEBUG
25 # include "log.h"
26 #endif
27 #include "conn.h"
28
29 #include "conf.h"
30 #include "conn-func.h"
31
32 /**
33  * Update "idle timestamp", the time of the last visible user action
34  * (e. g. like sending messages, joining or leaving channels).
35  *
36  * @param Idx Connection index.
37  */
38 GLOBAL void
39 Conn_UpdateIdle(CONN_ID Idx)
40 {
41         assert(Idx > NONE);
42         My_Connections[Idx].lastprivmsg = time(NULL);
43 }
44
45 /**
46  * Update "ping timestamp", the time of the last outgoing PING request.
47  *
48  * @param Idx Connection index.
49  */
50 GLOBAL void
51 Conn_UpdatePing(CONN_ID Idx)
52 {
53         assert(Idx > NONE);
54         My_Connections[Idx].lastping = time(NULL);
55 }
56
57 /*
58  * Get signon time of a connection.
59  */
60 GLOBAL time_t
61 Conn_GetSignon(CONN_ID Idx)
62 {
63         assert(Idx > NONE);
64         return My_Connections[Idx].signon;
65 }
66
67 GLOBAL time_t
68 Conn_GetIdle( CONN_ID Idx )
69 {
70         /* Return Idle-Timer of a connetion */
71         assert( Idx > NONE );
72         return time( NULL ) - My_Connections[Idx].lastprivmsg;
73 } /* Conn_GetIdle */
74
75 GLOBAL time_t
76 Conn_LastPing( CONN_ID Idx )
77 {
78         assert( Idx > NONE );
79         return My_Connections[Idx].lastping;
80 } /* Conn_LastPing */
81
82 /**
83  * Add "penalty time" for a connection.
84  *
85  * During the "penalty time" the socket is ignored completely, no new data
86  * is read. This function only increases the penalty, it is not possible to
87  * decrease the penalty time.
88  *
89  * @param Idx Connection index.
90  * @param Seconds Seconds to add.
91  * @see Conn_ResetPenalty
92  */
93 GLOBAL void
94 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
95 {
96         time_t t;
97
98         assert(Idx > NONE);
99         assert(Seconds >= 0);
100
101         /* Limit new penalty to maximum configured, when less than 10 seconds. *
102            The latter is used to limit brute force attacks, therefore we don't *
103            want to limit that! */
104         if (Conf_MaxPenaltyTime >= 0
105             && Seconds > Conf_MaxPenaltyTime
106             && Seconds < 10)
107                 Seconds = Conf_MaxPenaltyTime;
108
109         t = time(NULL);
110         if (My_Connections[Idx].delaytime < t)
111                 My_Connections[Idx].delaytime = t;
112
113         My_Connections[Idx].delaytime += Seconds;
114
115 #ifdef DEBUG
116         Log(LOG_DEBUG,
117             "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
118             Idx, (long)Seconds, Seconds != 1 ? "s" : "",
119             My_Connections[Idx].delaytime - t,
120             My_Connections[Idx].delaytime - t != 1 ? "s" : "");
121 #endif
122 } /* Conn_SetPenalty */
123
124 GLOBAL void
125 Conn_ClearFlags( void )
126 {
127         CONN_ID i;
128
129         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
130 } /* Conn_ClearFlags */
131
132 GLOBAL int
133 Conn_Flag( CONN_ID Idx )
134 {
135         assert( Idx > NONE );
136         return My_Connections[Idx].flag;
137 } /* Conn_Flag */
138
139 GLOBAL void
140 Conn_SetFlag( CONN_ID Idx, int Flag )
141 {
142         assert( Idx > NONE );
143         My_Connections[Idx].flag = Flag;
144 } /* Conn_SetFlag */
145
146 GLOBAL CONN_ID
147 Conn_First( void )
148 {
149         CONN_ID i;
150
151         for( i = 0; i < Pool_Size; i++ )
152         {
153                 if( My_Connections[i].sock != NONE ) return i;
154         }
155         return NONE;
156 } /* Conn_First */
157
158 GLOBAL CONN_ID
159 Conn_Next( CONN_ID Idx )
160 {
161         CONN_ID i = NONE;
162
163         assert( Idx > NONE );
164
165         for( i = Idx + 1; i < Pool_Size; i++ )
166         {
167                 if( My_Connections[i].sock != NONE ) return i;
168         }
169         return NONE;
170 } /* Conn_Next */
171
172 GLOBAL UINT16
173 Conn_Options( CONN_ID Idx )
174 {
175         assert( Idx > NONE );
176         return My_Connections[Idx].options;
177 } /* Conn_Options */
178
179 /**
180  * Set connection option.
181  */
182 GLOBAL void
183 Conn_SetOption(CONN_ID Idx, int Option)
184 {
185         assert(Idx > NONE);
186         Conn_OPTION_ADD(&My_Connections[Idx], Option);
187 } /* Conn_SetOption */
188
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 )
196 {
197         CLIENT *c;
198
199         assert(Idx > NONE);
200
201         /* Search client structure for this link ... */
202         c = Conn_GetClient(Idx);
203         if(c != NULL)
204                 return Client_StartTime(c);
205
206         return 0;
207 } /* Conn_StartTime */
208
209 /**
210  * return number of bytes queued for writing
211  */
212 GLOBAL size_t
213 Conn_SendQ( CONN_ID Idx )
214 {
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 */
223
224 /**
225  * return number of messages sent on this connection so far
226  */
227 GLOBAL long
228 Conn_SendMsg( CONN_ID Idx )
229 {
230
231         assert( Idx > NONE );
232         return My_Connections[Idx].msg_out;
233 } /* Conn_SendMsg */
234
235 /**
236  * return number of (uncompressed) bytes sent
237  * on this connection so far
238  */
239 GLOBAL long
240 Conn_SendBytes( CONN_ID Idx )
241 {
242         assert( Idx > NONE );
243         return My_Connections[Idx].bytes_out;
244 } /* Conn_SendBytes */
245
246 /**
247  * return number of bytes pending in read buffer
248  */
249 GLOBAL size_t
250 Conn_RecvQ( CONN_ID Idx )
251 {
252         assert( Idx > NONE );
253 #ifdef ZLIB
254         if( My_Connections[Idx].options & CONN_ZIP )
255                 return array_bytes(&My_Connections[Idx].zip.rbuf);
256         else
257 #endif
258         return array_bytes(&My_Connections[Idx].rbuf);
259 } /* Conn_RecvQ */
260
261 /**
262  * return number of messages received on this connection so far
263  */
264 GLOBAL long
265 Conn_RecvMsg( CONN_ID Idx )
266 {
267         assert( Idx > NONE );
268         return My_Connections[Idx].msg_in;
269 } /* Conn_RecvMsg */
270
271 /**
272  * return number of (uncompressed) bytes received on this
273  * connection so far
274  */
275 GLOBAL long
276 Conn_RecvBytes( CONN_ID Idx )
277 {
278         assert( Idx > NONE );
279         return My_Connections[Idx].bytes_in;
280 } /* Conn_RecvBytes */
281
282 /**
283  * Return the remote IP address of this connection as string.
284  */
285 GLOBAL const char *
286 Conn_IPA(CONN_ID Idx)
287 {
288         assert (Idx > NONE);
289         return ng_ipaddr_tostr(&My_Connections[Idx].addr);
290 }
291
292 GLOBAL void
293 Conn_ResetWCounter( void )
294 {
295         WCounter = 0;
296 } /* Conn_ResetWCounter */
297
298 GLOBAL long
299 Conn_WCounter( void )
300 {
301         return WCounter;
302 } /* Conn_WCounter */
303
304 /* -eof- */