[IOMultiplexerV2] IPv4 fallback for IPv6 Sockets
[NextIRCd.git] / src / IOHandler / IOHandler.c
1 /* IOHandler.c - IOMultiplexer v2
2  * Copyright (C) 2014  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 #define _IOHandler_internals
18 #include "IOInternal.h"
19 #include "IOHandler.h"
20
21 #include "IOLog.h"
22 #include "IOGarbageCollector.h"
23 #include "IOTimer.h"
24 #include "IODNSLookup.h"
25 #include "IOSockets.h"
26
27 #include <stdlib.h>
28 #include <time.h>
29
30 /* compat */
31 #include "compat/utime.h"
32
33 #define IOHANDLER_STATE_INITIALIZED  0x0001
34 #define IOHANDLER_STATE_RUNNING      0x0002
35
36 static int iohandler_state = 0;
37
38
39 void iohandler_init() {
40         if((iohandler_state & IOHANDLER_STATE_INITIALIZED)) 
41                 return;
42         
43         srand(time(NULL));
44         
45         iolog_init();
46         iogc_init();
47         
48         _init_timers();
49         _init_iodns();
50         _init_sockets();
51         
52         iohandler_state |= IOHANDLER_STATE_INITIALIZED;
53 }
54
55 void iohandler_stop() {
56         iohandler_state &= ~IOHANDLER_STATE_RUNNING;
57 }
58
59 static void iohandler_loop() {
60         while(iohandler_state & IOHANDLER_STATE_RUNNING) { // endless loop
61                 // iohandler calls
62                 iogc_exec();
63                 iodns_poll();
64                 iosocket_loop(IOHANDLER_LOOP_MAXTIME);
65                 
66         }
67 }
68
69 void iohandler_run() {
70         iohandler_state |= IOHANDLER_STATE_RUNNING;
71         
72         iohandler_loop();
73 }
74