An extensible multi-threaded TCP/TLS socket library
git clone git://soophie.de/sock
153
// @internal154
#define SOCK_SERVER_POLL_TIME 100156
// @impl sock_init157
sock_t *sock_init(void) {158
sock_t *self= (sock_t *) malloc(sizeof(sock_t));159
*self = (sock_t) {
167
return self;168
}170
// @impl sock_cleanup171
void sock_cleanup(sock_t *self) {172
if (self->binds != NULL) {173
for (size_t i = 0; i < self->binds_len; i++) {
185
free(self);186
}188
// @impl sock_set_ext189
void sock_set_ext(sock_t *self, void *ext) {190
self->ext = ext;191
}193
// @impl sock_on_global194
void sock_on_global(sock_t *self, sock_global_fn_t fn) {195
self->fn = fn;196
}198
// @impl sock_conn_read199
ssize_t sock_conn_read(sock_conn_t *conn, char *buffer, size_t len) {200
if (conn->ssl != NULL) {201
return SSL_read(conn->ssl, buffer, len);
203
return read(conn->sockfd, buffer, len);204
}206
// @impl sock_conn_write207
ssize_t sock_conn_write(sock_conn_t *conn, char *buffer, size_t len) {208
if (conn->ssl != NULL) {209
return SSL_write(conn->ssl, buffer, len);
326
return SSL_TLSEXT_ERR_NOACK;327
}329
// @impl sock_bind330
sock_err_t sock_bind(sock_t *self, char *hostname, int port, sock_host_t *hosts, int hosts_len) {331
self->binds = (sock_bind_t *) realloc(self->binds, sizeof(sock_bind_t) * (self->binds_len + 1));332
sock_bind_t *sock_bind = &self->binds[self->binds_len];
376
return SOCK_ERR_OK;377
}379
// @impl sock_listen380
sock_err_t sock_listen(sock_t *self) {381
signal(SIGPIPE, SIG_IGN);382
const SSL_METHOD *method = TLS_server_method();
389
return SOCK_ERR_OK;390
}392
// @impl sock_bind_listen393
sock_err_t sock_bind_listen(sock_t *self, char *hostname, int port) {394
sock_err_t err = sock_bind(self, hostname, port, NULL, 0);395
if (err != SOCK_ERR_OK) {
398
return sock_listen(self);399
}401
// @impl sock_close402
void sock_close(sock_t *self) {403
self->quit = true;404
pthread_join(self->thread, NULL);