mirror of https://git.kore.io/kore.git
278 lines
5.6 KiB
C
278 lines
5.6 KiB
C
/*
|
|
* Copyright (c) 2013 Joris Vink <joris@coders.se>
|
|
*
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
* copyright notice and this permission notice appear in all copies.
|
|
*
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
|
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
|
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
|
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
*/
|
|
|
|
#include <sys/param.h>
|
|
#include <sys/types.h>
|
|
#include <sys/socket.h>
|
|
#include <sys/queue.h>
|
|
#include <sys/epoll.h>
|
|
|
|
#include <netinet/in.h>
|
|
#include <arpa/inet.h>
|
|
|
|
#include <openssl/err.h>
|
|
#include <openssl/ssl.h>
|
|
|
|
#include <errno.h>
|
|
#include <fcntl.h>
|
|
#include <stdarg.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <zlib.h>
|
|
|
|
#include "spdy.h"
|
|
#include "kore.h"
|
|
|
|
void
|
|
net_send_queue(struct connection *c, u_int8_t *data, size_t len, int flags,
|
|
struct netbuf **out, int (*cb)(struct netbuf *))
|
|
{
|
|
struct netbuf *nb;
|
|
|
|
//kore_log("net_send_queue(%p, %p, %d, %p)", c, data, len, cb);
|
|
|
|
nb = (struct netbuf *)kore_malloc(sizeof(*nb));
|
|
nb->cb = cb;
|
|
nb->len = len;
|
|
nb->owner = c;
|
|
nb->offset = 0;
|
|
nb->flags = flags;
|
|
nb->type = NETBUF_SEND;
|
|
|
|
if (len > 0) {
|
|
nb->buf = (u_int8_t *)kore_malloc(nb->len);
|
|
memcpy(nb->buf, data, nb->len);
|
|
} else {
|
|
nb->buf = NULL;
|
|
}
|
|
|
|
TAILQ_INSERT_TAIL(&(c->send_queue), nb, list);
|
|
if (out != NULL)
|
|
*out = nb;
|
|
}
|
|
|
|
void
|
|
net_recv_queue(struct connection *c, size_t len, int flags,
|
|
struct netbuf **out, int (*cb)(struct netbuf *))
|
|
{
|
|
struct netbuf *nb;
|
|
|
|
//kore_log("net_recv_queue(%p, %d, %p)", c, len, cb);
|
|
|
|
nb = (struct netbuf *)kore_malloc(sizeof(*nb));
|
|
nb->cb = cb;
|
|
nb->len = len;
|
|
nb->owner = c;
|
|
nb->offset = 0;
|
|
nb->flags = flags;
|
|
nb->type = NETBUF_RECV;
|
|
nb->buf = (u_int8_t *)kore_malloc(nb->len);
|
|
|
|
TAILQ_INSERT_TAIL(&(c->recv_queue), nb, list);
|
|
if (out != NULL)
|
|
*out = nb;
|
|
}
|
|
|
|
int
|
|
net_recv_expand(struct connection *c, struct netbuf *nb, size_t len,
|
|
int (*cb)(struct netbuf *))
|
|
{
|
|
//kore_log("net_recv_expand(%p, %p, %d, %p)", c, nb, len, cb);
|
|
|
|
if (nb->type != NETBUF_RECV) {
|
|
kore_log("net_recv_expand(): wrong netbuf type");
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
|
|
nb->cb = cb;
|
|
nb->len += len;
|
|
nb->buf = (u_int8_t *)kore_realloc(nb->buf, nb->len);
|
|
TAILQ_INSERT_HEAD(&(c->recv_queue), nb, list);
|
|
|
|
return (KORE_RESULT_OK);
|
|
}
|
|
|
|
int
|
|
net_send(struct connection *c)
|
|
{
|
|
int r;
|
|
struct netbuf *nb;
|
|
|
|
if (TAILQ_EMPTY(&(c->send_queue)))
|
|
return (KORE_RESULT_OK);
|
|
|
|
nb = TAILQ_FIRST(&(c->send_queue));
|
|
if (nb->len == 0) {
|
|
kore_log("net_send(): len is 0");
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
|
|
r = SSL_write(c->ssl, (nb->buf + nb->offset), (nb->len - nb->offset));
|
|
|
|
kore_log("net_send(%ld/%ld bytes), progress with %d",
|
|
nb->offset, nb->len, r);
|
|
|
|
if (r <= 0) {
|
|
r = SSL_get_error(c->ssl, r);
|
|
switch (r) {
|
|
case SSL_ERROR_WANT_READ:
|
|
case SSL_ERROR_WANT_WRITE:
|
|
c->flags &= ~CONN_WRITE_POSSIBLE;
|
|
return (KORE_RESULT_OK);
|
|
default:
|
|
kore_log("SSL_write(): %s", ssl_errno_s);
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
}
|
|
|
|
nb->offset += (size_t)r;
|
|
if (nb->offset == nb->len) {
|
|
if (nb->offset == nb->len)
|
|
TAILQ_REMOVE(&(c->send_queue), nb, list);
|
|
|
|
if (nb->cb != NULL)
|
|
r = nb->cb(nb);
|
|
else
|
|
r = KORE_RESULT_OK;
|
|
|
|
if (nb->offset == nb->len) {
|
|
free(nb->buf);
|
|
free(nb);
|
|
}
|
|
} else {
|
|
r = KORE_RESULT_OK;
|
|
}
|
|
|
|
return (r);
|
|
}
|
|
|
|
int
|
|
net_send_flush(struct connection *c)
|
|
{
|
|
kore_log("net_send_flush(%p)", c);
|
|
|
|
while (!TAILQ_EMPTY(&(c->send_queue)) &&
|
|
(c->flags & CONN_WRITE_POSSIBLE)) {
|
|
if (!net_send(c))
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
|
|
return (KORE_RESULT_OK);
|
|
}
|
|
|
|
int
|
|
net_recv(struct connection *c)
|
|
{
|
|
int r;
|
|
struct netbuf *nb;
|
|
|
|
if (TAILQ_EMPTY(&(c->recv_queue)))
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
nb = TAILQ_FIRST(&(c->recv_queue));
|
|
r = SSL_read(c->ssl, (nb->buf + nb->offset), (nb->len - nb->offset));
|
|
|
|
kore_log("net_recv(%ld/%ld bytes), progress with %d",
|
|
nb->offset, nb->len, r);
|
|
|
|
if (r <= 0) {
|
|
r = SSL_get_error(c->ssl, r);
|
|
switch (r) {
|
|
case SSL_ERROR_WANT_READ:
|
|
case SSL_ERROR_WANT_WRITE:
|
|
c->flags &= ~CONN_READ_POSSIBLE;
|
|
return (KORE_RESULT_OK);
|
|
default:
|
|
kore_log("SSL_read(): %s", ssl_errno_s);
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
}
|
|
|
|
nb->offset += (size_t)r;
|
|
if (nb->offset == nb->len || (nb->flags & NETBUF_CALL_CB_ALWAYS)) {
|
|
if (nb->cb == NULL) {
|
|
kore_log("kore_read_client(): nb->cb == NULL");
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
|
|
r = nb->cb(nb);
|
|
if (nb->offset == nb->len ||
|
|
(nb->flags & NETBUF_FORCE_REMOVE)) {
|
|
TAILQ_REMOVE(&(c->recv_queue), nb, list);
|
|
|
|
if (!(nb->flags & NETBUF_RETAIN)) {
|
|
free(nb->buf);
|
|
free(nb);
|
|
}
|
|
}
|
|
} else {
|
|
r = KORE_RESULT_OK;
|
|
}
|
|
|
|
return (r);
|
|
}
|
|
|
|
int
|
|
net_recv_flush(struct connection *c)
|
|
{
|
|
kore_log("net_recv_flush(%p)", c);
|
|
|
|
while (!TAILQ_EMPTY(&(c->recv_queue)) &&
|
|
(c->flags & CONN_READ_POSSIBLE)) {
|
|
if (!net_recv(c))
|
|
return (KORE_RESULT_ERROR);
|
|
}
|
|
|
|
return (KORE_RESULT_OK);
|
|
}
|
|
|
|
u_int16_t
|
|
net_read16(u_int8_t *b)
|
|
{
|
|
u_int16_t r;
|
|
|
|
r = *(u_int16_t *)b;
|
|
return (ntohs(r));
|
|
}
|
|
|
|
u_int32_t
|
|
net_read32(u_int8_t *b)
|
|
{
|
|
u_int32_t r;
|
|
|
|
r = *(u_int32_t *)b;
|
|
return (ntohl(r));
|
|
}
|
|
|
|
void
|
|
net_write16(u_int8_t *p, u_int16_t n)
|
|
{
|
|
u_int16_t r;
|
|
|
|
r = htons(n);
|
|
memcpy(p, &r, sizeof(r));
|
|
}
|
|
|
|
void
|
|
net_write32(u_int8_t *p, u_int32_t n)
|
|
{
|
|
u_int32_t r;
|
|
|
|
r = htonl(n);
|
|
memcpy(p, &r, sizeof(r));
|
|
}
|