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