Загрузить файлы в «C»

This commit is contained in:
8nlight 2023-10-06 16:03:43 +03:00
parent bfd92a19b7
commit b0c07cdf38
3 changed files with 16 additions and 25 deletions

23
C/irc.c
View File

@ -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;
}

View File

@ -17,6 +17,7 @@
#include <stdlib.h>
#include <unistd.h>
#include <netdb.h>
#include <sys/time.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <arpa/inet.h>
@ -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);

View File

@ -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);