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
30 #include "conn-func.h"
33 * Update "idle timestamp", the time of the last visible user action
34 * (e. g. like sending messages, joining or leaving channels).
36 * @param Idx Connection index.
39 Conn_UpdateIdle(CONN_ID Idx)
42 My_Connections[Idx].lastprivmsg = time(NULL);
46 * Update "ping timestamp", the time of the last outgoing PING request.
48 * @param Idx Connection index.
51 Conn_UpdatePing(CONN_ID Idx)
54 My_Connections[Idx].lastping = time(NULL);
58 * Get signon time of a connection.
61 Conn_GetSignon(CONN_ID Idx)
64 return My_Connections[Idx].signon;
68 Conn_GetIdle( CONN_ID Idx )
70 /* Return Idle-Timer of a connetion */
72 return time( NULL ) - My_Connections[Idx].lastprivmsg;
76 Conn_LastPing( CONN_ID Idx )
79 return My_Connections[Idx].lastping;
83 * Add "penalty time" for a connection.
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.
89 * @param Idx Connection index.
90 * @param Seconds Seconds to add.
91 * @see Conn_ResetPenalty
94 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
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
107 Seconds = Conf_MaxPenaltyTime;
110 if (My_Connections[Idx].delaytime < t)
111 My_Connections[Idx].delaytime = t;
113 My_Connections[Idx].delaytime += Seconds;
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" : "");
122 } /* Conn_SetPenalty */
125 Conn_ClearFlags( void )
129 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
130 } /* Conn_ClearFlags */
133 Conn_Flag( CONN_ID Idx )
135 assert( Idx > NONE );
136 return My_Connections[Idx].flag;
140 Conn_SetFlag( CONN_ID Idx, int Flag )
142 assert( Idx > NONE );
143 My_Connections[Idx].flag = Flag;
151 for( i = 0; i < Pool_Size; i++ )
153 if( My_Connections[i].sock != NONE ) return i;
159 Conn_Next( CONN_ID Idx )
163 assert( Idx > NONE );
165 for( i = Idx + 1; i < Pool_Size; i++ )
167 if( My_Connections[i].sock != NONE ) return i;
173 Conn_Options( CONN_ID Idx )
175 assert( Idx > NONE );
176 return My_Connections[Idx].options;
180 * Set connection option.
183 Conn_SetOption(CONN_ID Idx, int Option)
186 Conn_OPTION_ADD(&My_Connections[Idx], Option);
187 } /* Conn_SetOption */
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).
195 Conn_StartTime( CONN_ID Idx )
201 /* Search client structure for this link ... */
202 c = Conn_GetClient(Idx);
204 return Client_StartTime(c);
207 } /* Conn_StartTime */
210 * return number of bytes queued for writing
213 Conn_SendQ( CONN_ID Idx )
215 assert( Idx > NONE );
217 if( My_Connections[Idx].options & CONN_ZIP )
218 return array_bytes(&My_Connections[Idx].zip.wbuf);
221 return array_bytes(&My_Connections[Idx].wbuf);
225 * return number of messages sent on this connection so far
228 Conn_SendMsg( CONN_ID Idx )
231 assert( Idx > NONE );
232 return My_Connections[Idx].msg_out;
236 * return number of (uncompressed) bytes sent
237 * on this connection so far
240 Conn_SendBytes( CONN_ID Idx )
242 assert( Idx > NONE );
243 return My_Connections[Idx].bytes_out;
244 } /* Conn_SendBytes */
247 * return number of bytes pending in read buffer
250 Conn_RecvQ( CONN_ID Idx )
252 assert( Idx > NONE );
254 if( My_Connections[Idx].options & CONN_ZIP )
255 return array_bytes(&My_Connections[Idx].zip.rbuf);
258 return array_bytes(&My_Connections[Idx].rbuf);
262 * return number of messages received on this connection so far
265 Conn_RecvMsg( CONN_ID Idx )
267 assert( Idx > NONE );
268 return My_Connections[Idx].msg_in;
272 * return number of (uncompressed) bytes received on this
276 Conn_RecvBytes( CONN_ID Idx )
278 assert( Idx > NONE );
279 return My_Connections[Idx].bytes_in;
280 } /* Conn_RecvBytes */
283 * Return the remote IP address of this connection as string.
286 Conn_IPA(CONN_ID Idx)
289 return ng_ipaddr_tostr(&My_Connections[Idx].addr);
293 Conn_ResetWCounter( void )
296 } /* Conn_ResetWCounter */
299 Conn_WCounter( void )
302 } /* Conn_WCounter */