1 /* IOHandler.c - TransparentIRC 0.1
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
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.
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.
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/>.
17 #include "IOHandler.h"
20 struct IODescriptor *first_descriptor = NULL;
23 extern struct IOEngine engine_select; /* select system call (should always be useable) */
25 struct IOEngine *engine = NULL;
27 static void iohandler_init_engine() {
31 //found an useable IO engine
32 } else if (engine_select.init())
33 engine = &engine_select;
35 //STDERR: found no useable IO engine
39 struct IODescriptor *iohandler_add(int sockfd, enum IOType type, iohandler_callback *callback) {
40 //just add a new IODescriptor
41 struct IODescriptor *descriptor = calloc(1, sizeof(*descriptor));
42 if(!descriptor) return NULL;
43 descriptor->fd = sockfd;
44 descriptor->type = type;
45 descriptor->state = IO_CLOSED;
46 descriptor->callback = callback;
47 if(type != IOTYPE_TIMER) {
48 descriptor->readbuf.buffer = malloc(IO_READ_BUFLEN + 2);
49 descriptor->readbuf.bufpos = 0;
50 descriptor->readbuf.buflen = IO_READ_BUFLEN;
51 descriptor->writebuf.buffer = malloc(IO_READ_BUFLEN + 2);
52 descriptor->writebuf.bufpos = 0;
53 descriptor->writebuf.buflen = IO_READ_BUFLEN;
57 iohandler_init_engine();
62 engine->add(descriptor);
64 //add IODescriptor to the list
65 descriptor->prev = NULL;
66 descriptor->next = first_descriptor;
68 first_descriptor->prev = descriptor;
69 first_descriptor = descriptor;
74 static void iohandler_remove(struct IODescriptor *descriptor) {
75 //remove IODescriptor from the list
77 descriptor->prev->next = descriptor->next;
79 first_descriptor = descriptor->next;
81 descriptor->next->prev = descriptor->prev;
82 engine->remove(descriptor);
83 if(descriptor->readbuf.buffer)
84 free(descriptor->readbuf.buffer);
85 if(descriptor->writebuf.buffer)
86 free(descriptor->writebuf.buffer);
90 static void iohandler_increase_iobuf(struct IOBuffer *iobuf, size_t required) {
91 if(iobuf->buflen >= required) return;
92 char *new_buf = realloc(iobuf->buffer, required + 2);
94 iobuf->buffer = new_buf;
95 iobuf->buflen = required;
99 struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback) {
100 struct IODescriptor *descriptor;
101 descriptor = iohandler_add(-1, IOTYPE_TIMER, callback);
102 if(!descriptor) return NULL;
103 descriptor->timeout = timeout;
104 engine->update(descriptor);
108 struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, const char *bindhost, iohandler_callback *callback) {
109 //non-blocking connect
111 struct addrinfo hints, *res, *freeres;
112 struct sockaddr_in *ip4 = NULL;
113 struct sockaddr_in6 *ip6 = NULL;
115 struct sockaddr *dstaddr = NULL;
116 struct IODescriptor *descriptor;
118 memset (&hints, 0, sizeof (hints));
119 hints.ai_family = PF_UNSPEC;
120 hints.ai_socktype = SOCK_STREAM;
121 hints.ai_flags |= AI_CANONNAME;
122 if (getaddrinfo (hostname, NULL, &hints, &res)) {
126 switch (res->ai_family) {
128 ip4 = (struct sockaddr_in *) res->ai_addr;
131 ip6 = (struct sockaddr_in6 *) res->ai_addr;
140 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
141 if(sockfd == -1) return NULL;
143 ip6->sin6_family = AF_INET6;
144 ip6->sin6_port = htons(port);
146 struct sockaddr_in6 *ip6vhost = NULL;
147 if (bindhost && !getaddrinfo(bindhost, NULL, &hints, &res)) {
149 switch (res->ai_family) {
151 ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
160 ip6vhost->sin6_family = AF_INET6;
161 ip6vhost->sin6_port = htons(0);
162 bind(sockfd, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
164 dstaddr = (struct sockaddr*)ip6;
165 dstaddrlen = sizeof(*ip6);
167 sockfd = socket(AF_INET, SOCK_STREAM, 0);
168 if(sockfd == -1) return NULL;
170 ip4->sin_family = AF_INET;
171 ip4->sin_port = htons(port);
173 struct sockaddr_in *ip4vhost = NULL;
174 if (bindhost && !getaddrinfo(bindhost, NULL, &hints, &res)) {
176 switch (res->ai_family) {
178 ip4vhost = (struct sockaddr_in *) res->ai_addr;
187 ip4vhost->sin_family = AF_INET;
188 ip4vhost->sin_port = htons(0);
189 bind(sockfd, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
191 dstaddr = (struct sockaddr*)ip4;
192 dstaddrlen = sizeof(*ip4);
195 //make sockfd unblocking
197 flags = fcntl(sockfd, F_GETFL);
198 fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
199 flags = fcntl(sockfd, F_GETFD);
200 fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
202 /* I hope you're using the Win32 backend or something else that
203 * automatically marks the file descriptor non-blocking...
206 descriptor = iohandler_add(sockfd, IOTYPE_CLIENT, callback);
211 connect(sockfd, dstaddr, dstaddrlen); //returns EINPROGRESS here (nonblocking)
212 descriptor->state = IO_CONNECTING;
213 descriptor->read_lines = 1;
214 engine->update(descriptor);
218 struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback) {
220 struct addrinfo hints, *res, *freeres;
221 struct sockaddr_in *ip4 = NULL;
222 struct sockaddr_in6 *ip6 = NULL;
223 struct IODescriptor *descriptor;
226 memset (&hints, 0, sizeof (hints));
227 hints.ai_family = PF_UNSPEC;
228 hints.ai_socktype = SOCK_STREAM;
229 hints.ai_flags |= AI_CANONNAME;
230 if (getaddrinfo (hostname, NULL, &hints, &res)) {
234 switch (res->ai_family) {
236 ip4 = (struct sockaddr_in *) res->ai_addr;
239 ip6 = (struct sockaddr_in6 *) res->ai_addr;
248 sockfd = socket(AF_INET6, SOCK_STREAM, 0);
249 if(sockfd == -1) return NULL;
252 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
254 ip6->sin6_family = AF_INET6;
255 ip6->sin6_port = htons(port);
257 bind(sockfd, (struct sockaddr*)ip6, sizeof(*ip6));
259 sockfd = socket(AF_INET, SOCK_STREAM, 0);
260 if(sockfd == -1) return NULL;
263 setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
265 ip4->sin_family = AF_INET;
266 ip4->sin_port = htons(port);
268 bind(sockfd, (struct sockaddr*)ip4, sizeof(*ip4));
271 //make sockfd unblocking
273 flags = fcntl(sockfd, F_GETFL);
274 fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
275 flags = fcntl(sockfd, F_GETFD);
276 fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
278 /* I hope you're using the Win32 backend or something else that
279 * automatically marks the file descriptor non-blocking...
282 descriptor = iohandler_add(sockfd, IOTYPE_SERVER, callback);
288 descriptor->state = IO_LISTENING;
289 engine->update(descriptor);
293 void iohandler_write(struct IODescriptor *iofd, const char *line) {
294 size_t linelen = strlen(line);
295 iohandler_send(iofd, line, linelen);
298 void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen) {
299 if(iofd->type == IOTYPE_TIMER) return; //can not write to timer? :D
300 if(iofd->writebuf.buflen < iofd->writebuf.bufpos + datalen) {
301 iohandler_increase_iobuf(&iofd->writebuf, iofd->writebuf.bufpos + datalen);
302 if(iofd->writebuf.buflen < iofd->writebuf.bufpos + datalen)
305 memcpy(iofd->writebuf.buffer + iofd->writebuf.bufpos, data, datalen);
306 iofd->writebuf.bufpos += datalen;
307 engine->update(iofd);
310 void iohandler_printf(struct IODescriptor *iofd, const char *text, ...) {
312 char sendBuf[LINELEN];
315 va_start(arg_list, text);
316 pos = vsnprintf(sendBuf, LINELEN - 2, text, arg_list);
318 if (pos < 0 || pos > (LINELEN - 2)) pos = LINELEN - 2;
320 sendBuf[pos+1] = '\0';
321 iohandler_send(iofd, sendBuf, pos+1);
324 void iohandler_try_write(struct IODescriptor *iofd) {
325 if(!iofd->writebuf.bufpos) return;
326 int res = send(iofd->fd, iofd->writebuf.buffer, iofd->writebuf.bufpos, 0);
328 if (errno != EAGAIN) {
329 //error: could not write
332 iofd->writebuf.bufpos -= res;
333 engine->update(iofd);
337 void iohandler_close(struct IODescriptor *iofd) {
339 if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN)
341 iohandler_remove(iofd);
344 void iohandler_update(struct IODescriptor *iofd) {
345 engine->update(iofd);
348 static void iohandler_trigger_event(struct IOEvent *event) {
349 if(!event->iofd->callback) return;
350 event->iofd->callback(event);
353 void iohandler_events(struct IODescriptor *iofd, int readable, int writeable) {
354 struct IOEvent callback_event;
355 callback_event.type = IOEVENT_IGNORE;
356 callback_event.iofd = iofd;
357 switch(iofd->state) {
359 if(iofd->type == IOTYPE_TIMER)
360 callback_event.type = IOEVENT_TIMEOUT;
363 callback_event.data.accept_fd = accept(iofd->fd, NULL, 0);
364 if(callback_event.data.accept_fd < 0) {
365 //error: could not accept
367 callback_event.type = IOEVENT_ACCEPT;
370 if(readable) { //could not connect
371 callback_event.type = IOEVENT_NOTCONNECTED;
373 arglen = sizeof(callback_event.data.errid);
374 if (getsockopt(iofd->fd, SOL_SOCKET, SO_ERROR, &callback_event.data.errid, &arglen) < 0)
375 callback_event.data.errid = errno;
376 iofd->state = IO_CLOSED;
377 } else if(writeable) {
378 callback_event.type = IOEVENT_CONNECTED;
379 iofd->state = IO_CONNECTED;
380 engine->update(iofd);
385 if(iofd->read_lines) {
386 int bytes = recv(iofd->fd, iofd->readbuf.buffer + iofd->readbuf.bufpos, iofd->readbuf.buflen - iofd->readbuf.bufpos, 0);
388 if (errno != EAGAIN) {
389 callback_event.type = IOEVENT_CLOSED;
390 callback_event.data.errid = errno;
393 int i, used_bytes = 0;
394 iofd->readbuf.bufpos += bytes;
395 callback_event.type = IOEVENT_RECV;
396 for(i = 0; i < iofd->readbuf.bufpos; i++) {
397 if(iofd->readbuf.buffer[i] == '\r' && iofd->readbuf.buffer[i+1] == '\n')
398 iofd->readbuf.buffer[i] = 0;
399 else if(iofd->readbuf.buffer[i] == '\n' || iofd->readbuf.buffer[i] == '\r') {
400 iofd->readbuf.buffer[i] = 0;
401 callback_event.data.recv_str = iofd->readbuf.buffer + used_bytes;
403 iohandler_trigger_event(&callback_event);
404 } else if(i + 1 - used_bytes >= LINELEN) { //512 max
405 iofd->readbuf.buffer[i] = 0;
406 callback_event.data.recv_str = iofd->readbuf.buffer + used_bytes;
407 for(; i < iofd->readbuf.bufpos; i++) { //skip the rest of the line
408 if(iofd->readbuf.buffer[i] == '\n' || (iofd->readbuf.buffer[i] == '\r' && iofd->readbuf.buffer[i+1] != '\n')) {
413 iohandler_trigger_event(&callback_event);
417 if(used_bytes == iofd->readbuf.bufpos)
418 iofd->readbuf.bufpos = 0;
420 memmove(iofd->readbuf.buffer, iofd->readbuf.buffer + used_bytes, iofd->readbuf.bufpos - used_bytes);
422 callback_event.type = IOEVENT_IGNORE;
425 callback_event.type = IOEVENT_READABLE;
428 iohandler_try_write(iofd);
432 if(callback_event.type != IOEVENT_IGNORE)
433 iohandler_trigger_event(&callback_event);
436 void iohandler_poll() {
438 struct timeval timeout;
439 timeout.tv_sec = IO_MAX_TIMEOUT;
441 engine->loop(&timeout);