Compare commits

...

7 Commits

Author SHA1 Message Date
thorium1256
167da3dd0e added submodules, minor updates 2026-05-24 11:17:37 +03:00
thorium1256
ff0f10295a add print.h 2026-05-15 23:02:30 +03:00
thorium1256
eafa4070e1 remove stray test binary 2026-05-15 23:00:01 +03:00
thorium1256
fda94c09b9 TODO 2026-05-15 22:56:45 +03:00
thorium1256
09017b581d added TLS/SSL support 2026-05-15 21:20:40 +03:00
thorium1256
da3aef1468 finalize SASL, move all IRC-related includes to a specific folder 2026-05-14 16:35:10 +03:00
thorium1256
634366a875 update .gitignore 2026-05-14 13:59:23 +03:00
18 changed files with 398 additions and 385 deletions

4
.gitignore vendored
View File

@@ -2,4 +2,6 @@ build
.vscode
bbirc-debug
bbirc
tests
tests
compile_commands.json
privinclude

3
.gitmodules vendored Normal file
View File

@@ -0,0 +1,3 @@
[submodule "libircc"]
path = libircc
url = https://gitea.codersquack.nl/thorium1256/libircc.git

View File

@@ -1,44 +1,55 @@
CC := clang
CFLAGS := -I include -Wall -Wextra
CFLAGS_DBG := $(CFLAGS) -g -O0
CFLAGS_REL := $(CFLAGS) -O2
LDFLAGS :=
LDFLAGS_REL := $(LDFLAGS) -s
BUILD_DIR := build
SRC_DIR := src
INCLUDE_DIR := include
PRIVATE_INCLUDE_DIR := privinclude
LIBIRCC_DIR := libircc
LIBIRCC_LIB := $(LIBIRCC_DIR)/libircc.so
CC := clang
CFLAGS := -I$(INCLUDE_DIR) -I$(PRIVATE_INCLUDE_DIR) -I$(LIBIRCC_DIR)/include -Wall -Wextra
CFLAGS_DBG := $(CFLAGS) -g -O0
CFLAGS_REL := $(CFLAGS) -O2
LDFLAGS := -L./libircc -Wl,-rpath,'$$ORIGIN/libircc' -lircc -lssl -lcrypto
LDFLAGS_REL := $(LDFLAGS) -s
APPLICATION := bbirc
APPLICATION_DBG := bbirc-debug
$(shell mkdir -p $(BUILD_DIR))
SRCS := $(wildcard $(SRC_DIR)/*.c)
LIBIRCC_SRCS := $(shell find $(LIBIRCC_DIR)/$(SRC_DIR) -name '*.c')
DBG_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/dbg_%.o,$(SRCS))
REL_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/rel_%.o,$(SRCS))
.PHONY: all debug release clean
.PHONY: all debug release both clean ircc
all: release
debug: $(APPLICATION_DBG)
release: $(APPLICATION)
debug: ircc $(APPLICATION_DBG)
release: ircc $(APPLICATION)
both: debug release
ircc: $(LIBIRCC_LIB)
$(APPLICATION): $(REL_OBJS)
$(CC) -o $@ $^ $(LDFLAGS_REL)
$(APPLICATION_DBG): $(DBG_OBJS)
+$(MAKE) -C $(LIBIRCC_DIR)
$(CC) -o $@ $^ $(LDFLAGS)
$(BUILD_DIR)/dbg_%.o: $(SRC_DIR)/%.c
$(CC) -c -o $@ $^ $(CFLAGS_DBG)
$(CC) -c -o $@ $< $(CFLAGS_DBG)
$(BUILD_DIR)/rel_%.o: $(SRC_DIR)/%.c
$(CC) -c -o $@ $^ $(CFLAGS_REL)
$(CC) -c -o $@ $< $(CFLAGS_REL)
$(LIBIRCC_LIB): $(LIBIRCC_SRCS)
+$(MAKE) -C libircc
clean:
rm -rf $(BUILD_DIR)
rm -rf $(APPLICATION)
rm -rf $(APPLICATION_DBG)
rm -rf $(APPLICATION_DBG)
+$(MAKE) -C $(LIBIRCC_DIR) clean

View File

@@ -1,5 +1,7 @@
# To-Do
1. Add actual IRC client functionality
2. Finish the netcode
3. Don't care about the looks of it yet, just have an ANSI command line or something
1. ~~Finish the netcode~~
2. ~~Add SASL PLAIN authentication~~
3. Add SASL EXTERNAL authentication
4. Add a line text user interface.
4. Add an event polling system

View File

@@ -1,19 +0,0 @@
[
{
"file": "src/main.c",
"arguments": [
"/usr/bin/clang",
"-c",
"-o",
"build/rel_main.o",
"src/main.c",
"-I",
"include",
"-Wall",
"-Wextra",
"-O2"
],
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
"output": "build/rel_main.o"
}
]

View File

@@ -1,27 +0,0 @@
#ifndef IRC_H
#define IRC_H
#include "IRC_structs.h"
void IRC_ParseMessage(char* line, irc_message_t *msg);
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc);
// various IRC commands
void IRC_PRIVMSG(char* to, char* msg, irc_client_t *irc);
void IRC_NOTICE(char* to, char* msg, irc_client_t *irc);
void IRC_NICK(char *nick, irc_client_t *irc);
void IRC_USER(char *ident, char* realname, irc_client_t *irc);
// registration
void IRC_Register(char *nick, char* ident, char *realname, bool saslEnabled, irc_sasl_mechanism_t saslMethod, char *saslUsername, char *saslPassword, irc_client_t *irc);
// Capability negotiation block
void IRC_StartCapabilityNegotiation(irc_client_t *irc);
void IRC_RequestCapabilities(char* caps, irc_client_t *irc);
void IRC_EndCapabilityNegotiation(irc_client_t *irc);
void IRC_NeedCapabilities(char* caps, irc_client_t *irc);
int IRC_ParseCapabilities(char* caps, irc_capability_t *capabilities, int capsLength);
#endif

47
include/IRC/IRC.h Normal file
View File

@@ -0,0 +1,47 @@
#ifndef IRC_H
#define IRC_H
#include "IRC_structs.h"
#include "IRC_numerics.h"
void IRC_ParseMessage(char* line, irc_message_t *msg);
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc);
// various IRC commands
void IRC_PRIVMSG(char* to, char* msg, irc_client_t *irc);
void IRC_NOTICE(char* to, char* msg, irc_client_t *irc);
void IRC_NICK(char *nick, irc_client_t *irc);
void IRC_USER(char *ident, char* realname, irc_client_t *irc);
void IRC_CTCPSend(char *to, char *cmd, irc_client_t *irc);
// registration
void IRC_SetConnectionParameters(char* host, int port, irc_connection_type_t conntype, irc_client_t *irc);
void IRC_SetIdentityParameters(char* nickname, char* username, char* realname, irc_client_t *irc);
void IRC_Register(irc_client_t *irc);
void IRC_SetSASLParameters(irc_sasl_mechanism_t, char* username, char* password, irc_client_t *irc);
void IRC_RequestSASLAuthentication(irc_client_t *irc);
// Capability negotiation block
void IRC_StartCapabilityNegotiation(irc_client_t *irc);
void IRC_RequestCapabilities(char* caps, irc_client_t *irc);
void IRC_EndCapabilityNegotiation(irc_client_t *irc);
void IRC_NeedCapabilities(char* caps, irc_client_t *irc);
int IRC_ParseCapabilities(char* caps, irc_capability_t *capabilities, int capsLength);
// raw socket things
void IRC_Connect(irc_client_t *irc);
ssize_t IRC_Receive(irc_client_t *irc, char* toBuf, size_t bufSize);
void IRC_Send(irc_client_t *irc, char* toSend);
void IRC_Close(irc_client_t *irc);
// init
int IRC_Init(irc_client_t *irc);
void IRC_Free(irc_client_t *irc);
int IRC_PollEvent(irc_event_t *event, irc_client_t *irc);
#endif

17
include/IRC/IRC_events.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef IRC_EVENTS_H
#define IRC_EVENTS_H
typedef enum
{
IRC_EVENT_PRIVMSG,
IRC_EVENT_NOTICE,
IRC_EVENT_JOIN,
IRC_EVENT_PART,
IRC_EVENT_QUIT,
IRC_EVENT_MODESET,
IRC_EVENT_AWAY,
IRC_EVENT_KICK
} irc_event_type_t;
#endif

View File

@@ -1,7 +1,10 @@
#ifndef IRC_STRUCTS_H
#define IRC_STRUCTS_H
#include "IRC/IRC_events.h"
#include "netcode.h"
#include <stdbool.h>
#include <openssl/ssl.h>
typedef enum
{
@@ -25,6 +28,12 @@ typedef struct {
int argc;
} irc_message_t;
typedef struct
{
irc_event_type_t type;
irc_message_t orig_msg;
} irc_event_t;
typedef struct
{
irc_hostmask_t hostmask;
@@ -34,7 +43,7 @@ typedef struct
typedef struct
{
char name[64];
char topic[512];
char topic[513];
irc_chanuser_t* users;
} irc_channel_t;
@@ -68,26 +77,43 @@ typedef struct
typedef struct
{
int sockfd;
irc_hostmask_t ownHostmask;
char supportedCapabilities[512];
char requestedCapabilities[512];
irc_capability_t acked[24];
struct
{
bool saslWorks;
} capabilities;
/* Authentication parameters */
irc_auth_t sasl_auth;
} irc_client_t;
char nick[32];
char altnick[32];
char altnick2[32];
char username[16];
char realname[128];
} irc_identity_t;
typedef enum
{
IRC_CONNECTION_PLAINTEXT,
IRC_CONNECTION_TLS
} irc_connection_type_t;
typedef struct
{
char nick[32];
char ident[16];
char realname[64];
} irc_identity_t;
char host[128];
int port;
irc_connection_type_t connType;
} irc_connection_info_t;
typedef struct
{
irc_connection_info_t connectionInfo;
sslSockfd_t connection;
irc_hostmask_t ownHostmask;
char supportedCapabilities[513];
char requestedCapabilities[513];
irc_capability_t acked[16];
/* Authentication parameters */
irc_auth_t saslAuth;
/* Identity */
irc_identity_t identity;
irc_event_t* eventQueue; // lives on the heap
int eventQueuePtr;
} irc_client_t;
typedef struct {
const char *name;

9
include/base64.h Normal file
View File

@@ -0,0 +1,9 @@
#ifndef BASE64_H
#define BASE64_H
#include <stddef.h>
#include <stdint.h>
int base64_encode(const uint8_t *in, size_t in_len, char *out, size_t out_size);
#endif

View File

@@ -1,9 +1,24 @@
#ifndef NETCODE_H
#define NETCODE_H
#include <openssl/crypto.h>
typedef struct
{
SSL *ssl;
int sockfd;
SSL_CTX *ctx;
} sslSockfd_t;
// Returns a socket file descriptor.
int NET_Connect(const char* host, int port);
void NET_Send(int sockfd, const char* toSend);
void NET_Close(int sockfd);
sslSockfd_t NET_Connect(const char* host, int port);
void NET_Send(sslSockfd_t info, const char* toSend);
void NET_Close(sslSockfd_t info);
// TLS versions of the same thing
sslSockfd_t NET_TLS_Connect(const char* host, int port);
void NET_TLS_Send(sslSockfd_t info, const char* toSend);
void NET_TLS_Close(sslSockfd_t info);
#endif

19
include/print.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef PRINT_H
#define PRINT_H
#define DEFRAWT() \
time_t rawtime; \
struct tm *timeinfo; \
time(&rawtime); \
timeinfo = localtime(&rawtime)
#define REDEFRAWT() \
time(&rawtime); \
timeinfo = localtime(&rawtime)
#define PRINT_INFO(tminfo) \
printf("\r\x1b[3m\x1b[38;5;8m[%02d:%02d]\x1b[0m \x1b[1m\x1b[34m-\x1b[0m!\x1b[34m-\x1b[0m ", tminfo->tm_hour, tminfo->tm_min)
#endif

1
libircc Submodule

Submodule libircc added at 7be1ef312e

249
src/IRC.c
View File

@@ -1,249 +0,0 @@
#include "IRC.h"
#include "defines.h"
#include "netcode.h"
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
void IRC_ParseMessage(char *line, irc_message_t *msg)
{
strncpy(msg->line, line, sizeof(msg->line) - 1);
char *ptr = msg->line;
ptr[sizeof(msg->line)] = NUL;
char *source = NULL;
char *command = NULL;
// get the source, if it exists (the : at the beginning of an IRC message)
if(*ptr == ':')
{
ptr++;
source = ptr;
while (*ptr && *ptr != ' ') ptr++;
if (*ptr) *ptr++ = NUL;
while (*ptr == ' ') ptr++; // skip any extra spaces
}
msg->source = source;
// read the command
command = ptr;
while(*ptr && *ptr != ' ') ptr++;
if (*ptr) *ptr++ = NUL;
msg->command = command;
while (*ptr == ' ') ptr++;
// read its arguments (until we encounter a colon/nullbyte)
msg->argc = 0;
while(*ptr && msg->argc <= 15)
{
if(*ptr == ':') // if it's a trailing arg
{
ptr++;
msg->argv[msg->argc] = ptr;
msg->argc++;
break;
}
msg->argv[msg->argc] = ptr;
while(*ptr && *ptr != ' ') ptr++;
if (*ptr)
{
*ptr++ = NUL;
while (*ptr == ' ') ptr++; // skip spaces for next arg
}
msg->argc++;
}
}
// Returns the number of capabilities found.
int IRC_ParseCapabilities(char *caps, irc_capability_t *capabilities, int capsLength)
{
char capBak[512];
strncpy(capBak, caps, sizeof(capBak) - 1); // copy the capline so we don't destroy our parameter
capBak[sizeof(capBak) - 1] = 0;
char *ptr = capBak;
char *tmp = ptr;
int argc = 0;
while(*ptr && argc < capsLength)
{
while(*ptr == ' ') ptr++; // skip any spaces before a capability
if(!*ptr) break;
tmp = ptr;
while(*tmp && *tmp != ' ') tmp++;
*tmp++ = 0;
// copy the cap line while it's untampered with
strncpy(capabilities[argc].capLine, ptr, sizeof(capabilities[argc].capLine) - 1);
capabilities[argc].capLine[sizeof(capabilities[argc].capLine) - 1] = 0;
// ptr now points to the start of the next cap
ptr = tmp;
// set the name pointer there
char* walker = capabilities[argc].capLine;
capabilities[argc].name = walker;
while(*walker && *walker != '=') walker++;
if(*walker == '=')
{
*walker++ = 0;
// we don't need to move walker since it will be reinitialized in the next `while` iteration, and capLine already ends with a nullbyte
capabilities[argc].args = walker;
argc++;
}
else if (*walker == 0)
{
// the cap has no args
capabilities[argc].args = NULL;
argc++;
}
}
return argc;
}
static void handlePing(irc_message_t *msg, irc_client_t *irc)
{
char buf[128]; // most pingpongs are usually short, with the servername being short too
snprintf(buf, sizeof buf, "PONG :%s", msg->argv[0]);
NET_Send(irc->sockfd, buf);
}
static void handleCapabilities(irc_message_t *msg, irc_client_t *irc)
{
// we are willing to tolerate some spaghetti...
if(strcmp(msg->argv[2], "LS") == 0)
{
snprintf(irc->supportedCapabilities, sizeof irc->supportedCapabilities, "%s", msg->argv[3]);
// parse the caps
irc_capability_t caps[24];
int capCount = IRC_ParseCapabilities(irc->supportedCapabilities, caps, sizeof(caps) / sizeof(irc_capability_t));
irc_capability_t reqCaps[24];
int reqCapCount = IRC_ParseCapabilities(irc->requestedCapabilities, reqCaps, sizeof(reqCaps) / sizeof(irc_capability_t));
char buf[512];
int bytesWritten = 0;
for(int i = 0; i < capCount; i++)
{
for(int j = 0; j < reqCapCount; j++)
{
if(strcmp(caps[i].name, reqCaps[j].name) == 0 == 0)
{
bytesWritten += snprintf(buf + bytesWritten, sizeof(buf) - bytesWritten, "%s ", reqCaps[j].name);
}
}
}
IRC_RequestCapabilities(buf, irc);
}
else if (strcmp(msg->argv[2], "ACK"))
{
int ackedCount = IRC_ParseCapabilities(msg->argv[3], irc->acked, sizeof(irc->acked) / sizeof(irc_capability_t));
for(int i = 0; i < ackedCount; i++)
{
}
}
}
/*
IRC_ProcessMessage is only used for commands that have no need to be a user-listenable event.
For pollable events, use IRC_PollEvent(&event).
*/
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
{
command_t commands[] =
{
{"PING", handlePing},
{"CAP", handleCapabilities},
{NULL, NULL}
};
for(int i = 0; commands[i].name; i++)
{
if(strcmp(msg->command, commands[i].name) == 0)
{
commands[i].handler(msg, irc);
}
}
}
void IRC_PRIVMSG(char *to, char *msg, irc_client_t *irc)
{
char buf[512];
snprintf(buf, sizeof buf, "PRIVMSG %s :%s", to, msg);
NET_Send(irc->sockfd, buf);
}
void IRC_NOTICE(char *to, char *msg, irc_client_t *irc)
{
char buf[512];
snprintf(buf, sizeof buf, "NOTICE %s :%s", to, msg);
NET_Send(irc->sockfd, buf);
}
void IRC_NICK(char *newNick, irc_client_t *irc)
{
char buf[512];
snprintf(buf, sizeof buf, "NICK %s", newNick);
NET_Send(irc->sockfd, buf);
}
void IRC_USER(char *ident, char *realname, irc_client_t *irc)
{
char buf[512];
snprintf(buf, sizeof buf, "USER %s 0 * :%s", ident, realname);
NET_Send(irc->sockfd, buf);
}
void IRC_REGISTER(char *nick, char *ident, char *realname, irc_client_t *irc)
{
IRC_StartCapabilityNegotiation(irc);
IRC_NICK(nick, irc);
IRC_USER(ident, realname, irc);
}
void IRC_StartCapabilityNegotiation(irc_client_t *irc)
{
NET_Send(irc->sockfd, "CAP LS 302");
}
void IRC_RequestCapabilities(char *caps, irc_client_t *irc)
{
char buf[512];
snprintf(buf, sizeof buf, "CAP REQ :%s", caps);
NET_Send(irc->sockfd, buf);
}
void IRC_NeedCapabilities(char *caps, irc_client_t *irc)
{
snprintf(irc->requestedCapabilities, sizeof irc->requestedCapabilities, "%s", caps);
}
void IRC_EndCapabilityNegotiation(irc_client_t *irc)
{
NET_Send(irc->sockfd, "CAP END");
}

View File

@@ -1,3 +1,4 @@
#include <openssl/ssl.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
@@ -8,53 +9,63 @@
#include <netinet/in.h>
#include <sys/select.h>
#include <stdbool.h>
#include <fcntl.h>
#include "IRC.h"
#include "IRC/IRC.h"
#include "IRC/IRC_structs.h"
#include "defines.h"
#include "netcode.h"
#include "credentials.h"
int main(int argc, char **argv)
{
int sockfd = NET_Connect("irc.libera.chat", 6667);
SSL_library_init();
OpenSSL_add_ssl_algorithms();
SSL_load_error_strings();
fd_set readfds;
static char linebuf[1026];
static size_t lineLen = 0;
char recvline[513];
char sendline[513];
irc_client_t irc;
irc.sockfd = sockfd;
IRC_Init(&irc);
IRC_SetConnectionParameters("irc.libera.chat", 6697, IRC_CONNECTION_TLS, &irc);
IRC_Connect(&irc);
IRC_SetIdentityParameters("BareBonesDude", "bbirc", "A BareBones IRC (https://gitea.codersquack.nl/thorium1256/bbirc) test...", &irc);
IRC_SetSASLParameters(SASL_MECHANISM_PLAIN, CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD, &irc);
IRC_Register( &irc);
if(sockfd >= 0)
{
NET_Send(sockfd, "NICK BareBonesDude\r\n");
NET_Send(sockfd, "USER BareBonesDude 0 * :The Postal Dudesksleton\r\n");
}
else
{
return 1;
}
while(1)
{
FD_ZERO(&readfds);
FD_SET(STDIN_FILENO, &readfds);
FD_SET(sockfd, &readfds);
FD_SET(irc.connection.sockfd, &readfds);
int maxfd = (sockfd > 0) ? sockfd + 1 : 1;
int maxfd = (irc.connection.sockfd > 0) ? irc.connection.sockfd + 1 : 1;
select(maxfd, &readfds, NULL, NULL, NULL);
if(FD_ISSET(STDIN_FILENO, &readfds))
{
fgets(sendline, sizeof(sendline), stdin);
NET_Send(sockfd, sendline);
IRC_Send(&irc, sendline);
}
if(FD_ISSET(sockfd, &readfds))
if(FD_ISSET(irc.connection.sockfd, &readfds))
{
ssize_t n = recv(sockfd, recvline, sizeof(recvline), 0);
// ssize_t n = recv(irc.connection.sockfd, recvline, sizeof(recvline), 0);
ssize_t n = IRC_Receive(&irc, recvline, sizeof(recvline));
if (n == -2) {
continue;
}
if(n <= 0) break;
recvline[n] = NUL;
@@ -72,24 +83,32 @@ int main(int argc, char **argv)
irc_message_t msg;
IRC_ParseMessage(linebuf, &msg);
IRC_ProcessMessage(&msg, &irc);
// try to print it
if(msg.source != NULL)
{
printf("%s: %s ", msg.source, msg.command);
printf("< %s: %s ", msg.source, msg.command);
}
else
{
printf("Server: %s ", msg.command);
printf("< %s ", msg.command);
}
for(int i = 0; i < msg.argc; i++)
{
printf("%s ", msg.argv[i]);
if(i == msg.argc-1)
{
printf("%s\n", msg.argv[i]);
}
else
{
printf("%s ", msg.argv[i]);
}
}
IRC_ProcessMessage(&msg, &irc);
// try to print it
lineLen = 0;
}
@@ -103,7 +122,8 @@ int main(int argc, char **argv)
printf("Connection closed by foreign host.\n");
NET_Close(sockfd);
IRC_Close(&irc);
IRC_Free(&irc);
return 0;
}

View File

@@ -1,5 +1,8 @@
#include "netcode.h"
#include "print.h"
#include <openssl/crypto.h>
#include <openssl/err.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
@@ -8,9 +11,12 @@
#include <netdb.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <openssl/ssl.h>
#include <fcntl.h>
int NET_Connect(const char *host, int port)
sslSockfd_t NET_Connect(const char *host, int port)
{
sslSockfd_t sock = {NULL, -1, NULL};
int status;
struct addrinfo hints, *res;
@@ -21,22 +27,26 @@ int NET_Connect(const char *host, int port)
// save us from user or programmer stupidity
if(!host)
{
return -1;
return sock;
}
char portNumber[6];
snprintf(portNumber, 6, "%d", port);
DEFRAWT();
PRINT_INFO(timeinfo);
printf("Looking up %s...\n", host);
if ((status = getaddrinfo(host, portNumber, &hints, &res)) == -1)
{
PRINT_INFO(timeinfo);
fprintf(stderr, "Unable to lookup %s: %s\n", host, gai_strerror(status));
return -1;
return sock;
}
char ipstr[INET6_ADDRSTRLEN];
int sockfd = -1;
for(struct addrinfo *p = res; p != NULL; p = p->ai_next)
{
@@ -56,9 +66,11 @@ int NET_Connect(const char *host, int port)
}
inet_ntop(p->ai_family, addr, ipstr, sizeof ipstr);
printf("Connecting to %s (%s)...\n", host, ipstr);
REDEFRAWT();
PRINT_INFO(timeinfo);
printf("Connecting to %s:%d (%s:%d)...\n", host, port, ipstr, port);
if((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
if((sock.sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1)
{
char perrorMsg[sizeof("Failed to connect to ") + sizeof ipstr];
snprintf(perrorMsg, sizeof perrorMsg, "Failed to connect to %s", ipstr);
@@ -66,37 +78,34 @@ int NET_Connect(const char *host, int port)
continue;
}
if((connect(sockfd, p->ai_addr, p->ai_addrlen)) == -1)
if((connect(sock.sockfd, p->ai_addr, p->ai_addrlen)) == -1)
{
close(sockfd);
close(sock.sockfd);
char perrorMsg[sizeof("Failed to connect to ") + sizeof ipstr];
snprintf(perrorMsg, sizeof perrorMsg, "Failed to connect to %s", ipstr);
perror(perrorMsg);
continue;
}
REDEFRAWT();
PRINT_INFO(timeinfo);
printf("Connected!\n");
break;
}
if(sockfd >= 0)
{
return sockfd;
}
else
{
return -1;
}
return sock;
}
void NET_Send(int sockfd, const char *toSend)
void NET_Send(sslSockfd_t info, const char *toSend)
{
size_t bytesSent = 0;
size_t sendLen = strlen(toSend);
// printf("> %s", toSend);
while(bytesSent < sendLen)
{
ssize_t sent = send(sockfd, toSend, sendLen - bytesSent, 0);
ssize_t sent = send(info.sockfd, toSend, sendLen - bytesSent, 0);
if(sent == -1)
{
perror("NET_Send");
@@ -105,7 +114,7 @@ void NET_Send(int sockfd, const char *toSend)
if(sent == 0)
{
fprintf(stderr, "NET_Send: connection closed");
fprintf(stderr, "NET_Send: connection closed\n");
return;
}
@@ -113,11 +122,138 @@ void NET_Send(int sockfd, const char *toSend)
}
}
void NET_Close(int sockfd)
void NET_Close(sslSockfd_t info)
{
if(sockfd >= 0)
if(info.sockfd >= 0)
{
shutdown(sockfd, SHUT_RDWR);
close(sockfd);
shutdown(info.sockfd, SHUT_RDWR);
close(info.sockfd);
}
}
}
sslSockfd_t NET_TLS_Connect(const char *host, int port)
{
sslSockfd_t result;
result = NET_Connect(host, port);
if(result.sockfd < 0) return result;
int flags = fcntl(result.sockfd, F_GETFL, 0);
fcntl(result.sockfd, F_SETFL, flags | O_NONBLOCK);
// const char *const CIPHER_LIST =
// "ECDHE-ECDSA-AES128-GCM-SHA256:"
// "ECDHE-RSA-AES128-GCM-SHA256:"
// "ECDHE-ECDSA-AES256-GCM-SHA384:"
// "ECDHE-RSA-AES256-GCM-SHA384:"
// "ECDHE-ECDSA-CHACHA20-POLY1305:"
// "ECDHE-RSA-CHACHA20-POLY1305:"
// "DHE-RSA-AES128-GCM-SHA256:"
// "DHE-RSA-AES256-GCM-SHA384:"
// "DHE-RSA-CHACHA20-POLY1305";
// if (SSL_CTX_set_cipher_list(result.ctx, CIPHER_LIST) != 1) {
// fprintf(stderr, "Failed to set cipher list\n");
// exit(1);
// }
result.ctx = SSL_CTX_new(TLS_client_method());
if (!result.ctx) return result;
// SSL_CTX_set_verify(result.ctx, SSL_VERIFY_NONE, NULL);
// SSL_CTX_set_min_proto_version(result.ctx, TLS1_2_VERSION);
// SSL_CTX_set_max_proto_version(result.ctx, TLS1_2_VERSION);
// SSL_CTX_set_options(result.ctx, SSL_OP_NO_SSLv2 | SSL_OP_NO_SSLv3 | SSL_OP_NO_COMPRESSION);
// SSL_CTX_set_mode(result.ctx, SSL_MODE_AUTO_RETRY);
result.ssl = SSL_new(result.ctx);
if(!result.ssl)
{
SSL_CTX_free(result.ctx);
return result;
}
// SSL_set_tlsext_host_name(result.ssl, host);
SSL_set_fd(result.ssl, result.sockfd);
SSL_set_connect_state(result.ssl);
int ret;
while((ret = SSL_connect(result.ssl)) != 1)
{
int err = SSL_get_error(result.ssl, ret);
if(err == SSL_ERROR_WANT_READ)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(result.sockfd, &fds);
select(result.sockfd + 1, &fds, NULL, NULL, NULL);
continue;
}
else if (err == SSL_ERROR_WANT_WRITE)
{
fd_set fds;
FD_ZERO(&fds);
FD_SET(result.sockfd, &fds);
select(result.sockfd + 1, NULL, &fds, NULL, NULL);
continue;
}
else
{
SSL_free(result.ssl);
SSL_CTX_free(result.ctx);
result.ssl = NULL;
result.ctx = NULL;
result.sockfd = -1;
return result;
}
}
return result;
}
void NET_TLS_Send(sslSockfd_t info, const char* toSend)
{
size_t bytesSent = 0;
size_t sendLen = strlen(toSend);
printf("> %s", toSend);
while(bytesSent < sendLen)
{
ssize_t sent = SSL_write(info.ssl, toSend + bytesSent, sendLen - bytesSent);
if(sent <= 0)
{
int err = SSL_get_error(info.ssl, sent);
if (err == SSL_ERROR_WANT_WRITE) {
usleep(1000);
continue;
} else if (err == SSL_ERROR_WANT_READ) {
usleep(1000);
continue;
} else {
unsigned long errcode = ERR_get_error();
char errbuf[256];
ERR_error_string_n(errcode, errbuf, sizeof(errbuf));
fprintf(stderr, "NET_TLS_Send: SSL_write error: %s\n", errbuf);
return;
}
}
bytesSent += sent;
}
}
void NET_TLS_Close(sslSockfd_t info)
{
if (info.ssl) {
SSL_shutdown(info.ssl);
SSL_free(info.ssl);
SSL_CTX_free(info.ctx);
}
if (info.sockfd >= 0) {
close(info.sockfd);
}
}

BIN
test

Binary file not shown.