[IOMultiplexerV2] added c++ interface
[NextIRCd.git] / src / IOHandler++ / IOSocket.cpp
1 /* IOSocket.cpp - 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 extern "C" {
18         #include "../IOHandler/IOSockets.h"
19 }
20 #include "IOSocket.h"
21 #include <cstdarg>
22 #include <cstdio>
23 #include <cstring>
24
25 static IOSOCKET_CALLBACK(c_socket_callback) {
26         CIOSocket *ciosock = (CIOSocket *) event->socket->data;
27         ciosock->socket_callback(event);
28 }
29
30 CIOSocket::CIOSocket() {
31
32 }
33 CIOSocket::CIOSocket(IOSocket *iosocket) {
34         this->iosocket = iosocket;
35 }
36
37 int CIOSocket::connect(char *hostname, unsigned int port, int ssl, char *bindhost) {
38         return this->connect(hostname, port, ssl, bindhost, IOSOCKET_ADDR_IPV6 | IOSOCKET_ADDR_IPV4);
39 }
40
41 int CIOSocket::connect(char *hostname, unsigned int port, int ssl, char *bindhost, int flags) {
42         this->iosocket = iosocket_connect_flags(hostname, port, (ssl ? 1 : 0), (bindhost ? bindhost : NULL), c_socket_callback, flags);
43         if(this->iosocket) {
44                 this->iosocket->data = this;
45                 return 1;
46         } else
47                 return 0;
48 }
49
50 int CIOSocket::listen(char *hostname, unsigned int port) {
51         return this->listen(hostname, port, IOSOCKET_ADDR_IPV6 | IOSOCKET_ADDR_IPV4);
52 }
53 int CIOSocket::listen(char *hostname, unsigned int port, int flags) {
54         this->iosocket = iosocket_listen_flags(hostname, port, c_socket_callback, flags);
55         if(this->iosocket) {
56                 this->iosocket->data = this;
57                 return 1;
58         } else
59                 return 0;
60 }
61
62 int CIOSocket::listen_ssl(char *hostname, unsigned int port, char *certfile, char *keyfile) {
63         return listen_ssl(hostname, port, certfile, keyfile, IOSOCKET_ADDR_IPV6 | IOSOCKET_ADDR_IPV4);
64 }
65 int CIOSocket::listen_ssl(char *hostname, unsigned int port, char *certfile, char *keyfile, int flags) {
66         this->iosocket = iosocket_listen_ssl_flags(hostname, port, certfile, keyfile, c_socket_callback, flags);
67         if(this->iosocket) {
68                 this->iosocket->data = this;
69                 return 1;
70         } else
71                 return 0;
72 }
73
74 void CIOSocket::write(const char *data, int len) {
75         iosocket_send(iosocket, data, len);
76 }
77 #define IOSOCKET_PRINTF_LEN 2048
78 void CIOSocket::writef(const char *format, ...) {
79         va_list arg_list;
80         char sendBuf[IOSOCKET_PRINTF_LEN];
81         int pos;
82         sendBuf[0] = '\0';
83         va_start(arg_list, format);
84         pos = vsnprintf(sendBuf, IOSOCKET_PRINTF_LEN - 1, format, arg_list);
85         va_end(arg_list);
86         if (pos < 0 || pos > (IOSOCKET_PRINTF_LEN - 1)) pos = IOSOCKET_PRINTF_LEN - 1;
87         sendBuf[pos] = '\0';
88         iosocket_send(iosocket, sendBuf, pos);
89 }
90
91 void CIOSocket::close() {
92         iosocket_close(iosocket);
93 };
94
95
96 void CIOSocket::socket_callback(IOSocketEvent *event) {
97         switch(event->type) {
98         case IOSOCKETEVENT_RECV:
99                 if(iosocket->parse_delimiter)
100                         this->recvLine(event->data.recv_str);
101                 else {
102                         IOSocketBuffer *recvbuf = event->data.recv_buf;
103                         int usedlen;
104                         usedlen = this->recvEvent(recvbuf->buffer, recvbuf->bufpos);
105                         if(usedlen == recvbuf->bufpos) {
106                                 recvbuf->bufpos = 0;
107                         } else {
108                                 memmove(recvbuf->buffer, recvbuf->buffer + usedlen, recvbuf->bufpos - usedlen);
109                                 recvbuf->bufpos -= usedlen;
110                         }
111                 }
112                 break;
113     case IOSOCKETEVENT_CONNECTED:
114                 this->connectedEvent();
115                 break;
116         case IOSOCKETEVENT_NOTCONNECTED:
117                 this->notConnectedEvent(event->data.errid);
118                 break;
119         case IOSOCKETEVENT_CLOSED:
120                 this->closedEvent(event->data.errid);
121                 break;
122         case IOSOCKETEVENT_ACCEPT:
123                 this->acceptedEvent(new CIOSocket(event->data.accept_socket));
124                 break;
125         case IOSOCKETEVENT_DNSFAILED:
126                 this->dnsErrEvent(event->data.recv_str);
127                 break;
128         }
129 }
130
131 int CIOSocket::getSSL() {
132         
133 }
134 int CIOSocket::getIPv6() {
135         
136 }
137 int CIOSocket::getConnected() {
138         
139 }
140 int CIOSocket::getListening() {
141         
142 }