ssh
src/io.c@0:c4152c628df5, 2019-11-25 (annotated)
- Committer:
- sPymbed
- Date:
- Mon Nov 25 14:24:05 2019 +0000
- Revision:
- 0:c4152c628df5
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sPymbed | 0:c4152c628df5 | 1 | /* io.c |
sPymbed | 0:c4152c628df5 | 2 | * |
sPymbed | 0:c4152c628df5 | 3 | * Copyright (C) 2014-2016 wolfSSL Inc. |
sPymbed | 0:c4152c628df5 | 4 | * |
sPymbed | 0:c4152c628df5 | 5 | * This file is part of wolfSSH. |
sPymbed | 0:c4152c628df5 | 6 | * |
sPymbed | 0:c4152c628df5 | 7 | * wolfSSH is free software; you can redistribute it and/or modify |
sPymbed | 0:c4152c628df5 | 8 | * it under the terms of the GNU General Public License as published by |
sPymbed | 0:c4152c628df5 | 9 | * the Free Software Foundation; either version 3 of the License, or |
sPymbed | 0:c4152c628df5 | 10 | * (at your option) any later version. |
sPymbed | 0:c4152c628df5 | 11 | * |
sPymbed | 0:c4152c628df5 | 12 | * wolfSSH is distributed in the hope that it will be useful, |
sPymbed | 0:c4152c628df5 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sPymbed | 0:c4152c628df5 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
sPymbed | 0:c4152c628df5 | 15 | * GNU General Public License for more details. |
sPymbed | 0:c4152c628df5 | 16 | * |
sPymbed | 0:c4152c628df5 | 17 | * You should have received a copy of the GNU General Public License |
sPymbed | 0:c4152c628df5 | 18 | * along with wolfSSH. If not, see <http://www.gnu.org/licenses/>. |
sPymbed | 0:c4152c628df5 | 19 | */ |
sPymbed | 0:c4152c628df5 | 20 | |
sPymbed | 0:c4152c628df5 | 21 | |
sPymbed | 0:c4152c628df5 | 22 | /* |
sPymbed | 0:c4152c628df5 | 23 | * The io module provides the default send and receive callbacks used |
sPymbed | 0:c4152c628df5 | 24 | * by the library to handle network I/O. By default they handle UNIX |
sPymbed | 0:c4152c628df5 | 25 | * style I/O. |
sPymbed | 0:c4152c628df5 | 26 | */ |
sPymbed | 0:c4152c628df5 | 27 | |
sPymbed | 0:c4152c628df5 | 28 | |
sPymbed | 0:c4152c628df5 | 29 | #ifdef HAVE_CONFIG_H |
sPymbed | 0:c4152c628df5 | 30 | #include <config.h> |
sPymbed | 0:c4152c628df5 | 31 | #endif |
sPymbed | 0:c4152c628df5 | 32 | |
sPymbed | 0:c4152c628df5 | 33 | #include <wolfssh/ssh.h> |
sPymbed | 0:c4152c628df5 | 34 | #include <wolfssh/internal.h> |
sPymbed | 0:c4152c628df5 | 35 | #include <wolfssh/log.h> |
sPymbed | 0:c4152c628df5 | 36 | |
sPymbed | 0:c4152c628df5 | 37 | #ifndef NULL |
sPymbed | 0:c4152c628df5 | 38 | #include <stddef.h> |
sPymbed | 0:c4152c628df5 | 39 | #endif |
sPymbed | 0:c4152c628df5 | 40 | |
sPymbed | 0:c4152c628df5 | 41 | |
sPymbed | 0:c4152c628df5 | 42 | /* allow I/O callback handlers whether user I/O or not */ |
sPymbed | 0:c4152c628df5 | 43 | |
sPymbed | 0:c4152c628df5 | 44 | /* install I/O recv callback */ |
sPymbed | 0:c4152c628df5 | 45 | void wolfSSH_SetIORecv(WOLFSSH_CTX* ctx, WS_CallbackIORecv cb) |
sPymbed | 0:c4152c628df5 | 46 | { |
sPymbed | 0:c4152c628df5 | 47 | if (ctx) |
sPymbed | 0:c4152c628df5 | 48 | ctx->ioRecvCb = cb; |
sPymbed | 0:c4152c628df5 | 49 | } |
sPymbed | 0:c4152c628df5 | 50 | |
sPymbed | 0:c4152c628df5 | 51 | |
sPymbed | 0:c4152c628df5 | 52 | /* install I/O send callback */ |
sPymbed | 0:c4152c628df5 | 53 | void wolfSSH_SetIOSend(WOLFSSH_CTX* ctx, WS_CallbackIOSend cb) |
sPymbed | 0:c4152c628df5 | 54 | { |
sPymbed | 0:c4152c628df5 | 55 | if (ctx) |
sPymbed | 0:c4152c628df5 | 56 | ctx->ioSendCb = cb; |
sPymbed | 0:c4152c628df5 | 57 | } |
sPymbed | 0:c4152c628df5 | 58 | |
sPymbed | 0:c4152c628df5 | 59 | |
sPymbed | 0:c4152c628df5 | 60 | /* install I/O read context */ |
sPymbed | 0:c4152c628df5 | 61 | void wolfSSH_SetIOReadCtx(WOLFSSH* ssh, void *ctx) |
sPymbed | 0:c4152c628df5 | 62 | { |
sPymbed | 0:c4152c628df5 | 63 | if (ssh) |
sPymbed | 0:c4152c628df5 | 64 | ssh->ioReadCtx = ctx; |
sPymbed | 0:c4152c628df5 | 65 | } |
sPymbed | 0:c4152c628df5 | 66 | |
sPymbed | 0:c4152c628df5 | 67 | |
sPymbed | 0:c4152c628df5 | 68 | /* install I/O read context */ |
sPymbed | 0:c4152c628df5 | 69 | void wolfSSH_SetIOWriteCtx(WOLFSSH* ssh, void *ctx) |
sPymbed | 0:c4152c628df5 | 70 | { |
sPymbed | 0:c4152c628df5 | 71 | if (ssh) |
sPymbed | 0:c4152c628df5 | 72 | ssh->ioWriteCtx = ctx; |
sPymbed | 0:c4152c628df5 | 73 | } |
sPymbed | 0:c4152c628df5 | 74 | |
sPymbed | 0:c4152c628df5 | 75 | |
sPymbed | 0:c4152c628df5 | 76 | /* get I/O read context */ |
sPymbed | 0:c4152c628df5 | 77 | void* wolfSSH_GetIOReadCtx(WOLFSSH* ssh) |
sPymbed | 0:c4152c628df5 | 78 | { |
sPymbed | 0:c4152c628df5 | 79 | if (ssh) |
sPymbed | 0:c4152c628df5 | 80 | return ssh->ioReadCtx; |
sPymbed | 0:c4152c628df5 | 81 | |
sPymbed | 0:c4152c628df5 | 82 | return NULL; |
sPymbed | 0:c4152c628df5 | 83 | } |
sPymbed | 0:c4152c628df5 | 84 | |
sPymbed | 0:c4152c628df5 | 85 | |
sPymbed | 0:c4152c628df5 | 86 | /* get I/O write context */ |
sPymbed | 0:c4152c628df5 | 87 | void* wolfSSH_GetIOWriteCtx(WOLFSSH* ssh) |
sPymbed | 0:c4152c628df5 | 88 | { |
sPymbed | 0:c4152c628df5 | 89 | if (ssh) |
sPymbed | 0:c4152c628df5 | 90 | return ssh->ioWriteCtx; |
sPymbed | 0:c4152c628df5 | 91 | |
sPymbed | 0:c4152c628df5 | 92 | return NULL; |
sPymbed | 0:c4152c628df5 | 93 | } |
sPymbed | 0:c4152c628df5 | 94 | |
sPymbed | 0:c4152c628df5 | 95 | |
sPymbed | 0:c4152c628df5 | 96 | #ifndef WOLFSSH_USER_IO |
sPymbed | 0:c4152c628df5 | 97 | |
sPymbed | 0:c4152c628df5 | 98 | /* default I/O callbacks, use BSD style sockets */ |
sPymbed | 0:c4152c628df5 | 99 | |
sPymbed | 0:c4152c628df5 | 100 | |
sPymbed | 0:c4152c628df5 | 101 | #ifndef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 102 | #ifdef WOLFSSH_LWIP |
sPymbed | 0:c4152c628df5 | 103 | /* lwIP needs to be configured to use sockets API in this mode */ |
sPymbed | 0:c4152c628df5 | 104 | /* LWIP_SOCKET 1 in lwip/opt.h or in build */ |
sPymbed | 0:c4152c628df5 | 105 | #include "lwip/sockets.h" |
sPymbed | 0:c4152c628df5 | 106 | #include <errno.h> |
sPymbed | 0:c4152c628df5 | 107 | #ifndef LWIP_PROVIDE_ERRNO |
sPymbed | 0:c4152c628df5 | 108 | #define LWIP_PROVIDE_ERRNO 1 |
sPymbed | 0:c4152c628df5 | 109 | #endif |
sPymbed | 0:c4152c628df5 | 110 | #elif defined(FREESCALE_MQX) |
sPymbed | 0:c4152c628df5 | 111 | #include <posix.h> |
sPymbed | 0:c4152c628df5 | 112 | #include <rtcs.h> |
sPymbed | 0:c4152c628df5 | 113 | #elif defined(WOLFSSH_MDK_ARM) |
sPymbed | 0:c4152c628df5 | 114 | #if defined(WOLFSSH_MDK5) |
sPymbed | 0:c4152c628df5 | 115 | #include "cmsis_os.h" |
sPymbed | 0:c4152c628df5 | 116 | #include "rl_fs.h" |
sPymbed | 0:c4152c628df5 | 117 | #include "rl_net.h" |
sPymbed | 0:c4152c628df5 | 118 | #else |
sPymbed | 0:c4152c628df5 | 119 | #include <rtl.h> |
sPymbed | 0:c4152c628df5 | 120 | #endif |
sPymbed | 0:c4152c628df5 | 121 | #undef RNG |
sPymbed | 0:c4152c628df5 | 122 | #include "WOLFSSH_MDK_ARM.h" |
sPymbed | 0:c4152c628df5 | 123 | #undef RNG |
sPymbed | 0:c4152c628df5 | 124 | #define RNG CyaSSL_RNG |
sPymbed | 0:c4152c628df5 | 125 | /* for avoiding name conflict in "stm32f2xx.h" */ |
sPymbed | 0:c4152c628df5 | 126 | static int errno; |
sPymbed | 0:c4152c628df5 | 127 | #else |
sPymbed | 0:c4152c628df5 | 128 | //#include <sys/types.h> |
sPymbed | 0:c4152c628df5 | 129 | #include <errno.h> |
sPymbed | 0:c4152c628df5 | 130 | #ifndef EBSNET |
sPymbed | 0:c4152c628df5 | 131 | //#include <unistd.h> |
sPymbed | 0:c4152c628df5 | 132 | #endif |
sPymbed | 0:c4152c628df5 | 133 | //#include <fcntl.h> |
sPymbed | 0:c4152c628df5 | 134 | #if !(defined(DEVKITPRO) || defined(HAVE_RTP_SYS) || defined(EBSNET)) |
sPymbed | 0:c4152c628df5 | 135 | #include <sys/socket.h> |
sPymbed | 0:c4152c628df5 | 136 | #include <arpa/inet.h> |
sPymbed | 0:c4152c628df5 | 137 | //#include <netinet/in.h> |
sPymbed | 0:c4152c628df5 | 138 | #include <netdb.h> |
sPymbed | 0:c4152c628df5 | 139 | #ifdef __PPU |
sPymbed | 0:c4152c628df5 | 140 | #include <netex/errno.h> |
sPymbed | 0:c4152c628df5 | 141 | #else |
sPymbed | 0:c4152c628df5 | 142 | //#include <sys/ioctl.h> |
sPymbed | 0:c4152c628df5 | 143 | #endif |
sPymbed | 0:c4152c628df5 | 144 | #endif |
sPymbed | 0:c4152c628df5 | 145 | #ifdef HAVE_RTP_SYS |
sPymbed | 0:c4152c628df5 | 146 | #include <socket.h> |
sPymbed | 0:c4152c628df5 | 147 | #endif |
sPymbed | 0:c4152c628df5 | 148 | #ifdef EBSNET |
sPymbed | 0:c4152c628df5 | 149 | #include "rtipapi.h" /* errno */ |
sPymbed | 0:c4152c628df5 | 150 | #include "socket.h" |
sPymbed | 0:c4152c628df5 | 151 | #endif |
sPymbed | 0:c4152c628df5 | 152 | #endif |
sPymbed | 0:c4152c628df5 | 153 | #endif /* USE_WINDOWS_API */ |
sPymbed | 0:c4152c628df5 | 154 | |
sPymbed | 0:c4152c628df5 | 155 | #ifdef __sun |
sPymbed | 0:c4152c628df5 | 156 | #include <sys/filio.h> |
sPymbed | 0:c4152c628df5 | 157 | #endif |
sPymbed | 0:c4152c628df5 | 158 | |
sPymbed | 0:c4152c628df5 | 159 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 160 | /* no epipe yet */ |
sPymbed | 0:c4152c628df5 | 161 | #ifndef WSAEPIPE |
sPymbed | 0:c4152c628df5 | 162 | #define WSAEPIPE -12345 |
sPymbed | 0:c4152c628df5 | 163 | #endif |
sPymbed | 0:c4152c628df5 | 164 | #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK |
sPymbed | 0:c4152c628df5 | 165 | #define SOCKET_EAGAIN WSAETIMEDOUT |
sPymbed | 0:c4152c628df5 | 166 | #define SOCKET_ECONNRESET WSAECONNRESET |
sPymbed | 0:c4152c628df5 | 167 | #define SOCKET_EINTR WSAEINTR |
sPymbed | 0:c4152c628df5 | 168 | #define SOCKET_EPIPE WSAEPIPE |
sPymbed | 0:c4152c628df5 | 169 | #define SOCKET_ECONNREFUSED WSAENOTCONN |
sPymbed | 0:c4152c628df5 | 170 | #define SOCKET_ECONNABORTED WSAECONNABORTED |
sPymbed | 0:c4152c628df5 | 171 | #define close(s) closesocket(s) |
sPymbed | 0:c4152c628df5 | 172 | #elif defined(__PPU) |
sPymbed | 0:c4152c628df5 | 173 | #define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK |
sPymbed | 0:c4152c628df5 | 174 | #define SOCKET_EAGAIN SYS_NET_EAGAIN |
sPymbed | 0:c4152c628df5 | 175 | #define SOCKET_ECONNRESET SYS_NET_ECONNRESET |
sPymbed | 0:c4152c628df5 | 176 | #define SOCKET_EINTR SYS_NET_EINTR |
sPymbed | 0:c4152c628df5 | 177 | #define SOCKET_EPIPE SYS_NET_EPIPE |
sPymbed | 0:c4152c628df5 | 178 | #define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED |
sPymbed | 0:c4152c628df5 | 179 | #define SOCKET_ECONNABORTED SYS_NET_ECONNABORTED |
sPymbed | 0:c4152c628df5 | 180 | #elif defined(FREESCALE_MQX) |
sPymbed | 0:c4152c628df5 | 181 | /* RTCS doesn't have an EWOULDBLOCK error */ |
sPymbed | 0:c4152c628df5 | 182 | #define SOCKET_EWOULDBLOCK EAGAIN |
sPymbed | 0:c4152c628df5 | 183 | #define SOCKET_EAGAIN EAGAIN |
sPymbed | 0:c4152c628df5 | 184 | #define SOCKET_ECONNRESET RTCSERR_TCP_CONN_RESET |
sPymbed | 0:c4152c628df5 | 185 | #define SOCKET_EINTR EINTR |
sPymbed | 0:c4152c628df5 | 186 | #define SOCKET_EPIPE EPIPE |
sPymbed | 0:c4152c628df5 | 187 | #define SOCKET_ECONNREFUSED RTCSERR_TCP_CONN_REFUSED |
sPymbed | 0:c4152c628df5 | 188 | #define SOCKET_ECONNABORTED RTCSERR_TCP_CONN_ABORTED |
sPymbed | 0:c4152c628df5 | 189 | #elif defined(WOLFSSH_MDK_ARM) |
sPymbed | 0:c4152c628df5 | 190 | #if defined(WOLFSSH_MDK5) |
sPymbed | 0:c4152c628df5 | 191 | #define SOCKET_EWOULDBLOCK BSD_ERROR_WOULDBLOCK |
sPymbed | 0:c4152c628df5 | 192 | #define SOCKET_EAGAIN BSD_ERROR_LOCKED |
sPymbed | 0:c4152c628df5 | 193 | #define SOCKET_ECONNRESET BSD_ERROR_CLOSED |
sPymbed | 0:c4152c628df5 | 194 | #define SOCKET_EINTR BSD_ERROR |
sPymbed | 0:c4152c628df5 | 195 | #define SOCKET_EPIPE BSD_ERROR |
sPymbed | 0:c4152c628df5 | 196 | #define SOCKET_ECONNREFUSED BSD_ERROR |
sPymbed | 0:c4152c628df5 | 197 | #define SOCKET_ECONNABORTED BSD_ERROR |
sPymbed | 0:c4152c628df5 | 198 | #else |
sPymbed | 0:c4152c628df5 | 199 | #define SOCKET_EWOULDBLOCK SCK_EWOULDBLOCK |
sPymbed | 0:c4152c628df5 | 200 | #define SOCKET_EAGAIN SCK_ELOCKED |
sPymbed | 0:c4152c628df5 | 201 | #define SOCKET_ECONNRESET SCK_ECLOSED |
sPymbed | 0:c4152c628df5 | 202 | #define SOCKET_EINTR SCK_ERROR |
sPymbed | 0:c4152c628df5 | 203 | #define SOCKET_EPIPE SCK_ERROR |
sPymbed | 0:c4152c628df5 | 204 | #define SOCKET_ECONNREFUSED SCK_ERROR |
sPymbed | 0:c4152c628df5 | 205 | #define SOCKET_ECONNABORTED SCK_ERROR |
sPymbed | 0:c4152c628df5 | 206 | #endif |
sPymbed | 0:c4152c628df5 | 207 | #else |
sPymbed | 0:c4152c628df5 | 208 | #define SOCKET_EWOULDBLOCK EWOULDBLOCK |
sPymbed | 0:c4152c628df5 | 209 | #define SOCKET_EAGAIN EAGAIN |
sPymbed | 0:c4152c628df5 | 210 | #define SOCKET_ECONNRESET ECONNRESET |
sPymbed | 0:c4152c628df5 | 211 | #define SOCKET_EINTR EINTR |
sPymbed | 0:c4152c628df5 | 212 | #define SOCKET_EPIPE EPIPE |
sPymbed | 0:c4152c628df5 | 213 | #define SOCKET_ECONNREFUSED ECONNREFUSED |
sPymbed | 0:c4152c628df5 | 214 | #define SOCKET_ECONNABORTED ECONNABORTED |
sPymbed | 0:c4152c628df5 | 215 | #endif /* USE_WINDOWS_API */ |
sPymbed | 0:c4152c628df5 | 216 | |
sPymbed | 0:c4152c628df5 | 217 | int recv(int a, void* b, int c, unsigned int d){ |
sPymbed | 0:c4152c628df5 | 218 | return 0; |
sPymbed | 0:c4152c628df5 | 219 | } |
sPymbed | 0:c4152c628df5 | 220 | |
sPymbed | 0:c4152c628df5 | 221 | int send(int a, const void* b, int c, unsigned int d){ |
sPymbed | 0:c4152c628df5 | 222 | return 0; |
sPymbed | 0:c4152c628df5 | 223 | } |
sPymbed | 0:c4152c628df5 | 224 | |
sPymbed | 0:c4152c628df5 | 225 | #ifdef DEVKITPRO |
sPymbed | 0:c4152c628df5 | 226 | /* from network.h */ |
sPymbed | 0:c4152c628df5 | 227 | int net_send(int, const void*, int, unsigned int); |
sPymbed | 0:c4152c628df5 | 228 | int net_recv(int, void*, int, unsigned int); |
sPymbed | 0:c4152c628df5 | 229 | #define SEND_FUNCTION net_send |
sPymbed | 0:c4152c628df5 | 230 | #define RECV_FUNCTION net_recv |
sPymbed | 0:c4152c628df5 | 231 | #elif defined(WOLFSSH_LWIP) |
sPymbed | 0:c4152c628df5 | 232 | #define SEND_FUNCTION lwip_send |
sPymbed | 0:c4152c628df5 | 233 | #define RECV_FUNCTION lwip_recv |
sPymbed | 0:c4152c628df5 | 234 | #else |
sPymbed | 0:c4152c628df5 | 235 | #define SEND_FUNCTION send |
sPymbed | 0:c4152c628df5 | 236 | #define RECV_FUNCTION recv |
sPymbed | 0:c4152c628df5 | 237 | #endif |
sPymbed | 0:c4152c628df5 | 238 | |
sPymbed | 0:c4152c628df5 | 239 | |
sPymbed | 0:c4152c628df5 | 240 | /* Translates return codes returned from |
sPymbed | 0:c4152c628df5 | 241 | * send() and recv() if need be. |
sPymbed | 0:c4152c628df5 | 242 | */ |
sPymbed | 0:c4152c628df5 | 243 | static INLINE int TranslateReturnCode(int old, int sd) |
sPymbed | 0:c4152c628df5 | 244 | { |
sPymbed | 0:c4152c628df5 | 245 | (void)sd; |
sPymbed | 0:c4152c628df5 | 246 | |
sPymbed | 0:c4152c628df5 | 247 | #ifdef FREESCALE_MQX |
sPymbed | 0:c4152c628df5 | 248 | if (old == 0) { |
sPymbed | 0:c4152c628df5 | 249 | errno = SOCKET_EWOULDBLOCK; |
sPymbed | 0:c4152c628df5 | 250 | return -1; /* convert to BSD style wouldblock as error */ |
sPymbed | 0:c4152c628df5 | 251 | } |
sPymbed | 0:c4152c628df5 | 252 | |
sPymbed | 0:c4152c628df5 | 253 | if (old < 0) { |
sPymbed | 0:c4152c628df5 | 254 | errno = RTCS_geterror(sd); |
sPymbed | 0:c4152c628df5 | 255 | if (errno == RTCSERR_TCP_CONN_CLOSING) |
sPymbed | 0:c4152c628df5 | 256 | return 0; /* convert to BSD style closing */ |
sPymbed | 0:c4152c628df5 | 257 | } |
sPymbed | 0:c4152c628df5 | 258 | #endif |
sPymbed | 0:c4152c628df5 | 259 | |
sPymbed | 0:c4152c628df5 | 260 | return old; |
sPymbed | 0:c4152c628df5 | 261 | } |
sPymbed | 0:c4152c628df5 | 262 | |
sPymbed | 0:c4152c628df5 | 263 | static INLINE int LastError(void) |
sPymbed | 0:c4152c628df5 | 264 | { |
sPymbed | 0:c4152c628df5 | 265 | #ifdef USE_WINDOWS_API |
sPymbed | 0:c4152c628df5 | 266 | return WSAGetLastError(); |
sPymbed | 0:c4152c628df5 | 267 | #elif defined(EBSNET) |
sPymbed | 0:c4152c628df5 | 268 | return xn_getlasterror(); |
sPymbed | 0:c4152c628df5 | 269 | #else |
sPymbed | 0:c4152c628df5 | 270 | return errno; |
sPymbed | 0:c4152c628df5 | 271 | #endif |
sPymbed | 0:c4152c628df5 | 272 | } |
sPymbed | 0:c4152c628df5 | 273 | |
sPymbed | 0:c4152c628df5 | 274 | |
sPymbed | 0:c4152c628df5 | 275 | /* The receive embedded callback |
sPymbed | 0:c4152c628df5 | 276 | * return : nb bytes read, or error |
sPymbed | 0:c4152c628df5 | 277 | */ |
sPymbed | 0:c4152c628df5 | 278 | int wsEmbedRecv(WOLFSSH* ssh, void* data, word32 sz, void* ctx) |
sPymbed | 0:c4152c628df5 | 279 | { |
sPymbed | 0:c4152c628df5 | 280 | int recvd; |
sPymbed | 0:c4152c628df5 | 281 | int err; |
sPymbed | 0:c4152c628df5 | 282 | int sd = *(int*)ctx; |
sPymbed | 0:c4152c628df5 | 283 | char* buf = (char*)data; |
sPymbed | 0:c4152c628df5 | 284 | |
sPymbed | 0:c4152c628df5 | 285 | recvd = (int)RECV_FUNCTION(sd, buf, sz, ssh->rflags); |
sPymbed | 0:c4152c628df5 | 286 | |
sPymbed | 0:c4152c628df5 | 287 | recvd = TranslateReturnCode(recvd, sd); |
sPymbed | 0:c4152c628df5 | 288 | |
sPymbed | 0:c4152c628df5 | 289 | if (recvd < 0) { |
sPymbed | 0:c4152c628df5 | 290 | err = LastError(); |
sPymbed | 0:c4152c628df5 | 291 | WLOG(WS_LOG_DEBUG,"Embed Receive error"); |
sPymbed | 0:c4152c628df5 | 292 | |
sPymbed | 0:c4152c628df5 | 293 | /*if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
sPymbed | 0:c4152c628df5 | 294 | WLOG(WS_LOG_DEBUG," Would block"); |
sPymbed | 0:c4152c628df5 | 295 | return WS_CBIO_ERR_WANT_READ; |
sPymbed | 0:c4152c628df5 | 296 | } |
sPymbed | 0:c4152c628df5 | 297 | else if (err == SOCKET_ECONNRESET) { |
sPymbed | 0:c4152c628df5 | 298 | WLOG(WS_LOG_DEBUG," Connection reset"); |
sPymbed | 0:c4152c628df5 | 299 | return WS_CBIO_ERR_CONN_RST; |
sPymbed | 0:c4152c628df5 | 300 | } |
sPymbed | 0:c4152c628df5 | 301 | else if (err == SOCKET_EINTR) { |
sPymbed | 0:c4152c628df5 | 302 | WLOG(WS_LOG_DEBUG," Socket interrupted"); |
sPymbed | 0:c4152c628df5 | 303 | return WS_CBIO_ERR_ISR; |
sPymbed | 0:c4152c628df5 | 304 | } |
sPymbed | 0:c4152c628df5 | 305 | else if (err == SOCKET_ECONNREFUSED) { |
sPymbed | 0:c4152c628df5 | 306 | WLOG(WS_LOG_DEBUG," Connection refused"); |
sPymbed | 0:c4152c628df5 | 307 | return WS_CBIO_ERR_WANT_READ; |
sPymbed | 0:c4152c628df5 | 308 | } |
sPymbed | 0:c4152c628df5 | 309 | else if (err == SOCKET_ECONNABORTED) { |
sPymbed | 0:c4152c628df5 | 310 | WLOG(WS_LOG_DEBUG," Connection aborted"); |
sPymbed | 0:c4152c628df5 | 311 | return WS_CBIO_ERR_CONN_CLOSE; |
sPymbed | 0:c4152c628df5 | 312 | } |
sPymbed | 0:c4152c628df5 | 313 | else*/ { |
sPymbed | 0:c4152c628df5 | 314 | WLOG(WS_LOG_DEBUG," General error"); |
sPymbed | 0:c4152c628df5 | 315 | return WS_CBIO_ERR_GENERAL; |
sPymbed | 0:c4152c628df5 | 316 | } |
sPymbed | 0:c4152c628df5 | 317 | } |
sPymbed | 0:c4152c628df5 | 318 | else if (recvd == 0) { |
sPymbed | 0:c4152c628df5 | 319 | WLOG(WS_LOG_DEBUG,"Embed receive connection closed"); |
sPymbed | 0:c4152c628df5 | 320 | return WS_CBIO_ERR_CONN_CLOSE; |
sPymbed | 0:c4152c628df5 | 321 | } |
sPymbed | 0:c4152c628df5 | 322 | |
sPymbed | 0:c4152c628df5 | 323 | return recvd; |
sPymbed | 0:c4152c628df5 | 324 | } |
sPymbed | 0:c4152c628df5 | 325 | |
sPymbed | 0:c4152c628df5 | 326 | |
sPymbed | 0:c4152c628df5 | 327 | /* The send embedded callback |
sPymbed | 0:c4152c628df5 | 328 | * return : nb bytes sent, or error |
sPymbed | 0:c4152c628df5 | 329 | */ |
sPymbed | 0:c4152c628df5 | 330 | int wsEmbedSend(WOLFSSH* ssh, void* data, word32 sz, void* ctx) |
sPymbed | 0:c4152c628df5 | 331 | { |
sPymbed | 0:c4152c628df5 | 332 | int sd = *(int*)ctx; |
sPymbed | 0:c4152c628df5 | 333 | int sent; |
sPymbed | 0:c4152c628df5 | 334 | int err; |
sPymbed | 0:c4152c628df5 | 335 | char* buf = (char*)data; |
sPymbed | 0:c4152c628df5 | 336 | |
sPymbed | 0:c4152c628df5 | 337 | WLOG(WS_LOG_DEBUG,"Embed Send trying to send %d", sz); |
sPymbed | 0:c4152c628df5 | 338 | |
sPymbed | 0:c4152c628df5 | 339 | sent = (int)SEND_FUNCTION(sd, buf, sz, ssh->wflags); |
sPymbed | 0:c4152c628df5 | 340 | |
sPymbed | 0:c4152c628df5 | 341 | WLOG(WS_LOG_DEBUG,"Embed Send sent %d", sent); |
sPymbed | 0:c4152c628df5 | 342 | |
sPymbed | 0:c4152c628df5 | 343 | if (sent < 0) { |
sPymbed | 0:c4152c628df5 | 344 | err = LastError(); |
sPymbed | 0:c4152c628df5 | 345 | WLOG(WS_LOG_DEBUG,"Embed Send error"); |
sPymbed | 0:c4152c628df5 | 346 | |
sPymbed | 0:c4152c628df5 | 347 | /*if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { |
sPymbed | 0:c4152c628df5 | 348 | WLOG(WS_LOG_DEBUG," Would Block"); |
sPymbed | 0:c4152c628df5 | 349 | return WS_CBIO_ERR_WANT_WRITE; |
sPymbed | 0:c4152c628df5 | 350 | } |
sPymbed | 0:c4152c628df5 | 351 | else if (err == SOCKET_ECONNRESET) { |
sPymbed | 0:c4152c628df5 | 352 | WLOG(WS_LOG_DEBUG," Connection reset"); |
sPymbed | 0:c4152c628df5 | 353 | return WS_CBIO_ERR_CONN_RST; |
sPymbed | 0:c4152c628df5 | 354 | } |
sPymbed | 0:c4152c628df5 | 355 | else if (err == SOCKET_EINTR) { |
sPymbed | 0:c4152c628df5 | 356 | WLOG(WS_LOG_DEBUG," Socket interrupted"); |
sPymbed | 0:c4152c628df5 | 357 | return WS_CBIO_ERR_ISR; |
sPymbed | 0:c4152c628df5 | 358 | } |
sPymbed | 0:c4152c628df5 | 359 | else if (err == SOCKET_EPIPE) { |
sPymbed | 0:c4152c628df5 | 360 | WLOG(WS_LOG_DEBUG," Socket EPIPE"); |
sPymbed | 0:c4152c628df5 | 361 | return WS_CBIO_ERR_CONN_CLOSE; |
sPymbed | 0:c4152c628df5 | 362 | } |
sPymbed | 0:c4152c628df5 | 363 | else */{ |
sPymbed | 0:c4152c628df5 | 364 | WLOG(WS_LOG_DEBUG," General error"); |
sPymbed | 0:c4152c628df5 | 365 | return WS_CBIO_ERR_GENERAL; |
sPymbed | 0:c4152c628df5 | 366 | } |
sPymbed | 0:c4152c628df5 | 367 | } |
sPymbed | 0:c4152c628df5 | 368 | |
sPymbed | 0:c4152c628df5 | 369 | return sent; |
sPymbed | 0:c4152c628df5 | 370 | } |
sPymbed | 0:c4152c628df5 | 371 | |
sPymbed | 0:c4152c628df5 | 372 | |
sPymbed | 0:c4152c628df5 | 373 | #endif /* WOLFSSH_USER_IO */ |
sPymbed | 0:c4152c628df5 | 374 |