This commit is contained in:
Your Name 2024-03-04 17:34:59 +03:00
parent de4223170b
commit 332c0ff43b
3 changed files with 31 additions and 31 deletions

28
C/irc.c
View File

@ -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 #ifdef ENABLE_SSL
irc->irc_ssl = NULL;
irc->irc_sslctx = NULL;
irc->irc_sslmethod = NULL;
OpenSSL_add_all_algorithms(); OpenSSL_add_all_algorithms();
if (SSL_library_init() < 0) { if (SSL_library_init() < 0)
*ircc_errno = IRCC_SSL_INIT;
return IRCC_ERROR; return IRCC_ERROR;
}
irc->irc_sslmethod = (SSL_METHOD *)SSLv23_client_method(); irc->irc_sslmethod = (SSL_METHOD *)SSLv23_client_method();
irc->irc_sslctx = SSL_CTX_new(irc->irc_sslmethod); irc->irc_sslctx = SSL_CTX_new(irc->irc_sslmethod);
if (irc->irc_sslctx == NULL) { if (irc->irc_sslctx == NULL)
*ircc_errno = IRCC_SSL_METHOD;
return IRCC_ERROR; return IRCC_ERROR;
}
SSL_CTX_set_options(irc->irc_sslctx, SSL_OP_NO_SSLv2); SSL_CTX_set_options(irc->irc_sslctx, SSL_OP_NO_SSLv2);
irc->irc_ssl = SSL_new(irc->irc_sslctx); irc->irc_ssl = SSL_new(irc->irc_sslctx);
SSL_set_fd(irc->irc_ssl, irc->irc_socket); SSL_set_fd(irc->irc_ssl, irc->irc_socket);
if (SSL_connect(irc->irc_ssl) != 1) { if (SSL_connect(irc->irc_ssl) != 1)
*ircc_errno = IRCC_SSL_SESSION;
return IRCC_ERROR; return IRCC_ERROR;
}
irc->irc_usingssl = 1; irc->irc_usingssl = 1;
#else #else
(void)irc; (void)irc;
*ircc_errno = 0;
#endif #endif
return IRCC_SUCCESS; return IRCC_SUCCESS;
@ -229,7 +226,12 @@ void IRCC_close(IRCC_client *irc) {
if (!irc->irc_usingssl) if (!irc->irc_usingssl)
return; return;
SSL_free(irc->irc_ssl); if (irc->irc_ssl != NULL) {
SSL_CTX_free(irc->irc_sslctx); SSL_shutdown(irc->irc_ssl);
SSL_free(irc->irc_ssl);
}
if (irc->irc_sslctx != NULL)
SSL_CTX_free(irc->irc_sslctx);
#endif #endif
} }

15
C/irc.h
View File

@ -32,19 +32,6 @@ enum {
IRCC_SUCCESS 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 { typedef struct {
int irc_socket; 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_join(IRCC_client *irc, const char *channel, const char *key);
int IRCC_send(IRCC_client *irc, const char *msg, const char *channel); int IRCC_send(IRCC_client *irc, const char *msg, const char *channel);
int IRCC_register(IRCC_client *irc, const char *nickname); 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); void IRCC_close(IRCC_client *irc);
//u - mean universal. Functions uses internal fields in structure //u - mean universal. Functions uses internal fields in structure

View File

@ -2,13 +2,14 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <signal.h>
#include "irc.h" #include "irc.h"
//Config //Config
#define HOST "irc.rizon.net" #define HOST "irc.rizon.net"
#define PORT 6697 #define PORT 6697
#define NICK "tester134" #define NICK "tester134"
char *channels[] = {"#testssl"}; char *channels[] = {"#test"};
#define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL) #define CHECK_NULL() (client.irc_nick != NULL && client.irc_channel != NULL && client.irc_msg != NULL)
IRCC_client client; IRCC_client client;
@ -19,6 +20,13 @@ void die(const char *msg) {
exit(1); exit(1);
} }
void handler(int sig) {
(void)sig;
IRCC_close(&client);
exit(0);
}
void recvinfo(void) { void recvinfo(void) {
while (1) { while (1) {
unsigned int irc_status = IRCC_recv(&client); unsigned int irc_status = IRCC_recv(&client);
@ -46,13 +54,16 @@ void recvinfo(void) {
} }
int main(void) { int main(void) {
signal(SIGINT, handler);
int status = IRCC_connect(&client, HOST, PORT); int status = IRCC_connect(&client, HOST, PORT);
if (status == IRCC_ERROR) if (status == IRCC_ERROR)
die("Conn refused"); die("Conn refused");
int ie = 0; if (IRCC_initssl(&client) == IRCC_ERROR) {
if (IRCC_initssl(&client, &ie) == IRCC_ERROR) IRCC_close(&client);
die(IRCC_errno[ie]); die("ssl error");
}
//Register and skip motd //Register and skip motd
IRCC_register(&client, NICK); IRCC_register(&client, NICK);