cya_u
Fork of CyaSSL-forEncrypt by
cyassl_io.c@2:d0516dc143b1, 2017-05-10 (annotated)
- Committer:
- vbahl2
- Date:
- Wed May 10 18:20:47 2017 +0000
- Revision:
- 2:d0516dc143b1
- Parent:
- 0:5045d2638c29
updated
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
toddouska | 0:5045d2638c29 | 1 | /* cyassl_io.c |
toddouska | 0:5045d2638c29 | 2 | * |
toddouska | 0:5045d2638c29 | 3 | * Copyright (C) 2006-2009 Sawtooth Consulting Ltd. |
toddouska | 0:5045d2638c29 | 4 | * |
toddouska | 0:5045d2638c29 | 5 | * This file is part of CyaSSL. |
toddouska | 0:5045d2638c29 | 6 | * |
toddouska | 0:5045d2638c29 | 7 | * CyaSSL is free software; you can redistribute it and/or modify |
toddouska | 0:5045d2638c29 | 8 | * it under the terms of the GNU General Public License as published by |
toddouska | 0:5045d2638c29 | 9 | * the Free Software Foundation; either version 2 of the License, or |
toddouska | 0:5045d2638c29 | 10 | * (at your option) any later version. |
toddouska | 0:5045d2638c29 | 11 | * |
toddouska | 0:5045d2638c29 | 12 | * CyaSSL is distributed in the hope that it will be useful, |
toddouska | 0:5045d2638c29 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
toddouska | 0:5045d2638c29 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
toddouska | 0:5045d2638c29 | 15 | * GNU General Public License for more details. |
toddouska | 0:5045d2638c29 | 16 | * |
toddouska | 0:5045d2638c29 | 17 | * You should have received a copy of the GNU General Public License |
toddouska | 0:5045d2638c29 | 18 | * along with this program; if not, write to the Free Software |
toddouska | 0:5045d2638c29 | 19 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA |
toddouska | 0:5045d2638c29 | 20 | */ |
toddouska | 0:5045d2638c29 | 21 | |
toddouska | 0:5045d2638c29 | 22 | |
toddouska | 0:5045d2638c29 | 23 | #ifdef _WIN32_WCE |
toddouska | 0:5045d2638c29 | 24 | /* On WinCE winsock2.h must be included before windows.h for socket stuff */ |
toddouska | 0:5045d2638c29 | 25 | #include <winsock2.h> |
toddouska | 0:5045d2638c29 | 26 | #endif |
toddouska | 0:5045d2638c29 | 27 | |
toddouska | 0:5045d2638c29 | 28 | #include "cyassl_int.h" |
toddouska | 0:5045d2638c29 | 29 | |
toddouska | 0:5045d2638c29 | 30 | /* if user writes own I/O callbacks they can define CYASSL_USER_IO to remove |
toddouska | 0:5045d2638c29 | 31 | automatic setting of defualt I/O functions EmbedSend() and EmbedReceive() |
toddouska | 0:5045d2638c29 | 32 | but they'll still nedd SetCallback xxx() at end of file |
toddouska | 0:5045d2638c29 | 33 | */ |
toddouska | 0:5045d2638c29 | 34 | #ifndef CYASSL_USER_IO |
toddouska | 0:5045d2638c29 | 35 | |
toddouska | 0:5045d2638c29 | 36 | #ifdef HAVE_LIBZ |
toddouska | 0:5045d2638c29 | 37 | #include "zlib.h" |
toddouska | 0:5045d2638c29 | 38 | #endif |
toddouska | 0:5045d2638c29 | 39 | |
toddouska | 0:5045d2638c29 | 40 | #ifndef USE_WINDOWS_API |
toddouska | 0:5045d2638c29 | 41 | #include <sys/types.h> |
toddouska | 0:5045d2638c29 | 42 | #include <errno.h> |
toddouska | 0:5045d2638c29 | 43 | #include <unistd.h> |
toddouska | 0:5045d2638c29 | 44 | #include <fcntl.h> |
toddouska | 0:5045d2638c29 | 45 | #if !(defined(DEVKITPRO) || defined(THREADX)) |
toddouska | 0:5045d2638c29 | 46 | #include <sys/socket.h> |
toddouska | 0:5045d2638c29 | 47 | #include <arpa/inet.h> |
toddouska | 0:5045d2638c29 | 48 | #include <netinet/in.h> |
toddouska | 0:5045d2638c29 | 49 | #include <netdb.h> |
toddouska | 0:5045d2638c29 | 50 | #include <sys/ioctl.h> |
toddouska | 0:5045d2638c29 | 51 | #endif |
toddouska | 0:5045d2638c29 | 52 | #ifdef THREADX |
toddouska | 0:5045d2638c29 | 53 | #include <socket.h> |
toddouska | 0:5045d2638c29 | 54 | #endif |
toddouska | 0:5045d2638c29 | 55 | #endif /* USE_WINDOWS_API */ |
toddouska | 0:5045d2638c29 | 56 | |
toddouska | 0:5045d2638c29 | 57 | #ifdef __sun |
toddouska | 0:5045d2638c29 | 58 | #include <sys/filio.h> |
toddouska | 0:5045d2638c29 | 59 | #endif |
toddouska | 0:5045d2638c29 | 60 | |
toddouska | 0:5045d2638c29 | 61 | #ifdef USE_WINDOWS_API |
toddouska | 0:5045d2638c29 | 62 | /* no epipe yet */ |
toddouska | 0:5045d2638c29 | 63 | #ifndef WSAEPIPE |
toddouska | 0:5045d2638c29 | 64 | #define WSAEPIPE -12345 |
toddouska | 0:5045d2638c29 | 65 | #endif |
toddouska | 0:5045d2638c29 | 66 | #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK |
toddouska | 0:5045d2638c29 | 67 | #define SOCKET_EAGAIN WSAEWOULDBLOCK |
toddouska | 0:5045d2638c29 | 68 | #define SOCKET_ECONNRESET WSAECONNRESET |
toddouska | 0:5045d2638c29 | 69 | #define SOCKET_EINTR WSAEINTR |
toddouska | 0:5045d2638c29 | 70 | #define SOCKET_EPIPE WSAEPIPE |
toddouska | 0:5045d2638c29 | 71 | #else |
toddouska | 0:5045d2638c29 | 72 | #define SOCKET_EWOULDBLOCK EWOULDBLOCK |
toddouska | 0:5045d2638c29 | 73 | #define SOCKET_EAGAIN EAGAIN |
toddouska | 0:5045d2638c29 | 74 | #define SOCKET_ECONNRESET ECONNRESET |
toddouska | 0:5045d2638c29 | 75 | #define SOCKET_EINTR EINTR |
toddouska | 0:5045d2638c29 | 76 | #define SOCKET_EPIPE EPIPE |
toddouska | 0:5045d2638c29 | 77 | #endif /* USE_WINDOWS_API */ |
toddouska | 0:5045d2638c29 | 78 | |
toddouska | 0:5045d2638c29 | 79 | |
toddouska | 0:5045d2638c29 | 80 | #ifdef DEVKITPRO |
toddouska | 0:5045d2638c29 | 81 | /* from network.h */ |
toddouska | 0:5045d2638c29 | 82 | int net_send(int, const void*, int, unsigned int); |
toddouska | 0:5045d2638c29 | 83 | int net_recv(int, void*, int, unsigned int); |
toddouska | 0:5045d2638c29 | 84 | #define SEND_FUNCTION net_send |
toddouska | 0:5045d2638c29 | 85 | #define RECV_FUNCTION net_recv |
toddouska | 0:5045d2638c29 | 86 | #else |
toddouska | 0:5045d2638c29 | 87 | #define SEND_FUNCTION send |
toddouska | 0:5045d2638c29 | 88 | #define RECV_FUNCTION recv |
toddouska | 0:5045d2638c29 | 89 | #endif |
toddouska | 0:5045d2638c29 | 90 | |
toddouska | 0:5045d2638c29 | 91 | |
toddouska | 0:5045d2638c29 | 92 | static INLINE int LastError(void) |
toddouska | 0:5045d2638c29 | 93 | { |
toddouska | 0:5045d2638c29 | 94 | #ifdef USE_WINDOWS_API |
toddouska | 0:5045d2638c29 | 95 | return WSAGetLastError(); |
toddouska | 0:5045d2638c29 | 96 | #else |
toddouska | 0:5045d2638c29 | 97 | return errno; |
toddouska | 0:5045d2638c29 | 98 | #endif |
toddouska | 0:5045d2638c29 | 99 | } |
toddouska | 0:5045d2638c29 | 100 | |
toddouska | 0:5045d2638c29 | 101 | /* The receive embedded callback |
toddouska | 0:5045d2638c29 | 102 | * return : nb bytes read, or error |
toddouska | 0:5045d2638c29 | 103 | */ |
toddouska | 0:5045d2638c29 | 104 | int EmbedReceive(char *buf, int sz, void *ctx) |
toddouska | 0:5045d2638c29 | 105 | { |
toddouska | 0:5045d2638c29 | 106 | int recvd; |
toddouska | 0:5045d2638c29 | 107 | int err; |
toddouska | 0:5045d2638c29 | 108 | int socket = *(int*)ctx; |
toddouska | 0:5045d2638c29 | 109 | |
toddouska | 0:5045d2638c29 | 110 | recvd = RECV_FUNCTION(socket, (char *)buf, sz, 0); |
toddouska | 0:5045d2638c29 | 111 | |
toddouska | 0:5045d2638c29 | 112 | if (recvd == -1) { |
toddouska | 0:5045d2638c29 | 113 | err = LastError(); |
toddouska | 0:5045d2638c29 | 114 | if (err == SOCKET_EWOULDBLOCK || |
toddouska | 0:5045d2638c29 | 115 | err == SOCKET_EAGAIN) |
toddouska | 0:5045d2638c29 | 116 | return IO_ERR_WANT_READ; |
toddouska | 0:5045d2638c29 | 117 | |
toddouska | 0:5045d2638c29 | 118 | else if (err == SOCKET_ECONNRESET) |
toddouska | 0:5045d2638c29 | 119 | return IO_ERR_CONN_RST; |
toddouska | 0:5045d2638c29 | 120 | |
toddouska | 0:5045d2638c29 | 121 | else if (err == SOCKET_EINTR) |
toddouska | 0:5045d2638c29 | 122 | return IO_ERR_ISR; |
toddouska | 0:5045d2638c29 | 123 | |
toddouska | 0:5045d2638c29 | 124 | else |
toddouska | 0:5045d2638c29 | 125 | return IO_ERR_GENERAL; |
toddouska | 0:5045d2638c29 | 126 | } |
toddouska | 0:5045d2638c29 | 127 | else if (recvd == 0) |
toddouska | 0:5045d2638c29 | 128 | return IO_ERR_CONN_CLOSE; |
toddouska | 0:5045d2638c29 | 129 | |
toddouska | 0:5045d2638c29 | 130 | return recvd; |
toddouska | 0:5045d2638c29 | 131 | } |
toddouska | 0:5045d2638c29 | 132 | |
toddouska | 0:5045d2638c29 | 133 | /* The send embedded callback |
toddouska | 0:5045d2638c29 | 134 | * return : nb bytes sent, or error |
toddouska | 0:5045d2638c29 | 135 | */ |
toddouska | 0:5045d2638c29 | 136 | int EmbedSend(char *buf, int sz, void *ctx) |
toddouska | 0:5045d2638c29 | 137 | { |
toddouska | 0:5045d2638c29 | 138 | int socket = *(int*)ctx; |
toddouska | 0:5045d2638c29 | 139 | int sent; |
toddouska | 0:5045d2638c29 | 140 | int len = sz; |
toddouska | 0:5045d2638c29 | 141 | |
toddouska | 0:5045d2638c29 | 142 | sent = SEND_FUNCTION(socket, &buf[sz - len], len, 0); |
toddouska | 0:5045d2638c29 | 143 | |
toddouska | 0:5045d2638c29 | 144 | if (sent == -1) { |
toddouska | 0:5045d2638c29 | 145 | if (LastError() == SOCKET_EWOULDBLOCK || |
toddouska | 0:5045d2638c29 | 146 | LastError() == SOCKET_EAGAIN) |
toddouska | 0:5045d2638c29 | 147 | return IO_ERR_WANT_WRITE; |
toddouska | 0:5045d2638c29 | 148 | |
toddouska | 0:5045d2638c29 | 149 | else if (LastError() == SOCKET_ECONNRESET) |
toddouska | 0:5045d2638c29 | 150 | return IO_ERR_CONN_RST; |
toddouska | 0:5045d2638c29 | 151 | |
toddouska | 0:5045d2638c29 | 152 | else if (LastError() == SOCKET_EINTR) |
toddouska | 0:5045d2638c29 | 153 | return IO_ERR_ISR; |
toddouska | 0:5045d2638c29 | 154 | |
toddouska | 0:5045d2638c29 | 155 | else if (LastError() == SOCKET_EPIPE) |
toddouska | 0:5045d2638c29 | 156 | return IO_ERR_CONN_CLOSE; |
toddouska | 0:5045d2638c29 | 157 | |
toddouska | 0:5045d2638c29 | 158 | else |
toddouska | 0:5045d2638c29 | 159 | return IO_ERR_GENERAL; |
toddouska | 0:5045d2638c29 | 160 | } |
toddouska | 0:5045d2638c29 | 161 | |
toddouska | 0:5045d2638c29 | 162 | return sent; |
toddouska | 0:5045d2638c29 | 163 | } |
toddouska | 0:5045d2638c29 | 164 | |
toddouska | 0:5045d2638c29 | 165 | |
toddouska | 0:5045d2638c29 | 166 | #endif /* CYASSL_USER_IO */ |
toddouska | 0:5045d2638c29 | 167 | |
toddouska | 0:5045d2638c29 | 168 | |
toddouska | 0:5045d2638c29 | 169 | |
toddouska | 0:5045d2638c29 | 170 | void CyaSSL_SetIORecv(SSL_CTX *ctx, CallbackIORecv CBIORecv) |
toddouska | 0:5045d2638c29 | 171 | { |
toddouska | 0:5045d2638c29 | 172 | ctx->CBIORecv = CBIORecv; |
toddouska | 0:5045d2638c29 | 173 | } |
toddouska | 0:5045d2638c29 | 174 | |
toddouska | 0:5045d2638c29 | 175 | |
toddouska | 0:5045d2638c29 | 176 | void CyaSSL_SetIOSend(SSL_CTX *ctx, CallbackIOSend CBIOSend) |
toddouska | 0:5045d2638c29 | 177 | { |
toddouska | 0:5045d2638c29 | 178 | ctx->CBIOSend = CBIOSend; |
toddouska | 0:5045d2638c29 | 179 | } |
toddouska | 0:5045d2638c29 | 180 | |
toddouska | 0:5045d2638c29 | 181 | |
toddouska | 0:5045d2638c29 | 182 | void CyaSSL_SetIOReadCtx(SSL* ssl, void *rctx) |
toddouska | 0:5045d2638c29 | 183 | { |
toddouska | 0:5045d2638c29 | 184 | ssl->IOCB_ReadCtx = rctx; |
toddouska | 0:5045d2638c29 | 185 | } |
toddouska | 0:5045d2638c29 | 186 | |
toddouska | 0:5045d2638c29 | 187 | |
toddouska | 0:5045d2638c29 | 188 | void CyaSSL_SetIOWriteCtx(SSL* ssl, void *wctx) |
toddouska | 0:5045d2638c29 | 189 | { |
toddouska | 0:5045d2638c29 | 190 | ssl->IOCB_WriteCtx = wctx; |
toddouska | 0:5045d2638c29 | 191 | } |
toddouska | 0:5045d2638c29 | 192 |