+/* IOHandler.c - TransparentIRC 0.1
+ * Copyright (C) 2011-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 <http://www.gnu.org/licenses/>.
+ */
+
+struct IODescriptor *first_descriptor = NULL;
+
+/* IO Engines */
+extern struct IOEngine engine_select; /* select system call (should always be useable) */
+
+struct IOEngine *engine = NULL;
+
+static void iohandler_init_engine() {
+ //try other engines
+
+ if(engine)
+ //found an useable IO engine
+ else if (engine_select.init())
+ engine = &engine_select;
+ else {
+ //STDERR: found no useable IO engine
+ }
+}
+
+struct IODescriptor *iohandler_add(int sockfd, IOType type, iohandler_callback *callback) {
+ //just add a new IODescriptor
+ struct IODescriptor *descriptor = calloc(1, sizeof(*descriptor));
+ if(!descriptor) return NULL;
+ descriptor->fd = sockfd;
+ descriptor->type = type;
+ descriptor->state = IO_CLOSED;
+ descriptor->callback = callback;
+ if(type != IOTYPE_TIMER) {
+ descriptor->readbuf.buffer = malloc(IOREADBUFLEN + 2);
+ descriptor->readbuf.bufpos = 0;
+ descriptor->readbuf.buflen = IOREADBUFLEN;
+ descriptor->writebuf.buffer = malloc(IOREADBUFLEN + 2);
+ descriptor->writebuf.bufpos = 0;
+ descriptor->writebuf.buflen = IOREADBUFLEN;
+ }
+
+ if(!engine) {
+ iohandler_init_engine();
+ if(!engine) {
+ return NULL;
+ }
+ }
+ engine->add(descriptor);
+
+ //add IODescriptor to the list
+ descriptor->prev = NULL;
+ descriptor->next = first_descriptor;
+ first_descriptor->prev = descriptor;
+ first_descriptor = descriptor;
+
+ return descriptor;
+}
+
+static void iohandler_remove(struct IODescriptor *descriptor) {
+ //remove IODescriptor from the list
+ if(descriptor->prev)
+ descriptor->prev->next = descriptor->next;
+ else
+ first_descriptor = descriptor->next;
+ if(descriptor->next)
+ descriptor->next->prev = descriptor->prev;
+ engine->remove(descriptor);
+ if(descriptor->readbuf.buffer)
+ free(descriptor->readbuf.buffer);
+ if(descriptor->writebuf.buffer)
+ free(descriptor->writebuf.buffer);
+ free(descriptor);
+}
+
+static void iohandler_increase_iobuf(struct IOBuffer *iobuf, size_t required) {
+ if(iobuf->buflen >= required) return;
+ char *new_buf = realloc(iobuf->buffer, required + 2);
+ if(new_buf) {
+ iobuf->buffer = new_buf;
+ iobuf->buflen = required;
+ }
+}
+
+struct IODescriptor *iohandler_timer(timeval timeout, iohandler_callback *callback) {
+ struct IODescriptor *descriptor;
+ descriptor = iohandler_add(-1, IOTYPE_TIMER, callback);
+ if(!descriptor) return NULL;
+ descriptor->timeout = timeout;
+ engine->update(descriptor);
+ return descriptor;
+}
+
+struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, const char *bind, iohandler_callback *callback) {
+ //non-blocking connect
+ int sockfd;
+ struct addrinfo hints, *res, *freeres;
+ struct sockaddr_in *ip4 = NULL;
+ struct sockaddr_in6 *ip6 = NULL;
+ size_t dstaddrlen;
+ struct sockaddr *dstaddr = NULL;
+ struct IODescriptor *descriptor;
+
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags |= AI_CANONNAME;
+ if (getaddrinfo (hostname, NULL, &hints, &res)) {
+ return NULL;
+ }
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET:
+ ip4 = (struct sockaddr_in *) res->ai_addr;
+ break;
+ case AF_INET6:
+ ip6 = (struct sockaddr_in6 *) res->ai_addr;
+ break;
+ }
+ freeres = res;
+ res = res->ai_next;
+ freeaddrinfo(res);
+ }
+
+ if(ip6) {
+ sockfd = socket(AF_INET6, SOCK_STREAM, 0);
+ if(sockfd == -1) return NULL;
+
+ ip6->sin6_family = AF_INET6;
+ ip6->sin6_port = htons(port);
+
+ struct sockaddr_in6 *ip6vhost = NULL;
+ if (bind && !getaddrinfo(bind, NULL, &hints, &res)) {
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET6:
+ ip6vhost = (struct sockaddr_in6 *) res->ai_addr;
+ break;
+ }
+ freeres = res;
+ res = res->ai_next;
+ freeaddrinfo(res);
+ }
+ }
+ if(ip6vhost) {
+ ip6vhost->sin6_family = AF_INET6;
+ ip6vhost->sin6_port = htons(0);
+ bind(sockfd, (struct sockaddr*)ip6vhost, sizeof(*ip6vhost));
+ }
+ dstaddr = (struct sockaddr*)ip6;
+ dstaddrlen = sizeof(*ip6);
+ } else if(ip4) {
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if(sockfd == -1) return NULL;
+
+ ip4->sin_family = AF_INET;
+ ip4->sin_port = htons(port);
+
+ struct sockaddr_in *ip4vhost = NULL;
+ if (bind && !getaddrinfo(bind, NULL, &hints, &res)) {
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET:
+ ip4vhost = (struct sockaddr_in *) res->ai_addr;
+ break;
+ }
+ freeres = res;
+ res = res->ai_next;
+ freeaddrinfo(res);
+ }
+ }
+ if(ip4vhost) {
+ ip4vhost->sin_family = AF_INET;
+ ip4vhost->sin_port = htons(0);
+ bind(sockfd, (struct sockaddr*)ip4vhost, sizeof(*ip4vhost));
+ }
+ dstaddr = (struct sockaddr*)ip4;
+ dstaddrlen = sizeof(*ip4);
+ } else
+ return NULL;
+ //make sockfd unblocking
+#if defined(F_GETFL)
+ flags = fcntl(sockfd, F_GETFL);
+ fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
+ flags = fcntl(sockfd, F_GETFD);
+ fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
+#else
+ /* I hope you're using the Win32 backend or something else that
+ * automatically marks the file descriptor non-blocking...
+ */
+ (void)flags;
+#endif
+ descriptor = iohandler_add(sockfd, IOTYPE_CLIENT, callback);
+ if(!descriptor) {
+ close(sockfd);
+ return NULL;
+ }
+ connect(sockfd, dstaddr, dstaddrlen); //returns EINPROGRESS here (nonblocking)
+ descriptor->state = IO_CONNECTING;
+ descriptor->read_lines = 1;
+ engine->update(descriptor);
+}
+
+struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback) {
+ int sockfd;
+ struct addrinfo hints, *res, *freeres;
+ struct sockaddr_in *ip4 = NULL;
+ struct sockaddr_in6 *ip6 = NULL;
+ struct IODescriptor *descriptor;
+ unsigned int opt;
+
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_family = PF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ hints.ai_flags |= AI_CANONNAME;
+ if (getaddrinfo (hostname, NULL, &hints, &res)) {
+ return NULL;
+ }
+ while (res) {
+ switch (res->ai_family) {
+ case AF_INET:
+ ip4 = (struct sockaddr_in *) res->ai_addr;
+ break;
+ case AF_INET6:
+ ip6 = (struct sockaddr_in6 *) res->ai_addr;
+ break;
+ }
+ freeres = res;
+ res = res->ai_next;
+ freeaddrinfo(res);
+ }
+
+ if(ip6) {
+ sockfd = socket(AF_INET6, SOCK_STREAM, 0);
+ if(sockfd == -1) return NULL;
+
+ opt = 1;
+ setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
+
+ ip6->sin6_family = AF_INET6;
+ ip6->sin6_port = htons(port);
+
+ bind(sockfd, (struct sockaddr*)ip6, sizeof(*ip6));
+ } else if(ip4) {
+ sockfd = socket(AF_INET, SOCK_STREAM, 0);
+ if(sockfd == -1) return NULL;
+
+ opt = 1;
+ setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const char*)&opt, sizeof(opt));
+
+ ip4->sin_family = AF_INET;
+ ip4->sin_port = htons(port);
+
+ bind(sockfd, (struct sockaddr*)ip4, sizeof(*ip4));
+ } else
+ return NULL;
+ //make sockfd unblocking
+#if defined(F_GETFL)
+ flags = fcntl(sockfd, F_GETFL);
+ fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
+ flags = fcntl(sockfd, F_GETFD);
+ fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
+#else
+ /* I hope you're using the Win32 backend or something else that
+ * automatically marks the file descriptor non-blocking...
+ */
+ (void)flags;
+#endif
+ descriptor = iohandler_add(sockfd, IOTYPE_SERVER, callback);
+ if(!descriptor) {
+ close(sockfd);
+ return NULL;
+ }
+ listen(sockfd, 1);
+ descriptor->state = IO_LISTENING;
+ engine->update(descriptor);
+}
+
+void iohandler_write(struct IODescriptor *iofd, const char *line) {
+ size_t linelen = strlen(line);
+ iohandler_send(iofd, line, linelen);
+}
+
+void iohandler_send(struct IODescriptor *iofd, const char *data, size_t datalen) {
+ if(iofd->type == IOTYPE_TIMER) return; //can not write to timer? :D
+ if(iofd->writebuf.buflen < iofd->writebuf.bufpos + datalen) {
+ iohandler_increase_iobuf(&iofd->writebuf, iofd->writebuf.bufpos + datalen);
+ if(iofd->writebuf.buflen < iofd->writebuf.bufpos + datalen)
+ return;
+ }
+ memcpy(iofd->writebuf.buffer + iofd->writebuf.bufpos, data, datalen);
+ engine->update(iofd);
+}
+
+void iohandler_printf(struct IODescriptor *iofd, const char *text, ...) {
+ va_list arg_list;
+ char sendBuf[LINELEN];
+ int pos;
+ sendBuf[0] = '\0';
+ va_start(arg_list, text);
+ pos = vsnprintf(sendBuf, LINELEN - 2, text, arg_list);
+ va_end(arg_list);
+ if (pos < 0 || pos > (LINELEN - 2)) pos = LINELEN - 2;
+ sendBuf[pos] = '\n';
+ sendBuf[pos+1] = '\0';
+ iohandler_send(iofd, sendBuf, pos+1);
+}
+
+void iohandler_try_write(struct IODescriptor *iofd) {
+ if(!iofd->writebuf.bufpos) return;
+ int res = send(iofd->fd, iofd->writebuf.buffer, iofd->writebuf.bufpos, 0);
+ if(res < 0) {
+ if (errno != EAGAIN) {
+ //error: could not write
+ }
+ } else {
+ iofd->writebuf.bufpos -= res;
+ engine->update(iofd);
+ }
+}
+
+void iohandler_close(struct IODescriptor *iofd) {
+ //close IODescriptor
+ if(iofd->type == IOTYPE_SERVER || iofd->type == IOTYPE_CLIENT || iofd->type == IOTYPE_STDIN)
+ close(iofd->fd);
+ iohandler_remove(iofd);
+}
+
+void iohandler_update(struct IODescriptor *iofd) {
+ engine->update(iofd);
+}
+
+static void iohandler_trigger_event(struct IOEvent *event) {
+ if(!event->iofd->callback) return;
+ event->iofd->callback(event);
+}
+
+void iohandler_events(struct IODescriptor *iofd, int readable, int writeable) {
+ struct IOEvent callback_event;
+ callback_event.type = IOEVENT_IGNORE;
+ callback_event.iofd = iofd;
+ switch(iofd->state) {
+ case IO_CLOSED:
+ if(iofd->type == IOTYPE_TIMER)
+ callback_event.type = IOEVENT_TIMEOUT;
+ break;
+ case IO_LISTENING:
+ callback_event.data.accept_fd = accept(iofd->fd, NULL, 0);
+ if(callback_event.data.accept_fd < 0) {
+ //error: could not accept
+ } else
+ callback_event.type = IOEVENT_ACCEPT;
+ break;
+ case IO_CONNECTING:
+ if(readable) { //could not connect
+ callback_event.type = IOEVENT_NOTCONNECTED;
+ socklen_t arglen;
+ arglen = sizeof(callback_event.errno);
+ if (getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &callback_event.errno, &arglen) < 0)
+ callback_event.data.errno = errno;
+ iofd->state = IO_CLOSED;
+ } else if(writeable) {
+ callback_event.type = IOEVENT_CONNECTED;
+ iofd->state = IO_CONNECTED;
+ engine->update(iofd);
+ }
+ break;
+ case IO_CONNECTED:
+ if(readable) {
+ if(iofd->read_lines) {
+ int bytes = recv(iofd->fd, iofd->readbuf.buffer + iofd->readbuf.bufpos, iofd->readbuf.buflen - iofd->readbuf.bufpos, 0);
+ if(bytes <= 0) {
+ if (errno != EAGAIN) {
+ callback_event.type = IOEVENT_CLOSED;
+ callback_event.data.errno = errno;
+ }
+ } else {
+ int i, used_bytes = 0, buffer_offset = 0;
+ iofd->readbuf.bufpos += bytes;
+ callback_event.type = IOEVENT_RECV;
+ for(i = 0; i < iofd->readbuf.bufpos; i++) {
+ if(iofd->readbuf.buffer[i] == '\r' && iofd->readbuf.buffer[i+1] == '\n')
+ iofd->readbuf.buffer[i] = 0;
+ else if(iofd->readbuf.buffer[i] == '\n' || iofd->readbuf.buffer[i] == '\r') {
+ iofd->readbuf.buffer[i] = 0;
+ callback_event.data.recv_str = iofd->readbuf.buffer + used_bytes;
+ used_bytes = i+1;
+ iohandler_trigger_event(&callback_event);
+ } else if(i + 1 - used_bytes >= LINELEN) { //512 max
+ iofd->readbuf.buffer[i] = 0;
+ callback_event.data.recv_str = iofd->readbuf.buffer + used_bytes;
+ for(; i < iofd->readbuf.bufpos; i++) { //skip the rest of the line
+ if(iofd->readbuf.buffer[i] == '\n' || (iofd->readbuf.buffer[i] == '\r' && iofd->readbuf.buffer[i+1] != '\n')) {
+ break;
+ }
+ }
+ used_bytes = i+1;
+ iohandler_trigger_event(&callback_event);
+ }
+ }
+ if(used_bytes) {
+ if(used_bytes == iofd->readbuf.bufpos)
+ iofd->readbuf.bufpos = 0;
+ else
+ memmove(iofd->readbuf.buffer, iofd->readbuf.buffer + used_bytes, iofd->readbuf.bufpos - used_bytes);
+ }
+ callback_event.type = IOEVENT_IGNORE;
+ }
+ } else
+ callback_event.type = IOEVENT_READABLE;
+ }
+ if(writeable) {
+ iohandler_try_write(iofd);
+ }
+ break;
+ }
+ if(callback_event.type != IOEVENT_IGNORE)
+ iohandler_trigger_event(&callback_event);
+}
+
+void iohandler_poll() {
+ if(engine) {
+ struct timeval timeout;
+ timeout.tv_sec = IO_MAX_TIMEOUT;
+ timeout.tv_usec = 0;
+ engine->loop(&timeout);
+ }
+}
+