Compare commits
5 Commits
da3aef1468
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
167da3dd0e | ||
|
|
ff0f10295a | ||
|
|
eafa4070e1 | ||
|
|
fda94c09b9 | ||
|
|
09017b581d |
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
|
||||||
24
Makefile
24
Makefile
@@ -3,12 +3,15 @@ SRC_DIR := src
|
|||||||
INCLUDE_DIR := include
|
INCLUDE_DIR := include
|
||||||
PRIVATE_INCLUDE_DIR := privinclude
|
PRIVATE_INCLUDE_DIR := privinclude
|
||||||
|
|
||||||
|
LIBIRCC_DIR := libircc
|
||||||
|
LIBIRCC_LIB := $(LIBIRCC_DIR)/libircc.so
|
||||||
|
|
||||||
CC := clang
|
CC := clang
|
||||||
CFLAGS := -I $(INCLUDE_DIR) -I $(PRIVATE_INCLUDE_DIR) -Wall -Wextra
|
CFLAGS := -I$(INCLUDE_DIR) -I$(PRIVATE_INCLUDE_DIR) -I$(LIBIRCC_DIR)/include -Wall -Wextra
|
||||||
CFLAGS_DBG := $(CFLAGS) -g -O0
|
CFLAGS_DBG := $(CFLAGS) -g -O0
|
||||||
CFLAGS_REL := $(CFLAGS) -O2
|
CFLAGS_REL := $(CFLAGS) -O2
|
||||||
|
|
||||||
LDFLAGS :=
|
LDFLAGS := -L./libircc -Wl,-rpath,'$$ORIGIN/libircc' -lircc -lssl -lcrypto
|
||||||
LDFLAGS_REL := $(LDFLAGS) -s
|
LDFLAGS_REL := $(LDFLAGS) -s
|
||||||
|
|
||||||
APPLICATION := bbirc
|
APPLICATION := bbirc
|
||||||
@@ -16,30 +19,37 @@ 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,78 +0,0 @@
|
|||||||
[
|
|
||||||
{
|
|
||||||
"file": "src/IRC.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_IRC.o",
|
|
||||||
"src/IRC.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_IRC.o"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"file": "src/base64.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_base64.o",
|
|
||||||
"src/base64.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_base64.o"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"file": "src/main.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_main.o",
|
|
||||||
"src/main.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_main.o"
|
|
||||||
},
|
|
||||||
{
|
|
||||||
"file": "src/netcode.c",
|
|
||||||
"arguments": [
|
|
||||||
"/usr/bin/clang",
|
|
||||||
"-c",
|
|
||||||
"-o",
|
|
||||||
"build/rel_netcode.o",
|
|
||||||
"src/netcode.c",
|
|
||||||
"-I",
|
|
||||||
"include",
|
|
||||||
"-I",
|
|
||||||
"privinclude",
|
|
||||||
"-Wall",
|
|
||||||
"-Wextra",
|
|
||||||
"-O2"
|
|
||||||
],
|
|
||||||
"directory": "/home/neon1246/Documents/stuff/code/c/ircclient",
|
|
||||||
"output": "build/rel_netcode.o"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
@@ -13,8 +13,12 @@ void IRC_NOTICE(char* to, char* msg, irc_client_t *irc);
|
|||||||
void IRC_NICK(char *nick, 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_USER(char *ident, char* realname, irc_client_t *irc);
|
||||||
|
|
||||||
|
void IRC_CTCPSend(char *to, char *cmd, irc_client_t *irc);
|
||||||
|
|
||||||
// registration
|
// registration
|
||||||
void IRC_Register(char *nick, char *username, char *realname, irc_client_t *irc);
|
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_SetSASLParameters(irc_sasl_mechanism_t, char* username, char* password, irc_client_t *irc);
|
||||||
|
|
||||||
void IRC_RequestSASLAuthentication(irc_client_t *irc);
|
void IRC_RequestSASLAuthentication(irc_client_t *irc);
|
||||||
@@ -26,7 +30,18 @@ void IRC_EndCapabilityNegotiation(irc_client_t *irc);
|
|||||||
|
|
||||||
void IRC_NeedCapabilities(char* caps, irc_client_t *irc);
|
void IRC_NeedCapabilities(char* caps, irc_client_t *irc);
|
||||||
|
|
||||||
|
|
||||||
int IRC_ParseCapabilities(char* caps, irc_capability_t *capabilities, int capsLength);
|
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
|
#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
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
#ifndef IRC_STRUCTS_H
|
#ifndef IRC_STRUCTS_H
|
||||||
#define IRC_STRUCTS_H
|
#define IRC_STRUCTS_H
|
||||||
|
|
||||||
|
#include "IRC/IRC_events.h"
|
||||||
|
#include "netcode.h"
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <openssl/ssl.h>
|
||||||
|
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
@@ -25,6 +28,12 @@ typedef struct {
|
|||||||
int argc;
|
int argc;
|
||||||
} irc_message_t;
|
} irc_message_t;
|
||||||
|
|
||||||
|
typedef struct
|
||||||
|
{
|
||||||
|
irc_event_type_t type;
|
||||||
|
irc_message_t orig_msg;
|
||||||
|
} irc_event_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
irc_hostmask_t hostmask;
|
irc_hostmask_t hostmask;
|
||||||
@@ -34,7 +43,7 @@ typedef struct
|
|||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char name[64];
|
char name[64];
|
||||||
char topic[512];
|
char topic[513];
|
||||||
irc_chanuser_t* users;
|
irc_chanuser_t* users;
|
||||||
} irc_channel_t;
|
} irc_channel_t;
|
||||||
|
|
||||||
@@ -68,22 +77,43 @@ typedef struct
|
|||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int sockfd;
|
char nick[32];
|
||||||
irc_hostmask_t ownHostmask;
|
char altnick[32];
|
||||||
char supportedCapabilities[512];
|
char altnick2[32];
|
||||||
char requestedCapabilities[512];
|
char username[16];
|
||||||
irc_capability_t acked[24];
|
char realname[128];
|
||||||
|
} irc_identity_t;
|
||||||
/* Authentication parameters */
|
typedef enum
|
||||||
irc_auth_t saslAuth;
|
{
|
||||||
} irc_client_t;
|
IRC_CONNECTION_PLAINTEXT,
|
||||||
|
IRC_CONNECTION_TLS
|
||||||
|
} irc_connection_type_t;
|
||||||
|
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
char nick[32];
|
char host[128];
|
||||||
char ident[16];
|
int port;
|
||||||
char realname[64];
|
irc_connection_type_t connType;
|
||||||
} irc_identity_t;
|
} 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 {
|
typedef struct {
|
||||||
const char *name;
|
const char *name;
|
||||||
|
|||||||
@@ -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
360
src/IRC.c
360
src/IRC.c
@@ -1,360 +0,0 @@
|
|||||||
#include "IRC/IRC_numerics.h"
|
|
||||||
#include "IRC/IRC_structs.h"
|
|
||||||
#include "defines.h"
|
|
||||||
#include "netcode.h"
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <string.h>
|
|
||||||
#include <sys/socket.h>
|
|
||||||
|
|
||||||
#include "IRC/IRC.h"
|
|
||||||
#include "base64.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++;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (msg->argc > 0) {
|
|
||||||
char *last = msg->argv[msg->argc - 1];
|
|
||||||
size_t len = strlen(last);
|
|
||||||
while (len > 0 && (last[len-1] == '\r' || last[len-1] == '\n')) {
|
|
||||||
last[len-1] = '\0';
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns the number of capabilities found.
|
|
||||||
int IRC_ParseCapabilities(char *caps, irc_capability_t *capabilities, int capsLength)
|
|
||||||
{
|
|
||||||
while (*caps == ' ') caps++;
|
|
||||||
|
|
||||||
size_t len = strlen(caps);
|
|
||||||
while (len > 0 && (caps[len-1] == '\n' || caps[len-1] == '\r' || caps[len-1] == ' ')) {
|
|
||||||
caps[len-1] = '\0';
|
|
||||||
len--;
|
|
||||||
}
|
|
||||||
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\r\n", 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[1], "LS") == 0)
|
|
||||||
{
|
|
||||||
snprintf(irc->supportedCapabilities, sizeof irc->supportedCapabilities, "%s", msg->argv[2]);
|
|
||||||
|
|
||||||
// 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)
|
|
||||||
{
|
|
||||||
bytesWritten += snprintf(buf + bytesWritten, sizeof(buf) - bytesWritten, "%s ", reqCaps[j].name);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
IRC_RequestCapabilities(buf, irc);
|
|
||||||
}
|
|
||||||
else if (strcmp(msg->argv[1], "ACK") == 0)
|
|
||||||
{
|
|
||||||
int ackedCount = IRC_ParseCapabilities(msg->argv[2], irc->acked, sizeof(irc->acked) / sizeof(irc_capability_t));
|
|
||||||
|
|
||||||
for(int i = 0; i < ackedCount; i++)
|
|
||||||
{
|
|
||||||
printf("%s = %s\n", irc->acked[i].name, irc->acked[i].args ? irc->acked[i].args : "(none)");
|
|
||||||
if(strcmp(irc->acked[i].name, "sasl") == 0)
|
|
||||||
{
|
|
||||||
IRC_RequestSASLAuthentication(irc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// NAK is a no-op anyway, if it isn't in our irc->acked we just won't use it then.
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handleAuthentication(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
if(strcmp(msg->argv[0], "+") == 0)
|
|
||||||
{
|
|
||||||
// send our base64 sasl username and password
|
|
||||||
switch(irc->saslAuth.mechanism)
|
|
||||||
{
|
|
||||||
case SASL_MECHANISM_NONE:
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_PLAIN:
|
|
||||||
{
|
|
||||||
const char *user = irc->saslAuth.plain_credentials.username;
|
|
||||||
const char *pass = irc->saslAuth.plain_credentials.password;
|
|
||||||
size_t user_len = strlen(user);
|
|
||||||
size_t pass_len = strlen(pass);
|
|
||||||
|
|
||||||
uint8_t sasl_data[user_len + pass_len + 3];
|
|
||||||
size_t idx = 0;
|
|
||||||
|
|
||||||
sasl_data[idx++] = 0;
|
|
||||||
memcpy(sasl_data + idx, user, user_len);
|
|
||||||
idx += user_len;
|
|
||||||
sasl_data[idx++] = 0;
|
|
||||||
memcpy(sasl_data + idx, pass, pass_len);
|
|
||||||
idx += pass_len;
|
|
||||||
|
|
||||||
char base64Out[512];
|
|
||||||
base64_encode(sasl_data, idx, base64Out, sizeof(base64Out));
|
|
||||||
char buf[512];
|
|
||||||
snprintf(buf, sizeof buf, "AUTHENTICATE %s\r\n", base64Out);
|
|
||||||
NET_Send(irc->sockfd, buf);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SASL_MECHANISM_EXTERNAL:
|
|
||||||
{
|
|
||||||
NET_Send(irc->sockfd, "AUTHENTICATE +\r\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void handleSaslSuccess(irc_message_t *msg, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
IRC_EndCapabilityNegotiation(irc);
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
|
||||||
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},
|
|
||||||
{"AUTHENTICATE", handleAuthentication},
|
|
||||||
{RPL_SASLSUCCESS, handleSaslSuccess},
|
|
||||||
{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\r\n", 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\r\n", 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\r\n", 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\r\n", ident, realname);
|
|
||||||
NET_Send(irc->sockfd, buf);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_Register(char *nick, char *username, char *realname, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
if(irc->saslAuth.mechanism != SASL_MECHANISM_NONE)
|
|
||||||
{
|
|
||||||
IRC_NeedCapabilities("sasl", irc);
|
|
||||||
IRC_StartCapabilityNegotiation(irc);
|
|
||||||
}
|
|
||||||
IRC_NICK(nick, irc);
|
|
||||||
IRC_USER(username, realname, irc);
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_SetSASLParameters(irc_sasl_mechanism_t mechanism, char *username, char *password, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
switch(mechanism)
|
|
||||||
{
|
|
||||||
case SASL_MECHANISM_NONE:
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_EXTERNAL:
|
|
||||||
// unimplemented
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_PLAIN:
|
|
||||||
irc->saslAuth.mechanism = mechanism;
|
|
||||||
strncpy(irc->saslAuth.plain_credentials.username, username, sizeof(irc->saslAuth.plain_credentials.username) - 1);
|
|
||||||
strncpy(irc->saslAuth.plain_credentials.password, password, sizeof(irc->saslAuth.plain_credentials.password) - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_StartCapabilityNegotiation(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
NET_Send(irc->sockfd, "CAP LS 302\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_RequestCapabilities(char *caps, irc_client_t *irc)
|
|
||||||
{
|
|
||||||
char buf[512];
|
|
||||||
snprintf(buf, sizeof buf, "CAP REQ :%s\r\n", 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\r\n");
|
|
||||||
}
|
|
||||||
|
|
||||||
void IRC_RequestSASLAuthentication(irc_client_t *irc)
|
|
||||||
{
|
|
||||||
switch(irc->saslAuth.mechanism)
|
|
||||||
{
|
|
||||||
case SASL_MECHANISM_NONE:
|
|
||||||
// why?
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_PLAIN:
|
|
||||||
NET_Send(irc->sockfd, "AUTHENTICATE PLAIN\r\n");
|
|
||||||
break;
|
|
||||||
case SASL_MECHANISM_EXTERNAL:
|
|
||||||
NET_Send(irc->sockfd, "AUTHENTICATE EXTERNAL\r\n");
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
34
src/base64.c
34
src/base64.c
@@ -1,34 +0,0 @@
|
|||||||
#include <openssl/bio.h>
|
|
||||||
#include <openssl/evp.h>
|
|
||||||
#include <openssl/buffer.h>
|
|
||||||
#include "base64.h"
|
|
||||||
#include <string.h>
|
|
||||||
|
|
||||||
static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
|
||||||
int base64_encode(const uint8_t *in, size_t in_len, char *out, size_t out_size)
|
|
||||||
{
|
|
||||||
size_t needed = ((in_len + 2) / 3) * 4 + 1;
|
|
||||||
|
|
||||||
if(out_size < needed) return -1;
|
|
||||||
|
|
||||||
size_t i = 0, o = 0;
|
|
||||||
while(i < in_len)
|
|
||||||
{
|
|
||||||
uint32_t n = 0;
|
|
||||||
int remaining = 3;
|
|
||||||
|
|
||||||
n |= (i < in_len ? in[i++] : 0) << 16;
|
|
||||||
n |= (i < in_len ? in[i++] : 0) << 8;
|
|
||||||
n |= (i < in_len ? in[i++] : 0);
|
|
||||||
|
|
||||||
if(i > in_len) remaining = in_len % 3;
|
|
||||||
|
|
||||||
out[o++] = b64[(n >> 18) & 63];
|
|
||||||
out[o++] = b64[(n >> 12) & 63];
|
|
||||||
out[o++] = remaining > 1 ? b64[(n >> 6) & 63] : '=';
|
|
||||||
out[o++] = remaining > 2 ? b64[n & 63] : '=';
|
|
||||||
}
|
|
||||||
|
|
||||||
out[o] = 0;
|
|
||||||
return o;
|
|
||||||
}
|
|
||||||
41
src/main.c
41
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,6 +9,7 @@
|
|||||||
#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/IRC.h"
|
#include "IRC/IRC.h"
|
||||||
#include "IRC/IRC_structs.h"
|
#include "IRC/IRC_structs.h"
|
||||||
@@ -16,9 +18,12 @@
|
|||||||
|
|
||||||
#include "credentials.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];
|
||||||
@@ -27,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);
|
||||||
|
|
||||||
if(sockfd >= 0)
|
|
||||||
{
|
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_SetSASLParameters(SASL_MECHANISM_PLAIN, CREDENTIALS_USERNAME, CREDENTIALS_PASSWORD, &irc);
|
||||||
IRC_Register("BareBonesDude", "bbirc", "A Barebones IRC (https://gitea.codersquack.nl/thorium1256/bbirc) test", &irc);
|
IRC_Register( &irc);
|
||||||
}
|
|
||||||
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;
|
||||||
@@ -114,7 +122,8 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
printf("Connection closed by foreign host.\n");
|
printf("Connection closed by foreign host.\n");
|
||||||
|
|
||||||
NET_Close(sockfd);
|
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,39 +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);
|
// 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");
|
||||||
@@ -107,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;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -115,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