+/* ssl.c - NeonServ v5.2
+ * Copyright (C) 2011 Philipp Kreil (pk910)
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "ssl.h"
+#include "ClientSocket.h"
+
+void ssl_init() {
+#ifdef HAVE_SSL
+ SSL_library_init();
+#endif
+}
+
+void ssl_connect(struct ClientSocket *client) {
+#ifdef HAVE_SSL
+ client->sslconn = NULL;
+ if(!(client->flags & SOCKET_FLAG_CONNECTED)) return;
+ struct SSLConnection *sslconn = malloc(sizeof(*sslconn));
+ sslconn->sslContext = SSL_CTX_new(SSLv23_client_method());
+ if(!sslconn->sslContext) goto ssl_connect_err;
+ sslconn->sslHandle = SSL_new(sslconn->sslContext);
+ if(!sslconn->sslHandle) goto ssl_connect_err;
+ if(!SSL_set_fd(sslconn->sslHandle, client->sock)) goto ssl_connect_err;
+ if(SSL_connect(sslconn->sslHandle) != 1) goto ssl_connect_err;
+ client->sslconn = sslconn;
+ssl_connect_err:
+ free(sslconn);
+#endif
+}
+
+void ssl_disconnect(struct ClientSocket *client) {
+#ifdef HAVE_SSL
+ if(!client->sslconn) return;
+ SSL_shutdown(client->sslconn->sslHandle);
+ SSL_free(client->sslconn->sslHandle);
+ SSL_CTX_free(client->sslconn->sslContext);
+ free(client->sslconn);
+ client->sslconn = NULL;
+#endif
+}
+
+int ssl_read(struct ClientSocket *client, char *buffer, int len) {
+#ifdef HAVE_SSL
+ if(!client->sslconn) return -2;
+ return SSL_read(client->sslconn->sslHandle, buffer, len);
+#endif
+ return -2;
+}
+
+int ssl_write(struct ClientSocket *client, char *buffer, int len) {
+#ifdef HAVE_SSL
+ if(!client->sslconn) return -2;
+ return SSL_write(client->sslconn->sslHandle, buffer, len);
+#endif
+ return -2;
+}
\ No newline at end of file