1 |
/** |
2 |
* gen_echoserver.C: An echo server. (proof of concept for integrated XMLRPC HTTPD) |
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 |
* Copyright © 2005 William Pitcock <nenolod -at- nenolod.net> |
10 |
* Rights to this code are as documented in doc/pod/license.pod. |
11 |
* |
12 |
* $Id: gen_echoserver.C,v 1.4 2007-08-30 19:56:22 pippijn Exp $ |
13 |
*/ |
14 |
|
15 |
#include "atheme.h" |
16 |
#include <ermyth/module.h> |
17 |
#include "datastream.h" |
18 |
#include "connection.h" |
19 |
|
20 |
static char const rcsid[] = "$Id: gen_echoserver.C,v 1.4 2007-08-30 19:56:22 pippijn Exp $"; |
21 |
|
22 |
REGISTER_MODULE ("contrib/gen_echoserver", false, "William Pitcock <nenolod -at- nenolod.net>"); |
23 |
|
24 |
static connection_t *listener; |
25 |
|
26 |
static int |
27 |
my_read (connection_t *cptr, char *buf) |
28 |
{ |
29 |
int n; |
30 |
|
31 |
if ((n = read (cptr->fd, buf, BUFSIZE)) > 0) |
32 |
{ |
33 |
buf[n] = '\0'; |
34 |
cnt.bin += n; |
35 |
} |
36 |
|
37 |
return n; |
38 |
} |
39 |
|
40 |
static void |
41 |
do_packet (connection_t *cptr, char *buf) |
42 |
{ |
43 |
char *ptr, buf2[BUFSIZE * 2]; |
44 |
static char tmp[BUFSIZE * 2 + 1]; |
45 |
|
46 |
while ((ptr = strchr (buf, '\n'))) |
47 |
{ |
48 |
snprintf (buf2, (BUFSIZE * 2), "%s%s", tmp, buf); |
49 |
*tmp = '\0'; |
50 |
|
51 |
slog (LG_DEBUG, "-{incoming}-> %s", buf2); |
52 |
cptr->write_raw (buf2); |
53 |
|
54 |
buf = ptr + 1; |
55 |
} |
56 |
|
57 |
if (*buf) |
58 |
{ |
59 |
strlcpy (tmp, buf, BUFSIZE * 2); |
60 |
tmp[BUFSIZE * 2] = '\0'; |
61 |
} |
62 |
} |
63 |
|
64 |
static void |
65 |
my_rhandler (connection_t *cptr) |
66 |
{ |
67 |
char buf[BUFSIZE * 2]; |
68 |
|
69 |
if (!my_read (cptr, buf)) |
70 |
cptr->close (); |
71 |
|
72 |
do_packet (cptr, buf); |
73 |
} |
74 |
|
75 |
static void |
76 |
do_listen (connection_t *cptr) |
77 |
{ |
78 |
connection_t::accept_tcp (cptr, my_rhandler, sendq_flush); |
79 |
slog (LG_DEBUG, "do_listen(): accepted %d", cptr->fd); |
80 |
} |
81 |
|
82 |
bool |
83 |
_modinit (module *m) |
84 |
{ |
85 |
listener = connection_t::open_listener_tcp ("127.0.0.1", 7100, do_listen); |
86 |
|
87 |
return true; |
88 |
} |
89 |
|
90 |
void |
91 |
_moddeinit (void) |
92 |
{ |
93 |
listener->close (); |
94 |
} |