2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2018 Alexander Barton (alex@barton.de) and Contributors.
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.
18 * Connection management: Global functions
27 #include "conn-func.h"
30 * Update "idle timestamp", the time of the last visible user action
31 * (e. g. like sending messages, joining or leaving channels).
33 * @param Idx Connection index.
36 Conn_UpdateIdle(CONN_ID Idx)
39 My_Connections[Idx].lastprivmsg = time(NULL);
43 * Update "ping timestamp", the time of the last outgoing PING request.
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.
48 * @param Idx Connection index.
49 * @param TimeStamp 0, 1, or time stamp.
52 Conn_UpdatePing(CONN_ID Idx, time_t TimeStamp)
55 My_Connections[Idx].lastping = TimeStamp;
59 * Get signon time of a connection.
62 Conn_GetSignon(CONN_ID Idx)
65 return My_Connections[Idx].signon;
69 Conn_GetIdle( CONN_ID Idx )
71 /* Return Idle-Timer of a connection */
73 return time( NULL ) - My_Connections[Idx].lastprivmsg;
77 Conn_LastPing( CONN_ID Idx )
80 return My_Connections[Idx].lastping;
84 * Add "penalty time" for a connection.
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.
90 * @param Idx Connection index.
91 * @param Seconds Seconds to add.
92 * @see Conn_ResetPenalty
95 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
100 assert(Seconds >= 0);
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
108 Seconds = Conf_MaxPenaltyTime;
111 if (My_Connections[Idx].delaytime < t)
112 My_Connections[Idx].delaytime = t;
114 My_Connections[Idx].delaytime += Seconds;
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 */
123 Conn_ClearFlags( void )
127 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
128 } /* Conn_ClearFlags */
131 Conn_Flag( CONN_ID Idx )
133 assert( Idx > NONE );
134 return My_Connections[Idx].flag;
138 Conn_SetFlag( CONN_ID Idx, int Flag )
140 assert( Idx > NONE );
141 My_Connections[Idx].flag = Flag;
149 for( i = 0; i < Pool_Size; i++ )
151 if( My_Connections[i].sock != NONE ) return i;
157 Conn_Next( CONN_ID Idx )
161 assert( Idx > NONE );
163 for( i = Idx + 1; i < Pool_Size; i++ )
165 if( My_Connections[i].sock != NONE ) return i;
171 Conn_Options( CONN_ID Idx )
173 assert( Idx > NONE );
174 return My_Connections[Idx].options;
178 * Set connection option.
181 Conn_SetOption(CONN_ID Idx, int Option)
184 Conn_OPTION_ADD(&My_Connections[Idx], Option);
185 } /* Conn_SetOption */
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).
193 Conn_StartTime( CONN_ID Idx )
199 /* Search client structure for this link ... */
200 c = Conn_GetClient(Idx);
202 return Client_StartTime(c);
205 } /* Conn_StartTime */
208 * return number of bytes queued for writing
211 Conn_SendQ( CONN_ID Idx )
213 assert( Idx > NONE );
215 if( My_Connections[Idx].options & CONN_ZIP )
216 return array_bytes(&My_Connections[Idx].zip.wbuf);
219 return array_bytes(&My_Connections[Idx].wbuf);
223 * return number of messages sent on this connection so far
226 Conn_SendMsg( CONN_ID Idx )
229 assert( Idx > NONE );
230 return My_Connections[Idx].msg_out;
234 * return number of (uncompressed) bytes sent
235 * on this connection so far
238 Conn_SendBytes( CONN_ID Idx )
240 assert( Idx > NONE );
241 return My_Connections[Idx].bytes_out;
242 } /* Conn_SendBytes */
245 * return number of bytes pending in read buffer
248 Conn_RecvQ( CONN_ID Idx )
250 assert( Idx > NONE );
252 if( My_Connections[Idx].options & CONN_ZIP )
253 return array_bytes(&My_Connections[Idx].zip.rbuf);
256 return array_bytes(&My_Connections[Idx].rbuf);
260 * return number of messages received on this connection so far
263 Conn_RecvMsg( CONN_ID Idx )
265 assert( Idx > NONE );
266 return My_Connections[Idx].msg_in;
270 * return number of (uncompressed) bytes received on this
274 Conn_RecvBytes( CONN_ID Idx )
276 assert( Idx > NONE );
277 return My_Connections[Idx].bytes_in;
278 } /* Conn_RecvBytes */
281 * Return the remote IP address of this connection as string.
284 Conn_IPA(CONN_ID Idx)
287 return ng_ipaddr_tostr(&My_Connections[Idx].addr);
291 Conn_ResetWCounter( void )
294 } /* Conn_ResetWCounter */
297 Conn_WCounter( void )
300 } /* Conn_WCounter */