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

« back to all changes in this revision

Viewing changes to extra/yassl/examples/echoserver/echoserver.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
/* echoserver.cpp */
 
2
 
 
3
#include "../../testsuite/test.hpp"
 
4
 
 
5
 
 
6
#ifndef NO_MAIN_DRIVER
 
7
    #define ECHO_OUT
 
8
 
 
9
    THREAD_RETURN YASSL_API echoserver_test(void*);
 
10
    int main(int argc, char** argv)
 
11
    {
 
12
        func_args args;
 
13
 
 
14
        args.argc = argc;
 
15
        args.argv = argv;
 
16
 
 
17
        echoserver_test(&args);
 
18
        yaSSL_CleanUp();
 
19
 
 
20
        return args.return_code;
 
21
    }
 
22
 
 
23
#endif // NO_MAIN_DRIVER
 
24
 
 
25
 
 
26
 
 
27
void EchoError(SSL_CTX* ctx, SSL* ssl, SOCKET_T& s1, SOCKET_T& s2,
 
28
               const char* msg)
 
29
{
 
30
    SSL_CTX_free(ctx);
 
31
    SSL_free(ssl);
 
32
    tcp_close(s1);
 
33
    tcp_close(s2);
 
34
    err_sys(msg);
 
35
}
 
36
 
 
37
 
 
38
THREAD_RETURN YASSL_API echoserver_test(void* args)
 
39
{
 
40
#ifdef _WIN32
 
41
    WSADATA wsd;
 
42
    WSAStartup(0x0002, &wsd);
 
43
#endif
 
44
 
 
45
    SOCKET_T sockfd = 0;
 
46
    int      argc = 0;
 
47
    char**   argv = 0;
 
48
 
 
49
    set_args(argc, argv, *static_cast<func_args*>(args));
 
50
 
 
51
#ifdef ECHO_OUT
 
52
    FILE* fout = stdout;
 
53
    if (argc >= 2) fout = fopen(argv[1], "w");
 
54
    if (!fout) err_sys("can't open output file");
 
55
#endif
 
56
 
 
57
    tcp_listen(sockfd);
 
58
 
 
59
    SSL_METHOD* method = SSLv23_server_method();
 
60
    SSL_CTX*    ctx    = SSL_CTX_new(method);
 
61
 
 
62
    set_serverCerts(ctx);
 
63
    DH* dh = set_tmpDH(ctx);
 
64
 
 
65
    bool shutdown(false);
 
66
 
 
67
#if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
 
68
    // signal ready to tcp_accept
 
69
    func_args& server_args = *((func_args*)args);
 
70
    tcp_ready& ready = *server_args.signal_;
 
71
    pthread_mutex_lock(&ready.mutex_);
 
72
    ready.ready_ = true;
 
73
    pthread_cond_signal(&ready.cond_);
 
74
    pthread_mutex_unlock(&ready.mutex_);
 
75
#endif
 
76
 
 
77
    while (!shutdown) {
 
78
        sockaddr_in client;
 
79
        socklen_t   client_len = sizeof(client);
 
80
        SOCKET_T    clientfd   = accept(sockfd, (sockaddr*)&client,
 
81
                                      (ACCEPT_THIRD_T)&client_len);
 
82
        if (clientfd == -1) {
 
83
            SSL_CTX_free(ctx);
 
84
            tcp_close(sockfd);
 
85
            err_sys("tcp accept failed");
 
86
        }
 
87
 
 
88
        SSL* ssl = SSL_new(ctx);
 
89
        SSL_set_fd(ssl, clientfd);
 
90
        if (SSL_accept(ssl) != SSL_SUCCESS) {
 
91
            printf("SSL_accept failed\n");
 
92
            SSL_free(ssl);
 
93
            tcp_close(clientfd);
 
94
            continue; 
 
95
        }
 
96
 
 
97
        char command[1024];
 
98
        int echoSz(0);
 
99
        while ( (echoSz = SSL_read(ssl, command, sizeof(command))) > 0) {
 
100
           
 
101
            if ( strncmp(command, "quit", 4) == 0) {
 
102
                printf("client sent quit command: shutting down!\n");
 
103
                shutdown = true;
 
104
                break;
 
105
            }
 
106
            else if ( strncmp(command, "GET", 3) == 0) {
 
107
                char type[]   = "HTTP/1.0 200 ok\r\nContent-type:"
 
108
                                " text/html\r\n\r\n";
 
109
                char header[] = "<html><body BGCOLOR=\"#ffffff\">\n<pre>\n";
 
110
                char body[]   = "greetings from yaSSL\n";
 
111
                char footer[] = "</body></html>\r\n\r\n";
 
112
            
 
113
                strncpy(command, type, sizeof(type));
 
114
                echoSz = sizeof(type) - 1;
 
115
 
 
116
                strncpy(&command[echoSz], header, sizeof(header));
 
117
                echoSz += sizeof(header) - 1;
 
118
                strncpy(&command[echoSz], body, sizeof(body));
 
119
                echoSz += sizeof(body) - 1;
 
120
                strncpy(&command[echoSz], footer, sizeof(footer));
 
121
                echoSz += sizeof(footer);
 
122
 
 
123
                if (SSL_write(ssl, command, echoSz) != echoSz)
 
124
                    EchoError(ctx, ssl, sockfd, clientfd, "SSL_write failed");
 
125
 
 
126
                break;
 
127
            }
 
128
            command[echoSz] = 0;
 
129
 
 
130
        #ifdef ECHO_OUT
 
131
            fputs(command, fout);
 
132
        #endif
 
133
 
 
134
            if (SSL_write(ssl, command, echoSz) != echoSz)
 
135
                EchoError(ctx, ssl, sockfd, clientfd, "SSL_write failed");
 
136
        }
 
137
        SSL_shutdown(ssl);
 
138
        SSL_free(ssl);
 
139
        tcp_close(clientfd);
 
140
    }
 
141
 
 
142
    tcp_close(sockfd);
 
143
 
 
144
    DH_free(dh);
 
145
    SSL_CTX_free(ctx);
 
146
 
 
147
    ((func_args*)args)->return_code = 0;
 
148
    return 0;
 
149
}