| 1 |
/* |
| 2 |
* Copyright (c) 2005 William Pitcock <nenolod -at- nenolod.net> |
| 3 |
* Rights to this code are as documented in doc/LICENSE. |
| 4 |
* |
| 5 |
* An echo server. (proof of concept for integrated XMLRPC HTTPD) |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "atheme-compat.h" |
| 9 |
|
| 10 |
#if (CURRENT_ABI_REVISION < 730000) |
| 11 |
# include "datastream.h" |
| 12 |
#endif |
| 13 |
|
| 14 |
static connection_t *listener = NULL; |
| 15 |
|
| 16 |
static int |
| 17 |
my_read(connection_t *cptr, char *buf) |
| 18 |
{ |
| 19 |
int n; |
| 20 |
|
| 21 |
if ((n = read(cptr->fd, buf, BUFSIZE)) > 0) |
| 22 |
{ |
| 23 |
buf[n] = '\0'; |
| 24 |
cnt.bin += n; |
| 25 |
} |
| 26 |
|
| 27 |
return n; |
| 28 |
} |
| 29 |
|
| 30 |
static void |
| 31 |
do_packet(connection_t *cptr, char *buf) |
| 32 |
{ |
| 33 |
char *ptr, buf2[BUFSIZE * 2]; |
| 34 |
static char tmp[BUFSIZE * 2 + 1]; |
| 35 |
|
| 36 |
while ((ptr = strchr(buf, '\n'))) |
| 37 |
{ |
| 38 |
snprintf(buf2, (BUFSIZE * 2), "%s%s", tmp, buf); |
| 39 |
*tmp = '\0'; |
| 40 |
|
| 41 |
slog(LG_DEBUG, "-{incoming}-> %s", buf2); |
| 42 |
sendq_add(cptr, buf2, strlen(buf2)); |
| 43 |
|
| 44 |
buf = ptr + 1; |
| 45 |
} |
| 46 |
|
| 47 |
if (*buf) |
| 48 |
{ |
| 49 |
mowgli_strlcpy(tmp, buf, BUFSIZE * 2); |
| 50 |
tmp[BUFSIZE * 2] = '\0'; |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
static void |
| 55 |
my_rhandler(connection_t * cptr) |
| 56 |
{ |
| 57 |
char buf[BUFSIZE * 2]; |
| 58 |
|
| 59 |
if (my_read(cptr, buf) <= 0) |
| 60 |
connection_close(cptr); |
| 61 |
else |
| 62 |
do_packet(cptr, buf); |
| 63 |
} |
| 64 |
|
| 65 |
static void |
| 66 |
do_listen(connection_t *cptr) |
| 67 |
{ |
| 68 |
connection_t *newptr; |
| 69 |
newptr = connection_accept_tcp(cptr, my_rhandler, NULL); |
| 70 |
slog(LG_DEBUG, "do_listen(): accepted %d", cptr->fd); |
| 71 |
} |
| 72 |
|
| 73 |
static void |
| 74 |
mod_init(module_t *const restrict m) |
| 75 |
{ |
| 76 |
listener = connection_open_listener_tcp("127.0.0.1", 7100, do_listen); |
| 77 |
} |
| 78 |
|
| 79 |
static void |
| 80 |
mod_deinit(const module_unload_intent_t intent) |
| 81 |
{ |
| 82 |
connection_close(listener); |
| 83 |
} |
| 84 |
|
| 85 |
VENDOR_DECLARE_MODULE_V1("contrib/gen_echoserver", MODULE_UNLOAD_CAPABILITY_OK, CONTRIB_VENDOR_NENOLOD) |