changed Makefile.am to compile IOMultiplexer files
[NeonServV5.git] / src / ssl.c
1 /* ssl.c - NeonServ v5.5
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  * 
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * 
14  * You should have received a copy of the GNU General Public License 
15  * along with this program. If not, see <http://www.gnu.org/licenses/>. 
16  */
17
18 #include "ssl.h"
19 #include "ClientSocket.h"
20
21 void ssl_init() {
22 #ifdef HAVE_SSL
23     SSL_library_init();
24     SSL_load_error_strings();
25 #endif
26 }
27
28 void ssl_connect(struct ClientSocket *client) {
29 #ifdef HAVE_SSL
30     client->sslconn = NULL;
31     if(!(client->flags & SOCKET_FLAG_CONNECTED)) return;
32     struct SSLConnection *sslconn = malloc(sizeof(*sslconn));
33     sslconn->sslContext = SSL_CTX_new(SSLv23_client_method());
34     if(!sslconn->sslContext) goto ssl_connect_err;
35     sslconn->sslHandle = SSL_new(sslconn->sslContext);
36     if(!sslconn->sslHandle) goto ssl_connect_err;
37     if(!SSL_set_fd(sslconn->sslHandle, client->sock)) goto ssl_connect_err;
38     if(SSL_connect(sslconn->sslHandle) != 1) goto ssl_connect_err;
39     client->sslconn = sslconn;
40     return;
41 ssl_connect_err:
42     free(sslconn);
43 #endif    
44 }
45
46 void ssl_disconnect(struct ClientSocket *client) {
47 #ifdef HAVE_SSL
48     if(!client->sslconn) return;
49     SSL_shutdown(client->sslconn->sslHandle);
50     SSL_free(client->sslconn->sslHandle);
51     SSL_CTX_free(client->sslconn->sslContext);
52     free(client->sslconn);
53     client->sslconn = NULL;
54 #endif
55 }
56
57 int ssl_read(struct ClientSocket *client, char *buffer, int len) {
58 #ifdef HAVE_SSL
59     if(!client->sslconn) return -2;
60     return SSL_read(client->sslconn->sslHandle, buffer, len);
61 #endif
62     return -2;
63 }
64
65 int ssl_write(struct ClientSocket *client, char *buffer, int len) {
66 #ifdef HAVE_SSL
67     if(!client->sslconn) return -2;
68     return SSL_write(client->sslconn->sslHandle, buffer, len);
69 #endif
70     return -2;
71 }