#include <winsock2.h>
#include <ws2tcpip.h>
#else
+#include <signal.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
+#include <fcntl.h>
+#endif
+
+#ifndef EWOULDBLOCK
+#define EWOULDBLOCK EAGAIN
#endif
#define MAXLOG 1024
struct IODescriptor *first_descriptor = NULL;
struct IODescriptor *timer_priority = NULL;
+#ifdef HAVE_PTHREAD_H
+static pthread_mutex_t io_thread_sync;
+static pthread_mutex_t io_poll_sync;
+#endif
+
void iohandler_log(enum IOLogType type, char *text, ...) {
va_list arg_list;
char logBuf[MAXLOG+1];
extern struct IOEngine engine_epoll;
extern struct IOEngine engine_win32;
+int iohandler_settings = 0;
struct IOEngine *engine = NULL;
+void iohandler_set(int setting, int value) {
+ if(value)
+ iohandler_settings |= setting;
+ else
+ iohandler_settings &= ~setting;
+}
+
static void iohandler_init_engine() {
if(engine) return;
+ IOTHREAD_MUTEX_INIT(io_thread_sync);
+ IOTHREAD_MUTEX_INIT(io_poll_sync);
+
//try other engines
- if(!engine && engine_kevent.init && engine_kevent.init())
- engine = &engine_kevent;
- if(!engine && engine_epoll.init && engine_epoll.init())
- engine = &engine_epoll;
- if(!engine && engine_win32.init && engine_win32.init())
- engine = &engine_win32;
+ if(!(iohandler_settings & IOHANDLER_SETTING_HIGH_PRECISION_TIMER)) {
+ if(!engine && engine_kevent.init && engine_kevent.init())
+ engine = &engine_kevent;
+ if(!engine && engine_epoll.init && engine_epoll.init())
+ engine = &engine_epoll;
+ if(!engine && engine_win32.init && engine_win32.init())
+ engine = &engine_win32;
+ }
if (!engine) {
if(engine_select.init())
}
static void iohandler_append(struct IODescriptor *descriptor) {
+ IOSYNCHRONIZE(io_thread_sync);
struct timeval *timeout = ((descriptor->timeout.tv_sec || descriptor->timeout.tv_usec) ? &descriptor->timeout : NULL);
if(timeout) {
struct IODescriptor *iofd;
if(timeval_is_smaler(timeout, (&iofd->timeout))) {
descriptor->prev = iofd->prev;
descriptor->next = iofd;
- iofd->prev = descriptor;
if(iofd->prev)
iofd->prev->next = descriptor;
+ iofd->prev = descriptor;
if(set_priority)
timer_priority = descriptor;
break;
first_descriptor->prev = descriptor;
first_descriptor = descriptor;
}
+ IODESYNCHRONIZE(io_thread_sync);
}
static void iohandler_remove(struct IODescriptor *descriptor, int engine_remove) {
//remove IODescriptor from the list
+ IOSYNCHRONIZE(io_thread_sync);
if(descriptor->prev)
descriptor->prev->next = descriptor->next;
else
free(descriptor->writebuf.buffer);
iohandler_log(IOLOG_DEBUG, "removed IODescriptor (%d) of type `%s`", descriptor->fd, iohandler_iotype_name(descriptor->type));
free(descriptor);
+ IODESYNCHRONIZE(io_thread_sync);
}
struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback) {
descriptor->type = type;
descriptor->state = (type == IOTYPE_STDIN ? IO_CONNECTED : IO_CLOSED);
descriptor->callback = callback;
- if(timeout)
+ if(timeout) {
descriptor->timeout = *timeout;
+ if(descriptor->timeout.tv_usec > 1000000) {
+ descriptor->timeout.tv_usec -= 1000000;
+ descriptor->timeout.tv_sec++;
+ }
+ }
if(type != IOTYPE_TIMER) {
descriptor->readbuf.buffer = malloc(IO_READ_BUFLEN + 2);
descriptor->readbuf.bufpos = 0;
descriptor->next->prev = descriptor->prev;
if(descriptor == timer_priority)
timer_priority = descriptor->next;
- if(timeout)
+ if(timeout && timeout->tv_sec == 0 && descriptor->constant_timeout) {
+ descriptor->timeout.tv_usec += (descriptor->constant_timeout % 1000) * 1000;
+ descriptor->timeout.tv_sec += (descriptor->constant_timeout / 1000);
+ if(descriptor->timeout.tv_usec > 1000000) {
+ descriptor->timeout.tv_sec += (descriptor->timeout.tv_usec / 1000000);
+ descriptor->timeout.tv_usec %= 1000000;
+ }
+ } else if(timeout) {
descriptor->timeout = *timeout;
- else {
+ if(descriptor->timeout.tv_usec > 1000000) {
+ descriptor->timeout.tv_usec -= 1000000;
+ descriptor->timeout.tv_sec++;
+ }
+ } else {
descriptor->timeout.tv_sec = 0;
descriptor->timeout.tv_usec = 0;
}
return descriptor;
}
+struct IODescriptor *iohandler_constant_timer(int msec, iohandler_callback *callback) {
+ struct IODescriptor *descriptor;
+ struct timeval timeout;
+ gettimeofday(&timeout, NULL);
+ timeout.tv_usec += (msec % 1000) * 1000;
+ timeout.tv_sec += (msec / 1000);
+ if(timeout.tv_usec > 1000000) {
+ timeout.tv_sec += (timeout.tv_usec / 1000000);
+ timeout.tv_usec %= 1000000;
+ }
+ descriptor = iohandler_add(-1, IOTYPE_TIMER, &timeout, callback);
+ if(!descriptor) {
+ iohandler_log(IOLOG_ERROR, "could not allocate memory for IODescriptor in %s:%d", __FILE__, __LINE__);
+ return NULL;
+ }
+ descriptor->constant_timeout = msec;
+ iohandler_log(IOLOG_DEBUG, "added timer descriptor (sec: %d; usec: %d)", timeout.tv_sec, timeout.tv_usec);
+ return descriptor;
+}
+
struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback) {
+ return iohandler_connect_flags(hostname, port, ssl, bindhost, callback, IOHANDLER_CONNECT_IPV4 | IOHANDLER_CONNECT_IPV6);
+}
+
+struct IODescriptor *iohandler_connect_flags(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback, int flags) {
//non-blocking connect
int sockfd, result;
struct addrinfo hints, *res;
freeaddrinfo(res);
}
- if(ip6) {
+ if(ip6 && (flags & IOHANDLER_CONNECT_IPV6)) {
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if(sockfd == -1) {
iohandler_log(IOLOG_ERROR, "could not create socket in %s:%d", __FILE__, __LINE__);
}
dstaddr = (struct sockaddr*)ip6;
dstaddrlen = sizeof(*ip6);
- } else if(ip4) {
+ } else if(ip4 && (flags & IOHANDLER_CONNECT_IPV4)) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) {
iohandler_log(IOLOG_ERROR, "could not create socket in %s:%d", __FILE__, __LINE__);
dstaddrlen = sizeof(*ip4);
} else
return NULL;
+ //prevent SIGPIPE
+ #ifndef WIN32
+ #if defined(SO_NOSIGPIPE)
+ {
+ int set = 1;
+ setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
+ }
+ #else
+ signal(SIGPIPE, SIG_IGN);
+ #endif
+ #endif
//make sockfd unblocking
-#if defined(F_GETFL)
+ #if defined(F_GETFL)
{
- int flags;
- flags = fcntl(sockfd, F_GETFL);
- fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
- flags = fcntl(sockfd, F_GETFD);
- fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
+ int fcntl_flags;
+ fcntl_flags = fcntl(sockfd, F_GETFL);
+ fcntl(sockfd, F_SETFL, fcntl_flags|O_NONBLOCK);
+ fcntl_flags = fcntl(sockfd, F_GETFD);
+ fcntl(sockfd, F_SETFD, fcntl_flags|FD_CLOEXEC);
}
-#else
+ #else
/* I hope you're using the Win32 backend or something else that
* automatically marks the file descriptor non-blocking...
*/
-#endif
+ #endif
descriptor = iohandler_add(sockfd, IOTYPE_CLIENT, NULL, callback);
if(!descriptor) {
close(sockfd);
}
struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback) {
+ return iohandler_listen_flags(hostname, port, callback, IOHANDLER_LISTEN_IPV4 | IOHANDLER_LISTEN_IPV6);
+}
+
+struct IODescriptor *iohandler_listen_flags(const char *hostname, unsigned int port, iohandler_callback *callback, int flags) {
int sockfd;
struct addrinfo hints, *res;
struct sockaddr_in *ip4 = NULL;
freeaddrinfo(res);
}
- if(ip6) {
+ if(ip6 && (flags & IOHANDLER_LISTEN_IPV6)) {
sockfd = socket(AF_INET6, SOCK_STREAM, 0);
if(sockfd == -1) return NULL;
ip6->sin6_port = htons(port);
bind(sockfd, (struct sockaddr*)ip6, sizeof(*ip6));
- } else if(ip4) {
+ } else if(ip4 && (flags && IOHANDLER_LISTEN_IPV4)) {
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if(sockfd == -1) return NULL;
bind(sockfd, (struct sockaddr*)ip4, sizeof(*ip4));
} else
return NULL;
+ //prevent SIGPIPE
+ #ifndef WIN32
+ #if defined(SO_NOSIGPIPE)
+ {
+ int set = 1;
+ setsockopt(sockfd, SOL_SOCKET, SO_NOSIGPIPE, (void *)&set, sizeof(int));
+ }
+ #else
+ signal(SIGPIPE, SIG_IGN);
+ #endif
+ #endif
//make sockfd unblocking
-#if defined(F_GETFL)
+ #if defined(F_GETFL)
{
- int flags;
- flags = fcntl(sockfd, F_GETFL);
- fcntl(sockfd, F_SETFL, flags|O_NONBLOCK);
- flags = fcntl(sockfd, F_GETFD);
- fcntl(sockfd, F_SETFD, flags|FD_CLOEXEC);
+ int flag;
+ flag = fcntl(sockfd, F_GETFL);
+ fcntl(sockfd, F_SETFL, flag|O_NONBLOCK);
+ flag = fcntl(sockfd, F_GETFD);
+ fcntl(sockfd, F_SETFD, flag|FD_CLOEXEC);
}
-#else
+ #else
/* I hope you're using the Win32 backend or something else that
* automatically marks the file descriptor non-blocking...
*/
-#endif
+ #endif
descriptor = iohandler_add(sockfd, IOTYPE_SERVER, NULL, callback);
if(!descriptor) {
close(sockfd);
return descriptor;
}
+struct IODescriptor *iohandler_listen_ssl(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iohandler_callback *callback) {
+ return iohandler_listen_ssl_flags(hostname, port, certfile, keyfile, callback, IOHANDLER_LISTEN_IPV4 | IOHANDLER_LISTEN_IPV6);
+}
+
+struct IODescriptor *iohandler_listen_ssl_flags(const char *hostname, unsigned int port, const char *certfile, const char *keyfile, iohandler_callback *callback, int flags) {
+ struct IODescriptor *descriptor = iohandler_listen_flags(hostname, port, callback, flags);
+ if(!descriptor)
+ return NULL;
+ //SSL Server Socket
+ iohandler_ssl_listen(descriptor, certfile, keyfile);
+ if(descriptor->sslnode)
+ descriptor->ssl = 1;
+ return descriptor;
+}
+
void iohandler_write(struct IODescriptor *iofd, const char *line) {
size_t linelen = strlen(line);
iohandler_send(iofd, line, linelen);
iohandler_send(iofd, sendBuf, pos+1);
}
-void iohandler_try_write(struct IODescriptor *iofd) {
- if(!iofd->writebuf.bufpos) return;
+static int iohandler_try_write(struct IODescriptor *iofd) {
+ if(!iofd->writebuf.bufpos) return 0;
iohandler_log(IOLOG_DEBUG, "write writebuf (%d bytes) to socket (fd: %d)", iofd->writebuf.bufpos, iofd->fd);
int res;
if(iofd->ssl_active)
else
res = send(iofd->fd, iofd->writebuf.buffer, iofd->writebuf.bufpos, 0);
if(res < 0) {
- if (errno != EAGAIN) {
+ if (errno != EAGAIN && errno != EWOULDBLOCK)
iohandler_log(IOLOG_ERROR, "could not write to socket (fd: %d): %d - %s", iofd->fd, errno, strerror(errno));
- }
+ else
+ res = 0;
} else {
iofd->writebuf.bufpos -= res;
if(iofd->state != IO_CLOSED)
engine->update(iofd);
}
+ return res;
}
void iohandler_close(struct IODescriptor *iofd) {
switch(iofd->state) {
case IO_SSLWAIT:
if(!readable && !writeable) {
- callback_event.type = IOEVENT_SSLFAILED;
- iofd->state = IO_CLOSED;
+ if(!iofd->ssl_server_hs) {
+ callback_event.type = IOEVENT_SSLFAILED;
+ iofd->state = IO_CLOSED;
+ engine->update(iofd);
+ } else
+ iohandler_close(iofd);
+ } else if(iofd->ssl_server_hs) {
+ iohandler_log(IOLOG_DEBUG, "triggering iohandler_ssl_server_handshake for %s (fd: %d)", iohandler_iotype_name(iofd->type), iofd->fd);
+ iohandler_ssl_server_handshake(iofd);
} else {
iohandler_log(IOLOG_DEBUG, "triggering iohandler_ssl_client_handshake for %s (fd: %d)", iohandler_iotype_name(iofd->type), iofd->fd);
iohandler_ssl_client_handshake(iofd);
callback_event.data.accept_fd = accept(iofd->fd, NULL, 0);
if(callback_event.data.accept_fd < 0) {
iohandler_log(IOLOG_ERROR, "could not accept client (server fd: %d): %d - %s", iofd->fd, errno, strerror(errno));
+ } else if(iofd->ssl) {
+ struct IODescriptor *client_iofd = iohandler_add(callback_event.data.accept_fd, IOTYPE_CLIENT, NULL, NULL);
+ iohandler_ssl_client_accepted(iofd, client_iofd);
} else
callback_event.type = IOEVENT_ACCEPT;
}
iohandler_ssl_connect(iofd);
return;
}
- callback_event.type = IOEVENT_CONNECTED;
+ if(iofd->ssl && iofd->ssl_server_hs) {
+ callback_event.type = IOEVENT_SSLACCEPT;
+ callback_event.iofd = iofd->data;
+ callback_event.data.accept_iofd = iofd;
+ iofd->data = NULL;
+ }
+ else
+ callback_event.type = IOEVENT_CONNECTED;
iofd->state = IO_CONNECTED;
engine->update(iofd);
}
bytes = recv(iofd->fd, iofd->readbuf.buffer + iofd->readbuf.bufpos, iofd->readbuf.buflen - iofd->readbuf.bufpos, 0);
}
if(bytes <= 0) {
- if (errno != EAGAIN) {
+ if (errno != EAGAIN || errno != EWOULDBLOCK) {
iofd->state = IO_CLOSED;
engine->update(iofd);
callback_event.type = IOEVENT_CLOSED;
callback_event.type = IOEVENT_READABLE;
}
if(writeable) {
- iohandler_try_write(iofd);
+ int bytes;
+ bytes = iohandler_try_write(iofd);
+ if(bytes < 0) {
+ iofd->state = IO_CLOSED;
+ engine->update(iofd);
+ callback_event.type = IOEVENT_CLOSED;
+ callback_event.data.errid = errno;
+ }
}
break;
}
}
void iohandler_poll() {
+ struct timeval timeout;
+ timeout.tv_sec = IO_MAX_TIMEOUT;
+ timeout.tv_usec = 0;
+ iohandler_poll_timeout(timeout);
+}
+
+void iohandler_poll_timeout(struct timeval timeout) {
if(engine) {
- struct timeval timeout;
- timeout.tv_sec = IO_MAX_TIMEOUT;
- timeout.tv_usec = 0;
+ IOSYNCHRONIZE(io_poll_sync); //quite senceless multithread support... better support will follow
engine->loop(&timeout);
+ IODESYNCHRONIZE(io_poll_sync);
}
}
return "IOEVENT_CLOSED";
case IOEVENT_ACCEPT:
return "IOEVENT_ACCEPT";
+ case IOEVENT_SSLACCEPT:
+ return "IOEVENT_SSLACCEPT";
case IOEVENT_TIMEOUT:
return "IOEVENT_TIMEOUT";
default: