added ssl support
This commit is contained in:
parent
b4825a3fb7
commit
de4223170b
7 changed files with 117 additions and 111 deletions
80
C/irc.c
80
C/irc.c
|
@ -9,6 +9,24 @@
|
|||
#include <arpa/inet.h>
|
||||
#include "irc.h"
|
||||
|
||||
int IRCC_urecv(IRCC_client *irc) {
|
||||
#ifdef ENABLE_SSL
|
||||
if (irc->irc_usingssl)
|
||||
return SSL_read(irc->irc_ssl, irc->irc_raw, sizeof(irc->irc_raw));
|
||||
#endif
|
||||
|
||||
return recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0);
|
||||
}
|
||||
|
||||
int IRCC_usend(IRCC_client *irc, off_t bytes) {
|
||||
#ifdef ENABLE_SSL
|
||||
if (irc->irc_usingssl)
|
||||
return SSL_write(irc->irc_ssl, irc->irc_raw, bytes);
|
||||
#endif
|
||||
|
||||
return send(irc->irc_socket, irc->irc_raw, bytes, 0);
|
||||
}
|
||||
|
||||
int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) {
|
||||
struct hostent *hp = gethostbyname(ip);
|
||||
if (!hp)
|
||||
|
@ -38,6 +56,7 @@ int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port) {
|
|||
|
||||
void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
|
||||
irc->irc_raw[strcspn(irc->irc_raw, "\r\n")] = '\0';
|
||||
|
||||
if (tmp != NULL) {
|
||||
//Message
|
||||
char *val = strchr(tmp, ':');
|
||||
|
@ -67,17 +86,14 @@ void IRCC_parse_msg(char *tmp, IRCC_client *irc) {
|
|||
|
||||
int IRCC_recv(IRCC_client *irc) {
|
||||
memset(irc->irc_raw, '\0', IRCC_MSG_MAX);
|
||||
irc->irc_channel = NULL;
|
||||
irc->irc_msg = NULL;
|
||||
irc->irc_nick = NULL;
|
||||
|
||||
int msg_size = recv(irc->irc_socket, irc->irc_raw, sizeof(irc->irc_raw), 0);
|
||||
int msg_size = IRCC_urecv(irc);
|
||||
if (msg_size == 0 || msg_size == -1)
|
||||
return IRCC_DISCONNECTED;
|
||||
|
||||
else if (!strncmp(irc->irc_raw, "PING", 4)) {
|
||||
*(strchr(irc->irc_raw, 'I')) = 'O';
|
||||
if (send(irc->irc_socket, irc->irc_raw, strlen(irc->irc_raw), 0) == -1)
|
||||
if (IRCC_usend(irc, strlen(irc->irc_raw)) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_PING;
|
||||
|
@ -87,6 +103,9 @@ int IRCC_recv(IRCC_client *irc) {
|
|||
}
|
||||
|
||||
int IRCC_parse(IRCC_client *irc) {
|
||||
irc->irc_channel = NULL;
|
||||
irc->irc_msg = NULL;
|
||||
irc->irc_nick = NULL;
|
||||
|
||||
//Check end of motd
|
||||
if (strstr(irc->irc_raw, "PRIVMSG ") == NULL && strstr(irc->irc_raw, "MOTD"))
|
||||
|
@ -129,11 +148,11 @@ int IRCC_parse(IRCC_client *irc) {
|
|||
|
||||
int IRCC_register(IRCC_client *irc, const char *nickname) {
|
||||
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "NICK %s\r\n", nickname);
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
if (IRCC_usend(irc, bytes) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "USER %s 0 localhost :%s\r\n", nickname, nickname);
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
if (IRCC_usend(irc, bytes) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
//Motd skip
|
||||
|
@ -152,7 +171,7 @@ int IRCC_register(IRCC_client *irc, const char *nickname) {
|
|||
|
||||
int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
|
||||
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "JOIN %s %s\r\n", channel, (key) ? key : "");
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
if (IRCC_usend(irc, bytes) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
|
@ -160,18 +179,57 @@ int IRCC_join(IRCC_client *irc, const char *channel, const char *key) {
|
|||
|
||||
int IRCC_send(IRCC_client *irc, const char *channel, const char *msg) {
|
||||
off_t bytes = snprintf(irc->irc_raw, sizeof(irc->irc_raw), "PRIVMSG %s :%s\r\n", channel, msg);
|
||||
if (send(irc->irc_socket, irc->irc_raw, bytes, 0) == -1)
|
||||
if (IRCC_usend(irc, bytes) == -1)
|
||||
return IRCC_ERROR;
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
|
||||
}
|
||||
|
||||
void IRCC_init(IRCC_client *irc) {
|
||||
irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
|
||||
int IRCC_initssl(IRCC_client *irc, int *ircc_errno) {
|
||||
#ifdef ENABLE_SSL
|
||||
OpenSSL_add_all_algorithms();
|
||||
|
||||
if (SSL_library_init() < 0) {
|
||||
*ircc_errno = IRCC_SSL_INIT;
|
||||
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;
|
||||
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;
|
||||
return IRCC_ERROR;
|
||||
}
|
||||
|
||||
irc->irc_usingssl = 1;
|
||||
|
||||
#else
|
||||
(void)irc;
|
||||
*ircc_errno = 0;
|
||||
#endif
|
||||
|
||||
return IRCC_SUCCESS;
|
||||
}
|
||||
|
||||
void IRCC_close(IRCC_client *irc) {
|
||||
close(irc->irc_socket);
|
||||
irc->irc_msg = irc->irc_nick = irc->irc_channel = NULL;
|
||||
|
||||
#ifdef ENABLE_SSL
|
||||
if (!irc->irc_usingssl)
|
||||
return;
|
||||
|
||||
SSL_free(irc->irc_ssl);
|
||||
SSL_CTX_free(irc->irc_sslctx);
|
||||
#endif
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue