/** * gen_echoserver.C: An echo server. (proof of concept for integrated XMLRPC HTTPD) * * Copyright © 2007 Pippijn van Steenhoven / The Ermyth Team * Rights to this code are as documented in COPYING. * * * Portions of this file were derived from sources bearing the following license: * Copyright © 2005 William Pitcock * Rights to this code are as documented in doc/pod/license.pod. * * $Id: gen_echoserver.C,v 1.5 2007/09/16 18:54:43 pippijn Exp $ */ #include "atheme.h" #include #include "datastream.h" #include "connection.h" static char const rcsid[] = "$Id: gen_echoserver.C,v 1.5 2007/09/16 18:54:43 pippijn Exp $"; REGISTER_MODULE ("contrib/gen_echoserver", false, "William Pitcock "); static connection_t *listener; static int my_read (connection_t *cptr, char *buf) { int n; if ((n = read (cptr->fd, buf, BUFSIZE)) > 0) { buf[n] = '\0'; cnt.bin += n; } return n; } static void do_packet (connection_t *cptr, char *buf) { char *ptr, buf2[BUFSIZE * 2]; static char tmp[BUFSIZE * 2 + 1]; while ((ptr = strchr (buf, '\n'))) { snprintf (buf2, (BUFSIZE * 2), "%s%s", tmp, buf); *tmp = '\0'; slog (LG_DEBUG, "-{incoming}-> %s", buf2); cptr->write_raw (buf2); buf = ptr + 1; } if (*buf) { strlcpy (tmp, buf, BUFSIZE * 2); tmp[BUFSIZE * 2] = '\0'; } } static void my_rhandler (connection_t *cptr) { char buf[BUFSIZE * 2]; if (!my_read (cptr, buf)) cptr->close (); do_packet (cptr, buf); } static void do_listen (connection_t *cptr) { connection_t::accept_tcp (cptr, my_rhandler, sendq_flush); slog (LG_DEBUG, "do_listen(): accepted %d", cptr->fd); } bool _modinit (module *m) { listener = connection_t::open_listener_tcp ("127.0.0.1", 7100, do_listen); return true; } void _moddeinit (void) { listener->close (); }