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