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