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 * the value 0 signals a newly connected client including servers during the
49 * initial "server burst"; and 1 means that no PONG is pending for a PING.
51 * @param Idx Connection index.
52 * @param TimeStamp 0, 1, or time stamp.
55 Conn_UpdatePing(CONN_ID Idx, time_t TimeStamp)
58 My_Connections[Idx].lastping = TimeStamp;
62 * Get signon time of a connection.
65 Conn_GetSignon(CONN_ID Idx)
68 return My_Connections[Idx].signon;
72 Conn_GetIdle( CONN_ID Idx )
74 /* Return Idle-Timer of a connetion */
76 return time( NULL ) - My_Connections[Idx].lastprivmsg;
80 Conn_LastPing( CONN_ID Idx )
83 return My_Connections[Idx].lastping;
87 * Add "penalty time" for a connection.
89 * During the "penalty time" the socket is ignored completely, no new data
90 * is read. This function only increases the penalty, it is not possible to
91 * decrease the penalty time.
93 * @param Idx Connection index.
94 * @param Seconds Seconds to add.
95 * @see Conn_ResetPenalty
98 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
103 assert(Seconds >= 0);
105 /* Limit new penalty to maximum configured, when less than 10 seconds. *
106 The latter is used to limit brute force attacks, therefore we don't *
107 want to limit that! */
108 if (Conf_MaxPenaltyTime >= 0
109 && Seconds > Conf_MaxPenaltyTime
111 Seconds = Conf_MaxPenaltyTime;
114 if (My_Connections[Idx].delaytime < t)
115 My_Connections[Idx].delaytime = t;
117 My_Connections[Idx].delaytime += Seconds;
121 "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
122 Idx, (long)Seconds, Seconds != 1 ? "s" : "",
123 My_Connections[Idx].delaytime - t,
124 My_Connections[Idx].delaytime - t != 1 ? "s" : "");
126 } /* Conn_SetPenalty */
129 Conn_ClearFlags( void )
133 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
134 } /* Conn_ClearFlags */
137 Conn_Flag( CONN_ID Idx )
139 assert( Idx > NONE );
140 return My_Connections[Idx].flag;
144 Conn_SetFlag( CONN_ID Idx, int Flag )
146 assert( Idx > NONE );
147 My_Connections[Idx].flag = Flag;
155 for( i = 0; i < Pool_Size; i++ )
157 if( My_Connections[i].sock != NONE ) return i;
163 Conn_Next( CONN_ID Idx )
167 assert( Idx > NONE );
169 for( i = Idx + 1; i < Pool_Size; i++ )
171 if( My_Connections[i].sock != NONE ) return i;
177 Conn_Options( CONN_ID Idx )
179 assert( Idx > NONE );
180 return My_Connections[Idx].options;
184 * Set connection option.
187 Conn_SetOption(CONN_ID Idx, int Option)
190 Conn_OPTION_ADD(&My_Connections[Idx], Option);
191 } /* Conn_SetOption */
194 * Get the start time of the connection.
195 * The result is the start time in seconds since 1970-01-01, as reported
196 * by the C function time(NULL).
199 Conn_StartTime( CONN_ID Idx )
205 /* Search client structure for this link ... */
206 c = Conn_GetClient(Idx);
208 return Client_StartTime(c);
211 } /* Conn_StartTime */
214 * return number of bytes queued for writing
217 Conn_SendQ( CONN_ID Idx )
219 assert( Idx > NONE );
221 if( My_Connections[Idx].options & CONN_ZIP )
222 return array_bytes(&My_Connections[Idx].zip.wbuf);
225 return array_bytes(&My_Connections[Idx].wbuf);
229 * return number of messages sent on this connection so far
232 Conn_SendMsg( CONN_ID Idx )
235 assert( Idx > NONE );
236 return My_Connections[Idx].msg_out;
240 * return number of (uncompressed) bytes sent
241 * on this connection so far
244 Conn_SendBytes( CONN_ID Idx )
246 assert( Idx > NONE );
247 return My_Connections[Idx].bytes_out;
248 } /* Conn_SendBytes */
251 * return number of bytes pending in read buffer
254 Conn_RecvQ( CONN_ID Idx )
256 assert( Idx > NONE );
258 if( My_Connections[Idx].options & CONN_ZIP )
259 return array_bytes(&My_Connections[Idx].zip.rbuf);
262 return array_bytes(&My_Connections[Idx].rbuf);
266 * return number of messages received on this connection so far
269 Conn_RecvMsg( CONN_ID Idx )
271 assert( Idx > NONE );
272 return My_Connections[Idx].msg_in;
276 * return number of (uncompressed) bytes received on this
280 Conn_RecvBytes( CONN_ID Idx )
282 assert( Idx > NONE );
283 return My_Connections[Idx].bytes_in;
284 } /* Conn_RecvBytes */
287 * Return the remote IP address of this connection as string.
290 Conn_IPA(CONN_ID Idx)
293 return ng_ipaddr_tostr(&My_Connections[Idx].addr);
297 Conn_ResetWCounter( void )
300 } /* Conn_ResetWCounter */
303 Conn_WCounter( void )
306 } /* Conn_WCounter */