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