[IOMultiplexerV2] added more socket examples
[NextIRCd.git] / src / IOHandler_test / server_ssl / iotest.c
1 /* main.c - IOMultiplexer
2  * Copyright (C) 2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include <stdio.h>
19 #include <string.h>
20 #include "../../IOHandler/IOHandler.h"
21 #include "../../IOHandler/IOSockets.h"
22 #include "../../IOHandler/IOLog.h"
23
24 #define CERTFILE "cert.pem"
25 #define KEYFILE "key.pen"
26
27 static IOSOCKET_CALLBACK(io_callback);
28 static IOLOG_CALLBACK(io_log);
29
30 static struct IOSocket *irc_iofd = NULL;
31
32 int main(int argc, char *argv[]) {
33         iohandler_init();
34         
35     iolog_register_callback(io_log);
36     
37     irc_iofd = iosocket_listen_ssl("0.0.0.0", 12345, CERTFILE, KEYFILE, io_callback);
38         
39         iohandler_run();
40         
41         return 0;
42 }
43
44 static IOSOCKET_CALLBACK(io_callback) {
45     switch(event->type) {
46         case IOSOCKETEVENT_ACCEPT:
47             printf("[client accepted]\n");
48                         struct IOSocket *client = event->data.accept_socket;
49                         client->callback = io_callback;
50                         
51                         char *html = "<html><head><title>Test Page</title></head><body><h1>IOHandler SSL Test</h1></body></html>";
52             iosocket_printf(client, "HTTP/1.1 200 OK\r\n");
53                         iosocket_printf(client, "Server: Apache\r\n");
54                         iosocket_printf(client, "Content-Length: %d\r\n", strlen(html));
55                         iosocket_printf(client, "Content-Type: text/html\r\n");
56                         iosocket_printf(client, "\r\n");
57                         iosocket_printf(client, "%s", html);
58                         
59             break;
60         case IOSOCKETEVENT_CLOSED:
61                         if(event->socket->listening)
62                                 printf("[server closed]\n");
63                         else
64                                 printf("[client disconnect]\n");
65             break;
66         case IOSOCKETEVENT_RECV:
67                         {
68                                 struct IOSocketBuffer *recv_buf = event->data.recv_buf;
69                                 int i;
70                                 for(i = 0; i < recv_buf->bufpos; i++)
71                                         putchar(recv_buf->buffer[i]);
72                                 recv_buf->bufpos = 0;
73                                 printf("\n");
74             }
75             break;
76         
77         default:
78             break;
79     }
80 }
81
82 static IOLOG_CALLBACK(io_log) {
83         printf("%s", message);
84 }