From b0c07cdf38b4e77327ecb24cae39579352311e14 Mon Sep 17 00:00:00 2001 From: 8nlight <8nlight@disroot.org> Date: Fri, 6 Oct 2023 16:03:43 +0300 Subject: [PATCH] =?UTF-8?q?=D0=97=D0=B0=D0=B3=D1=80=D1=83=D0=B7=D0=B8?= =?UTF-8?q?=D1=82=D1=8C=20=D1=84=D0=B0=D0=B9=D0=BB=D1=8B=20=D0=B2=20=C2=AB?= =?UTF-8?q?C=C2=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- C/irc.c | 23 +++++++++-------------- C/irc.h | 7 ++++--- C/main.c | 11 +++-------- 3 files changed, 16 insertions(+), 25 deletions(-) diff --git a/C/irc.c b/C/irc.c index 1c553b0..736c86f 100644 --- a/C/irc.c +++ b/C/irc.c @@ -16,6 +16,10 @@ unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int p if (irc->socket < 0) return IRCC_ERROR; + struct timeval tv = {IRCC_PING_TIMEOUT, 0}; + if (setsockopt(irc->socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) + return IRCC_ERROR; + int status = connect(irc->socket, (struct sockaddr *)&client_str, sizeof(client_str)); if (status == -1) return IRCC_ERROR; @@ -59,12 +63,12 @@ void IRCC_parse(char *tmp, IRCC_client *irc){ } unsigned int IRCC_recv(IRCC_client *irc){ - memset(irc->raw, '\0', irc->size); + memset(irc->raw, '\0', IRCC_MSG_MAX); irc->channel = NULL; irc->msg = NULL; irc->nick = NULL; - int msg_size = recv(irc->socket, irc->raw, irc->size, 0); + int msg_size = recv(irc->socket, irc->raw, IRCC_MSG_MAX, 0); if (msg_size == 0 || msg_size == -1) return IRCC_DISCONNECTED; @@ -117,7 +121,7 @@ unsigned int IRCC_recv(IRCC_client *irc){ return IRCC_SUCCESS; } -void IRCC_send(IRCC_client *irc, char *msg, char *channel){ +void IRCC_send(IRCC_client *irc, const char *msg, const char *channel){ size_t size = strlen("PRIVMSG :\r\n") + strlen(channel) + strlen(msg); char *tmp = (char *)malloc(size + 1); @@ -133,21 +137,14 @@ void IRCC_send(IRCC_client *irc, char *msg, char *channel){ free(tmp); } -unsigned int IRCC_init(IRCC_client *irc, size_t size){ - if (size <= 250){ - fprintf(stderr, "Low buffer size (IRCC_init) (Min 250/512)\n"); - return IRCC_ERROR; - } - - irc->raw = (char *)malloc(size + 1); +unsigned int IRCC_init(IRCC_client *irc) { + irc->raw = (char *)malloc(IRCC_MSG_MAX + 1); if (irc->raw == NULL){ fprintf(stderr, "malloc returned NULL (IRCC_init)\n"); exit(1); } irc->msg = irc->nick = irc->channel = NULL; - - irc->size = size; return IRCC_SUCCESS; } @@ -156,6 +153,4 @@ void IRCC_close(IRCC_client *irc){ free(irc->raw); irc->raw = irc->msg = irc->nick = irc->channel = NULL; - - irc->size = 0; } diff --git a/C/irc.h b/C/irc.h index 6f4ed67..d376ae1 100644 --- a/C/irc.h +++ b/C/irc.h @@ -17,6 +17,7 @@ #include #include #include +#include #include #include #include @@ -40,7 +41,7 @@ enum { typedef struct { int socket; - size_t size; + char *raw; char *msg; char *channel; @@ -50,6 +51,6 @@ typedef struct { unsigned int IRCC_connect(IRCC_client *irc, const char *ip, const unsigned int port); unsigned int IRCC_register(IRCC_client *irc, const char *nickname); unsigned int IRCC_recv(IRCC_client *irc); -void IRCC_send(IRCC_client *irc, char *msg, char *channel); -unsigned int IRCC_init(IRCC_client *irc, size_t size); +void IRCC_send(IRCC_client *irc, const char *msg, const char *channel); +unsigned int IRCC_init(IRCC_client *irc); void IRCC_close(IRCC_client *irc); diff --git a/C/main.c b/C/main.c index 5d06f5a..c95b4d1 100644 --- a/C/main.c +++ b/C/main.c @@ -1,10 +1,10 @@ #include "irc.h" //Config -#define HOST "localhost" +#define HOST "irc.rizon.net" #define PORT 6667 #define NICK "tester134" -char *channels[] = {"#channel"}; +char *channels[] = {"#hp"}; #define CHECK_NULL() (client.nick != NULL && client.channel != NULL && client.msg != NULL) @@ -46,16 +46,11 @@ void recvinfo(void) { int main(void) { //512 - size of raw buffer (max irc) - IRCC_init(&client, IRCC_MSG_MAX); + IRCC_init(&client); int status = IRCC_connect(&client, HOST, PORT); if (status == IRCC_ERROR) die("Conn refused"); - //Socket timeout - struct timeval tv = {IRCC_PING_TIMEOUT, IRCC_PING_TIMEOUT}; - if (setsockopt(client.socket, SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) < 0) - die("setsockopt"); - //Register IRCC_register(&client, NICK);