]> arthur.barton.de Git - ngircd-alex.git/blob - src/ngircd/conn-func.c
Code cleanup, remove blank lines
[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  * Update "idle timestamp", the time of the last visible user action
34  * (e. g. like sending messages, joining or leaving channels).
35  *
36  * @param Idx Connection index.
37  */
38 GLOBAL void
39 Conn_UpdateIdle(CONN_ID Idx)
40 {
41         assert(Idx > NONE);
42         My_Connections[Idx].lastprivmsg = time(NULL);
43 }
44
45 /**
46  * Update "ping timestamp", the time of the last outgoing PING request.
47  *
48  * @param Idx Connection index.
49  */
50 GLOBAL void
51 Conn_UpdatePing(CONN_ID Idx)
52 {
53         assert(Idx > NONE);
54         My_Connections[Idx].lastping = time(NULL);
55 }
56
57 /*
58  * Get signon time of a connection.
59  */
60 GLOBAL time_t
61 Conn_GetSignon(CONN_ID Idx)
62 {
63         assert(Idx > NONE);
64         return My_Connections[Idx].signon;
65 }
66
67 GLOBAL time_t
68 Conn_GetIdle( CONN_ID Idx )
69 {
70         /* Return Idle-Timer of a connetion */
71         assert( Idx > NONE );
72         return time( NULL ) - My_Connections[Idx].lastprivmsg;
73 } /* Conn_GetIdle */
74
75 GLOBAL time_t
76 Conn_LastPing( CONN_ID Idx )
77 {
78         assert( Idx > NONE );
79         return My_Connections[Idx].lastping;
80 } /* Conn_LastPing */
81
82 /**
83  * Add "penalty time" for a connection.
84  *
85  * During the "penalty time" the socket is ignored completely, no new data
86  * is read. This function only increases the penalty, it is not possible to
87  * decrease the penalty time.
88  *
89  * @param Idex Connection index.
90  * @param Seconds Seconds to add.
91  * @see Conn_ResetPenalty
92  */
93 GLOBAL void
94 Conn_SetPenalty(CONN_ID Idx, time_t Seconds)
95 {
96         time_t t;
97
98         assert(Idx > NONE);
99         assert(Seconds >= 0);
100
101         t = time(NULL);
102         if (My_Connections[Idx].delaytime < t)
103                 My_Connections[Idx].delaytime = t;
104
105         My_Connections[Idx].delaytime += Seconds;
106
107 #ifdef DEBUG
108         Log(LOG_DEBUG,
109             "Add penalty time on connection %d: %ld second%s, total %ld second%s.",
110             Idx, (long)Seconds, Seconds != 1 ? "s" : "",
111             My_Connections[Idx].delaytime - t,
112             My_Connections[Idx].delaytime - t != 1 ? "s" : "");
113 #endif
114 } /* Conn_SetPenalty */
115
116 GLOBAL void
117 Conn_ClearFlags( void )
118 {
119         CONN_ID i;
120
121         for( i = 0; i < Pool_Size; i++ ) My_Connections[i].flag = 0;
122 } /* Conn_ClearFlags */
123
124 GLOBAL int
125 Conn_Flag( CONN_ID Idx )
126 {
127         assert( Idx > NONE );
128         return My_Connections[Idx].flag;
129 } /* Conn_Flag */
130
131 GLOBAL void
132 Conn_SetFlag( CONN_ID Idx, int Flag )
133 {
134         assert( Idx > NONE );
135         My_Connections[Idx].flag = Flag;
136 } /* Conn_SetFlag */
137
138 GLOBAL CONN_ID
139 Conn_First( void )
140 {
141         CONN_ID i;
142         
143         for( i = 0; i < Pool_Size; i++ )
144         {
145                 if( My_Connections[i].sock != NONE ) return i;
146         }
147         return NONE;
148 } /* Conn_First */
149
150 GLOBAL CONN_ID
151 Conn_Next( CONN_ID Idx )
152 {
153         CONN_ID i = NONE;
154
155         assert( Idx > NONE );
156         
157         for( i = Idx + 1; i < Pool_Size; i++ )
158         {
159                 if( My_Connections[i].sock != NONE ) return i;
160         }
161         return NONE;
162 } /* Conn_Next */
163
164 GLOBAL UINT16
165 Conn_Options( CONN_ID Idx )
166 {
167         assert( Idx > NONE );
168         return My_Connections[Idx].options;
169 } /* Conn_Options */
170
171 /**
172  * Set connection option.
173  */
174 GLOBAL void
175 Conn_SetOption(CONN_ID Idx, int Option)
176 {
177         assert(Idx > NONE);
178         Conn_OPTION_ADD(&My_Connections[Idx], Option);
179 } /* Conn_SetOption */
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  * 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  * return number of (uncompressed) bytes sent
229  * on this connection so far
230  */
231 GLOBAL long
232 Conn_SendBytes( CONN_ID Idx )
233 {
234         assert( Idx > NONE );
235         return My_Connections[Idx].bytes_out;
236 } /* Conn_SendBytes */
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  * return number of messages received on this connection so far
255  */
256 GLOBAL long
257 Conn_RecvMsg( CONN_ID Idx )
258 {
259         assert( Idx > NONE );
260         return My_Connections[Idx].msg_in;
261 } /* Conn_RecvMsg */
262
263 /**
264  * return number of (uncompressed) bytes received on this
265  * connection so far
266  */
267 GLOBAL long
268 Conn_RecvBytes( CONN_ID Idx )
269 {
270         assert( Idx > NONE );
271         return My_Connections[Idx].bytes_in;
272 } /* Conn_RecvBytes */
273
274 /**
275  * Return the remote IP address of this connection as string.
276  */
277 GLOBAL const char *
278 Conn_IPA(CONN_ID Idx)
279 {
280         assert (Idx > NONE);
281         return ng_ipaddr_tostr(&My_Connections[Idx].addr);
282 }
283
284 GLOBAL void
285 Conn_ResetWCounter( void )
286 {
287         WCounter = 0;
288 } /* Conn_ResetWCounter */
289
290 GLOBAL long
291 Conn_WCounter( void )
292 {
293         return WCounter;
294 } /* Conn_WCounter */
295
296 /* -eof- */