1 /* io.h
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
6  *
7  * wolfSSL is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * wolfSSL is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20  */
21 
22 /*!
23     \file wolfssl/wolfio.h
24 */
25 
26 #ifndef WOLFSSL_IO_H
27 #define WOLFSSL_IO_H
28 
29 #ifdef __cplusplus
30     extern "C" {
31 #endif
32 
33 /* Micrium uses NetSock I/O callbacks in wolfio.c */
34 #if !defined(WOLFSSL_USER_IO)
35     /* OCSP and CRL_IO require HTTP client */
36     #if defined(HAVE_OCSP) || defined(HAVE_CRL_IO)
37         #ifndef HAVE_HTTP_CLIENT
38             #define HAVE_HTTP_CLIENT
39         #endif
40     #endif
41 #endif
42 
43 #if !defined(WOLFSSL_USER_IO)
44     /* Micrium uses NetSock I/O callbacks in wolfio.c */
45     #if !defined(USE_WOLFSSL_IO) && !defined(MICRIUM) && \
46         !defined(WOLFSSL_CONTIKI) && !defined(WOLFSSL_NO_SOCK)
47         #define USE_WOLFSSL_IO
48     #endif
49 #endif
50 
51 
52 #if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT)
53 
54 #ifdef HAVE_LIBZ
55     #include "zlib.h"
56 #endif
57 
58 #ifndef USE_WINDOWS_API
59     #if defined(WOLFSSL_LWIP) && !defined(WOLFSSL_APACHE_MYNEWT)
60         /* lwIP needs to be configured to use sockets API in this mode */
61         /* LWIP_SOCKET 1 in lwip/opt.h or in build */
62         #include "lwip/sockets.h"
63         #ifndef LWIP_PROVIDE_ERRNO
64             #include <errno.h>
65             #define LWIP_PROVIDE_ERRNO 1
66         #endif
67     #elif defined(FREESCALE_MQX)
68         #include <posix.h>
69         #include <rtcs.h>
70     #elif defined(FREESCALE_KSDK_MQX)
71         #include <rtcs.h>
72     #elif (defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET))
73         #include "rl_net.h"
74         #include "errno.h"
75     #elif defined(WOLFSSL_CMSIS_RTOS)
76         #include "cmsis_os.h"
77     #elif defined(WOLFSSL_CMSIS_RTOSv2)
78         #include "cmsis_os2.h"
79     #elif defined(WOLFSSL_TIRTOS)
80         #include <sys/socket.h>
81     #elif defined(FREERTOS_TCP)
82         #include "FreeRTOS_Sockets.h"
83     #elif defined(WOLFSSL_IAR_ARM)
84         /* nothing */
85     #elif defined(HAVE_NETX_BSD)
86         #ifdef NETX_DUO
87             #include "nxd_bsd.h"
88         #else
89             #include "nx_bsd.h"
90         #endif
91     #elif defined(WOLFSSL_VXWORKS)
92         #include <sockLib.h>
93         #include <errno.h>
94     #elif defined(WOLFSSL_NUCLEUS_1_2)
95         #include <externs.h>
96         #include <errno.h>
97     #elif defined(WOLFSSL_LINUXKM)
98         /* the requisite linux/net.h is included in wc_port.h, with incompatible warnings masked out. */
99     #elif defined(WOLFSSL_ATMEL)
100         #include "socket/include/socket.h"
101     #elif defined(INTIME_RTOS)
102         #undef MIN
103         #undef MAX
104         #include <rt.h>
105         #include <sys/types.h>
106         #include <sys/socket.h>
107         #include <netdb.h>
108         #include <netinet/in.h>
109         #include <io.h>
110         /* <sys/socket.h> defines these, to avoid conflict, do undef */
111         #undef SOCKADDR
112         #undef SOCKADDR_IN
113     #elif defined(WOLFSSL_PRCONNECT_PRO)
114         #include <prconnect_pro/prconnect_pro.h>
115         #include <sys/types.h>
116         #include <errno.h>
117         #include <unistd.h>
118         #include <fcntl.h>
119         #include <netdb.h>
120         #include <sys/ioctl.h>
121     #elif defined(WOLFSSL_SGX)
122         #include <errno.h>
123     #elif defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
124         #include <mn_socket/mn_socket.h>
125     #elif defined(WOLFSSL_DEOS)
126         #include <socketapi.h>
127         #include <lwip-socket.h>
128         #include <errno.h>
129     #elif defined(WOLFSSL_ZEPHYR)
130         #include <net/socket.h>
131     #elif defined(MICROCHIP_PIC32)
132         #include <sys/errno.h>
133     #elif defined(HAVE_NETX)
134         #include "nx_api.h"
135         #include "errno.h"
136     #elif defined(FUSION_RTOS)
137         #include <sys/fcltypes.h>
138         #include <fclerrno.h>
139         #include <fclfcntl.h>
140     #elif !defined(WOLFSSL_NO_SOCK)
141         #include <sys/types.h>
142         #include <errno.h>
143         #ifndef EBSNET
144             #include <unistd.h>
145         #endif
146         #include <fcntl.h>
147         #define XFCNTL(fd, flag, block) fcntl((fd), (flag), (block))
148 
149         #if defined(HAVE_RTP_SYS)
150             #include <socket.h>
151         #elif defined(EBSNET)
152             #include "rtipapi.h"  /* errno */
153             #include "socket.h"
154         #elif defined(NETOS)
155             #include <sockapi.h>
156         #elif !defined(DEVKITPRO) && !defined(WOLFSSL_PICOTCP) \
157                 && !defined(WOLFSSL_CONTIKI) && !defined(WOLFSSL_WICED) \
158                 && !defined(WOLFSSL_GNRC) && !defined(WOLFSSL_RIOT_OS)
159             #include <sys/socket.h>
160             #include <arpa/inet.h>
161             #include <netinet/in.h>
162             #include <netdb.h>
163             #ifdef __PPU
164                 #include <netex/errno.h>
165             #else
166                 #include <sys/ioctl.h>
167             #endif
168         #endif
169     #endif
170 
171     #if defined(WOLFSSL_RENESAS_RA6M3G) || defined(WOLFSSL_RENESAS_RA6M3) ||\
172                     defined(WOLFSSL_RENESAS_RA6M4)
173       /* Uses FREERTOS_TCP */
174         #include <errno.h>
175     #endif
176 
177 #endif /* USE_WINDOWS_API */
178 
179 #ifdef __sun
180     #include <sys/filio.h>
181 #endif
182 
183 #ifdef USE_WINDOWS_API
184     /* no epipe yet */
185     #ifndef WSAEPIPE
186         #define WSAEPIPE       -12345
187     #endif
188     #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
189     #define SOCKET_EAGAIN      WSAETIMEDOUT
190     #define SOCKET_ECONNRESET  WSAECONNRESET
191     #define SOCKET_EINTR       WSAEINTR
192     #define SOCKET_EPIPE       WSAEPIPE
193     #define SOCKET_ECONNREFUSED WSAENOTCONN
194     #define SOCKET_ECONNABORTED WSAECONNABORTED
195 #elif defined(__PPU)
196     #define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK
197     #define SOCKET_EAGAIN      SYS_NET_EAGAIN
198     #define SOCKET_ECONNRESET  SYS_NET_ECONNRESET
199     #define SOCKET_EINTR       SYS_NET_EINTR
200     #define SOCKET_EPIPE       SYS_NET_EPIPE
201     #define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED
202     #define SOCKET_ECONNABORTED SYS_NET_ECONNABORTED
203 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
204     #if MQX_USE_IO_OLD
205         /* RTCS old I/O doesn't have an EWOULDBLOCK */
206         #define SOCKET_EWOULDBLOCK  EAGAIN
207         #define SOCKET_EAGAIN       EAGAIN
208         #define SOCKET_ECONNRESET   RTCSERR_TCP_CONN_RESET
209         #define SOCKET_EINTR        EINTR
210         #define SOCKET_EPIPE        EPIPE
211         #define SOCKET_ECONNREFUSED RTCSERR_TCP_CONN_REFUSED
212         #define SOCKET_ECONNABORTED RTCSERR_TCP_CONN_ABORTED
213     #else
214         #define SOCKET_EWOULDBLOCK  NIO_EWOULDBLOCK
215         #define SOCKET_EAGAIN       NIO_EAGAIN
216         #define SOCKET_ECONNRESET   NIO_ECONNRESET
217         #define SOCKET_EINTR        NIO_EINTR
218         #define SOCKET_EPIPE        NIO_EPIPE
219         #define SOCKET_ECONNREFUSED NIO_ECONNREFUSED
220         #define SOCKET_ECONNABORTED NIO_ECONNABORTED
221     #endif
222 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET)
223         #define SOCKET_EWOULDBLOCK BSD_ERROR_WOULDBLOCK
224         #define SOCKET_EAGAIN      BSD_ERROR_LOCKED
225         #define SOCKET_ECONNRESET  BSD_ERROR_CLOSED
226         #define SOCKET_EINTR       BSD_ERROR
227         #define SOCKET_EPIPE       BSD_ERROR
228         #define SOCKET_ECONNREFUSED BSD_ERROR
229         #define SOCKET_ECONNABORTED BSD_ERROR
230 #elif defined(WOLFSSL_PICOTCP)
231     #define SOCKET_EWOULDBLOCK  PICO_ERR_EAGAIN
232     #define SOCKET_EAGAIN       PICO_ERR_EAGAIN
233     #define SOCKET_ECONNRESET   PICO_ERR_ECONNRESET
234     #define SOCKET_EINTR        PICO_ERR_EINTR
235     #define SOCKET_EPIPE        PICO_ERR_EIO
236     #define SOCKET_ECONNREFUSED PICO_ERR_ECONNREFUSED
237     #define SOCKET_ECONNABORTED PICO_ERR_ESHUTDOWN
238 #elif defined(FREERTOS_TCP)
239     #define SOCKET_EWOULDBLOCK FREERTOS_EWOULDBLOCK
240     #define SOCKET_EAGAIN       FREERTOS_EWOULDBLOCK
241     #define SOCKET_ECONNRESET   FREERTOS_SOCKET_ERROR
242     #define SOCKET_EINTR        FREERTOS_SOCKET_ERROR
243     #define SOCKET_EPIPE        FREERTOS_SOCKET_ERROR
244     #define SOCKET_ECONNREFUSED FREERTOS_SOCKET_ERROR
245     #define SOCKET_ECONNABORTED FREERTOS_SOCKET_ERROR
246 #elif defined(WOLFSSL_NUCLEUS_1_2)
247     #define SOCKET_EWOULDBLOCK  NU_WOULD_BLOCK
248     #define SOCKET_EAGAIN       NU_WOULD_BLOCK
249     #define SOCKET_ECONNRESET   NU_NOT_CONNECTED
250     #define SOCKET_EINTR        NU_NOT_CONNECTED
251     #define SOCKET_EPIPE        NU_NOT_CONNECTED
252     #define SOCKET_ECONNREFUSED NU_CONNECTION_REFUSED
253     #define SOCKET_ECONNABORTED NU_NOT_CONNECTED
254 #elif defined(WOLFSSL_DEOS)
255     /* `sockaddr_storage` is not defined in DEOS. This workaround will
256      * work for IPV4, but not IPV6
257      */
258     #define sockaddr_storage   sockaddr_in
259     #define SOCKET_EWOULDBLOCK EAGAIN
260     #define SOCKET_EAGAIN      EAGAIN
261     #define SOCKET_ECONNRESET  EINTR
262     #define SOCKET_EINTR       EINTR
263     #define SOCKET_EPIPE       EPIPE
264     #define SOCKET_ECONNREFUSED SOCKET_ERROR
265     #define SOCKET_ECONNABORTED SOCKET_ERROR
266 #elif defined(HAVE_NETX)
267     #define SOCKET_EWOULDBLOCK NX_NOT_CONNECTED
268     #define SOCKET_EAGAIN      NX_NOT_CONNECTED
269     #define SOCKET_ECONNRESET  NX_NOT_CONNECTED
270     #define SOCKET_EINTR       NX_NOT_CONNECTED
271     #define SOCKET_EPIPE       NX_NOT_CONNECTED
272     #define SOCKET_ECONNREFUSED NX_NOT_CONNECTED
273     #define SOCKET_ECONNABORTED NX_NOT_CONNECTED
274 #elif defined(FUSION_RTOS)
275     #define SOCKET_EWOULDBLOCK FCL_EWOULDBLOCK
276     #define SOCKET_EAGAIN      FCL_EAGAIN
277     #define SOCKET_ECONNRESET  FNS_ECONNRESET
278     #define SOCKET_EINTR       FCL_EINTR
279     #define SOCKET_EPIPE       FCL_EPIPE
280     #define SOCKET_ECONNREFUSED FCL_ECONNREFUSED
281     #define SOCKET_ECONNABORTED FNS_ECONNABORTED
282 #elif defined(WOLFSSL_LWIP_NATIVE)
283     #define SOCKET_EWOULDBLOCK ERR_WOULDBLOCK
284     #define SOCKET_EAGAIN      ERR_WOULDBLOCK
285     #define SOCKET_ECONNRESET  ERR_RST
286     #define SOCKET_EINTR       ERR_CLSD
287     #define SOCKET_EPIPE       ERR_CLSD
288     #define SOCKET_ECONNREFUSED ERR_CONN
289     #define SOCKET_ECONNABORTED ERR_ABRT
290 #else
291     #define SOCKET_EWOULDBLOCK EWOULDBLOCK
292     #define SOCKET_EAGAIN      EAGAIN
293     #define SOCKET_ECONNRESET  ECONNRESET
294     #define SOCKET_EINTR       EINTR
295     #define SOCKET_EPIPE       EPIPE
296     #define SOCKET_ECONNREFUSED ECONNREFUSED
297     #define SOCKET_ECONNABORTED ECONNABORTED
298 #endif /* USE_WINDOWS_API */
299 
300 #ifdef DEVKITPRO
301     /* from network.h */
302     #include <network.h>
303     #define SEND_FUNCTION net_send
304     #define RECV_FUNCTION net_recv
305 #elif defined(WOLFSSL_LWIP) && !defined(WOLFSSL_APACHE_MYNEWT)
306     #define SEND_FUNCTION lwip_send
307     #define RECV_FUNCTION lwip_recv
308 #elif defined(WOLFSSL_PICOTCP)
309     #define SEND_FUNCTION pico_send
310     #define RECV_FUNCTION pico_recv
311 #elif defined(FREERTOS_TCP)
312     #define RECV_FUNCTION(a,b,c,d)  FreeRTOS_recv((Socket_t)(a),(void*)(b), (size_t)(c), (BaseType_t)(d))
313     #define SEND_FUNCTION(a,b,c,d)  FreeRTOS_send((Socket_t)(a),(void*)(b), (size_t)(c), (BaseType_t)(d))
314 #elif defined(WOLFSSL_VXWORKS)
315     #define SEND_FUNCTION send
316     #define RECV_FUNCTION recv
317 #elif defined(WOLFSSL_NUCLEUS_1_2)
318     #define SEND_FUNCTION NU_Send
319     #define RECV_FUNCTION NU_Recv
320 #elif defined(FUSION_RTOS)
321     #define SEND_FUNCTION FNS_SEND
322     #define RECV_FUNCTION FNS_RECV
323 #elif defined(WOLFSSL_ZEPHYR)
324     #ifndef WOLFSSL_MAX_SEND_SZ
325         #define WOLFSSL_MAX_SEND_SZ       256
326     #endif
327 
328     #define SEND_FUNCTION send
329     #define RECV_FUNCTION recv
330 #elif defined(WOLFSSL_LINUXKM)
331     #define SEND_FUNCTION linuxkm_send
332     #define RECV_FUNCTION linuxkm_recv
333 #elif defined(WOLFSSL_SGX)
334     #define SEND_FUNCTION send
335     #define RECV_FUNCTION recv
336 #else
337     #define SEND_FUNCTION send
338     #define RECV_FUNCTION recv
339     #if !defined(HAVE_SOCKADDR) && !defined(WOLFSSL_NO_SOCK)
340         #define HAVE_SOCKADDR
341     #endif
342 #endif
343 
344 #ifdef USE_WINDOWS_API
345     typedef unsigned int SOCKET_T;
346     #ifndef SOCKET_INVALID
347         #define SOCKET_INVALID INVALID_SOCKET
348     #endif
349 #else
350     typedef int SOCKET_T;
351     #ifndef SOCKET_INVALID
352         #define SOCKET_INVALID -1
353     #endif
354 #endif
355 
356 #ifndef WOLFSSL_NO_SOCK
357     #ifndef XSOCKLENT
358         #ifdef USE_WINDOWS_API
359             #define XSOCKLENT int
360         #else
361             #define XSOCKLENT socklen_t
362         #endif
363     #endif
364 
365     /* Socket Addr Support */
366     #ifdef HAVE_SOCKADDR
367         typedef struct sockaddr         SOCKADDR;
368         typedef struct sockaddr_storage SOCKADDR_S;
369         typedef struct sockaddr_in      SOCKADDR_IN;
370         #ifdef WOLFSSL_IPV6
371             typedef struct sockaddr_in6 SOCKADDR_IN6;
372         #endif
373         typedef struct hostent          HOSTENT;
374     #endif /* HAVE_SOCKADDR */
375 
376     /* use gethostbyname for c99 */
377     #if defined(HAVE_GETADDRINFO) && !defined(WOLF_C99)
378         typedef struct addrinfo         ADDRINFO;
379     #endif
380 #endif /* WOLFSSL_NO_SOCK */
381 
382 
383 /* IO API's */
384 #ifdef HAVE_IO_TIMEOUT
385     WOLFSSL_API  int wolfIO_SetBlockingMode(SOCKET_T sockfd, int non_blocking);
386     WOLFSSL_API void wolfIO_SetTimeout(int to_sec);
387     WOLFSSL_API  int wolfIO_Select(SOCKET_T sockfd, int to_sec);
388 #endif
389 WOLFSSL_API  int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip,
390     unsigned short port, int to_sec);
391 #ifdef HAVE_SOCKADDR
392 WOLFSSL_API int wolfIO_TcpAccept(SOCKET_T sockfd, SOCKADDR* peer_addr, XSOCKLENT* peer_len);
393 #endif
394 WOLFSSL_API int wolfIO_TcpBind(SOCKET_T* sockfd, word16 port);
395 WOLFSSL_API  int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags);
396 WOLFSSL_API  int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags);
397 
398 #endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */
399 
400 #ifndef WOLFSSL_NO_SOCK
401 #ifdef USE_WINDOWS_API
402     #ifndef CloseSocket
403         #define CloseSocket(s) closesocket(s)
404     #endif
405     #define StartTCP() { WSADATA wsd; WSAStartup(0x0002, &wsd); }
406 #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
407     #ifndef CloseSocket
408         extern int closesocket(int);
409         #define CloseSocket(s) closesocket(s)
410     #endif
411     #define StartTCP()
412 #elif defined(FUSION_RTOS)
413     #ifndef CloseSocket
414         #define CloseSocket(s) do {                     \
415                                     int err;            \
416                                     FNS_CLOSE(s, &err); \
417                                 } while(0)
418     #endif
419 #else
420     #ifndef CloseSocket
421         #define CloseSocket(s) close(s)
422     #endif
423     #define StartTCP()
424     #ifdef FREERTOS_TCP_WINSIM
425         extern int close(int);
426     #endif
427 #endif
428 #endif /* WOLFSSL_NO_SOCK */
429 
430 
431 WOLFSSL_API int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx);
432 WOLFSSL_API int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx);
433 #if defined(USE_WOLFSSL_IO)
434     /* default IO callbacks */
435     WOLFSSL_API int EmbedReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx);
436     WOLFSSL_API int EmbedSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
437 
438     #ifdef WOLFSSL_DTLS
439         WOLFSSL_API int EmbedReceiveFrom(WOLFSSL* ssl, char* buf, int sz, void*);
440         WOLFSSL_API int EmbedSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
441         WOLFSSL_API int EmbedGenerateCookie(WOLFSSL* ssl, unsigned char* buf,
442                                            int sz, void*);
443         #ifdef WOLFSSL_MULTICAST
444             WOLFSSL_API int EmbedReceiveFromMcast(WOLFSSL* ssl,
445                                                   char* buf, int sz, void*);
446         #endif /* WOLFSSL_MULTICAST */
447     #endif /* WOLFSSL_DTLS */
448 #endif /* USE_WOLFSSL_IO */
449 
450 #ifdef HAVE_OCSP
451     WOLFSSL_API int wolfIO_HttpBuildRequestOcsp(const char* domainName,
452         const char* path, int ocspReqSz, unsigned char* buf, int bufSize);
453     WOLFSSL_API int wolfIO_HttpProcessResponseOcsp(int sfd,
454         unsigned char** respBuf, unsigned char* httpBuf, int httpBufSz,
455         void* heap);
456 
457     WOLFSSL_API int EmbedOcspLookup(void*, const char*, int, unsigned char*,
458                                    int, unsigned char**);
459     WOLFSSL_API void EmbedOcspRespFree(void*, unsigned char*);
460 #endif
461 
462 #ifdef HAVE_CRL_IO
463     WOLFSSL_API int wolfIO_HttpBuildRequestCrl(const char* url, int urlSz,
464         const char* domainName, unsigned char* buf, int bufSize);
465     WOLFSSL_API int wolfIO_HttpProcessResponseCrl(WOLFSSL_CRL* crl, int sfd,
466         unsigned char* httpBuf, int httpBufSz);
467 
468     WOLFSSL_API int EmbedCrlLookup(WOLFSSL_CRL* crl, const char* url,
469         int urlSz);
470 #endif
471 
472 
473 #if defined(HAVE_HTTP_CLIENT)
474     WOLFSSL_API  int wolfIO_DecodeUrl(const char* url, int urlSz, char* outName,
475         char* outPath, unsigned short* outPort);
476 
477     WOLFSSL_API  int wolfIO_HttpBuildRequest(const char* reqType,
478         const char* domainName, const char* path, int pathLen, int reqSz,
479         const char* contentType, unsigned char* buf, int bufSize);
480     WOLFSSL_LOCAL int wolfIO_HttpBuildRequest_ex(const char* reqType,
481         const char* domainName, const char* path, int pathLen, int reqSz,
482         const char* contentType, const char *exHdrs, unsigned char* buf, int bufSize);
483     WOLFSSL_API  int wolfIO_HttpProcessResponse(int sfd, const char** appStrList,
484         unsigned char** respBuf, unsigned char* httpBuf, int httpBufSz,
485         int dynType, void* heap);
486 #endif /* HAVE_HTTP_CLIENT */
487 
488 
489 /* I/O callbacks */
490 typedef int (*CallbackIORecv)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
491 typedef int (*CallbackIOSend)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
492 WOLFSSL_API void wolfSSL_CTX_SetIORecv(WOLFSSL_CTX*, CallbackIORecv);
493 WOLFSSL_API void wolfSSL_CTX_SetIOSend(WOLFSSL_CTX*, CallbackIOSend);
494 WOLFSSL_API void wolfSSL_SSLSetIORecv(WOLFSSL*, CallbackIORecv);
495 WOLFSSL_API void wolfSSL_SSLSetIOSend(WOLFSSL*, CallbackIOSend);
496 /* deprecated old name */
497 #define wolfSSL_SetIORecv wolfSSL_CTX_SetIORecv
498 #define wolfSSL_SetIOSend wolfSSL_CTX_SetIOSend
499 
500 WOLFSSL_API void wolfSSL_SetIOReadCtx( WOLFSSL* ssl, void *ctx);
501 WOLFSSL_API void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *ctx);
502 
503 WOLFSSL_API void* wolfSSL_GetIOReadCtx( WOLFSSL* ssl);
504 WOLFSSL_API void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl);
505 
506 WOLFSSL_API void wolfSSL_SetIOReadFlags( WOLFSSL* ssl, int flags);
507 WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
508 
509 
510 #ifdef HAVE_NETX
511     WOLFSSL_LOCAL int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx);
512     WOLFSSL_LOCAL int NetX_Send(WOLFSSL *ssl, char *buf, int sz, void *ctx);
513 
514     WOLFSSL_API void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket,
515                                       ULONG waitoption);
516 #endif /* HAVE_NETX */
517 
518 #ifdef MICRIUM
519     WOLFSSL_LOCAL int MicriumSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
520     WOLFSSL_LOCAL int MicriumReceive(WOLFSSL* ssl, char* buf, int sz,
521                                      void* ctx);
522     WOLFSSL_LOCAL int MicriumReceiveFrom(WOLFSSL* ssl, char* buf, int sz,
523                                          void* ctx);
524     WOLFSSL_LOCAL int MicriumSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
525 #endif /* MICRIUM */
526 
527 #if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP)
528     WOLFSSL_LOCAL int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx);
529     WOLFSSL_LOCAL int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx);
530     WOLFSSL_API void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket,
531                                           struct mn_sockaddr_in* mnSockAddrIn);
532 #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */
533 
534 #ifdef WOLFSSL_UIP
535 
536     struct uip_wolfssl_ctx {
537         union socket_connector {
538             struct tcp_socket tcp;
539             struct udp_socket udp;
540         } conn;
541         WOLFSSL_CTX *ctx;
542         WOLFSSL *ssl;
543         byte *input_databuf;
544         byte *output_databuf;
545         byte *ssl_rx_databuf;
546         int ssl_rb_len;
547         int ssl_rb_off;
548         struct process *process;
549         tcp_socket_data_callback_t input_callback;
550         tcp_socket_event_callback_t event_callback;
551         int closing;
552         uip_ipaddr_t peer_addr;
553         word16 peer_port;
554     };
555 
556     typedef struct uip_wolfssl_ctx uip_wolfssl_ctx;
557 
558     WOLFSSL_LOCAL int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
559     WOLFSSL_LOCAL int uIPReceive(WOLFSSL* ssl, char* buf, int sz,
560                                      void* ctx);
561     WOLFSSL_LOCAL int uIPReceiveFrom(WOLFSSL* ssl, char* buf, int sz,
562                                          void* ctx);
563     WOLFSSL_LOCAL int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
564 
565 #endif
566 
567 #ifdef WOLFSSL_GNRC
568     #include <sock_types.h>
569     #include <net/gnrc.h>
570     #include <net/af.h>
571     #include <net/sock.h>
572     #include <net/gnrc/tcp.h>
573     #include <net/gnrc/udp.h>
574 
575     struct gnrc_wolfssl_ctx {
576         union socket_connector {
577         #ifdef MODULE_SOCK_TCP
578             sock_tcp_t tcp;
579         #endif
580             sock_udp_t udp;
581         } conn;
582         WOLFSSL_CTX *ctx;
583         WOLFSSL *ssl;
584 
585         int closing;
586         struct _sock_tl_ep peer_addr;
587     };
588 
589     typedef struct gnrc_wolfssl_ctx sock_tls_t;
590 
591     WOLFSSL_LOCAL int GNRC_ReceiveFrom(WOLFSSL* ssl, char* buf, int sz,
592                                      void* ctx);
593     WOLFSSL_LOCAL int GNRC_SendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
594 
595 #endif
596 
597 #ifdef WOLFSSL_LWIP_NATIVE
598     #include "lwip/tcp.h"
599     #include "lwip/sockets.h"
600 
601     typedef struct WOLFSSL_LWIP_NATIVE_STATE {
602         struct tcp_pcb * pcb;
603         tcp_recv_fn recv_fn;
604         tcp_sent_fn sent_fn;
605         int    pulled;
606         struct pbuf *pbuf;
607         int    wait;
608         void * arg;   /* arg for application */
609         int    idle_count;
610     } WOLFSSL_LWIP_NATIVE_STATE;
611 
612     WOLFSSL_LOCAL int LwIPNativeSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
613     WOLFSSL_LOCAL int LwIPNativeReceive(WOLFSSL* ssl, char* buf, int sz,
614                                      void* ctx);
615     WOLFSSL_API   int wolfSSL_SetIO_LwIP(WOLFSSL* ssl, void *pcb,
616                                 tcp_recv_fn recv, tcp_sent_fn sent, void *arg);
617 #endif
618 
619 #ifdef WOLFSSL_DTLS
620     typedef int (*CallbackGenCookie)(WOLFSSL* ssl, unsigned char* buf, int sz,
621                                      void* ctx);
622     WOLFSSL_API void  wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX*, CallbackGenCookie);
623     WOLFSSL_API void  wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx);
624     WOLFSSL_API void* wolfSSL_GetCookieCtx(WOLFSSL* ssl);
625 
626 #endif
627 #ifdef WOLFSSL_SESSION_EXPORT
628     typedef int (*CallbackGetPeer)(WOLFSSL* ssl, char* ip, int* ipSz,
629                                         unsigned short* port, int* fam);
630     typedef int (*CallbackSetPeer)(WOLFSSL* ssl, char* ip, int ipSz,
631                                           unsigned short port, int fam);
632 
633     WOLFSSL_API void wolfSSL_CTX_SetIOGetPeer(WOLFSSL_CTX*, CallbackGetPeer);
634     WOLFSSL_API void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX*, CallbackSetPeer);
635     WOLFSSL_API int EmbedGetPeer(WOLFSSL* ssl, char* ip, int* ipSz,
636                                                 unsigned short* port, int* fam);
637     WOLFSSL_API int EmbedSetPeer(WOLFSSL* ssl, char* ip, int ipSz,
638                                                   unsigned short port, int fam);
639 #endif /* WOLFSSL_SESSION_EXPORT */
640 
641 
642 
643 #ifndef XINET_NTOP
644     #define XINET_NTOP(a,b,c,d) inet_ntop((a),(b),(c),(d))
645     #ifdef USE_WINDOWS_API /* Windows-friendly definition */
646         #undef  XINET_NTOP
647         #define XINET_NTOP(a,b,c,d) InetNtop((a),(b),(c),(d))
648     #endif
649 #endif
650 #ifndef XINET_PTON
651     #define XINET_PTON(a,b,c)   inet_pton((a),(b),(c))
652     #ifdef USE_WINDOWS_API /* Windows-friendly definition */
653         #undef  XINET_PTON
654         #define XINET_PTON(a,b,c)   InetPton((a),(b),(c))
655     #endif
656 #endif
657 
658 #ifndef XHTONS
659     #if !defined(WOLFSSL_NO_SOCK) && (defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT))
660         #define XHTONS(a) htons((a))
661     #else
662         /* we don't have sockets, so define our own htons and ntohs */
663         #ifdef BIG_ENDIAN_ORDER
664             #define XHTONS(a) (a)
665         #else
666             #define XHTONS(a) ((((a) >> 8) & 0xff) | (((a) & 0xff) << 8))
667         #endif
668     #endif
669 #endif
670 #ifndef XNTOHS
671     #if !defined(WOLFSSL_NO_SOCK) && (defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT))
672         #define XNTOHS(a) ntohs((a))
673     #else
674         /* we don't have sockets, so define our own htons and ntohs */
675         #ifdef BIG_ENDIAN_ORDER
676             #define XNTOHS(a) (a)
677         #else
678             #define XNTOHS(a) ((((a) >> 8) & 0xff) | (((a) & 0xff) << 8))
679         #endif
680     #endif
681 #endif
682 
683 #ifndef WOLFSSL_IP4
684     #define WOLFSSL_IP4 AF_INET
685 #endif
686 #ifndef WOLFSSL_IP6
687     #define WOLFSSL_IP6 AF_INET6
688 #endif
689 
690 
691 #ifdef __cplusplus
692     }  /* extern "C" */
693 #endif
694 
695 #endif /* WOLFSSL_IO_H */
696