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