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