1 |
pippijn |
1.1 |
/* |
2 |
|
|
* packet.C: IRC packet handling. |
3 |
pippijn |
1.7 |
* |
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 |
pippijn |
1.2 |
* Rights to this code are documented in doc/pod/license.pod. |
10 |
pippijn |
1.4 |
* Copyright © 2005-2007 Atheme Project (http://www.atheme.org) |
11 |
pippijn |
1.1 |
*/ |
12 |
|
|
|
13 |
pippijn |
1.8 |
static char const rcsid[] = "$Id: packet.C,v 1.7 2007-09-16 18:54:45 pippijn Exp $"; |
14 |
pippijn |
1.1 |
|
15 |
|
|
#include "atheme.h" |
16 |
pippijn |
1.8 |
#include <libermyth.h> |
17 |
|
|
#include <util/time.h> |
18 |
pippijn |
1.1 |
#include "uplink.h" |
19 |
|
|
#include "datastream.h" |
20 |
pippijn |
1.5 |
#include "connection.h" |
21 |
pippijn |
1.1 |
|
22 |
pippijn |
1.4 |
static void on_connected (connection_t *cptr); |
23 |
|
|
|
24 |
pippijn |
1.1 |
/* 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 |
pippijn |
1.6 |
wasnonl = cptr->flags & CF_NONEWLINE ? true : false; |
37 |
pippijn |
1.1 |
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 |
pippijn |
1.4 |
phandler->ping_sts (); |
61 |
pippijn |
1.1 |
|
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 |
pippijn |
1.5 |
curr_uplink->conn->close (); |
73 |
pippijn |
1.1 |
} |
74 |
|
|
} |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
if (!me.connected) |
78 |
|
|
event_delete (ping_uplink, NULL); |
79 |
|
|
} |
80 |
|
|
|
81 |
|
|
static void |
82 |
pippijn |
1.4 |
on_connected (connection_t *cptr) |
83 |
pippijn |
1.1 |
{ |
84 |
|
|
/* add our server */ |
85 |
|
|
|
86 |
|
|
if (cptr == curr_uplink->conn) |
87 |
|
|
{ |
88 |
pippijn |
1.6 |
cptr->flags = CF_UPLINK; |
89 |
pippijn |
1.1 |
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 |
pippijn |
1.4 |
phandler->server_login (); |
97 |
pippijn |
1.1 |
|
98 |
|
|
#ifdef HAVE_GETTIMEOFDAY |
99 |
|
|
/* start our burst timer */ |
100 |
|
|
s_time (&burstime); |
101 |
|
|
#endif |
102 |
|
|
|
103 |
|
|
/* done bursting by this time... */ |
104 |
pippijn |
1.4 |
phandler->ping_sts (); |
105 |
pippijn |
1.1 |
|
106 |
|
|
/* ping our uplink every 5 minutes */ |
107 |
|
|
event_delete (ping_uplink, NULL); |
108 |
|
|
event_add ("ping_uplink", ping_uplink, NULL, 300); |
109 |
pippijn |
1.4 |
me.uplinkpong = NOW; |
110 |
pippijn |
1.1 |
} |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
void |
114 |
|
|
init_ircpacket (void) |
115 |
|
|
{ |
116 |
pippijn |
1.4 |
connection_t::callback.connected.attach (on_connected); |
117 |
pippijn |
1.1 |
} |