diff --git a/C/irc.c b/C/irc.c index 4e0360a..11e73e3 100644 --- a/C/irc.c +++ b/C/irc.c @@ -186,36 +186,33 @@ int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) { } -int IRCC_initssl(IRCC_client *irc, int *ircc_errno) { +int IRCC_initssl(IRCC_client *irc) { #ifdef ENABLE_SSL + irc->irc_ssl = NULL; + irc->irc_sslctx = NULL; + irc->irc_sslmethod = NULL; + OpenSSL_add_all_algorithms(); - if (SSL_library_init() < 0) { - *ircc_errno = IRCC_SSL_INIT; + if (SSL_library_init() < 0) return IRCC_ERROR; - } irc->irc_sslmethod = (SSL_METHOD *)SSLv23_client_method(); irc->irc_sslctx = SSL_CTX_new(irc->irc_sslmethod); - if (irc->irc_sslctx == NULL) { - *ircc_errno = IRCC_SSL_METHOD; + if (irc->irc_sslctx == NULL) return IRCC_ERROR; - } SSL_CTX_set_options(irc->irc_sslctx, SSL_OP_NO_SSLv2); irc->irc_ssl = SSL_new(irc->irc_sslctx); SSL_set_fd(irc->irc_ssl, irc->irc_socket); - if (SSL_connect(irc->irc_ssl) != 1) { - *ircc_errno = IRCC_SSL_SESSION; + if (SSL_connect(irc->irc_ssl) != 1) return IRCC_ERROR; - } irc->irc_usingssl = 1; #else (void)irc; - *ircc_errno = 0; #endif return IRCC_SUCCESS; @@ -229,7 +226,12 @@ void IRCC_close(IRCC_client *irc) { if (!irc->irc_usingssl) return; - SSL_free(irc->irc_ssl); - SSL_CTX_free(irc->irc_sslctx); + if (irc->irc_ssl != NULL) { + SSL_shutdown(irc->irc_ssl); + SSL_free(irc->irc_ssl); + } + + if (irc->irc_sslctx != NULL) + SSL_CTX_free(irc->irc_sslctx); #endif } diff --git a/C/irc.h b/C/irc.h index 6a05c21..2240db4 100644 --- a/C/irc.h +++ b/C/irc.h @@ -32,19 +32,6 @@ enum { IRCC_SUCCESS }; -enum { - IRCC_SSL_INIT = 1, - IRCC_SSL_METHOD, - IRCC_SSL_SESSION -}; - -static const char *IRCC_errno[] = { - "", - "Could not initialize the SSL library", - "Unable to create a new SSL context struct", - "Could not build a SSL session" -}; - typedef struct { int irc_socket; @@ -68,7 +55,7 @@ int IRCC_parse(IRCC_client *irc); int IRCC_join(IRCC_client *irc, const char *channel, const char *key); int IRCC_send(IRCC_client *irc, const char *msg, const char *channel); int IRCC_register(IRCC_client *irc, const char *nickname); -int IRCC_initssl(IRCC_client *irc, int *ircc_errno); +int IRCC_initssl(IRCC_client *irc); void IRCC_close(IRCC_client *irc); //u - mean universal. Functions uses internal fields in structure diff --git a/C/main.c b/C/main.c index 28bf65a..89427bd 100644 --- a/C/main.c +++ b/C/main.c @@ -2,13 +2,14 @@ #include #include #include +#include #include "irc.h" //Config #define HOST "irc.rizon.net" #define PORT 6697 #define NICK "tester134" -char *channels[] = {"#testssl"}; +char *channels[] = {"#test"}; #define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL) IRCC_client client; @@ -19,6 +20,13 @@ void die(const char *msg) { exit(1); } +void handler(int sig) { + (void)sig; + + IRCC_close(&client); + exit(0); +} + void recvinfo(void) { while (1) { unsigned int irc_status = IRCC_recv(&client); @@ -46,13 +54,16 @@ void recvinfo(void) { } int main(void) { + signal(SIGINT, handler); + int status = IRCC_connect(&client, HOST, PORT); if (status == IRCC_ERROR) die("Conn refused"); - int ie = 0; - if (IRCC_initssl(&client, &ie) == IRCC_ERROR) - die(IRCC_errno[ie]); + if (IRCC_initssl(&client) == IRCC_ERROR) { + IRCC_close(&client); + die("ssl error"); + } //Register and skip motd IRCC_register(&client, NICK);