/* Collection of code snippets by Arne Vajhøj */
/* (from articles on eksperten.dk / vajhoej.dk written sometime between 2004 and now) */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <netdb.h>
#endif

#include <errno.h>

#include <openssl/ssl.h>
#include <openssl/x509.h>

#define HOST "localhost"
#define PORT "12345"

void recv_line(SSL *ssl, char *buf, int buflen)
{
    int ix;
    ix = 0;
    buf[ix] = '\0';
    while(ix < buflen && strstr(buf, "\r\n") == NULL)
    {
        ix = ix + SSL_read(ssl, buf + ix, buflen - ix);
        buf[ix] = '\0';
    }
}

int verify_callback(int preverify, X509_STORE_CTX *x509ctx)
{
    printf("(server is %s)\n",  x509ctx->cert->name);
    return 1;
}

int main()
{
    int sd, status, buflen;
    long int a, b, c;
    char *buf, *v, *v2;
    SSL_CTX *sslctx;
    SSL *ssl;
    struct addrinfo hints, *res;
#ifdef WIN32
    WSADATA WSAData;
    WSAStartup(0x0101, &WSAData);
#endif
    /* setup SSL */
    SSL_library_init();
    sslctx = SSL_CTX_new(TLSv1_2_client_method());
    if(sslctx == NULL)
    {
        printf("Error creating SSL context\n");
        exit(0);
    }
    SSL_CTX_set_verify(sslctx, SSL_VERIFY_PEER, verify_callback);
    SSL_CTX_set_verify_depth(sslctx, 4);
    SSL_CTX_set_options(sslctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION | SSL_OP_NO_TLSv1 | SSL_OP_NO_TLSv1_1);
    /* lookup host */
    memset(&hints, 0, sizeof(hints));
    hints.ai_family = AF_INET; //AF_UNSPEC;
    hints.ai_socktype = SOCK_STREAM;
    hints.ai_protocol = IPPROTO_TCP;
    hints.ai_flags = 0;
    status = getaddrinfo(HOST, PORT, &hints, &res);
    if(status != 0)
    {
        printf("Error looking up host: %s\n", HOST);
        exit(0);
    }
    /* create socket */
    sd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if(sd < 0)
    {
        printf("Error creating socket: %s\n", strerror(errno));
        exit(0);
    }
    /* connect to host */
    status = connect(sd, res->ai_addr, res->ai_addrlen);
    if(status != 0)
    {
        printf("Error connecting to host %s port %s: %s\n", HOST, PORT, strerror(errno));
        exit(0);
    }
    freeaddrinfo(res);
    /* SSL handshake */
    ssl = SSL_new(sslctx);
    if(ssl == NULL)
    {
        printf("Error initalizing SSL\n");
        exit(0);
    }
    SSL_set_fd(ssl, sd);
    status = SSL_connect(ssl);
    if(status <= 0)
    {
        printf("Error connecting SSL handshake\n");
        exit(0);
    }
    /* send a and b */
    a = 123;
    b = 456;
    buflen = 24;
    buf = malloc(buflen);
    sprintf(buf, "%ld %ld\r\n", a, b);
    status = SSL_write(ssl, buf, strlen(buf));
    if(status < 0)
    {
        printf("Error sending a and b: %s\n", strerror(errno));
        exit(0);
    }
    free(buf);
    /* receive c and print */
    buflen = 13;
    buf = malloc(buflen);
    recv_line(ssl, buf, buflen);
    sscanf(buf, "%ld\r\n", &c);
    printf("%ld\n", c);
    free(buf);
    /* send v */
    v = "ABC";
    buflen = strlen(v) + 3;
    buf = malloc(buflen);
    sprintf(buf, "%s\r\n", v);
    status = SSL_write(ssl, buf, strlen(buf));
    if(status < 0)
    {
        printf("Error sending v\n");
        exit(0);
    }
    free(buf);
    /* receive v2 and print */
    buflen = 258;
    buf = malloc(buflen);
    recv_line(ssl, buf, buflen);
    v2 = malloc(strlen(buf) + 1);
    sscanf(buf, "%s\r\n", v2);
    printf("%s\n", v2);
    free(buf);
    free(v2);
    /* close socket */
    SSL_free(ssl);
    SSL_CTX_free(sslctx);
#ifdef WIN32
    closesocket(sd);
    WSACleanup();
#else
    close(sd);
#endif
    return 0;
}
