Changeset 372 for asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/src/wolfio.c
- Timestamp:
- Feb 7, 2019, 8:36:33 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
asp3_tinet_ecnl_arm/trunk/wolfssl-3.12.2/src/wolfio.c
r352 r372 47 47 * WOLFSSL_USER_IO: Disables default Embed* callbacks and default: off 48 48 allows user to define their own using 49 wolfSSL_ SetIORecv and wolfSSL_SetIOSend49 wolfSSL_CTX_SetIORecv and wolfSSL_CTX_SetIOSend 50 50 * USE_WOLFSSL_IO: Enables the wolfSSL IO functions default: off 51 51 * HAVE_HTTP_CLIENT: Enables HTTP client API's default: off … … 65 65 * send() and recv() if need be. 66 66 */ 67 static INLINE int TranslateReturnCode(int old, int sd)67 static WC_INLINE int TranslateReturnCode(int old, int sd) 68 68 { 69 69 (void)sd; … … 89 89 } 90 90 91 static INLINE intLastError(void)91 static WC_INLINE int wolfSSL_LastError(void) 92 92 { 93 93 #ifdef USE_WINDOWS_API … … 103 103 104 104 105 #ifdef OPENSSL_EXTRA 106 /* Use the WOLFSSL read BIO for receiving data. This is set by the function 107 * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIORecv. 108 * 109 * ssl WOLFSSL struct passed in that has this function set as the receive 110 * callback. 111 * buf buffer to fill with data read 112 * sz size of buf buffer 113 * ctx a user set context 114 * 115 * returns the amount of data read or want read. See WOLFSSL_CBIO_ERR_* values. 116 */ 117 int BioReceive(WOLFSSL* ssl, char* buf, int sz, void* ctx) 118 { 119 int recvd = WOLFSSL_CBIO_ERR_GENERAL; 120 121 WOLFSSL_ENTER("BioReceive"); 122 123 if (ssl->biord == NULL) { 124 WOLFSSL_MSG("WOLFSSL biord not set"); 125 return WOLFSSL_CBIO_ERR_GENERAL; 126 } 127 128 switch (ssl->biord->type) { 129 case WOLFSSL_BIO_MEMORY: 130 case WOLFSSL_BIO_BIO: 131 if (wolfSSL_BIO_ctrl_pending(ssl->biord) == 0) { 132 return WOLFSSL_CBIO_ERR_WANT_READ; 133 } 134 recvd = wolfSSL_BIO_read(ssl->biord, buf, sz); 135 if (recvd <= 0) { 136 return WOLFSSL_CBIO_ERR_GENERAL; 137 } 138 break; 139 140 default: 141 WOLFSSL_MSG("This BIO type is unknown / unsupported"); 142 return WOLFSSL_CBIO_ERR_GENERAL; 143 } 144 145 (void)ctx; 146 return recvd; 147 } 148 149 150 /* Use the WOLFSSL write BIO for sending data. This is set by the function 151 * wolfSSL_set_bio and can also be set by wolfSSL_CTX_SetIOSend. 152 * 153 * ssl WOLFSSL struct passed in that has this function set as the send callback. 154 * buf buffer with data to write out 155 * sz size of buf buffer 156 * ctx a user set context 157 * 158 * returns the amount of data sent or want send. See WOLFSSL_CBIO_ERR_* values. 159 */ 160 int BioSend(WOLFSSL* ssl, char *buf, int sz, void *ctx) 161 { 162 int sent = WOLFSSL_CBIO_ERR_GENERAL; 163 164 if (ssl->biowr == NULL) { 165 WOLFSSL_MSG("WOLFSSL biowr not set\n"); 166 return WOLFSSL_CBIO_ERR_GENERAL; 167 } 168 169 switch (ssl->biowr->type) { 170 case WOLFSSL_BIO_MEMORY: 171 case WOLFSSL_BIO_BIO: 172 sent = wolfSSL_BIO_write(ssl->biowr, buf, sz); 173 if (sent < 0) { 174 return WOLFSSL_CBIO_ERR_GENERAL; 175 } 176 break; 177 178 default: 179 WOLFSSL_MSG("This BIO type is unknown / unsupported"); 180 return WOLFSSL_CBIO_ERR_GENERAL; 181 } 182 (void)ctx; 183 184 return sent; 185 } 186 #endif 187 188 105 189 #ifdef USE_WOLFSSL_IO 106 190 … … 113 197 int recvd; 114 198 115 #ifdef WOLFSSL_DTLS116 {117 int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);118 if (wolfSSL_dtls(ssl)119 && !wolfSSL_get_using_nonblock(ssl)120 && dtls_timeout != 0) {121 #ifdef USE_WINDOWS_API122 DWORD timeout = dtls_timeout * 1000;123 #else124 struct timeval timeout;125 XMEMSET(&timeout, 0, sizeof(timeout));126 timeout.tv_sec = dtls_timeout;127 #endif128 if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,129 sizeof(timeout)) != 0) {130 WOLFSSL_MSG("setsockopt rcvtimeo failed");131 }132 }133 }134 #endif135 136 199 recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags); 137 200 if (recvd < 0) { 138 int err = LastError();201 int err = wolfSSL_LastError(); 139 202 WOLFSSL_MSG("Embed Receive error"); 140 203 141 204 if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { 142 if (!wolfSSL_dtls(ssl) || wolfSSL_get_using_nonblock(ssl)) {143 205 WOLFSSL_MSG("\tWould block"); 144 206 return WOLFSSL_CBIO_ERR_WANT_READ; 145 207 } 146 else {147 WOLFSSL_MSG("\tSocket timeout");148 return WOLFSSL_CBIO_ERR_TIMEOUT;149 }150 }151 208 else if (err == SOCKET_ECONNRESET) { 152 209 WOLFSSL_MSG("\tConnection reset"); … … 157 214 return WOLFSSL_CBIO_ERR_ISR; 158 215 } 159 else if (err == SOCKET_ECONNREFUSED) {160 WOLFSSL_MSG("\tConnection refused");161 return WOLFSSL_CBIO_ERR_WANT_READ;162 }163 216 else if (err == SOCKET_ECONNABORTED) { 164 217 WOLFSSL_MSG("\tConnection aborted"); … … 188 241 sent = wolfIO_Send(sd, buf, sz, ssl->wflags); 189 242 if (sent < 0) { 190 int err = LastError();243 int err = wolfSSL_LastError(); 191 244 WOLFSSL_MSG("Embed Send error"); 192 245 … … 263 316 264 317 if (recvd < 0) { 265 err = LastError();318 err = wolfSSL_LastError(); 266 319 WOLFSSL_MSG("Embed Receive From error"); 267 320 268 321 if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { 269 if (wolfSSL_ get_using_nonblock(ssl)) {322 if (wolfSSL_dtls_get_using_nonblock(ssl)) { 270 323 WOLFSSL_MSG("\tWould block"); 271 324 return WOLFSSL_CBIO_ERR_WANT_READ; … … 326 379 327 380 if (sent < 0) { 328 err = LastError();381 err = wolfSSL_LastError(); 329 382 WOLFSSL_MSG("Embed Send To error"); 330 383 … … 374 427 375 428 if (recvd < 0) { 376 err = LastError();429 err = wolfSSL_LastError(); 377 430 WOLFSSL_MSG("Embed Receive From error"); 378 431 379 432 if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) { 380 if (wolfSSL_ get_using_nonblock(ssl)) {433 if (wolfSSL_dtls_get_using_nonblock(ssl)) { 381 434 WOLFSSL_MSG("\tWould block"); 382 435 return WOLFSSL_CBIO_ERR_WANT_READ; … … 626 679 } 627 680 628 #ifdef _MSC_VER629 /* 4204: non-constant aggregate initializer (nfds = sockfd + 1) */630 #pragma warning(disable: 4204)631 #endif632 681 int wolfIO_Select(SOCKET_T sockfd, int to_sec) 633 682 { 634 fd_set fds;635 SOCKET_T nfds = sockfd + 1;683 fd_set rfds, wfds; 684 int nfds = 0; 636 685 struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0}; 637 686 int ret; 638 687 639 FD_ZERO(&fds); 640 FD_SET(sockfd, &fds); 641 642 ret = select(nfds, &fds, &fds, NULL, &timeout); 688 #ifndef USE_WINDOWS_API 689 nfds = (int)sockfd + 1; 690 #endif 691 692 FD_ZERO(&rfds); 693 FD_SET(sockfd, &rfds); 694 wfds = rfds; 695 696 ret = select(nfds, &rfds, &wfds, NULL, &timeout); 643 697 if (ret == 0) { 644 698 #ifdef DEBUG_HTTP … … 648 702 } 649 703 else if (ret > 0) { 650 if (FD_ISSET(sockfd, &fds)) 704 if (FD_ISSET(sockfd, &wfds)) { 705 if (!FD_ISSET(sockfd, &rfds)) { 651 706 return 0; 707 } 708 } 652 709 } 653 710 return SOCKET_ERROR_E; … … 934 991 } 935 992 936 int wolfIO_HttpProcessResponse(int sfd, const char* appStr,993 int wolfIO_HttpProcessResponse(int sfd, const char** appStrList, 937 994 byte** respBuf, byte* httpBuf, int httpBufSz, int dynType, void* heap) 938 995 { … … 1017 1074 case phr_have_type: 1018 1075 if (XSTRNCASECMP(start, "Content-Type:", 13) == 0) { 1076 int i; 1077 1019 1078 start += 13; 1020 1079 while (*start == ' ' && *start != '\0') start++; 1021 if (XSTRNCASECMP(start, appStr, XSTRLEN(appStr)) != 0) { 1080 1081 /* try and match against appStrList */ 1082 i = 0; 1083 while (appStrList[i] != NULL) { 1084 if (XSTRNCASECMP(start, appStrList[i], 1085 XSTRLEN(appStrList[i])) == 0) { 1086 break; 1087 } 1088 i++; 1089 } 1090 if (appStrList[i] == NULL) { 1022 1091 WOLFSSL_MSG("wolfIO_HttpProcessResponse appstr mismatch"); 1023 1092 return -1; … … 1117 1186 return 0; 1118 1187 1119 XSTRNCPY((char*)buf, reqType, reqTypeLen);1120 buf += reqTypeLen; 1121 XSTRNCPY((char*)buf, blankStr, b lankStrLen+1);1122 buf += blankStrLen; 1123 XSTRNCPY((char*)buf, path, pathLen);1124 buf += pathLen; 1125 XSTRNCPY((char*)buf, http11Str, http11StrLen+1);1126 buf += http11StrLen; 1188 XSTRNCPY((char*)buf, reqType, bufSize); 1189 buf += reqTypeLen; bufSize -= reqTypeLen; 1190 XSTRNCPY((char*)buf, blankStr, bufSize); 1191 buf += blankStrLen; bufSize -= blankStrLen; 1192 XSTRNCPY((char*)buf, path, bufSize); 1193 buf += pathLen; bufSize -= pathLen; 1194 XSTRNCPY((char*)buf, http11Str, bufSize); 1195 buf += http11StrLen; bufSize -= http11StrLen; 1127 1196 if (domainNameLen > 0) { 1128 XSTRNCPY((char*)buf, hostStr, hostStrLen+1);1129 buf += hostStrLen; 1130 XSTRNCPY((char*)buf, domainName, domainNameLen);1131 buf += domainNameLen; 1197 XSTRNCPY((char*)buf, hostStr, bufSize); 1198 buf += hostStrLen; bufSize -= hostStrLen; 1199 XSTRNCPY((char*)buf, domainName, bufSize); 1200 buf += domainNameLen; bufSize -= domainNameLen; 1132 1201 } 1133 1202 if (reqSz > 0 && reqSzStrLen > 0) { 1134 XSTRNCPY((char*)buf, contentLenStr, contentLenStrLen+1);1135 buf += contentLenStrLen; 1136 XSTRNCPY((char*)buf, reqSzStr, reqSzStrLen);1137 buf += reqSzStrLen; 1203 XSTRNCPY((char*)buf, contentLenStr, bufSize); 1204 buf += contentLenStrLen; bufSize -= contentLenStrLen; 1205 XSTRNCPY((char*)buf, reqSzStr, bufSize); 1206 buf += reqSzStrLen; bufSize -= reqSzStrLen; 1138 1207 } 1139 1208 if (contentTypeLen > 0) { 1140 XSTRNCPY((char*)buf, contentTypeStr, contentTypeStrLen+1);1141 buf += contentTypeStrLen; 1142 XSTRNCPY((char*)buf, contentType, contentTypeLen);1143 buf += contentTypeLen; 1144 } 1145 XSTRNCPY((char*)buf, doubleCrLfStr, doubleCrLfStrLen+1);1209 XSTRNCPY((char*)buf, contentTypeStr, bufSize); 1210 buf += contentTypeStrLen; bufSize -= contentTypeStrLen; 1211 XSTRNCPY((char*)buf, contentType, bufSize); 1212 buf += contentTypeLen; bufSize -= contentTypeLen; 1213 } 1214 XSTRNCPY((char*)buf, doubleCrLfStr, bufSize); 1146 1215 buf += doubleCrLfStrLen; 1147 1216 … … 1169 1238 byte* httpBuf, int httpBufSz, void* heap) 1170 1239 { 1171 return wolfIO_HttpProcessResponse(sfd, "application/ocsp-response", 1240 const char* appStrList[] = { 1241 "application/ocsp-response", 1242 NULL 1243 }; 1244 1245 return wolfIO_HttpProcessResponse(sfd, appStrList, 1172 1246 respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_OCSP, heap); 1173 1247 } … … 1223 1297 1224 1298 ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec); 1225 if ((ret != 0) || ( sfd < 0)) {1299 if ((ret != 0) || ((int)sfd < 0)) { 1226 1300 WOLFSSL_MSG("OCSP Responder connection failed"); 1227 1301 } … … 1239 1313 } 1240 1314 1241 close(sfd);1315 CloseSocket(sfd); 1242 1316 XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP); 1243 1317 } … … 1278 1352 byte *respBuf = NULL; 1279 1353 1280 result = wolfIO_HttpProcessResponse(sfd, "application/pkix-crl", 1354 const char* appStrList[] = { 1355 "application/pkix-crl", 1356 "application/x-pkcs7-crl", 1357 NULL 1358 }; 1359 1360 result = wolfIO_HttpProcessResponse(sfd, appStrList, 1281 1361 &respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_CRL, crl->heap); 1282 1362 if (result >= 0) { … … 1334 1414 } 1335 1415 1336 close(sfd);1416 CloseSocket(sfd); 1337 1417 XFREE(httpBuf, crl->heap, DYNAMIC_TYPE_CRL); 1338 1418 } … … 1351 1431 1352 1432 1353 WOLFSSL_API void wolfSSL_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv) 1354 { 1433 WOLFSSL_API void wolfSSL_CTX_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv) 1434 { 1435 if (ctx != NULL) { 1355 1436 ctx->CBIORecv = CBIORecv; 1356 } 1357 1358 1359 WOLFSSL_API void wolfSSL_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend) 1360 { 1437 #ifdef OPENSSL_EXTRA 1438 ctx->cbioFlag |= WOLFSSL_CBIO_RECV; 1439 #endif 1440 } 1441 } 1442 1443 1444 WOLFSSL_API void wolfSSL_CTX_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend) 1445 { 1446 if (ctx != NULL) { 1361 1447 ctx->CBIOSend = CBIOSend; 1448 #ifdef OPENSSL_EXTRA 1449 ctx->cbioFlag |= WOLFSSL_CBIO_SEND; 1450 #endif 1451 } 1362 1452 } 1363 1453 … … 1611 1701 int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl); 1612 1702 if (wolfSSL_dtls(ssl) 1613 && !wolfSSL_ get_using_nonblock(ssl)1703 && !wolfSSL_dtls_get_using_nonblock(ssl) 1614 1704 && dtls_timeout != 0) { 1615 1705 /* needs timeout in milliseconds */ … … 1628 1718 if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY || 1629 1719 err == NET_ERR_FAULT_LOCK_ACQUIRE) { 1630 if (!wolfSSL_dtls(ssl) || wolfSSL_ get_using_nonblock(ssl)) {1720 if (!wolfSSL_dtls(ssl) || wolfSSL_dtls_get_using_nonblock(ssl)) { 1631 1721 WOLFSSL_MSG("\tWould block"); 1632 1722 return WOLFSSL_CBIO_ERR_WANT_READ; … … 1668 1758 dtls_timeout = 0; 1669 1759 1670 if (!wolfSSL_ get_using_nonblock(ssl)) {1760 if (!wolfSSL_dtls_get_using_nonblock(ssl)) { 1671 1761 /* needs timeout in milliseconds */ 1672 1762 NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err); … … 1683 1773 if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY || 1684 1774 err == NET_ERR_FAULT_LOCK_ACQUIRE) { 1685 if (wolfSSL_ get_using_nonblock(ssl)) {1775 if (wolfSSL_dtls_get_using_nonblock(ssl)) { 1686 1776 WOLFSSL_MSG("\tWould block"); 1687 1777 return WOLFSSL_CBIO_ERR_WANT_READ; … … 1773 1863 #endif /* MICRIUM */ 1774 1864 1865 #if defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) 1866 1867 #include <os/os_error.h> 1868 #include <os/os_mbuf.h> 1869 #include <os/os_mempool.h> 1870 1871 #define MB_NAME "wolfssl_mb" 1872 1873 typedef struct Mynewt_Ctx { 1874 struct mn_socket *mnSocket; /* send/recv socket handler */ 1875 struct mn_sockaddr_in mnSockAddrIn; /* socket address */ 1876 struct os_mbuf *mnPacket; /* incoming packet handle 1877 for short reads */ 1878 int reading; /* reading flag */ 1879 1880 /* private */ 1881 void *mnMemBuffer; /* memory buffer for mempool */ 1882 struct os_mempool mnMempool; /* mempool */ 1883 struct os_mbuf_pool mnMbufpool; /* mbuf pool */ 1884 } Mynewt_Ctx; 1885 1886 void mynewt_ctx_clear(void *ctx) { 1887 Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; 1888 if(!mynewt_ctx) return; 1889 1890 if(mynewt_ctx->mnPacket) { 1891 os_mbuf_free_chain(mynewt_ctx->mnPacket); 1892 mynewt_ctx->mnPacket = NULL; 1893 } 1894 os_mempool_clear(&mynewt_ctx->mnMempool); 1895 XFREE(mynewt_ctx->mnMemBuffer, 0, 0); 1896 XFREE(mynewt_ctx, 0, 0); 1897 } 1898 1899 /* return Mynewt_Ctx instance */ 1900 void* mynewt_ctx_new() { 1901 int rc = 0; 1902 Mynewt_Ctx *mynewt_ctx = NULL; 1903 int mem_buf_count = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_COUNT); 1904 int mem_buf_size = MYNEWT_VAL(WOLFSSL_MNSOCK_MEM_BUF_SIZE); 1905 int mempool_bytes = OS_MEMPOOL_BYTES(mem_buf_count, mem_buf_size); 1906 1907 mynewt_ctx = (Mynewt_Ctx *)XMALLOC(sizeof(struct Mynewt_Ctx), 1908 NULL, DYNAMIC_TYPE_TMP_BUFFER); 1909 if(!mynewt_ctx) return NULL; 1910 1911 XMEMSET(mynewt_ctx, 0, sizeof(Mynewt_Ctx)); 1912 mynewt_ctx->mnMemBuffer = XMALLOC(mempool_bytes, 0, 0); 1913 if(!mynewt_ctx->mnMemBuffer) { 1914 mynewt_ctx_clear((void*)mynewt_ctx); 1915 return NULL; 1916 } 1917 1918 rc = os_mempool_init(&mynewt_ctx->mnMempool, 1919 mem_buf_count, mem_buf_size, 1920 mynewt_ctx->mnMemBuffer, MB_NAME); 1921 if(rc != 0) { 1922 mynewt_ctx_clear((void*)mynewt_ctx); 1923 return NULL; 1924 } 1925 rc = os_mbuf_pool_init(&mynewt_ctx->mnMbufpool, &mynewt_ctx->mnMempool, 1926 mem_buf_count, mem_buf_size); 1927 if(rc != 0) { 1928 mynewt_ctx_clear((void*)mynewt_ctx); 1929 return NULL; 1930 } 1931 1932 return mynewt_ctx; 1933 } 1934 1935 static void mynewt_sock_writable(void *arg, int err); 1936 static void mynewt_sock_readable(void *arg, int err); 1937 static const union mn_socket_cb mynewt_sock_cbs = { 1938 .socket.writable = mynewt_sock_writable, 1939 .socket.readable = mynewt_sock_readable, 1940 }; 1941 static void mynewt_sock_writable(void *arg, int err) 1942 { 1943 /* do nothing */ 1944 } 1945 static void mynewt_sock_readable(void *arg, int err) 1946 { 1947 Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)arg; 1948 if (err && mynewt_ctx->reading) { 1949 mynewt_ctx->reading = 0; 1950 } 1951 } 1952 1953 /* The Mynewt receive callback 1954 * return : bytes read, or error 1955 */ 1956 int Mynewt_Receive(WOLFSSL *ssl, char *buf, int sz, void *ctx) 1957 { 1958 Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; 1959 int rc = 0; 1960 struct mn_sockaddr_in from; 1961 struct os_mbuf *m; 1962 int read_sz = 0; 1963 uint16_t total; 1964 1965 if (mynewt_ctx == NULL || mynewt_ctx->mnSocket == NULL) { 1966 WOLFSSL_MSG("Mynewt Recv NULL parameters"); 1967 return WOLFSSL_CBIO_ERR_GENERAL; 1968 } 1969 1970 if(mynewt_ctx->mnPacket == NULL) { 1971 mynewt_ctx->mnPacket = os_mbuf_get_pkthdr(&mynewt_ctx->mnMbufpool, 0); 1972 if(mynewt_ctx->mnPacket == NULL) { 1973 return MEMORY_E; 1974 } 1975 1976 mynewt_ctx->reading = 1; 1977 while(mynewt_ctx->reading && rc == 0) { 1978 rc = mn_recvfrom(mynewt_ctx->mnSocket, &m, (struct mn_sockaddr *) &from); 1979 if(rc == MN_ECONNABORTED) { 1980 rc = 0; 1981 mynewt_ctx->reading = 0; 1982 break; 1983 } 1984 if (!(rc == 0 || rc == MN_EAGAIN)) { 1985 WOLFSSL_MSG("Mynewt Recv receive error"); 1986 mynewt_ctx->reading = 0; 1987 break; 1988 } 1989 if(rc == 0) { 1990 int len = OS_MBUF_PKTLEN(m); 1991 if(len == 0) { 1992 break; 1993 } 1994 rc = os_mbuf_appendfrom(mynewt_ctx->mnPacket, m, 0, len); 1995 if(rc != 0) { 1996 WOLFSSL_MSG("Mynewt Recv os_mbuf_appendfrom error"); 1997 break; 1998 } 1999 os_mbuf_free_chain(m); 2000 m = NULL; 2001 } else if(rc == MN_EAGAIN) { 2002 /* continue to until reading all of packet data. */ 2003 rc = 0; 2004 break; 2005 } 2006 } 2007 if(rc != 0) { 2008 mynewt_ctx->reading = 0; 2009 os_mbuf_free_chain(mynewt_ctx->mnPacket); 2010 mynewt_ctx->mnPacket = NULL; 2011 return rc; 2012 } 2013 } 2014 2015 if(mynewt_ctx->mnPacket) { 2016 total = OS_MBUF_PKTLEN(mynewt_ctx->mnPacket); 2017 read_sz = (total >= sz)? sz : total; 2018 2019 os_mbuf_copydata(mynewt_ctx->mnPacket, 0, read_sz, (void*)buf); 2020 os_mbuf_adj(mynewt_ctx->mnPacket, read_sz); 2021 2022 if (read_sz == total) { 2023 WOLFSSL_MSG("Mynewt Recv Drained packet"); 2024 os_mbuf_free_chain(mynewt_ctx->mnPacket); 2025 mynewt_ctx->mnPacket = NULL; 2026 } 2027 } 2028 2029 return read_sz; 2030 } 2031 2032 /* The Mynewt send callback 2033 * return : bytes sent, or error 2034 */ 2035 int Mynewt_Send(WOLFSSL* ssl, char *buf, int sz, void *ctx) 2036 { 2037 Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx*)ctx; 2038 int rc = 0; 2039 struct os_mbuf *m = NULL; 2040 int write_sz = 0; 2041 m = os_msys_get_pkthdr(sz, 0); 2042 if (!m) { 2043 WOLFSSL_MSG("Mynewt Send os_msys_get_pkthdr error"); 2044 return WOLFSSL_CBIO_ERR_GENERAL; 2045 } 2046 rc = os_mbuf_copyinto(m, 0, buf, sz); 2047 if (rc != 0) { 2048 WOLFSSL_MSG("Mynewt Send os_mbuf_copyinto error"); 2049 os_mbuf_free_chain(m); 2050 return rc; 2051 } 2052 rc = mn_sendto(mynewt_ctx->mnSocket, m, (struct mn_sockaddr *)&mynewt_ctx->mnSockAddrIn); 2053 if(rc != 0) { 2054 WOLFSSL_MSG("Mynewt Send mn_sendto error"); 2055 os_mbuf_free_chain(m); 2056 return rc; 2057 } 2058 write_sz = sz; 2059 return write_sz; 2060 } 2061 2062 /* like set_fd, but for default NetX context */ 2063 void wolfSSL_SetIO_Mynewt(WOLFSSL* ssl, struct mn_socket* mnSocket, struct mn_sockaddr_in* mnSockAddrIn) 2064 { 2065 if (ssl && ssl->mnCtx) { 2066 Mynewt_Ctx *mynewt_ctx = (Mynewt_Ctx *)ssl->mnCtx; 2067 mynewt_ctx->mnSocket = mnSocket; 2068 memcpy(&mynewt_ctx->mnSockAddrIn, mnSockAddrIn, sizeof(struct mn_sockaddr_in)); 2069 mn_socket_set_cbs(mynewt_ctx->mnSocket, mnSocket, &mynewt_sock_cbs); 2070 } 2071 } 2072 2073 #endif /* defined(WOLFSSL_APACHE_MYNEWT) && !defined(WOLFSSL_LWIP) */ 2074 2075 #ifdef WOLFSSL_UIP 2076 #include <uip.h> 2077 #include <stdio.h> 2078 2079 /* uIP TCP/IP port, using the native tcp/udp socket api. 2080 * TCP and UDP are currently supported with the callbacks below. 2081 * 2082 */ 2083 /* The uIP tcp send callback 2084 * return : bytes sent, or error 2085 */ 2086 int uIPSend(WOLFSSL* ssl, char* buf, int sz, void* _ctx) 2087 { 2088 uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; 2089 int ret; 2090 unsigned int max_sendlen; 2091 int total_written = 0; 2092 (void)ssl; 2093 do { 2094 unsigned int bytes_left = sz - total_written; 2095 max_sendlen = tcp_socket_max_sendlen(&ctx->conn.tcp); 2096 if (bytes_left > max_sendlen) { 2097 printf("Send limited by buffer\r\n"); 2098 bytes_left = max_sendlen; 2099 } 2100 if (bytes_left == 0) { 2101 printf("Buffer full!\r\n"); 2102 break; 2103 } 2104 ret = tcp_socket_send(&ctx->conn.tcp, (unsigned char *)buf + total_written, bytes_left); 2105 if (ret <= 0) 2106 break; 2107 total_written += ret; 2108 } while(total_written < sz); 2109 return total_written; 2110 } 2111 2112 int uIPSendTo(WOLFSSL* ssl, char* buf, int sz, void* _ctx) 2113 { 2114 uip_wolfssl_ctx *ctx = (struct uip_wolfssl_ctx *)_ctx; 2115 int ret = 0; 2116 (void)ssl; 2117 ret = udp_socket_sendto(&ctx->conn.udp, (unsigned char *)buf, sz, &ctx->peer_addr, ctx->peer_port ); 2118 if (ret <= 0) 2119 return 0; 2120 return ret; 2121 } 2122 2123 /* The uIP uTCP/IP receive callback 2124 * return : nb bytes read, or error 2125 */ 2126 int uIPReceive(WOLFSSL *ssl, char *buf, int sz, void *_ctx) 2127 { 2128 uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)_ctx; 2129 if (!ctx || !ctx->ssl_rx_databuf) 2130 return -1; 2131 (void)ssl; 2132 if (ctx->ssl_rb_len > 0) { 2133 if (sz > ctx->ssl_rb_len - ctx->ssl_rb_off) 2134 sz = ctx->ssl_rb_len - ctx->ssl_rb_off; 2135 XMEMCPY(buf, ctx->ssl_rx_databuf + ctx->ssl_rb_off, sz); 2136 ctx->ssl_rb_off += sz; 2137 if (ctx->ssl_rb_off >= ctx->ssl_rb_len) { 2138 ctx->ssl_rb_len = 0; 2139 ctx->ssl_rb_off = 0; 2140 } 2141 return sz; 2142 } else { 2143 return WOLFSSL_CBIO_ERR_WANT_READ; 2144 } 2145 } 2146 2147 /* uIP DTLS Generate Cookie callback 2148 * return : number of bytes copied into buf, or error 2149 */ 2150 int uIPGenerateCookie(WOLFSSL* ssl, byte *buf, int sz, void *_ctx) 2151 { 2152 uip_wolfssl_ctx *ctx = (uip_wolfssl_ctx *)ctx; 2153 byte token[32]; 2154 byte digest[WC_SHA_DIGEST_SIZE]; 2155 int ret = 0; 2156 XMEMSET(token, 0, sizeof(token)); 2157 XMEMCPY(token, &ctx->peer_addr, sizeof(uip_ipaddr_t)); 2158 XMEMCPY(token + sizeof(uip_ipaddr_t), &ctx->peer_port, sizeof(word16)); 2159 ret = wc_ShaHash(token, sizeof(uip_ipaddr_t) + sizeof(word16), digest); 2160 if (ret != 0) 2161 return ret; 2162 if (sz > WC_SHA_DIGEST_SIZE) 2163 sz = WC_SHA_DIGEST_SIZE; 2164 XMEMCPY(buf, digest, sz); 2165 return sz; 2166 } 2167 2168 #endif /* WOLFSSL_UIP */ 2169 1775 2170 #endif /* WOLFCRYPT_ONLY */
Note:
See TracChangeset
for help on using the changeset viewer.