Compare commits
11 Commits
3dba55ebb8
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
167da3dd0e | ||
|
|
ff0f10295a | ||
|
|
eafa4070e1 | ||
|
|
fda94c09b9 | ||
|
|
09017b581d | ||
|
|
da3aef1468 | ||
|
|
634366a875 | ||
|
|
672b541cc1 | ||
|
|
f0275a67bb | ||
|
|
1b896f3dd7 | ||
|
|
97fefe63e5 |
7
.gitignore
vendored
7
.gitignore
vendored
@@ -0,0 +1,7 @@
|
|||||||
|
build
|
||||||
|
.vscode
|
||||||
|
bbirc-debug
|
||||||
|
bbirc
|
||||||
|
tests
|
||||||
|
compile_commands.json
|
||||||
|
privinclude
|
||||||
3
.gitmodules
vendored
Normal file
3
.gitmodules
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
[submodule "libircc"]
|
||||||
|
path = libircc
|
||||||
|
url = https://gitea.codersquack.nl/thorium1256/libircc.git
|
||||||
37
Makefile
37
Makefile
@@ -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
|
BUILD_DIR := build
|
||||||
SRC_DIR := src
|
SRC_DIR := src
|
||||||
INCLUDE_DIR := include
|
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 := bbirc
|
||||||
APPLICATION_DBG := bbirc-debug
|
APPLICATION_DBG := bbirc-debug
|
||||||
|
|
||||||
$(shell mkdir -p $(BUILD_DIR))
|
$(shell mkdir -p $(BUILD_DIR))
|
||||||
SRCS := $(wildcard $(SRC_DIR)/*.c)
|
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))
|
DBG_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/dbg_%.o,$(SRCS))
|
||||||
REL_OBJS := $(patsubst $(SRC_DIR)/%.c,$(BUILD_DIR)/rel_%.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
|
all: release
|
||||||
|
|
||||||
debug: $(APPLICATION_DBG)
|
debug: ircc $(APPLICATION_DBG)
|
||||||
release: $(APPLICATION)
|
release: ircc $(APPLICATION)
|
||||||
both: debug release
|
both: debug release
|
||||||
|
ircc: $(LIBIRCC_LIB)
|
||||||
|
|
||||||
$(APPLICATION): $(REL_OBJS)
|
$(APPLICATION): $(REL_OBJS)
|
||||||
$(CC) -o $@ $^ $(LDFLAGS_REL)
|
$(CC) -o $@ $^ $(LDFLAGS_REL)
|
||||||
|
|
||||||
$(APPLICATION_DBG): $(DBG_OBJS)
|
$(APPLICATION_DBG): $(DBG_OBJS)
|
||||||
|
+$(MAKE) -C $(LIBIRCC_DIR)
|
||||||
$(CC) -o $@ $^ $(LDFLAGS)
|
$(CC) -o $@ $^ $(LDFLAGS)
|
||||||
|
|
||||||
$(BUILD_DIR)/dbg_%.o: $(SRC_DIR)/%.c
|
$(BUILD_DIR)/dbg_%.o: $(SRC_DIR)/%.c
|
||||||
$(CC) -c -o $@ $^ $(CFLAGS_DBG)
|
$(CC) -c -o $@ $< $(CFLAGS_DBG)
|
||||||
|
|
||||||
$(BUILD_DIR)/rel_%.o: $(SRC_DIR)/%.c
|
$(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:
|
clean:
|
||||||
rm -rf $(BUILD_DIR)
|
rm -rf $(BUILD_DIR)
|
||||||
rm -rf $(APPLICATION)
|
rm -rf $(APPLICATION)
|
||||||
rm -rf $(APPLICATION_DBG)
|
rm -rf $(APPLICATION_DBG)
|
||||||
|
+$(MAKE) -C $(LIBIRCC_DIR) clean
|
||||||
8
TODO.md
8
TODO.md
@@ -1,5 +1,7 @@
|
|||||||
# To-Do
|
# To-Do
|
||||||
|
|
||||||
1. Add actual IRC client functionality
|
1. ~~Finish the netcode~~
|
||||||
2. Finish the netcode
|
2. ~~Add SASL PLAIN authentication~~
|
||||||
3. Don't care about the looks of it yet, just have an ANSI command line or something
|
3. Add SASL EXTERNAL authentication
|
||||||
|
4. Add a line text user interface.
|
||||||
|
4. Add an event polling system
|
||||||
@@ -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"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -1,39 +0,0 @@
|
|||||||
#ifndef IRC_H
|
|
||||||
#define IRC_H
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
char *nickname;
|
|
||||||
int nicklen;
|
|
||||||
char *ident;
|
|
||||||
int identlen;
|
|
||||||
char *hostname;
|
|
||||||
int hostlen;
|
|
||||||
|
|
||||||
} irc_hostmask_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
char line[513];
|
|
||||||
char *source;
|
|
||||||
char *command;
|
|
||||||
char *argv[16];
|
|
||||||
int argc;
|
|
||||||
} irc_message_t;
|
|
||||||
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
int sockfd;
|
|
||||||
irc_hostmask_t ownHostmask;
|
|
||||||
} irc_client_t;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const char *name;
|
|
||||||
void (*handler)(irc_message_t *msg, irc_client_t *irc);
|
|
||||||
} command_t;
|
|
||||||
|
|
||||||
int IRC_ParseMessage(char* line, irc_message_t *msg);
|
|
||||||
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc);
|
|
||||||
|
|
||||||
void IRC_PRIVMSG(char* msg, irc_client_t irc);
|
|
||||||
|
|
||||||
#endif
|
|
||||||
47
include/IRC/IRC.h
Normal file
47
include/IRC/IRC.h
Normal 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
17
include/IRC/IRC_events.h
Normal 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
|
||||||
123
include/IRC/IRC_structs.h
Normal file
123
include/IRC/IRC_structs.h
Normal file
@@ -0,0 +1,123 @@
|
|||||||
|
#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
|
||||||
|
{
|
||||||
|
SASL_MECHANISM_NONE,
|
||||||
|
SASL_MECHANISM_PLAIN,
|
||||||
|
SASL_MECHANISM_EXTERNAL
|
||||||
|
} irc_sasl_mechanism_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char nickname[32];
|
||||||
|
char ident[16];
|
||||||
|
char hostname;
|
||||||
|
} irc_hostmask_t;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
char line[513];
|
||||||
|
char *source;
|
||||||
|
char *command;
|
||||||
|
char *argv[16];
|
||||||
|
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;
|
||||||
|
char prefix;
|
||||||
|
} irc_chanuser_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char name[64];
|
||||||
|
char topic[513];
|
||||||
|
irc_chanuser_t* users;
|
||||||
|
} irc_channel_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char capLine[96]; // A place for name and args to point to.
|
||||||
|
char *name;
|
||||||
|
char *args;
|
||||||
|
} irc_capability_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char username[32];
|
||||||
|
char password[64];
|
||||||
|
} irc_sasl_plain_credentials_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
char nickname[32];
|
||||||
|
unsigned char fingerprint[20];
|
||||||
|
} irc_sasl_external_credentials_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
irc_sasl_mechanism_t mechanism;
|
||||||
|
union {
|
||||||
|
irc_sasl_plain_credentials_t plain_credentials;
|
||||||
|
irc_sasl_external_credentials_t external_credentials;
|
||||||
|
};
|
||||||
|
} irc_auth_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
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 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;
|
||||||
|
void (*handler)(irc_message_t *msg, irc_client_t *irc);
|
||||||
|
} command_t;
|
||||||
|
|
||||||
|
#endif
|
||||||
9
include/base64.h
Normal file
9
include/base64.h
Normal 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
|
||||||
@@ -1,9 +1,24 @@
|
|||||||
#ifndef NETCODE_H
|
#ifndef NETCODE_H
|
||||||
#define 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.
|
// Returns a socket file descriptor.
|
||||||
int NET_Connect(const char* host, int port);
|
sslSockfd_t NET_Connect(const char* host, int port);
|
||||||
void NET_Send(int sockfd, const char* toSend);
|
void NET_Send(sslSockfd_t info, const char* toSend);
|
||||||
void NET_Close(int sockfd);
|
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
|
#endif
|
||||||
19
include/print.h
Normal file
19
include/print.h
Normal 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
1
libircc
Submodule
Submodule libircc added at 7be1ef312e
92
src/IRC.c
92
src/IRC.c
@@ -1,92 +0,0 @@
|
|||||||
#include "IRC.h"
|
|
||||||
#include "defines.h"
|
|
||||||
#include "netcode.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
int 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++;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handlePing(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[128];
|
|
||||||
snprintf(buf, sizeof buf, "PONG :%s", msg->argv[0]);
|
|
||||||
NET_Send(irc->sockfd, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_ProcessMessage(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
command_t commands[] =
|
|
||||||
{
|
|
||||||
{"PING", handlePing},
|
|
||||||
{NULL, NULL}
|
|
||||||
};
|
|
||||||
|
|
||||||
for(int i = 0; commands[i].name; i++)
|
|
||||||
{
|
|
||||||
if(strcmp(msg->command, commands[i].name) == 0)
|
|
||||||
{
|
|
||||||
commands[i].handler(msg, irc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
66
src/main.c
66
src/main.c
@@ -1,3 +1,4 @@
|
|||||||
|
#include <openssl/ssl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
@@ -8,14 +9,21 @@
|
|||||||
#include <netinet/in.h>
|
#include <netinet/in.h>
|
||||||
#include <sys/select.h>
|
#include <sys/select.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "IRC.h"
|
#include "IRC/IRC.h"
|
||||||
|
#include "IRC/IRC_structs.h"
|
||||||
#include "defines.h"
|
#include "defines.h"
|
||||||
#include "netcode.h"
|
#include "netcode.h"
|
||||||
|
|
||||||
|
#include "credentials.h"
|
||||||
|
|
||||||
|
|
||||||
int main(int argc, char **argv)
|
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;
|
fd_set readfds;
|
||||||
static char linebuf[1026];
|
static char linebuf[1026];
|
||||||
@@ -24,37 +32,40 @@ int main(int argc, char **argv)
|
|||||||
char sendline[513];
|
char sendline[513];
|
||||||
|
|
||||||
irc_client_t irc;
|
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)
|
while(1)
|
||||||
{
|
{
|
||||||
FD_ZERO(&readfds);
|
FD_ZERO(&readfds);
|
||||||
FD_SET(STDIN_FILENO, &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);
|
select(maxfd, &readfds, NULL, NULL, NULL);
|
||||||
|
|
||||||
if(FD_ISSET(STDIN_FILENO, &readfds))
|
if(FD_ISSET(STDIN_FILENO, &readfds))
|
||||||
{
|
{
|
||||||
fgets(sendline, sizeof(sendline), stdin);
|
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;
|
if(n <= 0) break;
|
||||||
recvline[n] = NUL;
|
recvline[n] = NUL;
|
||||||
@@ -72,21 +83,31 @@ int main(int argc, char **argv)
|
|||||||
irc_message_t msg;
|
irc_message_t msg;
|
||||||
IRC_ParseMessage(linebuf, &msg);
|
IRC_ParseMessage(linebuf, &msg);
|
||||||
|
|
||||||
// try to print it
|
|
||||||
|
|
||||||
if(msg.source != NULL)
|
if(msg.source != NULL)
|
||||||
{
|
{
|
||||||
printf("%s: %s ", msg.source, msg.command);
|
printf("< %s: %s ", msg.source, msg.command);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Server: %s ", msg.command);
|
printf("< %s ", msg.command);
|
||||||
}
|
}
|
||||||
|
|
||||||
for(int i = 0; i < msg.argc; i++)
|
for(int i = 0; i < msg.argc; i++)
|
||||||
|
{
|
||||||
|
if(i == msg.argc-1)
|
||||||
|
{
|
||||||
|
printf("%s\n", msg.argv[i]);
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
printf("%s ", msg.argv[i]);
|
printf("%s ", msg.argv[i]);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
IRC_ProcessMessage(&msg, &irc);
|
||||||
|
|
||||||
|
// try to print it
|
||||||
|
|
||||||
lineLen = 0;
|
lineLen = 0;
|
||||||
}
|
}
|
||||||
@@ -101,5 +122,8 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
printf("Connection closed by foreign host.\n");
|
printf("Connection closed by foreign host.\n");
|
||||||
|
|
||||||
|
IRC_Close(&irc);
|
||||||
|
IRC_Free(&irc);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
182
src/netcode.c
182
src/netcode.c
@@ -1,5 +1,8 @@
|
|||||||
#include "netcode.h"
|
#include "netcode.h"
|
||||||
|
#include "print.h"
|
||||||
|
|
||||||
|
#include <openssl/crypto.h>
|
||||||
|
#include <openssl/err.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -8,9 +11,12 @@
|
|||||||
#include <netdb.h>
|
#include <netdb.h>
|
||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <netinet/in.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;
|
int status;
|
||||||
struct addrinfo hints, *res;
|
struct addrinfo hints, *res;
|
||||||
|
|
||||||
@@ -21,22 +27,26 @@ int NET_Connect(const char *host, int port)
|
|||||||
// save us from user or programmer stupidity
|
// save us from user or programmer stupidity
|
||||||
if(!host)
|
if(!host)
|
||||||
{
|
{
|
||||||
return -1;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
char portNumber[6];
|
char portNumber[6];
|
||||||
snprintf(portNumber, 6, "%d", port);
|
snprintf(portNumber, 6, "%d", port);
|
||||||
|
|
||||||
|
DEFRAWT();
|
||||||
|
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
|
|
||||||
printf("Looking up %s...\n", host);
|
printf("Looking up %s...\n", host);
|
||||||
|
|
||||||
if ((status = getaddrinfo(host, portNumber, &hints, &res)) == -1)
|
if ((status = getaddrinfo(host, portNumber, &hints, &res)) == -1)
|
||||||
{
|
{
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
fprintf(stderr, "Unable to lookup %s: %s\n", host, gai_strerror(status));
|
fprintf(stderr, "Unable to lookup %s: %s\n", host, gai_strerror(status));
|
||||||
return -1;
|
return sock;
|
||||||
}
|
}
|
||||||
|
|
||||||
char ipstr[INET6_ADDRSTRLEN];
|
char ipstr[INET6_ADDRSTRLEN];
|
||||||
int sockfd = -1;
|
|
||||||
|
|
||||||
for(struct addrinfo *p = res; p != NULL; p = p->ai_next)
|
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);
|
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];
|
char perrorMsg[sizeof("Failed to connect to ") + sizeof ipstr];
|
||||||
snprintf(perrorMsg, sizeof perrorMsg, "Failed to connect to %s", ipstr);
|
snprintf(perrorMsg, sizeof perrorMsg, "Failed to connect to %s", ipstr);
|
||||||
@@ -66,37 +78,34 @@ int NET_Connect(const char *host, int port)
|
|||||||
continue;
|
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];
|
char perrorMsg[sizeof("Failed to connect to ") + sizeof ipstr];
|
||||||
snprintf(perrorMsg, sizeof perrorMsg, "Failed to connect to %s", ipstr);
|
snprintf(perrorMsg, sizeof perrorMsg, "Failed to connect to %s", ipstr);
|
||||||
perror(perrorMsg);
|
perror(perrorMsg);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
REDEFRAWT();
|
||||||
|
PRINT_INFO(timeinfo);
|
||||||
printf("Connected!\n");
|
printf("Connected!\n");
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(sockfd >= 0)
|
return sock;
|
||||||
{
|
|
||||||
return sockfd;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void NET_Send(int sockfd, const char *toSend)
|
void NET_Send(sslSockfd_t info, const char *toSend)
|
||||||
{
|
{
|
||||||
size_t bytesSent = 0;
|
size_t bytesSent = 0;
|
||||||
size_t sendLen = strlen(toSend);
|
size_t sendLen = strlen(toSend);
|
||||||
|
|
||||||
|
// printf("> %s", toSend);
|
||||||
|
|
||||||
while(bytesSent < sendLen)
|
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)
|
if(sent == -1)
|
||||||
{
|
{
|
||||||
perror("NET_Send");
|
perror("NET_Send");
|
||||||
@@ -105,7 +114,7 @@ void NET_Send(int sockfd, const char *toSend)
|
|||||||
|
|
||||||
if(sent == 0)
|
if(sent == 0)
|
||||||
{
|
{
|
||||||
fprintf(stderr, "NET_Send: connection closed");
|
fprintf(stderr, "NET_Send: connection closed\n");
|
||||||
return;
|
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);
|
shutdown(info.sockfd, SHUT_RDWR);
|
||||||
close(sockfd);
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user