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