wolf SSL / wolfSSL-TLS13-Beta

Fork of wolfSSL by wolf SSL

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers io.h Source File

io.h

00001 /* io.h
00002  *
00003  * Copyright (C) 2006-2016 wolfSSL Inc.
00004  *
00005  * This file is part of wolfSSL.
00006  *
00007  * wolfSSL is free software; you can redistribute it and/or modify
00008  * it under the terms of the GNU General Public License as published by
00009  * the Free Software Foundation; either version 2 of the License, or
00010  * (at your option) any later version.
00011  *
00012  * wolfSSL is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
00020  */
00021 
00022 
00023 #ifndef WOLFSSL_IO_H
00024 #define WOLFSSL_IO_H
00025 
00026 #ifdef __cplusplus
00027     extern "C" {
00028 #endif
00029 
00030 /* OCSP and CRL_IO require HTTP client */
00031 #if defined(HAVE_OCSP) || defined(HAVE_CRL_IO)
00032     #ifndef HAVE_HTTP_CLIENT
00033         #define HAVE_HTTP_CLIENT
00034     #endif
00035 #endif
00036 
00037 #if !defined(WOLFSSL_USER_IO)
00038     #ifndef USE_WOLFSSL_IO
00039         #define USE_WOLFSSL_IO
00040     #endif
00041 #endif
00042 
00043 
00044 #if defined(USE_WOLFSSL_IO) || defined(HAVE_HTTP_CLIENT)
00045 
00046 #ifdef HAVE_LIBZ
00047     #include "zlib.h"
00048 #endif
00049 
00050 #ifndef USE_WINDOWS_API
00051     #ifdef WOLFSSL_LWIP
00052         /* lwIP needs to be configured to use sockets API in this mode */
00053         /* LWIP_SOCKET 1 in lwip/opt.h or in build */
00054         #include "lwip/sockets.h"
00055         #include <errno.h>
00056         #ifndef LWIP_PROVIDE_ERRNO
00057             #define LWIP_PROVIDE_ERRNO 1
00058         #endif
00059     #elif defined(FREESCALE_MQX)
00060         #include <posix.h>
00061         #include <rtcs.h>
00062     #elif defined(FREESCALE_KSDK_MQX)
00063         #include <rtcs.h>
00064     #elif defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
00065         #if !defined(WOLFSSL_MDK_ARM)
00066             #include "cmsis_os.h"
00067             #include "rl_net.h"
00068         #else
00069             #include <rtl.h>
00070         #endif
00071         #include "errno.h"
00072         #define SOCKET_T int
00073     #elif defined(WOLFSSL_TIRTOS)
00074         #include <sys/socket.h>
00075     #elif defined(FREERTOS_TCP)
00076         #include "FreeRTOS_Sockets.h"
00077     #elif defined(WOLFSSL_IAR_ARM)
00078         /* nothing */
00079     #elif defined(WOLFSSL_VXWORKS)
00080         #include <sockLib.h>
00081         #include <errno.h>
00082     #elif defined(WOLFSSL_ATMEL)
00083         #include "socket/include/socket.h"
00084     #elif defined(INTIME_RTOS)
00085         #undef MIN
00086         #undef MAX
00087         #include <rt.h>
00088         #include <sys/types.h>
00089         #include <sys/socket.h>
00090         #include <netdb.h>
00091         #include <netinet/in.h>
00092         #include <io.h>
00093         /* <sys/socket.h> defines these, to avoid conflict, do undef */
00094         #undef SOCKADDR
00095         #undef SOCKADDR_IN
00096     #elif defined(WOLFSSL_PRCONNECT_PRO)
00097         #include <prconnect_pro/prconnect_pro.h>
00098         #include <sys/types.h>
00099         #include <errno.h>
00100         #include <unistd.h>
00101         #include <fcntl.h>
00102         #include <netdb.h>
00103         #include <sys/ioctl.h>
00104     #elif !defined(WOLFSSL_NO_SOCK)
00105         #include <sys/types.h>
00106         #include <errno.h>
00107         #ifndef EBSNET
00108             #include <unistd.h>
00109         #endif
00110         #include <fcntl.h>
00111 
00112         #if defined(HAVE_RTP_SYS)
00113             #include <socket.h>
00114         #elif defined(EBSNET)
00115             #include "rtipapi.h"  /* errno */
00116             #include "socket.h"
00117         #elif !defined(DEVKITPRO) && !defined(WOLFSSL_PICOTCP)
00118             #include <sys/socket.h>
00119             #include <arpa/inet.h>
00120             #include <netinet/in.h>
00121             #include <netdb.h>
00122             #ifdef __PPU
00123                 #include <netex/errno.h>
00124             #else
00125                 #include <sys/ioctl.h>
00126             #endif
00127         #endif
00128     #endif
00129 #endif /* USE_WINDOWS_API */
00130 
00131 #ifdef __sun
00132     #include <sys/filio.h>
00133 #endif
00134 
00135 #ifdef USE_WINDOWS_API
00136     /* no epipe yet */
00137     #ifndef WSAEPIPE
00138         #define WSAEPIPE       -12345
00139     #endif
00140     #define SOCKET_EWOULDBLOCK WSAEWOULDBLOCK
00141     #define SOCKET_EAGAIN      WSAETIMEDOUT
00142     #define SOCKET_ECONNRESET  WSAECONNRESET
00143     #define SOCKET_EINTR       WSAEINTR
00144     #define SOCKET_EPIPE       WSAEPIPE
00145     #define SOCKET_ECONNREFUSED WSAENOTCONN
00146     #define SOCKET_ECONNABORTED WSAECONNABORTED
00147     #define close(s) closesocket(s)
00148 #elif defined(__PPU)
00149     #define SOCKET_EWOULDBLOCK SYS_NET_EWOULDBLOCK
00150     #define SOCKET_EAGAIN      SYS_NET_EAGAIN
00151     #define SOCKET_ECONNRESET  SYS_NET_ECONNRESET
00152     #define SOCKET_EINTR       SYS_NET_EINTR
00153     #define SOCKET_EPIPE       SYS_NET_EPIPE
00154     #define SOCKET_ECONNREFUSED SYS_NET_ECONNREFUSED
00155     #define SOCKET_ECONNABORTED SYS_NET_ECONNABORTED
00156 #elif defined(FREESCALE_MQX) || defined(FREESCALE_KSDK_MQX)
00157     #if MQX_USE_IO_OLD
00158         /* RTCS old I/O doesn't have an EWOULDBLOCK */
00159         #define SOCKET_EWOULDBLOCK  EAGAIN
00160         #define SOCKET_EAGAIN       EAGAIN
00161         #define SOCKET_ECONNRESET   RTCSERR_TCP_CONN_RESET
00162         #define SOCKET_EINTR        EINTR
00163         #define SOCKET_EPIPE        EPIPE
00164         #define SOCKET_ECONNREFUSED RTCSERR_TCP_CONN_REFUSED
00165         #define SOCKET_ECONNABORTED RTCSERR_TCP_CONN_ABORTED
00166     #else
00167         #define SOCKET_EWOULDBLOCK  NIO_EWOULDBLOCK
00168         #define SOCKET_EAGAIN       NIO_EAGAIN
00169         #define SOCKET_ECONNRESET   NIO_ECONNRESET
00170         #define SOCKET_EINTR        NIO_EINTR
00171         #define SOCKET_EPIPE        NIO_EPIPE
00172         #define SOCKET_ECONNREFUSED NIO_ECONNREFUSED
00173         #define SOCKET_ECONNABORTED NIO_ECONNABORTED
00174     #endif
00175 #elif defined(WOLFSSL_MDK_ARM)|| defined(WOLFSSL_KEIL_TCP_NET)
00176     #if !defined(WOLFSSL_MDK_ARM)
00177         #define SOCKET_EWOULDBLOCK BSD_ERROR_WOULDBLOCK
00178         #define SOCKET_EAGAIN      BSD_ERROR_LOCKED
00179         #define SOCKET_ECONNRESET  BSD_ERROR_CLOSED
00180         #define SOCKET_EINTR       BSD_ERROR
00181         #define SOCKET_EPIPE       BSD_ERROR
00182         #define SOCKET_ECONNREFUSED BSD_ERROR
00183         #define SOCKET_ECONNABORTED BSD_ERROR
00184     #else
00185         #define SOCKET_EWOULDBLOCK SCK_EWOULDBLOCK
00186         #define SOCKET_EAGAIN      SCK_ELOCKED
00187         #define SOCKET_ECONNRESET  SCK_ECLOSED
00188         #define SOCKET_EINTR       SCK_ERROR
00189         #define SOCKET_EPIPE       SCK_ERROR
00190         #define SOCKET_ECONNREFUSED SCK_ERROR
00191         #define SOCKET_ECONNABORTED SCK_ERROR
00192     #endif
00193 #elif defined(WOLFSSL_PICOTCP)
00194     #define SOCKET_EWOULDBLOCK  PICO_ERR_EAGAIN
00195     #define SOCKET_EAGAIN       PICO_ERR_EAGAIN
00196     #define SOCKET_ECONNRESET   PICO_ERR_ECONNRESET
00197     #define SOCKET_EINTR        PICO_ERR_EINTR
00198     #define SOCKET_EPIPE        PICO_ERR_EIO
00199     #define SOCKET_ECONNREFUSED PICO_ERR_ECONNREFUSED
00200     #define SOCKET_ECONNABORTED PICO_ERR_ESHUTDOWN
00201 #elif defined(FREERTOS_TCP)
00202     #define SOCKET_EWOULDBLOCK FREERTOS_EWOULDBLOCK
00203     #define SOCKET_EAGAIN       FREERTOS_EWOULDBLOCK
00204     #define SOCKET_ECONNRESET   FREERTOS_SOCKET_ERROR
00205     #define SOCKET_EINTR        FREERTOS_SOCKET_ERROR
00206     #define SOCKET_EPIPE        FREERTOS_SOCKET_ERROR
00207     #define SOCKET_ECONNREFUSED FREERTOS_SOCKET_ERROR
00208     #define SOCKET_ECONNABORTED FREERTOS_SOCKET_ERROR
00209 #else
00210     #define SOCKET_EWOULDBLOCK EWOULDBLOCK
00211     #define SOCKET_EAGAIN      EAGAIN
00212     #define SOCKET_ECONNRESET  ECONNRESET
00213     #define SOCKET_EINTR       EINTR
00214     #define SOCKET_EPIPE       EPIPE
00215     #define SOCKET_ECONNREFUSED ECONNREFUSED
00216     #define SOCKET_ECONNABORTED ECONNABORTED
00217 #endif /* USE_WINDOWS_API */
00218 
00219 
00220 #ifdef DEVKITPRO
00221     /* from network.h */
00222     int net_send(int, const void*, int, unsigned int);
00223     int net_recv(int, void*, int, unsigned int);
00224     #define SEND_FUNCTION net_send
00225     #define RECV_FUNCTION net_recv
00226 #elif defined(WOLFSSL_LWIP)
00227     #define SEND_FUNCTION lwip_send
00228     #define RECV_FUNCTION lwip_recv
00229 #elif defined(WOLFSSL_PICOTCP)
00230     #define SEND_FUNCTION pico_send
00231     #define RECV_FUNCTION pico_recv
00232 #elif defined(FREERTOS_TCP)
00233     #define RECV_FUNCTION(a,b,c,d)  FreeRTOS_recv((Socket_t)(a),(void*)(b), (size_t)(c), (BaseType_t)(d))
00234     #define SEND_FUNCTION(a,b,c,d)  FreeRTOS_send((Socket_t)(a),(void*)(b), (size_t)(c), (BaseType_t)(d))
00235 #else
00236     #define SEND_FUNCTION send
00237     #define RECV_FUNCTION recv
00238     #if !defined(HAVE_SOCKADDR) && !defined(WOLFSSL_NO_SOCK)
00239         #define HAVE_SOCKADDR
00240     #endif
00241 #endif
00242 
00243 #ifdef USE_WINDOWS_API
00244     typedef unsigned int SOCKET_T;
00245 #else
00246     typedef int SOCKET_T;
00247 #endif
00248 
00249 #ifndef WOLFSSL_NO_SOCK
00250     #ifndef XSOCKLENT
00251         #ifdef USE_WINDOWS_API
00252             #define XSOCKLENT int
00253         #else
00254             #define XSOCKLENT socklen_t
00255         #endif
00256     #endif
00257 
00258     /* Socket Addr Support */
00259     #ifdef HAVE_SOCKADDR
00260         typedef struct sockaddr         SOCKADDR;
00261         typedef struct sockaddr_storage SOCKADDR_S;
00262         typedef struct sockaddr_in      SOCKADDR_IN;
00263         #ifdef WOLFSSL_IPV6
00264             typedef struct sockaddr_in6 SOCKADDR_IN6;
00265         #endif
00266         typedef struct hostent          HOSTENT;
00267     #endif /* HAVE_SOCKADDR */
00268 
00269     #ifdef HAVE_GETADDRINFO
00270         typedef struct addrinfo         ADDRINFO;
00271     #endif
00272 #endif /* WOLFSSL_NO_SOCK */
00273 
00274 
00275 /* IO API's */
00276 #ifdef HAVE_IO_TIMEOUT
00277     WOLFSSL_API  int wolfIO_SetBlockingMode(SOCKET_T sockfd, int non_blocking);
00278     WOLFSSL_API void wolfIO_SetTimeout(int to_sec);;
00279     WOLFSSL_API  int wolfIO_Select(SOCKET_T sockfd, int to_sec);
00280 #endif
00281 WOLFSSL_API  int wolfIO_TcpConnect(SOCKET_T* sockfd, const char* ip,
00282     unsigned short port, int to_sec);
00283 WOLFSSL_API  int wolfIO_Send(SOCKET_T sd, char *buf, int sz, int wrFlags);
00284 WOLFSSL_API  int wolfIO_Recv(SOCKET_T sd, char *buf, int sz, int rdFlags);
00285 
00286 #endif /* USE_WOLFSSL_IO || HAVE_HTTP_CLIENT */
00287 
00288 
00289 #if defined(USE_WOLFSSL_IO)
00290     /* default IO callbacks */
00291     WOLFSSL_API int EmbedReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx);
00292     WOLFSSL_API int EmbedSend(WOLFSSL* ssl, char* buf, int sz, void* ctx);
00293 
00294     #ifdef WOLFSSL_DTLS
00295         WOLFSSL_API int EmbedReceiveFrom(WOLFSSL* ssl, char* buf, int sz, void*);
00296         WOLFSSL_API int EmbedSendTo(WOLFSSL* ssl, char* buf, int sz, void* ctx);
00297         WOLFSSL_API int EmbedGenerateCookie(WOLFSSL* ssl, unsigned char* buf,
00298                                            int sz, void*);
00299         #ifdef WOLFSSL_SESSION_EXPORT
00300             WOLFSSL_API int EmbedGetPeer(WOLFSSL* ssl, char* ip, int* ipSz,
00301                                                 unsigned short* port, int* fam);
00302             WOLFSSL_API int EmbedSetPeer(WOLFSSL* ssl, char* ip, int ipSz,
00303                                                   unsigned short port, int fam);
00304         #endif /* WOLFSSL_SESSION_EXPORT */
00305     #endif /* WOLFSSL_DTLS */
00306 #endif /* USE_WOLFSSL_IO */
00307 
00308 #ifdef HAVE_OCSP
00309     WOLFSSL_API int wolfIO_HttpBuildRequestOcsp(const char* domainName,
00310         const char* path, int ocspReqSz, unsigned char* buf, int bufSize);
00311     WOLFSSL_API int wolfIO_HttpProcessResponseOcsp(int sfd,
00312         unsigned char** respBuf, unsigned char* httpBuf, int httpBufSz,
00313         void* heap);
00314 
00315     WOLFSSL_API int EmbedOcspLookup(void*, const char*, int, unsigned char*,
00316                                    int, unsigned char**);
00317     WOLFSSL_API void EmbedOcspRespFree(void*, unsigned char*);
00318 #endif
00319 
00320 #ifdef HAVE_CRL_IO
00321     WOLFSSL_API int wolfIO_HttpBuildRequestCrl(const char* url, int urlSz,
00322         const char* domainName, unsigned char* buf, int bufSize);
00323     WOLFSSL_API int wolfIO_HttpProcessResponseCrl(WOLFSSL_CRL* crl, int sfd,
00324         unsigned char* httpBuf, int httpBufSz);
00325 
00326     WOLFSSL_API int EmbedCrlLookup(WOLFSSL_CRL* crl, const char* url,
00327         int urlSz);
00328 #endif
00329 
00330 
00331 #if defined(HAVE_HTTP_CLIENT)
00332     WOLFSSL_API  int wolfIO_DecodeUrl(const char* url, int urlSz, char* outName,
00333         char* outPath, unsigned short* outPort);
00334 
00335     WOLFSSL_API  int wolfIO_HttpBuildRequest(const char* reqType,
00336         const char* domainName, const char* path, int pathLen, int reqSz,
00337         const char* contentType, unsigned char* buf, int bufSize);
00338     WOLFSSL_API  int wolfIO_HttpProcessResponse(int sfd, const char* appStr,
00339         unsigned char** respBuf, unsigned char* httpBuf, int httpBufSz,
00340         int dynType, void* heap);
00341 #endif /* HAVE_HTTP_CLIENT */
00342 
00343 
00344 /* I/O callbacks */
00345 typedef int (*CallbackIORecv)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
00346 typedef int (*CallbackIOSend)(WOLFSSL *ssl, char *buf, int sz, void *ctx);
00347 WOLFSSL_API void wolfSSL_SetIORecv(WOLFSSL_CTX*, CallbackIORecv);
00348 WOLFSSL_API void wolfSSL_SetIOSend(WOLFSSL_CTX*, CallbackIOSend);
00349 
00350 WOLFSSL_API void wolfSSL_SetIOReadCtx( WOLFSSL* ssl, void *ctx);
00351 WOLFSSL_API void wolfSSL_SetIOWriteCtx(WOLFSSL* ssl, void *ctx);
00352 
00353 WOLFSSL_API void* wolfSSL_GetIOReadCtx( WOLFSSL* ssl);
00354 WOLFSSL_API void* wolfSSL_GetIOWriteCtx(WOLFSSL* ssl);
00355 
00356 WOLFSSL_API void wolfSSL_SetIOReadFlags( WOLFSSL* ssl, int flags);
00357 WOLFSSL_API void wolfSSL_SetIOWriteFlags(WOLFSSL* ssl, int flags);
00358 
00359 
00360 #ifdef HAVE_NETX
00361     WOLFSSL_LOCAL int NetX_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx);
00362     WOLFSSL_LOCAL int NetX_Send(WOLFSSL *ssl, char *buf, int sz, void *ctx);
00363 
00364     WOLFSSL_API void wolfSSL_SetIO_NetX(WOLFSSL* ssl, NX_TCP_SOCKET* nxsocket,
00365                                       ULONG waitoption);
00366 #endif /* HAVE_NETX */
00367 
00368 #ifdef WOLFSSL_DTLS
00369     typedef int (*CallbackGenCookie)(WOLFSSL* ssl, unsigned char* buf, int sz,
00370                                      void* ctx);
00371     WOLFSSL_API void  wolfSSL_CTX_SetGenCookie(WOLFSSL_CTX*, CallbackGenCookie);
00372     WOLFSSL_API void  wolfSSL_SetCookieCtx(WOLFSSL* ssl, void *ctx);
00373     WOLFSSL_API void* wolfSSL_GetCookieCtx(WOLFSSL* ssl);
00374 
00375     #ifdef WOLFSSL_SESSION_EXPORT
00376         typedef int (*CallbackGetPeer)(WOLFSSL* ssl, char* ip, int* ipSz,
00377                                             unsigned short* port, int* fam);
00378         typedef int (*CallbackSetPeer)(WOLFSSL* ssl, char* ip, int ipSz,
00379                                               unsigned short port, int fam);
00380 
00381         WOLFSSL_API void wolfSSL_CTX_SetIOGetPeer(WOLFSSL_CTX*, CallbackGetPeer);
00382         WOLFSSL_API void wolfSSL_CTX_SetIOSetPeer(WOLFSSL_CTX*, CallbackSetPeer);
00383     #endif /* WOLFSSL_SESSION_EXPORT */
00384 #endif
00385 
00386 
00387 
00388 #ifndef XINET_NTOP
00389     #define XINET_NTOP(a,b,c,d) inet_ntop((a),(b),(c),(d))
00390 #endif
00391 #ifndef XINET_PTON
00392     #define XINET_PTON(a,b,c)   inet_pton((a),(b),(c))
00393 #endif
00394 #ifndef XHTONS
00395     #define XHTONS(a) htons((a))
00396 #endif
00397 #ifndef XNTOHS
00398     #define XNTOHS(a) ntohs((a))
00399 #endif
00400 
00401 #ifndef WOLFSSL_IP4
00402     #define WOLFSSL_IP4 AF_INET
00403 #endif
00404 #ifndef WOLFSSL_IP6
00405     #define WOLFSSL_IP6 AF_INET6
00406 #endif
00407 
00408 
00409 #ifdef __cplusplus
00410     }  /* extern "C" */
00411 #endif
00412 
00413 #endif /* WOLFSSL_IO_H */
00414