2 * ngIRCd -- The Next Generation IRC Daemon
3 * Copyright (c)2001-2008 Alexander Barton (alex@barton.de)
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"
34 * Update "idle timestamp", the time of the last visible user action
35 * (e. g. like sending messages, joining or leaving channels).
37 * @param Idx Connection index.
40 Conn_UpdateIdle(CONN_ID Idx)
43 My_Connections[Idx].lastprivmsg = time(NULL);
47 * Update "ping timestamp", the time of the last outgoing PING request.
49 * @param Idx Connection index.
52 Conn_UpdatePing(CONN_ID Idx)
55 My_Connections[Idx].lastping = time(NULL);
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 connetion */
73 return time( NULL ) - My_Connections[Idx].lastprivmsg;
78 Conn_LastPing( CONN_ID Idx )
81 return My_Connections[Idx].lastping;
86 * Add "penalty time" for a connection.
88 * During the "penalty time" the socket is ignored completely, no new data
89 * is read. This function only increases the penalty, it is not possible to
90 * decrease the penalty time.
92 * @param Idex Connection index.
93 * @param Seconds Seconds to add.
94 * @see Conn_ResetPenalty
97 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
102 assert(Seconds >= 0);
105 if (My_Connections[Idx].delaytime < t)
106 My_Connections[Idx].delaytime = t;
108 My_Connections[Idx].delaytime += Seconds;
112 "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
113 Idx, (long)Seconds, Seconds != 1 ? "s" : "",
114 My_Connections[Idx].delaytime - t,
115 My_Connections[Idx].delaytime - t != 1 ? "s" : "");
117 } /* Conn_SetPenalty */
121 * Reset the "penalty time" for one connection.
123 * @param Idx Connection index.
124 * @see Conn_SetPenalty
127 Conn_ResetPenalty(CONN_ID Idx)
131 My_Connections[Idx].delaytime = 0;
133 Log(LOG_DEBUG, "Penalty time on connection %d has been reset.");
135 } /* Conn_ResetPenalty */
139 Conn_ClearFlags( void )
143 for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
144 } /* Conn_ClearFlags */
148 Conn_Flag( CONN_ID Idx )
150 assert( Idx > NONE );
151 return My_Connections[Idx].flag;
156 Conn_SetFlag( CONN_ID Idx, int Flag )
158 /* Connection markieren */
160 assert( Idx > NONE );
161 My_Connections[Idx].flag = Flag;
168 /* Connection-Struktur der ersten Verbindung liefern;
169 * Ist keine Verbindung vorhanden, wird NONE geliefert. */
173 for( i = 0; i < Pool_Size; i++ )
175 if( My_Connections[i].sock != NONE ) return i;
182 Conn_Next( CONN_ID Idx )
184 /* Naechste Verbindungs-Struktur liefern; existiert keine
185 * weitere, so wird NONE geliefert. */
189 assert( Idx > NONE );
191 for( i = Idx + 1; i < Pool_Size; i++ )
193 if( My_Connections[i].sock != NONE ) return i;
200 Conn_Options( CONN_ID Idx )
202 assert( Idx > NONE );
203 return My_Connections[Idx].options;
208 * Set connection option.
211 Conn_SetOption(CONN_ID Idx, int Option)
214 Conn_OPTION_ADD(&My_Connections[Idx], Option);
215 } /* Conn_SetOption */
219 * Get the start time of the connection.
220 * The result is the start time in seconds since 1970-01-01, as reported
221 * by the C function time(NULL).
224 Conn_StartTime( CONN_ID Idx )
230 /* Search client structure for this link ... */
231 c = Conn_GetClient(Idx);
233 return Client_StartTime(c);
236 } /* Conn_StartTime */
239 * return number of bytes queued for writing
242 Conn_SendQ( CONN_ID Idx )
244 assert( Idx > NONE );
246 if( My_Connections[Idx].options & CONN_ZIP )
247 return array_bytes(&My_Connections[Idx].zip.wbuf);
250 return array_bytes(&My_Connections[Idx].wbuf);
255 * return number of messages sent on this connection so far
258 Conn_SendMsg( CONN_ID Idx )
261 assert( Idx > NONE );
262 return My_Connections[Idx].msg_out;
267 * return number of (uncompressed) bytes sent
268 * on this connection so far
271 Conn_SendBytes( CONN_ID Idx )
273 assert( Idx > NONE );
274 return My_Connections[Idx].bytes_out;
275 } /* Conn_SendBytes */
279 * return number of bytes pending in read buffer
282 Conn_RecvQ( CONN_ID Idx )
284 assert( Idx > NONE );
286 if( My_Connections[Idx].options & CONN_ZIP )
287 return array_bytes(&My_Connections[Idx].zip.rbuf);
290 return array_bytes(&My_Connections[Idx].rbuf);
295 * return number of messages received on this connection so far
298 Conn_RecvMsg( CONN_ID Idx )
300 assert( Idx > NONE );
301 return My_Connections[Idx].msg_in;
306 * return number of (uncompressed) bytes received on this
310 Conn_RecvBytes( CONN_ID Idx )
312 assert( Idx > NONE );
313 return My_Connections[Idx].bytes_in;
314 } /* Conn_RecvBytes */
317 * Return the remote IP address of this connection as string.
320 Conn_IPA(CONN_ID Idx)
323 return ng_ipaddr_tostr(&My_Connections[Idx].addr);
328 Conn_ResetWCounter( void )
331 } /* Conn_ResetWCounter */
335 Conn_WCounter( void )
338 } /* Conn_WCounter */