1/* echoclient.c  */
2
3#include "openssl/ssl.h"
4#include "../test.h"
5
6
7int main(int argc, char** argv)
8{
9    SOCKET_T sockfd = 0;
10
11    FILE* fin  = stdin;
12    FILE* fout = stdout;
13
14    int inCreated  = 0;
15    int outCreated = 0;
16
17    char send[1024];
18    char reply[1024];
19
20    SSL_METHOD* method = 0;
21    SSL_CTX*    ctx    = 0;
22    SSL*        ssl    = 0;
23
24#ifdef _WIN32
25    WSADATA wsd;
26    WSAStartup(0x0002, &wsd);
27#endif
28
29    if (argc >= 2) {
30        fin  = fopen(argv[1], "r");
31        inCreated = 1;
32    }
33    if (argc >= 3) {
34        fout = fopen(argv[2], "w");
35        outCreated = 1;
36    }
37
38    if (!fin)  err_sys("can't open input file");
39    if (!fout) err_sys("can't open output file");
40
41    tcp_connect(&sockfd);
42
43    method = SSLv3_client_method();
44    ctx    = SSL_CTX_new(method);
45
46    if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
47        err_sys("can't load ca file");
48
49    ssl = SSL_new(ctx);
50
51    if (SSL_set_fd(ssl, sockfd) != SSL_SUCCESS)
52        err_sys("can't set ssl fd");
53
54    if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
55
56    while (fgets(send, sizeof(send), fin)) {
57
58        int  sendSz = strlen(send) + 1;
59
60        if (SSL_write(ssl, send, sendSz) != sendSz)
61            err_sys("SSL_write failed");
62
63        if (strncmp(send, "quit", 4) == 0) {
64            fputs("sending server shutdown command: quit!\n", fout);
65            break;
66        }
67
68        if (SSL_read(ssl, reply, sizeof(reply)) > 0)
69            fputs(reply, fout);
70    }
71
72    SSL_shutdown(ssl);
73    SSL_free(ssl);
74    SSL_CTX_free(ctx);
75
76    fflush(fout);
77    if (inCreated)  fclose(fin);
78    if (outCreated) fclose(fout);
79
80#ifdef _WIN32
81    closesocket(sockfd);
82#else
83    close(sockfd);
84#endif
85
86    return 0;
87}
88
89
90