2013-04-28 19:11:44 +02:00
|
|
|
/*
|
2019-02-22 16:57:28 +01:00
|
|
|
* Copyright (c) 2013-2019 Joris Vink <joris@coders.se>
|
2013-04-28 19:11:44 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-10-26 00:48:09 +02:00
|
|
|
#include <sys/param.h>
|
2019-03-06 09:29:46 +01:00
|
|
|
#include <sys/types.h>
|
2013-10-26 00:48:09 +02:00
|
|
|
|
2014-11-24 11:01:12 +01:00
|
|
|
#if defined(__linux__)
|
|
|
|
#include <endian.h>
|
|
|
|
#elif defined(__MACH__)
|
|
|
|
#include <libkern/OSByteOrder.h>
|
|
|
|
#define htobe64(x) OSSwapHostToBigInt64(x)
|
|
|
|
#define be64toh(x) OSSwapBigToHostInt64(x)
|
|
|
|
#else
|
|
|
|
#include <sys/endian.h>
|
|
|
|
#endif
|
|
|
|
|
2013-04-28 19:11:44 +02:00
|
|
|
#include "kore.h"
|
|
|
|
|
2013-07-15 10:13:36 +02:00
|
|
|
struct kore_pool nb_pool;
|
|
|
|
|
|
|
|
void
|
|
|
|
net_init(void)
|
|
|
|
{
|
2018-02-14 13:48:49 +01:00
|
|
|
u_int32_t elm;
|
|
|
|
|
|
|
|
/* Add some overhead so we don't roll over for internal items. */
|
|
|
|
elm = worker_max_connections + 10;
|
|
|
|
kore_pool_init(&nb_pool, "nb_pool", sizeof(struct netbuf), elm);
|
2013-07-15 10:13:36 +02:00
|
|
|
}
|
|
|
|
|
2015-12-29 20:39:39 +01:00
|
|
|
void
|
2016-01-04 22:40:14 +01:00
|
|
|
net_cleanup(void)
|
2015-12-29 20:39:39 +01:00
|
|
|
{
|
2016-01-04 22:40:14 +01:00
|
|
|
kore_debug("net_cleanup()");
|
|
|
|
kore_pool_cleanup(&nb_pool);
|
2015-12-29 20:39:39 +01:00
|
|
|
}
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
struct netbuf *
|
|
|
|
net_netbuf_get(void)
|
|
|
|
{
|
|
|
|
struct netbuf *nb;
|
|
|
|
|
|
|
|
nb = kore_pool_get(&nb_pool);
|
|
|
|
|
|
|
|
nb->cb = NULL;
|
|
|
|
nb->buf = NULL;
|
|
|
|
nb->owner = NULL;
|
|
|
|
nb->extra = NULL;
|
|
|
|
nb->file_ref = NULL;
|
|
|
|
|
|
|
|
nb->type = 0;
|
|
|
|
nb->s_off = 0;
|
|
|
|
nb->b_len = 0;
|
|
|
|
nb->m_len = 0;
|
|
|
|
nb->flags = 0;
|
|
|
|
|
|
|
|
#if defined(KORE_USE_PLATFORM_SENDFILE)
|
|
|
|
nb->fd_off = -1;
|
|
|
|
nb->fd_len = -1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return (nb);
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:47:02 +02:00
|
|
|
void
|
2016-07-08 10:02:18 +02:00
|
|
|
net_send_queue(struct connection *c, const void *data, size_t len)
|
2013-04-28 19:11:44 +02:00
|
|
|
{
|
2015-06-23 18:17:14 +02:00
|
|
|
const u_int8_t *d;
|
2013-04-28 19:11:44 +02:00
|
|
|
struct netbuf *nb;
|
2016-07-08 10:02:18 +02:00
|
|
|
size_t avail;
|
2013-10-25 14:22:29 +02:00
|
|
|
|
2016-07-08 10:02:18 +02:00
|
|
|
kore_debug("net_send_queue(%p, %p, %zu)", c, data, len);
|
2014-08-08 14:18:15 +02:00
|
|
|
|
2014-07-04 11:25:05 +02:00
|
|
|
d = data;
|
2015-11-27 16:22:50 +01:00
|
|
|
nb = TAILQ_LAST(&(c->send_queue), netbuf_head);
|
|
|
|
if (nb != NULL && !(nb->flags & NETBUF_IS_STREAM) &&
|
|
|
|
nb->b_len < nb->m_len) {
|
|
|
|
avail = nb->m_len - nb->b_len;
|
|
|
|
if (len < avail) {
|
|
|
|
memcpy(nb->buf + nb->b_len, d, len);
|
|
|
|
nb->b_len += len;
|
|
|
|
return;
|
2015-12-29 23:07:24 +01:00
|
|
|
} else {
|
2015-11-27 16:22:50 +01:00
|
|
|
memcpy(nb->buf + nb->b_len, d, avail);
|
|
|
|
nb->b_len += avail;
|
|
|
|
|
|
|
|
len -= avail;
|
|
|
|
d += avail;
|
|
|
|
if (len == 0)
|
2013-10-25 14:22:29 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2013-04-28 19:11:44 +02:00
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
nb = net_netbuf_get();
|
|
|
|
|
2013-04-28 19:11:44 +02:00
|
|
|
nb->owner = c;
|
2013-10-25 14:22:29 +02:00
|
|
|
nb->b_len = len;
|
2013-04-28 23:42:13 +02:00
|
|
|
nb->type = NETBUF_SEND;
|
2013-05-01 13:43:47 +02:00
|
|
|
|
2013-10-26 00:48:09 +02:00
|
|
|
if (nb->b_len < NETBUF_SEND_PAYLOAD_MAX)
|
|
|
|
nb->m_len = NETBUF_SEND_PAYLOAD_MAX;
|
|
|
|
else
|
|
|
|
nb->m_len = nb->b_len;
|
|
|
|
|
|
|
|
nb->buf = kore_malloc(nb->m_len);
|
2018-02-14 13:48:49 +01:00
|
|
|
memcpy(nb->buf, d, nb->b_len);
|
2013-04-28 19:11:44 +02:00
|
|
|
|
2015-11-27 16:22:50 +01:00
|
|
|
TAILQ_INSERT_TAIL(&(c->send_queue), nb, list);
|
2013-04-28 19:11:44 +02:00
|
|
|
}
|
|
|
|
|
2014-08-07 14:23:26 +02:00
|
|
|
void
|
2016-07-08 10:02:18 +02:00
|
|
|
net_send_stream(struct connection *c, void *data, size_t len,
|
2015-11-27 16:22:50 +01:00
|
|
|
int (*cb)(struct netbuf *), struct netbuf **out)
|
2014-08-07 14:23:26 +02:00
|
|
|
{
|
|
|
|
struct netbuf *nb;
|
|
|
|
|
2016-07-08 10:02:18 +02:00
|
|
|
kore_debug("net_send_stream(%p, %p, %zu)", c, data, len);
|
2014-08-08 14:18:15 +02:00
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
nb = net_netbuf_get();
|
2014-08-10 18:46:44 +02:00
|
|
|
nb->cb = cb;
|
2014-08-07 14:23:26 +02:00
|
|
|
nb->owner = c;
|
|
|
|
nb->buf = data;
|
|
|
|
nb->b_len = len;
|
|
|
|
nb->m_len = nb->b_len;
|
|
|
|
nb->type = NETBUF_SEND;
|
|
|
|
nb->flags = NETBUF_IS_STREAM;
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&(c->send_queue), nb, list);
|
2018-12-22 09:25:00 +01:00
|
|
|
|
2014-08-10 18:46:44 +02:00
|
|
|
if (out != NULL)
|
|
|
|
*out = nb;
|
2014-08-07 14:23:26 +02:00
|
|
|
}
|
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
void
|
|
|
|
net_send_fileref(struct connection *c, struct kore_fileref *ref)
|
|
|
|
{
|
|
|
|
struct netbuf *nb;
|
|
|
|
|
|
|
|
nb = net_netbuf_get();
|
|
|
|
nb->owner = c;
|
|
|
|
nb->file_ref = ref;
|
|
|
|
nb->type = NETBUF_SEND;
|
|
|
|
nb->flags = NETBUF_IS_FILEREF;
|
|
|
|
|
|
|
|
#if defined(KORE_USE_PLATFORM_SENDFILE)
|
|
|
|
nb->fd_off = 0;
|
|
|
|
nb->fd_len = ref->size;
|
|
|
|
#else
|
|
|
|
nb->buf = ref->base;
|
|
|
|
nb->b_len = ref->size;
|
|
|
|
nb->m_len = nb->b_len;
|
|
|
|
nb->flags |= NETBUF_IS_STREAM;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
TAILQ_INSERT_TAIL(&(c->send_queue), nb, list);
|
|
|
|
}
|
|
|
|
|
2013-05-02 14:47:02 +02:00
|
|
|
void
|
2016-07-08 10:02:18 +02:00
|
|
|
net_recv_reset(struct connection *c, size_t len, int (*cb)(struct netbuf *))
|
2013-04-28 19:11:44 +02:00
|
|
|
{
|
2016-07-08 10:02:18 +02:00
|
|
|
kore_debug("net_recv_reset(): %p %zu", c, len);
|
2013-04-28 19:11:44 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
c->rnb->cb = cb;
|
|
|
|
c->rnb->s_off = 0;
|
|
|
|
c->rnb->b_len = len;
|
|
|
|
|
2018-02-14 13:48:49 +01:00
|
|
|
if (c->rnb->buf != NULL && c->rnb->b_len <= c->rnb->m_len &&
|
2014-10-22 21:16:49 +02:00
|
|
|
c->rnb->m_len < (NETBUF_SEND_PAYLOAD_MAX / 2))
|
|
|
|
return;
|
|
|
|
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(c->rnb->buf);
|
2014-10-22 21:16:49 +02:00
|
|
|
c->rnb->m_len = len;
|
|
|
|
c->rnb->buf = kore_malloc(c->rnb->m_len);
|
2013-04-28 23:42:13 +02:00
|
|
|
}
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
void
|
2016-07-08 10:02:18 +02:00
|
|
|
net_recv_queue(struct connection *c, size_t len, int flags,
|
2013-04-28 23:42:13 +02:00
|
|
|
int (*cb)(struct netbuf *))
|
|
|
|
{
|
2016-07-08 10:02:18 +02:00
|
|
|
kore_debug("net_recv_queue(): %p %zu %d", c, len, flags);
|
2014-10-22 21:16:49 +02:00
|
|
|
|
|
|
|
if (c->rnb != NULL)
|
2018-02-14 13:48:49 +01:00
|
|
|
fatal("net_recv_queue(): called incorrectly");
|
2014-10-22 21:16:49 +02:00
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
c->rnb = net_netbuf_get();
|
2014-10-22 21:16:49 +02:00
|
|
|
c->rnb->cb = cb;
|
|
|
|
c->rnb->owner = c;
|
|
|
|
c->rnb->b_len = len;
|
|
|
|
c->rnb->m_len = len;
|
|
|
|
c->rnb->flags = flags;
|
|
|
|
c->rnb->type = NETBUF_RECV;
|
|
|
|
c->rnb->buf = kore_malloc(c->rnb->b_len);
|
|
|
|
}
|
2013-04-28 23:42:13 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
void
|
2016-07-08 10:02:18 +02:00
|
|
|
net_recv_expand(struct connection *c, size_t len, int (*cb)(struct netbuf *))
|
2014-10-22 21:16:49 +02:00
|
|
|
{
|
2015-03-16 16:52:40 +01:00
|
|
|
kore_debug("net_recv_expand(): %p %d", c, len);
|
2013-07-12 15:49:49 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
c->rnb->cb = cb;
|
|
|
|
c->rnb->b_len += len;
|
|
|
|
c->rnb->m_len = c->rnb->b_len;
|
|
|
|
c->rnb->buf = kore_realloc(c->rnb->buf, c->rnb->b_len);
|
2013-04-28 19:11:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
net_send(struct connection *c)
|
|
|
|
{
|
2017-03-16 09:54:46 +01:00
|
|
|
size_t r, len, smin;
|
2014-08-08 14:18:15 +02:00
|
|
|
|
|
|
|
c->snb = TAILQ_FIRST(&(c->send_queue));
|
2018-06-28 13:27:44 +02:00
|
|
|
|
|
|
|
#if defined(KORE_USE_PLATFORM_SENDFILE)
|
|
|
|
if ((c->snb->flags & NETBUF_IS_FILEREF) &&
|
|
|
|
!(c->snb->flags & NETBUF_IS_STREAM)) {
|
|
|
|
return (kore_platform_sendfile(c, c->snb));
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-08-08 14:18:15 +02:00
|
|
|
if (c->snb->b_len != 0) {
|
|
|
|
smin = c->snb->b_len - c->snb->s_off;
|
|
|
|
len = MIN(NETBUF_SEND_PAYLOAD_MAX, smin);
|
2013-10-15 11:09:33 +02:00
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
if (!c->write(c, len, &r))
|
|
|
|
return (KORE_RESULT_ERROR);
|
2018-10-09 19:34:40 +02:00
|
|
|
if (!(c->evt.flags & KORE_EVENT_WRITE))
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
c->snb->s_off += r;
|
2014-08-08 14:18:15 +02:00
|
|
|
c->snb->flags &= ~NETBUF_MUST_RESEND;
|
|
|
|
}
|
2014-08-07 14:23:26 +02:00
|
|
|
|
2014-08-08 14:18:15 +02:00
|
|
|
if (c->snb->s_off == c->snb->b_len ||
|
|
|
|
(c->snb->flags & NETBUF_FORCE_REMOVE)) {
|
2018-12-22 09:25:00 +01:00
|
|
|
net_remove_netbuf(c, c->snb);
|
2014-08-08 14:18:15 +02:00
|
|
|
c->snb = NULL;
|
2013-04-28 19:11:44 +02:00
|
|
|
}
|
|
|
|
|
2013-06-27 00:22:48 +02:00
|
|
|
return (KORE_RESULT_OK);
|
2013-04-28 19:11:44 +02:00
|
|
|
}
|
|
|
|
|
2013-05-01 13:43:47 +02:00
|
|
|
int
|
|
|
|
net_send_flush(struct connection *c)
|
|
|
|
{
|
2013-06-04 16:30:53 +02:00
|
|
|
kore_debug("net_send_flush(%p)", c);
|
2013-05-02 09:10:35 +02:00
|
|
|
|
2013-05-01 13:43:47 +02:00
|
|
|
while (!TAILQ_EMPTY(&(c->send_queue)) &&
|
2018-10-09 19:34:40 +02:00
|
|
|
(c->evt.flags & KORE_EVENT_WRITE)) {
|
2013-05-01 13:43:47 +02:00
|
|
|
if (!net_send(c))
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
|
2018-02-14 13:48:49 +01:00
|
|
|
if ((c->flags & CONN_CLOSE_EMPTY) && TAILQ_EMPTY(&(c->send_queue))) {
|
2014-01-29 22:48:51 +01:00
|
|
|
kore_connection_disconnect(c);
|
2018-02-14 13:48:49 +01:00
|
|
|
}
|
2014-01-29 22:48:51 +01:00
|
|
|
|
2013-05-01 13:43:47 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-04-28 19:11:44 +02:00
|
|
|
int
|
2014-10-22 21:16:49 +02:00
|
|
|
net_recv_flush(struct connection *c)
|
2013-04-28 19:11:44 +02:00
|
|
|
{
|
2017-03-16 09:54:46 +01:00
|
|
|
size_t r;
|
2013-04-28 19:11:44 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
kore_debug("net_recv_flush(%p)", c);
|
2013-04-28 19:11:44 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
if (c->rnb == NULL)
|
2015-11-27 16:22:50 +01:00
|
|
|
return (KORE_RESULT_OK);
|
2014-09-17 08:25:45 +02:00
|
|
|
|
2018-10-09 19:34:40 +02:00
|
|
|
while (c->evt.flags & KORE_EVENT_READ) {
|
2018-07-18 16:10:41 +02:00
|
|
|
if (c->rnb->buf == NULL)
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
if (!c->read(c, &r))
|
|
|
|
return (KORE_RESULT_ERROR);
|
2018-10-09 19:34:40 +02:00
|
|
|
if (!(c->evt.flags & KORE_EVENT_READ))
|
2014-10-22 21:16:49 +02:00
|
|
|
break;
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
c->rnb->s_off += r;
|
2014-08-08 14:18:15 +02:00
|
|
|
if (c->rnb->s_off == c->rnb->b_len ||
|
2014-10-22 21:16:49 +02:00
|
|
|
(c->rnb->flags & NETBUF_CALL_CB_ALWAYS)) {
|
|
|
|
r = c->rnb->cb(c->rnb);
|
|
|
|
if (r != KORE_RESULT_OK)
|
|
|
|
return (r);
|
2013-04-28 23:42:13 +02:00
|
|
|
}
|
2013-05-01 13:43:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2014-08-07 10:22:54 +02:00
|
|
|
void
|
2018-12-22 09:25:00 +01:00
|
|
|
net_remove_netbuf(struct connection *c, struct netbuf *nb)
|
2014-08-07 10:22:54 +02:00
|
|
|
{
|
2018-12-22 09:25:00 +01:00
|
|
|
kore_debug("net_remove_netbuf(%p, %p)", c, nb);
|
2014-08-07 14:23:26 +02:00
|
|
|
|
2014-10-22 21:16:49 +02:00
|
|
|
if (nb->type == NETBUF_RECV)
|
|
|
|
fatal("net_remove_netbuf(): cannot remove recv netbuf");
|
|
|
|
|
2014-08-07 10:22:54 +02:00
|
|
|
if (nb->flags & NETBUF_MUST_RESEND) {
|
|
|
|
kore_debug("retaining %p (MUST_RESEND)", nb);
|
2014-08-08 14:18:15 +02:00
|
|
|
nb->flags |= NETBUF_FORCE_REMOVE;
|
2014-08-07 10:22:54 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-10 18:46:44 +02:00
|
|
|
if (!(nb->flags & NETBUF_IS_STREAM)) {
|
2016-07-12 13:54:14 +02:00
|
|
|
kore_free(nb->buf);
|
2014-08-10 18:46:44 +02:00
|
|
|
} else if (nb->cb != NULL) {
|
|
|
|
(void)nb->cb(nb);
|
|
|
|
}
|
2014-08-07 14:23:26 +02:00
|
|
|
|
2018-06-28 13:27:44 +02:00
|
|
|
if (nb->flags & NETBUF_IS_FILEREF)
|
|
|
|
kore_fileref_release(nb->file_ref);
|
|
|
|
|
2018-12-22 09:25:00 +01:00
|
|
|
TAILQ_REMOVE(&(c->send_queue), nb, list);
|
|
|
|
|
2014-08-07 10:22:54 +02:00
|
|
|
kore_pool_put(&nb_pool, nb);
|
|
|
|
}
|
|
|
|
|
2015-05-25 15:42:34 +02:00
|
|
|
#if !defined(KORE_NO_TLS)
|
2014-09-17 08:25:45 +02:00
|
|
|
int
|
2017-03-16 09:54:46 +01:00
|
|
|
net_write_tls(struct connection *c, size_t len, size_t *written)
|
2014-09-17 08:25:45 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
if (len > INT_MAX)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2016-12-05 14:24:22 +01:00
|
|
|
ERR_clear_error();
|
2014-09-17 08:25:45 +02:00
|
|
|
r = SSL_write(c->ssl, (c->snb->buf + c->snb->s_off), len);
|
2015-05-20 16:36:13 +02:00
|
|
|
if (c->tls_reneg > 1)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
if (r <= 0) {
|
|
|
|
r = SSL_get_error(c->ssl, r);
|
|
|
|
switch (r) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_WRITE;
|
2014-09-17 08:25:45 +02:00
|
|
|
c->snb->flags |= NETBUF_MUST_RESEND;
|
|
|
|
return (KORE_RESULT_OK);
|
2016-01-14 10:50:46 +01:00
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
*written = 0;
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
case EAGAIN:
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_WRITE;
|
2016-01-14 10:50:46 +01:00
|
|
|
c->snb->flags |= NETBUF_MUST_RESEND;
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2014-09-17 08:25:45 +02:00
|
|
|
default:
|
|
|
|
kore_debug("SSL_write(): %s", ssl_errno_s);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
*written = (size_t)r;
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-16 09:54:46 +01:00
|
|
|
net_read_tls(struct connection *c, size_t *bytes)
|
2014-09-17 08:25:45 +02:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
|
2016-12-05 14:24:22 +01:00
|
|
|
ERR_clear_error();
|
2014-09-17 08:25:45 +02:00
|
|
|
r = SSL_read(c->ssl, (c->rnb->buf + c->rnb->s_off),
|
|
|
|
(c->rnb->b_len - c->rnb->s_off));
|
2015-05-20 16:36:13 +02:00
|
|
|
|
|
|
|
if (c->tls_reneg > 1)
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
if (r <= 0) {
|
|
|
|
r = SSL_get_error(c->ssl, r);
|
|
|
|
switch (r) {
|
|
|
|
case SSL_ERROR_WANT_READ:
|
|
|
|
case SSL_ERROR_WANT_WRITE:
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_READ;
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
2016-01-14 10:50:46 +01:00
|
|
|
case SSL_ERROR_SYSCALL:
|
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
|
|
|
*bytes = 0;
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
case EAGAIN:
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_READ;
|
2016-01-14 10:50:46 +01:00
|
|
|
c->snb->flags |= NETBUF_MUST_RESEND;
|
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* FALLTHROUGH */
|
2014-09-17 08:25:45 +02:00
|
|
|
default:
|
|
|
|
kore_debug("SSL_read(): %s", ssl_errno_s);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
*bytes = (size_t)r;
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
int
|
2017-03-16 09:54:46 +01:00
|
|
|
net_write(struct connection *c, size_t len, size_t *written)
|
2014-09-17 08:25:45 +02:00
|
|
|
{
|
2017-03-16 09:54:46 +01:00
|
|
|
ssize_t r;
|
2014-09-17 08:25:45 +02:00
|
|
|
|
2019-09-26 09:52:31 +02:00
|
|
|
r = send(c->fd, (c->snb->buf + c->snb->s_off), len, 0);
|
2018-07-05 14:36:47 +02:00
|
|
|
if (r == -1) {
|
2014-09-17 08:25:45 +02:00
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
2016-01-14 09:02:53 +01:00
|
|
|
*written = 0;
|
|
|
|
return (KORE_RESULT_OK);
|
2014-09-17 08:25:45 +02:00
|
|
|
case EAGAIN:
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_WRITE;
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
default:
|
|
|
|
kore_debug("write: %s", errno_s);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
*written = (size_t)r;
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2017-03-16 09:54:46 +01:00
|
|
|
net_read(struct connection *c, size_t *bytes)
|
2014-09-17 08:25:45 +02:00
|
|
|
{
|
2017-03-16 09:54:46 +01:00
|
|
|
ssize_t r;
|
2014-09-17 08:25:45 +02:00
|
|
|
|
2019-09-26 09:52:31 +02:00
|
|
|
r = recv(c->fd, (c->rnb->buf + c->rnb->s_off),
|
|
|
|
(c->rnb->b_len - c->rnb->s_off), 0);
|
2018-07-05 14:36:47 +02:00
|
|
|
if (r == -1) {
|
2014-09-17 08:25:45 +02:00
|
|
|
switch (errno) {
|
|
|
|
case EINTR:
|
2016-01-14 09:02:53 +01:00
|
|
|
*bytes = 0;
|
|
|
|
return (KORE_RESULT_OK);
|
2014-09-17 08:25:45 +02:00
|
|
|
case EAGAIN:
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_READ;
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
default:
|
|
|
|
kore_debug("read(): %s", errno_s);
|
|
|
|
return (KORE_RESULT_ERROR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-05 14:36:47 +02:00
|
|
|
if (r == 0) {
|
|
|
|
kore_connection_disconnect(c);
|
2018-10-09 19:34:40 +02:00
|
|
|
c->evt.flags &= ~KORE_EVENT_READ;
|
2018-07-05 14:36:47 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2017-03-16 09:54:46 +01:00
|
|
|
*bytes = (size_t)r;
|
|
|
|
|
2014-09-17 08:25:45 +02:00
|
|
|
return (KORE_RESULT_OK);
|
|
|
|
}
|
|
|
|
|
2013-05-01 00:35:33 +02:00
|
|
|
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));
|
|
|
|
}
|
2013-05-01 08:09:04 +02:00
|
|
|
|
|
|
|
void
|
|
|
|
net_write16(u_int8_t *p, u_int16_t n)
|
|
|
|
{
|
2013-05-01 12:23:21 +02:00
|
|
|
u_int16_t r;
|
|
|
|
|
|
|
|
r = htons(n);
|
|
|
|
memcpy(p, &r, sizeof(r));
|
2013-05-01 08:09:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
net_write32(u_int8_t *p, u_int32_t n)
|
|
|
|
{
|
2013-05-01 12:23:21 +02:00
|
|
|
u_int32_t r;
|
|
|
|
|
|
|
|
r = htonl(n);
|
|
|
|
memcpy(p, &r, sizeof(r));
|
2013-05-01 08:09:04 +02:00
|
|
|
}
|
2014-11-24 11:01:12 +01:00
|
|
|
|
|
|
|
u_int64_t
|
|
|
|
net_read64(u_int8_t *b)
|
|
|
|
{
|
|
|
|
u_int64_t r;
|
|
|
|
|
|
|
|
r = *(u_int64_t *)b;
|
|
|
|
return (be64toh(r));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
net_write64(u_int8_t *p, u_int64_t n)
|
|
|
|
{
|
|
|
|
u_int64_t r;
|
|
|
|
|
|
|
|
r = htobe64(n);
|
|
|
|
memcpy(p, &r, sizeof(r));
|
|
|
|
}
|