wolfio.h 23 KB

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