From 88a5f99033da59a0865f4ca631a40fa780ebfdda Mon Sep 17 00:00:00 2001 From: pk910 Date: Wed, 28 Jan 2015 00:53:25 +0100 Subject: [PATCH] [IOMultiplexerV2] Added simple Server example --- configure.ac | 28 ++++++--- src/IOHandler_test/Makefile.am | 2 +- src/IOHandler_test/server/.gitignore | 7 +++ src/IOHandler_test/server/Makefile.am | 8 +++ src/IOHandler_test/server/iotest.c | 81 +++++++++++++++++++++++++++ 5 files changed, 117 insertions(+), 9 deletions(-) create mode 100644 src/IOHandler_test/server/.gitignore create mode 100644 src/IOHandler_test/server/Makefile.am create mode 100644 src/IOHandler_test/server/iotest.c diff --git a/configure.ac b/configure.ac index 4275959..f424ed7 100644 --- a/configure.ac +++ b/configure.ac @@ -35,7 +35,15 @@ AC_FUNC_MALLOC AC_CHECK_FUNCS([usleep select socket inet_pton inet_ntop]) AC_CHECK_HEADERS([fcntl.h sys/socket.h sys/select.h sys/time.h sys/types.h unistd.h windows.h winsock2.h errno.h sys/epoll.h sys/event.h]) -AC_CHECK_LIB(ws2_32, main, [ LIBS="$LIBS -lws2_32" ], []) + + +AC_CHECK_LIB(ws2_32, main, [ + LIBS="$LIBS -lws2_32" + is_win32="yes" +], [ + is_win32="no" +]) + have_gnutls="no" AC_CHECK_LIB(gnutls, gnutls_init, [ AC_CHECK_HEADERS(gnutls/gnutls.h, [ @@ -44,14 +52,16 @@ AC_CHECK_LIB(gnutls, gnutls_init, [ ]) ]) if test x"$have_gnutls" = xno; then - AC_CHECK_LIB(ssl, SSL_read, [ - AC_CHECK_LIB(crypto, X509_new, [ - AC_CHECK_HEADERS(openssl/ssl.h openssl/err.h openssl/rand.h, [ - LIBS="$LIBS -lssl -lcrypto" - ]) - ]) - ]) + if test x$is_win32 = xyes ; then + openssl_deps="-lcrypto -lgdi32" + else + openssl_deps="-lcrypto" + fi + AC_CHECK_LIB([ssl],[SSL_library_init], [ + LIBS="$LIBS -lssl $openssl_deps" + ], [AC_MSG_ERROR([OpenSSL libraries required])], $openssl_deps) fi + AC_CHECK_LIB(pthread, pthread_create, [ AC_CHECK_HEADERS(pthread.h, [ LIBS="$LIBS -lpthread" @@ -63,6 +73,7 @@ AC_CHECK_LIB(cares, ares_init, [ ]) ]) + AC_CONFIG_FILES([ Makefile src/Makefile @@ -72,6 +83,7 @@ AC_CONFIG_FILES([ src/IOHandler_test/client/Makefile src/IOHandler_test/client++/Makefile src/IOHandler_test/client_ssl/Makefile + src/IOHandler_test/server/Makefile src/IOHandler_test/server_ssl/Makefile src/IOHandler_test/timer/Makefile src/IOHandler_test/timer++/Makefile diff --git a/src/IOHandler_test/Makefile.am b/src/IOHandler_test/Makefile.am index d8ca43d..ef3454a 100644 --- a/src/IOHandler_test/Makefile.am +++ b/src/IOHandler_test/Makefile.am @@ -1,3 +1,3 @@ ##Process this file with automake to create Makefile.in ACLOCAL_AMFLAGS = -I m4 -SUBDIRS = client client++ client_ssl server_ssl timer timer++ resolv +SUBDIRS = client client++ client_ssl server server_ssl timer timer++ resolv diff --git a/src/IOHandler_test/server/.gitignore b/src/IOHandler_test/server/.gitignore new file mode 100644 index 0000000..7af2f69 --- /dev/null +++ b/src/IOHandler_test/server/.gitignore @@ -0,0 +1,7 @@ +.deps +.libs +*.o +*.exe +iotest +Makefile +Makefile.in diff --git a/src/IOHandler_test/server/Makefile.am b/src/IOHandler_test/server/Makefile.am new file mode 100644 index 0000000..feda450 --- /dev/null +++ b/src/IOHandler_test/server/Makefile.am @@ -0,0 +1,8 @@ +##Process this file with automake to create Makefile.in +ACLOCAL_AMFLAGS = -I m4 + +noinst_PROGRAMS = iotest +iotest_LDADD = ../../IOHandler/libiohandler.la + +iotest_SOURCES = iotest.c + diff --git a/src/IOHandler_test/server/iotest.c b/src/IOHandler_test/server/iotest.c new file mode 100644 index 0000000..8894cf7 --- /dev/null +++ b/src/IOHandler_test/server/iotest.c @@ -0,0 +1,81 @@ +/* main.c - IOMultiplexer + * Copyright (C) 2012 Philipp Kreil (pk910) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "../../IOHandler/IOHandler.h" +#include "../../IOHandler/IOSockets.h" +#include "../../IOHandler/IOLog.h" + +static IOSOCKET_CALLBACK(io_callback); +static IOLOG_CALLBACK(io_log); + +static struct IOSocket *irc_iofd = NULL; + +int main(int argc, char *argv[]) { + iohandler_init(); + + iolog_register_callback(io_log); + + irc_iofd = iosocket_listen("0.0.0.0", 12345, io_callback); + + iohandler_run(); + + return 0; +} + +static IOSOCKET_CALLBACK(io_callback) { + switch(event->type) { + case IOSOCKETEVENT_ACCEPT: + printf("[client accepted]\n"); + struct IOSocket *client = event->data.accept_socket; + client->callback = io_callback; + + char *html = "Test Page

IOHandler SSL Test

"; + iosocket_printf(client, "HTTP/1.1 200 OK\r\n"); + iosocket_printf(client, "Server: Apache\r\n"); + iosocket_printf(client, "Content-Length: %d\r\n", strlen(html)); + iosocket_printf(client, "Content-Type: text/html\r\n"); + iosocket_printf(client, "\r\n"); + iosocket_printf(client, "%s", html); + + break; + case IOSOCKETEVENT_CLOSED: + if(event->socket->listening) + printf("[server closed]\n"); + else + printf("[client disconnect]\n"); + break; + case IOSOCKETEVENT_RECV: + { + struct IOSocketBuffer *recv_buf = event->data.recv_buf; + int i; + for(i = 0; i < recv_buf->bufpos; i++) + putchar(recv_buf->buffer[i]); + recv_buf->bufpos = 0; + printf("\n"); + } + break; + + default: + break; + } +} + +static IOLOG_CALLBACK(io_log) { + printf("%s", message); +} -- 2.20.1