2015-02-07 02:59:05 +01:00
|
|
|
/*
|
2016-03-12 22:29:18 +01:00
|
|
|
This file is part of actube.
|
2015-02-07 02:59:05 +01:00
|
|
|
|
2016-03-12 22:29:18 +01:00
|
|
|
actube is free software: you can redistribute it and/or modify
|
2015-02-07 02:59:05 +01:00
|
|
|
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.
|
|
|
|
|
|
|
|
libcapwap 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 Foobar. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2016-03-12 22:29:18 +01:00
|
|
|
|
2015-03-15 17:45:02 +01:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* @brief Implements DTLS BIO read/write functions.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2015-02-07 02:59:05 +01:00
|
|
|
#include <arpa/inet.h>
|
2018-03-03 17:42:28 +01:00
|
|
|
#include "mbag.h"
|
2015-04-10 17:52:01 +02:00
|
|
|
#include "log.h"
|
2015-04-11 19:00:51 +02:00
|
|
|
#include "dbg.h"
|
2015-02-07 02:59:05 +01:00
|
|
|
#include "dtls.h"
|
|
|
|
|
|
|
|
|
2015-03-15 17:45:02 +01:00
|
|
|
/**
|
|
|
|
* Reads CAPWAP DTLS data from a connection object.
|
|
|
|
* @param conn conn object
|
|
|
|
* @param out where to write data to
|
|
|
|
* @param maxlen maximum number of bytes to read
|
|
|
|
* @return the number of bytes read
|
2018-02-18 01:28:11 +01:00
|
|
|
*/
|
2015-02-07 02:59:05 +01:00
|
|
|
int dtls_bio_read(struct conn *conn, char *out, int maxlen)
|
|
|
|
{
|
|
|
|
if (conn->dtls_buffer_len == 0) {
|
|
|
|
int len = conn->recv_packet(conn, conn->dtls_buffer, 2048);
|
|
|
|
if (len < 4)
|
|
|
|
return 0;
|
|
|
|
conn->dtls_buffer_len = len - 4;
|
|
|
|
conn->dtls_buffer_pos = 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (conn->dtls_buffer_len > maxlen) {
|
|
|
|
memcpy(out, conn->dtls_buffer + conn->dtls_buffer_pos, maxlen);
|
|
|
|
conn->dtls_buffer_len -= maxlen;
|
|
|
|
conn->dtls_buffer_pos += maxlen;
|
2018-02-18 01:28:11 +01:00
|
|
|
cw_dbg(DBG_DTLS_BIO, "SSL BIO read: (maxlen = %d), read %d, remain %d",
|
|
|
|
maxlen, maxlen, conn->dtls_buffer_len);
|
2016-03-12 16:12:36 +01:00
|
|
|
cw_dbg_dmp(DBG_DTLS_BIO_DMP, (uint8_t *) out, maxlen, "Dump...");
|
2015-02-07 02:59:05 +01:00
|
|
|
|
|
|
|
return maxlen;
|
|
|
|
}
|
2015-02-08 11:42:01 +01:00
|
|
|
|
2015-02-07 02:59:05 +01:00
|
|
|
memcpy(out, conn->dtls_buffer + conn->dtls_buffer_pos, conn->dtls_buffer_len);
|
|
|
|
int ret = conn->dtls_buffer_len;
|
|
|
|
conn->dtls_buffer_len = 0;
|
2018-02-18 01:28:11 +01:00
|
|
|
cw_dbg(DBG_DTLS_BIO, "SSL BIO read: (maxlen = %d), read %d, remain %d", maxlen,
|
|
|
|
ret, conn->dtls_buffer_len);
|
2016-03-12 16:12:36 +01:00
|
|
|
cw_dbg_dmp(DBG_DTLS_BIO_DMP, (uint8_t *) out, ret, "Dump...");
|
2015-02-07 02:59:05 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-03-15 17:45:02 +01:00
|
|
|
/**
|
|
|
|
* Write DTLS data to a CAPWAP connection.
|
|
|
|
* @param conn the connection
|
|
|
|
* @param data data to write
|
|
|
|
* @param len number of bytes to write
|
|
|
|
* @return the number of bytes written
|
|
|
|
*/
|
2015-02-08 11:42:01 +01:00
|
|
|
int dtls_bio_write(struct conn *conn, const char *data, int len)
|
2015-02-07 02:59:05 +01:00
|
|
|
{
|
|
|
|
uint8_t buffer[2048];
|
|
|
|
*((uint32_t *) buffer) = htonl(1 << 24);
|
|
|
|
memcpy(buffer + 4, data, len);
|
|
|
|
int rc = conn->send_packet(conn, buffer, len + 4);
|
2018-02-18 01:28:11 +01:00
|
|
|
if (rc >= 0)
|
|
|
|
rc -= 4;
|
2015-02-07 02:59:05 +01:00
|
|
|
|
2015-02-08 11:42:01 +01:00
|
|
|
cw_dbg(DBG_DTLS_BIO, "SSL BIO write: %d bytes, wrote=%d, ptr: %p", len, rc, data);
|
2016-03-12 16:12:36 +01:00
|
|
|
cw_dbg_dmp(DBG_DTLS_BIO_DMP, (uint8_t *) data, len, "Dump ...");
|
2015-02-07 02:59:05 +01:00
|
|
|
|
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2018-02-18 01:28:11 +01:00
|
|
|
return rc;
|
2015-02-07 02:59:05 +01:00
|
|
|
}
|