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