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