~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to extra/yassl/examples/echoclient/echoclient.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* echoclient.cpp  */
 
2
 
 
3
#include "../../testsuite/test.hpp"
 
4
 
 
5
 
 
6
void EchoClientError(SSL_CTX* ctx, SSL* ssl, SOCKET_T& sockfd, const char* msg)
 
7
{
 
8
    SSL_CTX_free(ctx);
 
9
    SSL_free(ssl);
 
10
    tcp_close(sockfd);
 
11
    err_sys(msg);
 
12
}
 
13
 
 
14
 
 
15
void echoclient_test(void* args)
 
16
{
 
17
#ifdef _WIN32
 
18
    WSADATA wsd;
 
19
    WSAStartup(0x0002, &wsd);
 
20
#endif
 
21
 
 
22
    SOCKET_T sockfd = 0;
 
23
    int      argc = 0;
 
24
    char**   argv = 0;
 
25
 
 
26
    FILE* fin  = stdin;
 
27
    FILE* fout = stdout;
 
28
 
 
29
    bool inCreated  = false;
 
30
    bool outCreated = false;
 
31
 
 
32
    set_args(argc, argv, *static_cast<func_args*>(args));
 
33
    if (argc >= 2) {
 
34
        fin  = fopen(argv[1], "r"); 
 
35
        inCreated = true;
 
36
    }
 
37
    if (argc >= 3) {
 
38
        fout = fopen(argv[2], "w");
 
39
        outCreated = true;
 
40
    }
 
41
 
 
42
    if (!fin)  err_sys("can't open input file");
 
43
    if (!fout) err_sys("can't open output file");
 
44
 
 
45
    tcp_connect(sockfd);
 
46
 
 
47
    SSL_METHOD* method = SSLv23_client_method();
 
48
    SSL_CTX*    ctx = SSL_CTX_new(method);
 
49
    set_certs(ctx);
 
50
    SSL*        ssl = SSL_new(ctx);
 
51
 
 
52
    SSL_set_fd(ssl, sockfd);
 
53
 
 
54
    if (SSL_connect(ssl) != SSL_SUCCESS)
 
55
        EchoClientError(ctx, ssl, sockfd, "SSL_connect failed");
 
56
 
 
57
    char send[1024];
 
58
    char reply[1024];
 
59
 
 
60
    while (fgets(send, sizeof(send), fin)) {
 
61
 
 
62
        int sendSz = strlen(send) + 1;
 
63
        if (SSL_write(ssl, send, sendSz) != sendSz)
 
64
            EchoClientError(ctx, ssl, sockfd, "SSL_write failed");
 
65
 
 
66
        if (strncmp(send, "quit", 4) == 0) {
 
67
            fputs("sending server shutdown command: quit!\n", fout);
 
68
            break;
 
69
        }
 
70
 
 
71
        if (SSL_read(ssl, reply, sizeof(reply)) > 0) 
 
72
            fputs(reply, fout);
 
73
    }
 
74
 
 
75
    SSL_CTX_free(ctx);
 
76
    SSL_free(ssl);
 
77
    tcp_close(sockfd);
 
78
 
 
79
    fflush(fout);
 
80
    if (inCreated)  fclose(fin);
 
81
    if (outCreated) fclose(fout);
 
82
 
 
83
    ((func_args*)args)->return_code = 0;
 
84
}
 
85
 
 
86
 
 
87
#ifndef NO_MAIN_DRIVER
 
88
 
 
89
    int main(int argc, char** argv)
 
90
    {
 
91
        func_args args;
 
92
 
 
93
        args.argc = argc;
 
94
        args.argv = argv;
 
95
 
 
96
        echoclient_test(&args);
 
97
        yaSSL_CleanUp();
 
98
 
 
99
        return args.return_code;
 
100
    }
 
101
 
 
102
#endif // NO_MAIN_DRIVER