2011-08-25 12:41:19 -07:00
|
|
|
/* io.c
|
2011-02-05 11:14:47 -08:00
|
|
|
*
|
2012-02-13 11:54:10 -08:00
|
|
|
* Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
|
2011-02-05 11:14:47 -08:00
|
|
|
*
|
|
|
|
|
* This file is part of CyaSSL.
|
|
|
|
|
*
|
|
|
|
|
* CyaSSL is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* CyaSSL 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 this program; if not, write to the Free Software
|
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
|
*/
|
|
|
|
|
|
2011-08-24 16:37:16 -07:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
#include <config.h>
|
|
|
|
|
#endif
|
2011-02-05 11:14:47 -08:00
|
|
|
|
|
|
|
|
#ifdef _WIN32_WCE
|
|
|
|
|
/* On WinCE winsock2.h must be included before windows.h for socket stuff */
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-08-25 12:41:19 -07:00
|
|
|
#include <cyassl/internal.h>
|
2011-02-05 11:14:47 -08:00
|
|
|
|
|
|
|
|
/* if user writes own I/O callbacks they can define CYASSL_USER_IO to remove
|
|
|
|
|
automatic setting of default I/O functions EmbedSend() and EmbedReceive()
|
2011-03-22 07:35:18 -07:00
|
|
|
but they'll still need SetCallback xxx() at end of file
|
2011-02-05 11:14:47 -08:00
|
|
|
*/
|
|
|
|
|
#ifndef CYASSL_USER_IO
|
|
|
|
|
|
|
|
|
|
#ifdef HAVE_LIBZ
|
|
|
|
|
#include "zlib.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2011-05-16 16:20:32 -07:00
|
|
|
#ifndef USE_WINDOWS_API
|
2011-05-26 09:55:59 -07:00
|
|
|
#ifdef CYASSL_LWIP
|
|
|
|
|
/* lwIP needs to be configured to use sockets API in this mode */
|
2012-08-15 17:00:11 -07:00
|
|
|
/* LWIP_SOCKET 1 in lwip/opt.h or in build */
|
|
|
|
|
#include "lwip/sockets.h"
|
2012-12-27 10:16:08 -07:00
|
|
|
#include <errno.h>
|
2012-08-15 17:00:11 -07:00
|
|
|
#ifndef LWIP_PROVIDE_ERRNO
|
|
|
|
|
#define LWIP_PROVIDE_ERRNO 1
|
|
|
|
|
#endif
|
2012-11-01 11:23:42 -06:00
|
|
|
#elif defined(FREESCALE_MQX)
|
2012-11-12 14:25:59 -07:00
|
|
|
#include <posix.h>
|
2012-11-01 11:23:42 -06:00
|
|
|
#include <rtcs.h>
|
2011-05-16 16:20:32 -07:00
|
|
|
#else
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
|
#include <errno.h>
|
2012-08-13 17:10:05 -06:00
|
|
|
#ifndef EBSNET
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#endif
|
2011-05-16 16:20:32 -07:00
|
|
|
#include <fcntl.h>
|
2012-08-14 13:10:27 -06:00
|
|
|
#if !(defined(DEVKITPRO) || defined(THREADX) || defined(EBSNET))
|
2011-05-16 16:20:32 -07:00
|
|
|
#include <sys/socket.h>
|
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
#include <netdb.h>
|
2011-07-13 12:25:08 -07:00
|
|
|
#ifdef __PPU
|
2011-07-12 13:28:48 -07:00
|
|
|
#include <netex/errno.h>
|
|
|
|
|
#else
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
|
#endif
|
2011-05-16 16:20:32 -07:00
|
|
|
#endif
|
|
|
|
|
#ifdef THREADX
|
|
|
|
|
#include <socket.h>
|
|
|
|
|
#endif
|
2012-08-13 17:10:05 -06:00
|
|
|
#ifdef EBSNET
|
|
|
|
|
#include "rtipapi.h" /* errno */
|
|
|
|
|
#include "socket.h"
|
|
|
|
|
#endif
|
2011-02-05 11:14:47 -08:00
|
|
|
#endif
|
|
|
|
|
#endif /* USE_WINDOWS_API */
|
|
|
|
|
|
|
|
|
|
#ifdef __sun
|
|
|
|
|
#include <sys/filio.h>
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
|
/* no epipe yet */
|
|
|
|
|
#ifndef WSAEPIPE
|
|
|
|
|
#define WSAEPIPE -12345
|
|
|
|
|
#endif
|
|
|
|
|
#define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
|
2012-10-02 09:15:50 -07:00
|
|
|
#define SOCKET_EAGAIN WSAETIMEDOUT
|
2011-02-05 11:14:47 -08:00
|
|
|
#define SOCKET_ECONNRESET WSAECONNRESET
|
|
|
|
|
#define SOCKET_EINTR WSAEINTR
|
|
|
|
|
#define SOCKET_EPIPE WSAEPIPE
|
2012-09-21 09:36:01 -07:00
|
|
|
#define SOCKET_ECONNREFUSED WSAENOTCONN
|
2011-07-13 12:25:08 -07:00
|
|
|
#elif defined(__PPU)
|
2011-07-12 13:28:48 -07:00
|
|
|
#define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK
|
|
|
|
|
#define SOCKET_EAGAIN SYS_NET_EAGAIN
|
|
|
|
|
#define SOCKET_ECONNRESET SYS_NET_ECONNRESET
|
|
|
|
|
#define SOCKET_EINTR SYS_NET_EINTR
|
|
|
|
|
#define SOCKET_EPIPE SYS_NET_EPIPE
|
2012-09-21 09:36:01 -07:00
|
|
|
#define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED
|
2012-11-01 11:23:42 -06:00
|
|
|
#elif defined(FREESCALE_MQX)
|
|
|
|
|
/* RTCS doesn't have an EWOULDBLOCK error */
|
|
|
|
|
#define SOCKET_EWOULDBLOCK EAGAIN
|
|
|
|
|
#define SOCKET_EAGAIN EAGAIN
|
|
|
|
|
#define SOCKET_ECONNRESET RTCSERR_TCP_CONN_RESET
|
|
|
|
|
#define SOCKET_EINTR EINTR
|
|
|
|
|
#define SOCKET_EPIPE EPIPE
|
|
|
|
|
#define SOCKET_ECONNREFUSED RTCSERR_TCP_CONN_REFUSED
|
2011-02-05 11:14:47 -08:00
|
|
|
#else
|
|
|
|
|
#define SOCKET_EWOULDBLOCK EWOULDBLOCK
|
|
|
|
|
#define SOCKET_EAGAIN EAGAIN
|
|
|
|
|
#define SOCKET_ECONNRESET ECONNRESET
|
|
|
|
|
#define SOCKET_EINTR EINTR
|
|
|
|
|
#define SOCKET_EPIPE EPIPE
|
2012-09-21 09:36:01 -07:00
|
|
|
#define SOCKET_ECONNREFUSED ECONNREFUSED
|
2011-02-05 11:14:47 -08:00
|
|
|
#endif /* USE_WINDOWS_API */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef DEVKITPRO
|
|
|
|
|
/* from network.h */
|
|
|
|
|
int net_send(int, const void*, int, unsigned int);
|
|
|
|
|
int net_recv(int, void*, int, unsigned int);
|
|
|
|
|
#define SEND_FUNCTION net_send
|
|
|
|
|
#define RECV_FUNCTION net_recv
|
2012-08-15 17:00:11 -07:00
|
|
|
#elif defined(CYASSL_LWIP)
|
|
|
|
|
#define SEND_FUNCTION lwip_send
|
|
|
|
|
#define RECV_FUNCTION lwip_recv
|
2011-02-05 11:14:47 -08:00
|
|
|
#else
|
|
|
|
|
#define SEND_FUNCTION send
|
|
|
|
|
#define RECV_FUNCTION recv
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2012-10-31 15:43:08 -07:00
|
|
|
#ifdef CYASSL_DTLS
|
|
|
|
|
/* sizeof(struct timeval) will pass uninit bytes to setsockopt if padded */
|
|
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
|
#define TIMEVAL_BYTES sizeof(timeout)
|
|
|
|
|
#else
|
|
|
|
|
#define TIMEVAL_BYTES sizeof(timeout.tv_sec) + sizeof(timeout.tv_usec)
|
|
|
|
|
#endif
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2012-11-01 11:23:42 -06:00
|
|
|
/* Translates return codes returned from
|
|
|
|
|
* send() and recv() if need be.
|
|
|
|
|
*/
|
|
|
|
|
static INLINE int TranslateReturnCode(int old, int sd)
|
|
|
|
|
{
|
|
|
|
|
(void)sd;
|
|
|
|
|
|
|
|
|
|
#ifdef FREESCALE_MQX
|
|
|
|
|
if (old == 0) {
|
|
|
|
|
errno = SOCKET_EWOULDBLOCK;
|
|
|
|
|
return -1; /* convert to BSD style wouldblock as error */
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (old < 0) {
|
|
|
|
|
errno = RTCS_geterror(sd);
|
|
|
|
|
if (errno == RTCSERR_TCP_CONN_CLOSING)
|
|
|
|
|
return 0; /* convert to BSD style closing */
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
return old;
|
|
|
|
|
}
|
|
|
|
|
|
2011-02-05 11:14:47 -08:00
|
|
|
static INLINE int LastError(void)
|
|
|
|
|
{
|
|
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
|
return WSAGetLastError();
|
2012-08-13 17:10:05 -06:00
|
|
|
#elif defined(EBSNET)
|
2012-08-14 13:10:27 -06:00
|
|
|
return xn_getlasterror();
|
2011-02-05 11:14:47 -08:00
|
|
|
#else
|
|
|
|
|
return errno;
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The receive embedded callback
|
|
|
|
|
* return : nb bytes read, or error
|
|
|
|
|
*/
|
2012-09-06 22:41:55 -07:00
|
|
|
int EmbedReceive(CYASSL *ssl, char *buf, int sz, void *ctx)
|
2011-02-05 11:14:47 -08:00
|
|
|
{
|
|
|
|
|
int recvd;
|
|
|
|
|
int err;
|
2011-05-19 16:36:34 -07:00
|
|
|
int sd = *(int*)ctx;
|
2011-02-05 11:14:47 -08:00
|
|
|
|
2012-09-06 22:41:55 -07:00
|
|
|
#ifdef CYASSL_DTLS
|
2012-09-25 15:51:56 -07:00
|
|
|
{
|
|
|
|
|
int dtls_timeout = CyaSSL_dtls_get_current_timeout(ssl);
|
|
|
|
|
if (CyaSSL_dtls(ssl)
|
|
|
|
|
&& !CyaSSL_get_using_nonblock(ssl)
|
|
|
|
|
&& dtls_timeout != 0) {
|
|
|
|
|
#ifdef USE_WINDOWS_API
|
2012-10-03 15:29:28 -07:00
|
|
|
DWORD timeout = dtls_timeout * 1000;
|
2012-09-25 15:51:56 -07:00
|
|
|
#else
|
2012-10-31 15:43:08 -07:00
|
|
|
struct timeval timeout = {dtls_timeout, 0};
|
2012-09-25 15:51:56 -07:00
|
|
|
#endif
|
|
|
|
|
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO,
|
2012-10-31 15:43:08 -07:00
|
|
|
(char*)&timeout, TIMEVAL_BYTES);
|
2012-09-25 15:51:56 -07:00
|
|
|
}
|
2012-09-06 22:41:55 -07:00
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-10-25 14:17:11 -07:00
|
|
|
recvd = (int)RECV_FUNCTION(sd, buf, sz, ssl->rflags);
|
2011-02-05 11:14:47 -08:00
|
|
|
|
2012-11-01 11:23:42 -06:00
|
|
|
recvd = TranslateReturnCode(recvd, sd);
|
|
|
|
|
|
2011-07-12 13:28:48 -07:00
|
|
|
if (recvd < 0) {
|
2011-02-05 11:14:47 -08:00
|
|
|
err = LastError();
|
2011-04-25 09:24:21 -07:00
|
|
|
CYASSL_MSG("Embed Receive error");
|
2011-02-05 11:14:47 -08:00
|
|
|
|
2011-04-25 09:24:21 -07:00
|
|
|
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
2012-09-28 15:10:35 -07:00
|
|
|
if (!CyaSSL_dtls(ssl) || CyaSSL_get_using_nonblock(ssl)) {
|
2012-09-06 22:41:55 -07:00
|
|
|
CYASSL_MSG(" Would block");
|
|
|
|
|
return IO_ERR_WANT_READ;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CYASSL_MSG(" Socket timeout");
|
|
|
|
|
return IO_ERR_TIMEOUT;
|
|
|
|
|
}
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_ECONNRESET) {
|
|
|
|
|
CYASSL_MSG(" Connection reset");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_CONN_RST;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_EINTR) {
|
|
|
|
|
CYASSL_MSG(" Socket interrupted");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_ISR;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
2012-09-21 09:36:01 -07:00
|
|
|
else if (err == SOCKET_ECONNREFUSED) {
|
|
|
|
|
CYASSL_MSG(" Connection refused");
|
|
|
|
|
return IO_ERR_WANT_READ;
|
|
|
|
|
}
|
2011-04-25 09:24:21 -07:00
|
|
|
else {
|
|
|
|
|
CYASSL_MSG(" General error");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_GENERAL;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
2011-02-05 11:14:47 -08:00
|
|
|
}
|
2011-04-25 09:24:21 -07:00
|
|
|
else if (recvd == 0) {
|
|
|
|
|
CYASSL_MSG("Embed receive connection closed");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_CONN_CLOSE;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
2011-02-05 11:14:47 -08:00
|
|
|
|
|
|
|
|
return recvd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* The send embedded callback
|
|
|
|
|
* return : nb bytes sent, or error
|
|
|
|
|
*/
|
2012-09-06 22:41:55 -07:00
|
|
|
int EmbedSend(CYASSL* ssl, char *buf, int sz, void *ctx)
|
2011-02-05 11:14:47 -08:00
|
|
|
{
|
2011-05-19 16:36:34 -07:00
|
|
|
int sd = *(int*)ctx;
|
2011-02-05 11:14:47 -08:00
|
|
|
int sent;
|
|
|
|
|
int len = sz;
|
2011-04-25 09:24:21 -07:00
|
|
|
int err;
|
2011-02-05 11:14:47 -08:00
|
|
|
|
2012-10-25 14:17:11 -07:00
|
|
|
sent = (int)SEND_FUNCTION(sd, &buf[sz - len], len, ssl->wflags);
|
2011-02-05 11:14:47 -08:00
|
|
|
|
2011-07-12 13:28:48 -07:00
|
|
|
if (sent < 0) {
|
2011-04-25 09:24:21 -07:00
|
|
|
err = LastError();
|
|
|
|
|
CYASSL_MSG("Embed Send error");
|
2011-02-05 11:14:47 -08:00
|
|
|
|
2011-04-25 09:24:21 -07:00
|
|
|
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
|
|
|
|
CYASSL_MSG(" Would Block");
|
|
|
|
|
return IO_ERR_WANT_WRITE;
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_ECONNRESET) {
|
|
|
|
|
CYASSL_MSG(" Connection reset");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_CONN_RST;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_EINTR) {
|
|
|
|
|
CYASSL_MSG(" Socket interrupted");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_ISR;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_EPIPE) {
|
|
|
|
|
CYASSL_MSG(" Socket EPIPE");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_CONN_CLOSE;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CYASSL_MSG(" General error");
|
2011-02-05 11:14:47 -08:00
|
|
|
return IO_ERR_GENERAL;
|
2011-04-25 09:24:21 -07:00
|
|
|
}
|
2011-02-05 11:14:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2012-08-08 17:21:03 -07:00
|
|
|
#ifdef CYASSL_DTLS
|
|
|
|
|
|
|
|
|
|
#include <cyassl/ctaocrypt/sha.h>
|
|
|
|
|
|
2012-08-13 15:05:28 -07:00
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
|
#define XSOCKLENT int
|
|
|
|
|
#else
|
|
|
|
|
#define XSOCKLENT socklen_t
|
|
|
|
|
#endif
|
|
|
|
|
|
2012-10-02 09:15:50 -07:00
|
|
|
#define SENDTO_FUNCTION sendto
|
|
|
|
|
#define RECVFROM_FUNCTION recvfrom
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The receive embedded callback
|
|
|
|
|
* return : nb bytes read, or error
|
|
|
|
|
*/
|
|
|
|
|
int EmbedReceiveFrom(CYASSL *ssl, char *buf, int sz, void *ctx)
|
|
|
|
|
{
|
|
|
|
|
CYASSL_DTLS_CTX* dtlsCtx = (CYASSL_DTLS_CTX*)ctx;
|
|
|
|
|
int recvd;
|
|
|
|
|
int err;
|
|
|
|
|
int sd = dtlsCtx->fd;
|
|
|
|
|
int dtls_timeout = CyaSSL_dtls_get_current_timeout(ssl);
|
|
|
|
|
struct sockaddr_in peer;
|
|
|
|
|
XSOCKLENT peerSz = sizeof(peer);
|
|
|
|
|
|
|
|
|
|
CYASSL_ENTER("EmbedReceiveFrom()");
|
|
|
|
|
if (!CyaSSL_get_using_nonblock(ssl) && dtls_timeout != 0) {
|
|
|
|
|
#ifdef USE_WINDOWS_API
|
2012-10-03 15:29:28 -07:00
|
|
|
DWORD timeout = dtls_timeout * 1000;
|
2012-10-02 09:15:50 -07:00
|
|
|
#else
|
2012-10-31 15:43:08 -07:00
|
|
|
struct timeval timeout = { dtls_timeout, 0 };
|
2012-10-02 09:15:50 -07:00
|
|
|
#endif
|
|
|
|
|
setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO,
|
2012-10-31 15:43:08 -07:00
|
|
|
(char*)&timeout, TIMEVAL_BYTES);
|
2012-10-02 09:15:50 -07:00
|
|
|
}
|
|
|
|
|
|
2012-10-25 14:17:11 -07:00
|
|
|
recvd = (int)RECVFROM_FUNCTION(sd, buf, sz, ssl->rflags,
|
2012-10-19 12:44:23 -07:00
|
|
|
(struct sockaddr*)&peer, &peerSz);
|
2012-10-02 09:15:50 -07:00
|
|
|
|
2012-11-01 11:23:42 -06:00
|
|
|
recvd = TranslateReturnCode(recvd, sd);
|
|
|
|
|
|
2012-10-02 09:15:50 -07:00
|
|
|
if (recvd < 0) {
|
|
|
|
|
err = LastError();
|
|
|
|
|
CYASSL_MSG("Embed Receive From error");
|
|
|
|
|
|
|
|
|
|
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
|
|
|
|
if (CyaSSL_get_using_nonblock(ssl)) {
|
|
|
|
|
CYASSL_MSG(" Would block");
|
|
|
|
|
return IO_ERR_WANT_READ;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CYASSL_MSG(" Socket timeout");
|
|
|
|
|
return IO_ERR_TIMEOUT;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_ECONNRESET) {
|
|
|
|
|
CYASSL_MSG(" Connection reset");
|
|
|
|
|
return IO_ERR_CONN_RST;
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_EINTR) {
|
|
|
|
|
CYASSL_MSG(" Socket interrupted");
|
|
|
|
|
return IO_ERR_ISR;
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_ECONNREFUSED) {
|
|
|
|
|
CYASSL_MSG(" Connection refused");
|
|
|
|
|
return IO_ERR_WANT_READ;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CYASSL_MSG(" General error");
|
|
|
|
|
return IO_ERR_GENERAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
if (dtlsCtx != NULL
|
|
|
|
|
&& dtlsCtx->peer.sz > 0
|
2012-10-09 16:13:05 -07:00
|
|
|
&& peerSz != (XSOCKLENT)dtlsCtx->peer.sz
|
2012-10-02 09:15:50 -07:00
|
|
|
&& memcmp(&peer, dtlsCtx->peer.sa, peerSz) != 0) {
|
|
|
|
|
CYASSL_MSG(" Ignored packet from invalid peer");
|
|
|
|
|
return IO_ERR_WANT_READ;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return recvd;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* The send embedded callback
|
|
|
|
|
* return : nb bytes sent, or error
|
|
|
|
|
*/
|
|
|
|
|
int EmbedSendTo(CYASSL* ssl, char *buf, int sz, void *ctx)
|
|
|
|
|
{
|
|
|
|
|
CYASSL_DTLS_CTX* dtlsCtx = (CYASSL_DTLS_CTX*)ctx;
|
|
|
|
|
int sd = dtlsCtx->fd;
|
|
|
|
|
int sent;
|
|
|
|
|
int len = sz;
|
|
|
|
|
int err;
|
|
|
|
|
|
|
|
|
|
CYASSL_ENTER("EmbedSendTo()");
|
2012-10-25 14:17:11 -07:00
|
|
|
sent = (int)SENDTO_FUNCTION(sd, &buf[sz - len], len, ssl->wflags,
|
2012-10-19 12:44:23 -07:00
|
|
|
dtlsCtx->peer.sa, dtlsCtx->peer.sz);
|
2012-10-02 09:15:50 -07:00
|
|
|
if (sent < 0) {
|
|
|
|
|
err = LastError();
|
|
|
|
|
CYASSL_MSG("Embed Send To error");
|
|
|
|
|
|
|
|
|
|
if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
|
|
|
|
|
CYASSL_MSG(" Would Block");
|
|
|
|
|
return IO_ERR_WANT_WRITE;
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_ECONNRESET) {
|
|
|
|
|
CYASSL_MSG(" Connection reset");
|
|
|
|
|
return IO_ERR_CONN_RST;
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_EINTR) {
|
|
|
|
|
CYASSL_MSG(" Socket interrupted");
|
|
|
|
|
return IO_ERR_ISR;
|
|
|
|
|
}
|
|
|
|
|
else if (err == SOCKET_EPIPE) {
|
|
|
|
|
CYASSL_MSG(" Socket EPIPE");
|
|
|
|
|
return IO_ERR_CONN_CLOSE;
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
CYASSL_MSG(" General error");
|
|
|
|
|
return IO_ERR_GENERAL;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sent;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-13 15:05:28 -07:00
|
|
|
|
2012-08-08 10:38:12 -07:00
|
|
|
/* The DTLS Generate Cookie callback
|
|
|
|
|
* return : number of bytes copied into buf, or error
|
|
|
|
|
*/
|
|
|
|
|
int EmbedGenerateCookie(byte *buf, int sz, void *ctx)
|
|
|
|
|
{
|
|
|
|
|
CYASSL* ssl = (CYASSL*)ctx;
|
|
|
|
|
int sd = ssl->wfd;
|
2012-08-13 15:05:28 -07:00
|
|
|
struct sockaddr_in peer;
|
|
|
|
|
XSOCKLENT peerSz = sizeof(peer);
|
|
|
|
|
byte cookieSrc[sizeof(struct in_addr) + sizeof(int)];
|
2012-08-08 10:38:12 -07:00
|
|
|
int cookieSrcSz = 0;
|
|
|
|
|
Sha sha;
|
|
|
|
|
|
|
|
|
|
getpeername(sd, (struct sockaddr*)&peer, &peerSz);
|
|
|
|
|
|
2012-08-13 15:05:28 -07:00
|
|
|
if (peer.sin_family == AF_INET) {
|
2012-08-08 10:38:12 -07:00
|
|
|
struct sockaddr_in *s = (struct sockaddr_in*)&peer;
|
|
|
|
|
|
|
|
|
|
cookieSrcSz = sizeof(struct in_addr) + sizeof(s->sin_port);
|
|
|
|
|
XMEMCPY(cookieSrc, &s->sin_port, sizeof(s->sin_port));
|
|
|
|
|
XMEMCPY(cookieSrc + sizeof(s->sin_port),
|
|
|
|
|
&s->sin_addr, sizeof(struct in_addr));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
InitSha(&sha);
|
|
|
|
|
ShaUpdate(&sha, cookieSrc, cookieSrcSz);
|
2012-08-13 15:05:28 -07:00
|
|
|
|
|
|
|
|
if (sz < SHA_DIGEST_SIZE) {
|
|
|
|
|
byte digest[SHA_DIGEST_SIZE];
|
|
|
|
|
ShaFinal(&sha, digest);
|
|
|
|
|
XMEMCPY(buf, digest, sz);
|
|
|
|
|
return sz;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 10:38:12 -07:00
|
|
|
ShaFinal(&sha, buf);
|
|
|
|
|
|
|
|
|
|
return SHA_DIGEST_SIZE;
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-08 17:21:03 -07:00
|
|
|
#endif /* CYASSL_DTLS */
|
|
|
|
|
|
2012-08-08 10:38:12 -07:00
|
|
|
|
2011-02-05 11:14:47 -08:00
|
|
|
#endif /* CYASSL_USER_IO */
|
|
|
|
|
|
2011-08-26 14:40:51 -07:00
|
|
|
CYASSL_API void CyaSSL_SetIORecv(CYASSL_CTX *ctx, CallbackIORecv CBIORecv)
|
2011-02-05 11:14:47 -08:00
|
|
|
{
|
|
|
|
|
ctx->CBIORecv = CBIORecv;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-26 14:40:51 -07:00
|
|
|
CYASSL_API void CyaSSL_SetIOSend(CYASSL_CTX *ctx, CallbackIOSend CBIOSend)
|
2011-02-05 11:14:47 -08:00
|
|
|
{
|
|
|
|
|
ctx->CBIOSend = CBIOSend;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-26 14:40:51 -07:00
|
|
|
CYASSL_API void CyaSSL_SetIOReadCtx(CYASSL* ssl, void *rctx)
|
2011-02-05 11:14:47 -08:00
|
|
|
{
|
|
|
|
|
ssl->IOCB_ReadCtx = rctx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-08-26 14:40:51 -07:00
|
|
|
CYASSL_API void CyaSSL_SetIOWriteCtx(CYASSL* ssl, void *wctx)
|
2011-02-05 11:14:47 -08:00
|
|
|
{
|
|
|
|
|
ssl->IOCB_WriteCtx = wctx;
|
|
|
|
|
}
|
|
|
|
|
|
2012-10-25 14:17:11 -07:00
|
|
|
|
|
|
|
|
CYASSL_API void CyaSSL_SetIOReadFlags(CYASSL* ssl, int flags)
|
|
|
|
|
{
|
|
|
|
|
ssl->rflags = flags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
CYASSL_API void CyaSSL_SetIOWriteFlags(CYASSL* ssl, int flags)
|
|
|
|
|
{
|
|
|
|
|
ssl->wflags = flags;
|
|
|
|
|
}
|
|
|
|
|
|