1 |
/* |
2 |
* packet.C: IRC packet handling. |
3 |
* |
4 |
* Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team |
5 |
* Rights to this code are as documented in COPYING. |
6 |
* |
7 |
* |
8 |
* Portions of this file were derived from sources bearing the following license: |
9 |
* Rights to this code are documented in doc/pod/license.pod. |
10 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
11 |
*/ |
12 |
|
13 |
static char const rcsid[] = "$Id: packet.C,v 1.6 2007-09-05 11:23:15 pippijn Exp $"; |
14 |
|
15 |
#include "atheme.h" |
16 |
#include "uplink.h" |
17 |
#include "datastream.h" |
18 |
#include "connection.h" |
19 |
|
20 |
static void on_connected (connection_t *cptr); |
21 |
|
22 |
/* bursting timer */ |
23 |
#if HAVE_GETTIMEOFDAY |
24 |
struct timeval burstime; |
25 |
#endif |
26 |
|
27 |
static void |
28 |
irc_recvq_handler (connection_t *cptr) |
29 |
{ |
30 |
bool wasnonl; |
31 |
char parsebuf[BUFSIZE + 1]; |
32 |
int count; |
33 |
|
34 |
wasnonl = cptr->flags & CF_NONEWLINE ? true : false; |
35 |
count = recvq_getline (cptr, parsebuf, sizeof parsebuf - 1); |
36 |
if (count <= 0) |
37 |
return; |
38 |
cnt.bin += count; |
39 |
/* ignore the excessive part of a too long line */ |
40 |
if (wasnonl) |
41 |
return; |
42 |
me.uplinkpong = NOW; |
43 |
if (parsebuf[count - 1] == '\n') |
44 |
count--; |
45 |
if (count > 0 && parsebuf[count - 1] == '\r') |
46 |
count--; |
47 |
parsebuf[count] = '\0'; |
48 |
parse (parsebuf); |
49 |
} |
50 |
|
51 |
static void |
52 |
ping_uplink (void *arg) |
53 |
{ |
54 |
unsigned int diff; |
55 |
|
56 |
if (me.connected) |
57 |
{ |
58 |
phandler->ping_sts (); |
59 |
|
60 |
diff = NOW - me.uplinkpong; |
61 |
|
62 |
if (diff >= 600) |
63 |
{ |
64 |
slog (LG_INFO, "ping_uplink(): uplink appears to be dead, disconnecting"); |
65 |
sts ("ERROR :Closing Link: 127.0.0.1 (Ping timeout: %d seconds)", diff); |
66 |
sendq_flush (curr_uplink->conn); |
67 |
if (me.connected) |
68 |
{ |
69 |
errno = 0; |
70 |
curr_uplink->conn->close (); |
71 |
} |
72 |
} |
73 |
} |
74 |
|
75 |
if (!me.connected) |
76 |
event_delete (ping_uplink, NULL); |
77 |
} |
78 |
|
79 |
static void |
80 |
on_connected (connection_t *cptr) |
81 |
{ |
82 |
/* add our server */ |
83 |
|
84 |
if (cptr == curr_uplink->conn) |
85 |
{ |
86 |
cptr->flags = CF_UPLINK; |
87 |
cptr->recvq_handler = irc_recvq_handler; |
88 |
me.connected = true; |
89 |
/* no SERVER message received */ |
90 |
me.recvsvr = false; |
91 |
|
92 |
slog (LG_INFO, "irc_handle_connect(): connection to uplink established"); |
93 |
|
94 |
phandler->server_login (); |
95 |
|
96 |
#ifdef HAVE_GETTIMEOFDAY |
97 |
/* start our burst timer */ |
98 |
s_time (&burstime); |
99 |
#endif |
100 |
|
101 |
/* done bursting by this time... */ |
102 |
phandler->ping_sts (); |
103 |
|
104 |
/* ping our uplink every 5 minutes */ |
105 |
event_delete (ping_uplink, NULL); |
106 |
event_add ("ping_uplink", ping_uplink, NULL, 300); |
107 |
me.uplinkpong = NOW; |
108 |
} |
109 |
} |
110 |
|
111 |
void |
112 |
init_ircpacket (void) |
113 |
{ |
114 |
connection_t::callback.connected.attach (on_connected); |
115 |
} |