Ignore:
Timestamp:
Feb 7, 2019, 8:36:33 AM (5 years ago)
Author:
coas-nagasima
Message:

wolfsslを3.15.7にバージョンアップ

File:
1 edited

Legend:

Unmodified
Added
Removed
  • asp3_tinet_ecnl_rx/trunk/wolfssl-3.12.2/src/wolfio.c

    r337 r372  
    4747 * WOLFSSL_USER_IO:     Disables default Embed* callbacks and     default: off
    4848                        allows user to define their own using
    49                         wolfSSL_SetIORecv and wolfSSL_SetIOSend
     49                        wolfSSL_CTX_SetIORecv and wolfSSL_CTX_SetIOSend
    5050 * USE_WOLFSSL_IO:      Enables the wolfSSL IO functions          default: off
    5151 * HAVE_HTTP_CLIENT:    Enables HTTP client API's                 default: off
     
    6565 * send() and recv() if need be.
    6666 */
    67 static INLINE int TranslateReturnCode(int old, int sd)
     67static WC_INLINE int TranslateReturnCode(int old, int sd)
    6868{
    6969    (void)sd;
     
    8989}
    9090
    91 static INLINE int LastError(void)
     91static WC_INLINE int wolfSSL_LastError(void)
    9292{
    9393#ifdef USE_WINDOWS_API
     
    103103
    104104
     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 */
     117int 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 */
     160int 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
    105189#ifdef USE_WOLFSSL_IO
    106190
     
    113197    int recvd;
    114198
    115 #ifdef WOLFSSL_DTLS
    116     {
    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_API
    122                 DWORD timeout = dtls_timeout * 1000;
    123             #else
    124                 struct timeval timeout;
    125                 XMEMSET(&timeout, 0, sizeof(timeout));
    126                 timeout.tv_sec = dtls_timeout;
    127             #endif
    128             if (setsockopt(sd, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout,
    129                            sizeof(timeout)) != 0) {
    130                 WOLFSSL_MSG("setsockopt rcvtimeo failed");
    131             }
    132         }
    133     }
    134 #endif
    135 
    136199    recvd = wolfIO_Recv(sd, buf, sz, ssl->rflags);
    137200    if (recvd < 0) {
    138         int err = LastError();
     201        int err = wolfSSL_LastError();
    139202        WOLFSSL_MSG("Embed Receive error");
    140203
    141204        if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
    142             if (!wolfSSL_dtls(ssl) || wolfSSL_get_using_nonblock(ssl)) {
    143205                WOLFSSL_MSG("\tWould block");
    144206                return WOLFSSL_CBIO_ERR_WANT_READ;
    145207            }
    146             else {
    147                 WOLFSSL_MSG("\tSocket timeout");
    148                 return WOLFSSL_CBIO_ERR_TIMEOUT;
    149             }
    150         }
    151208        else if (err == SOCKET_ECONNRESET) {
    152209            WOLFSSL_MSG("\tConnection reset");
     
    157214            return WOLFSSL_CBIO_ERR_ISR;
    158215        }
    159         else if (err == SOCKET_ECONNREFUSED) {
    160             WOLFSSL_MSG("\tConnection refused");
    161             return WOLFSSL_CBIO_ERR_WANT_READ;
    162         }
    163216        else if (err == SOCKET_ECONNABORTED) {
    164217            WOLFSSL_MSG("\tConnection aborted");
     
    188241    sent = wolfIO_Send(sd, buf, sz, ssl->wflags);
    189242    if (sent < 0) {
    190         int err = LastError();
     243        int err = wolfSSL_LastError();
    191244        WOLFSSL_MSG("Embed Send error");
    192245
     
    263316
    264317    if (recvd < 0) {
    265         err = LastError();
     318        err = wolfSSL_LastError();
    266319        WOLFSSL_MSG("Embed Receive From error");
    267320
    268321        if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
    269             if (wolfSSL_get_using_nonblock(ssl)) {
     322            if (wolfSSL_dtls_get_using_nonblock(ssl)) {
    270323                WOLFSSL_MSG("\tWould block");
    271324                return WOLFSSL_CBIO_ERR_WANT_READ;
     
    326379
    327380    if (sent < 0) {
    328         err = LastError();
     381        err = wolfSSL_LastError();
    329382        WOLFSSL_MSG("Embed Send To error");
    330383
     
    374427
    375428    if (recvd < 0) {
    376         err = LastError();
     429        err = wolfSSL_LastError();
    377430        WOLFSSL_MSG("Embed Receive From error");
    378431
    379432        if (err == SOCKET_EWOULDBLOCK || err == SOCKET_EAGAIN) {
    380             if (wolfSSL_get_using_nonblock(ssl)) {
     433            if (wolfSSL_dtls_get_using_nonblock(ssl)) {
    381434                WOLFSSL_MSG("\tWould block");
    382435                return WOLFSSL_CBIO_ERR_WANT_READ;
     
    626679    }
    627680
    628     #ifdef _MSC_VER
    629         /* 4204: non-constant aggregate initializer (nfds = sockfd + 1) */
    630         #pragma warning(disable: 4204)
    631     #endif
    632681    int wolfIO_Select(SOCKET_T sockfd, int to_sec)
    633682    {
    634         fd_set fds;
    635         SOCKET_T nfds = sockfd + 1;
     683        fd_set rfds, wfds;
     684        int nfds = 0;
    636685        struct timeval timeout = { (to_sec > 0) ? to_sec : 0, 0};
    637686        int ret;
    638687
    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);
    643697        if (ret == 0) {
    644698        #ifdef DEBUG_HTTP
     
    648702        }
    649703        else if (ret > 0) {
    650             if (FD_ISSET(sockfd, &fds))
     704            if (FD_ISSET(sockfd, &wfds)) {
     705                if (!FD_ISSET(sockfd, &rfds)) {
    651706                return 0;
     707        }
     708            }
    652709        }
    653710        return SOCKET_ERROR_E;
     
    934991}
    935992
    936 int wolfIO_HttpProcessResponse(int sfd, const char* appStr,
     993int wolfIO_HttpProcessResponse(int sfd, const char** appStrList,
    937994    byte** respBuf, byte* httpBuf, int httpBufSz, int dynType, void* heap)
    938995{
     
    10171074                case phr_have_type:
    10181075                    if (XSTRNCASECMP(start, "Content-Type:", 13) == 0) {
     1076                        int i;
     1077
    10191078                        start += 13;
    10201079                        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) {
    10221091                            WOLFSSL_MSG("wolfIO_HttpProcessResponse appstr mismatch");
    10231092                            return -1;
     
    11171186        return 0;
    11181187
    1119     XSTRNCPY((char*)buf, reqType, reqTypeLen);
    1120     buf += reqTypeLen;
    1121     XSTRNCPY((char*)buf, blankStr, blankStrLen+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;
    11271196    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;
    11321201    }
    11331202    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;
    11381207    }
    11391208    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);
    11461215    buf += doubleCrLfStrLen;
    11471216
     
    11691238                                       byte* httpBuf, int httpBufSz, void* heap)
    11701239{
    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,
    11721246        respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_OCSP, heap);
    11731247}
     
    12231297
    12241298            ret = wolfIO_TcpConnect(&sfd, domainName, port, io_timeout_sec);
    1225             if ((ret != 0) || (sfd < 0)) {
     1299            if ((ret != 0) || ((int)sfd < 0)) {
    12261300                WOLFSSL_MSG("OCSP Responder connection failed");
    12271301            }
     
    12391313            }
    12401314
    1241             close(sfd);
     1315            CloseSocket(sfd);
    12421316            XFREE(httpBuf, ctx, DYNAMIC_TYPE_OCSP);
    12431317        }
     
    12781352    byte *respBuf = NULL;
    12791353
    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,
    12811361        &respBuf, httpBuf, httpBufSz, DYNAMIC_TYPE_CRL, crl->heap);
    12821362    if (result >= 0) {
     
    13341414            }
    13351415
    1336             close(sfd);
     1416            CloseSocket(sfd);
    13371417            XFREE(httpBuf, crl->heap, DYNAMIC_TYPE_CRL);
    13381418        }
     
    13511431
    13521432
    1353 WOLFSSL_API void wolfSSL_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv)
    1354 {
     1433WOLFSSL_API void wolfSSL_CTX_SetIORecv(WOLFSSL_CTX *ctx, CallbackIORecv CBIORecv)
     1434{
     1435    if (ctx != NULL) {
    13551436    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
     1444WOLFSSL_API void wolfSSL_CTX_SetIOSend(WOLFSSL_CTX *ctx, CallbackIOSend CBIOSend)
     1445{
     1446    if (ctx != NULL) {
    13611447    ctx->CBIOSend = CBIOSend;
     1448        #ifdef OPENSSL_EXTRA
     1449        ctx->cbioFlag |= WOLFSSL_CBIO_SEND;
     1450        #endif
     1451    }
    13621452}
    13631453
     
    16111701        int dtls_timeout = wolfSSL_dtls_get_current_timeout(ssl);
    16121702        if (wolfSSL_dtls(ssl)
    1613                      && !wolfSSL_get_using_nonblock(ssl)
     1703                     && !wolfSSL_dtls_get_using_nonblock(ssl)
    16141704                     && dtls_timeout != 0) {
    16151705            /* needs timeout in milliseconds */
     
    16281718        if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY ||
    16291719            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)) {
    16311721                WOLFSSL_MSG("\tWould block");
    16321722                return WOLFSSL_CBIO_ERR_WANT_READ;
     
    16681758        dtls_timeout = 0;
    16691759
    1670     if (!wolfSSL_get_using_nonblock(ssl)) {
     1760    if (!wolfSSL_dtls_get_using_nonblock(ssl)) {
    16711761        /* needs timeout in milliseconds */
    16721762        NetSock_CfgTimeoutRxQ_Set(sd, dtls_timeout * 1000, &err);
     
    16831773        if (err == NET_ERR_RX || err == NET_SOCK_ERR_RX_Q_EMPTY ||
    16841774            err == NET_ERR_FAULT_LOCK_ACQUIRE) {
    1685             if (wolfSSL_get_using_nonblock(ssl)) {
     1775            if (wolfSSL_dtls_get_using_nonblock(ssl)) {
    16861776                WOLFSSL_MSG("\tWould block");
    16871777                return WOLFSSL_CBIO_ERR_WANT_READ;
     
    17731863#endif /* MICRIUM */
    17741864
     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
     1873typedef 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
     1886void 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 */
     1900void* 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
     1935static void mynewt_sock_writable(void *arg, int err);
     1936static void mynewt_sock_readable(void *arg, int err);
     1937static const union mn_socket_cb mynewt_sock_cbs = {
     1938    .socket.writable = mynewt_sock_writable,
     1939    .socket.readable = mynewt_sock_readable,
     1940};
     1941static void mynewt_sock_writable(void *arg, int err)
     1942{
     1943    /* do nothing */
     1944}
     1945static 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 */
     1956int 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 */
     2035int 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 */
     2063void 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 */
     2086int 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
     2112int 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 */
     2126int 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 */
     2150int 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
    17752170#endif /* WOLFCRYPT_ONLY */
Note: See TracChangeset for help on using the changeset viewer.