/* client.c
 *
 * Copyright (C) 2006-2014 wolfSSL Inc.
 *
 * This file is part of CyaSSL.
 *
 * CyaSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * CyaSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#ifdef HAVE_CONFIG_H
    #include <config.h>
#endif

#include <cyassl/ctaocrypt/settings.h>
#include <cyassl/options.h>

#include <cyassl/ssl.h>
#include <cyassl/test.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/errno.h>

#include <fcntl.h>
#include <sys/socket.h>
#include <arpa/inet.h>

/*look in diffrent location for cert*/
#if defined(caCert) || defined(cliCert) || defined(cliKey)
    #undef caCert
    #undef cliCert
    #undef cliKey
    #define caCert     "../certs/ca-cert.pem"
    #define cliCert    "../certs/client-cert.pem"
    #define cliKey     "../certs/client-key.pem"
#endif

#define MAXSZ     1024
#define YASSLPORT 11111

#define SOCKET_EWOULDBLOCK EWOULDBLOCK
#define SOCKET_EAGAIN      EAGAIN
#define SOCKET_ECONNRESET  ECONNRESET
#define SOCKET_EINTR       EINTR
#define SOCKET_EPIPE       EPIPE
#define SOCKET_ECONNREFUSED ECONNREFUSED
#define SOCKET_ECONNABORTED ECONNABORTED


/*
 * function with specific parameters : inbetween process of receiving msg
 * based from embeded receive in src/io.c
 */
int CbIORecv(CYASSL *ssl, char *buf, int sz, void *ctx)
{
    int recvd;
    int sd = *(int*)ctx;

    recvd = recv(sd, buf, sz, 0);

    if (recvd < 0) {
        if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
            if (!CyaSSL_dtls(ssl) || CyaSSL_get_using_nonblock(ssl)) {
                printf(" Would block\n");
                return CYASSL_CBIO_ERR_WANT_READ;
            }
            else {
                printf("Socket timeout\n");
                return CYASSL_CBIO_ERR_TIMEOUT;
            }
        }
        else if (errno == SOCKET_ECONNRESET) {
            printf("Connection reset\n");
            return CYASSL_CBIO_ERR_CONN_RST;
        }
        else if (errno == SOCKET_EINTR) {
            printf("Socket interrupted\n");
            return CYASSL_CBIO_ERR_ISR;
        }
        else if (errno == SOCKET_ECONNREFUSED) {
            printf("Connection refused\n");
            return CYASSL_CBIO_ERR_WANT_READ;
        }
        else if (errno == SOCKET_ECONNABORTED) {
            printf("Connection aborted\n");
            return CYASSL_CBIO_ERR_CONN_CLOSE;
        }
        else {
            printf("General error\n");
            return CYASSL_CBIO_ERR_GENERAL;
        }
    }
    else if (recvd == 0) {
        printf("Embed receive connection closed\n");
        return CYASSL_CBIO_ERR_CONN_CLOSE;
    }

    printf("Received %d bytes\n", sz);
    
    return recvd;
}


/*
 *function with specific parameters : inbetween process of sending out msg
 *based from embeded receive in src/io.c
 */
int CbIOSend(CYASSL *ssl, char *buf, int sz, void *ctx)
{
    int sd = *(int*)ctx;
    int sent;
    int len = sz;

    sent =send(sd, &buf[sz - len], len, 0);

    if (sent < 0) {
        printf("IO Send error\n");
        if (errno == SOCKET_EWOULDBLOCK || errno == SOCKET_EAGAIN) {
            printf("Would Block\n");
            return CYASSL_CBIO_ERR_WANT_WRITE;
        }
        else if (errno == SOCKET_ECONNRESET) {
            printf("Connection reset\n");
            return CYASSL_CBIO_ERR_CONN_RST;
        }
        else if (errno == SOCKET_EINTR) {
            printf("Socket interrupted\n");
            return CYASSL_CBIO_ERR_ISR;
        }
        else if (errno == SOCKET_EPIPE) {
            printf("Socket EPIPE\n");
            return CYASSL_CBIO_ERR_CONN_CLOSE;
        }
        else {
            printf("General error\n");
            return CYASSL_CBIO_ERR_GENERAL;
        }
    }

    printf("CbIOSend: sent %d bytes to %d\n", sz, sd);
    
    return sent;
}


int Client(const char* ip, word16 port)
{
    int  n;
    char msg[] = "hello cyassl";
    char reply[MAXSZ]; 
    int  msgSz = strlen(msg);
    SOCKET_T    fd;
    CYASSL_CTX* ctx;
    CYASSL*     ssl;

    if ((ctx = CyaSSL_CTX_new(CyaTLSv1_2_client_method())) == NULL)
        err_sys("Error in setting client ctx\n");
    
    if (CyaSSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
        err_sys("trouble loading client cert");
    if (CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
            != SSL_SUCCESS)
        err_sys("trouble loading client cert");
    if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
            != SSL_SUCCESS)
        err_sys("trouble loading client cert");

    /*sets the IO callback methods*/
    CyaSSL_SetIORecv(ctx, CbIORecv);
    CyaSSL_SetIOSend(ctx, CbIOSend);

    if ((ssl = CyaSSL_new(ctx)) == NULL)
        err_sys("issue when creating ssl");

    tcp_connect(&fd, ip, port, 0);
    CyaSSL_set_fd(ssl, fd);
    if (CyaSSL_connect(ssl) != SSL_SUCCESS)
        err_sys("client connect failed");

    if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
        err_sys("client write failed");

    memset(reply, 0, MAXSZ);
    if ((n = CyaSSL_read(ssl, reply, MAXSZ - 1)) > 0) {
        reply[n] = '\0';
    }
    else {
        printf("client read returned %d\n", n);
        return -1;
    }
    printf("Server sent : %s\n", reply);

    CyaSSL_shutdown(ssl);
    CyaSSL_free(ssl);
    CyaSSL_CTX_free(ctx);

    return 0;
}


int main(int argc, char* argv[])
{
    if (argc < 2 || argc > 3) {
        printf("please specifiy ip address\n"
                "Use case     ./client 127.0.0.1\n"
                "or with port ./client 127.0.0.1 1111\n");
        return 1;
    }

    CyaSSL_Init();
    if (argc < 3) {
        if (Client(argv[1], YASSLPORT) != 0)
            err_sys("error creating client");
        }
    else {
        if (Client(argv[1], (word16)atoi(argv[2])) != 0)
            err_sys("error creating client");
    }
    CyaSSL_Cleanup();

    return 0;
}
