summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Bratch <luke@bratch.co.uk>2019-05-12 19:11:30 +0100
committerLuke Bratch <luke@bratch.co.uk>2019-05-12 19:11:30 +0100
commit34836ab2c9124e17d80bd8e8a1601398297391a8 (patch)
tree3079a0db3bc95c105c04b7730ef0d4c2c4af29c4
parent62fe87f6052dc28e38f2edc940b2002ede087277 (diff)
Make client port configurable and move settings structure to be initialised in main() instead of dochat().
-rw-r--r--blabouncer.c23
-rw-r--r--blabouncer.conf3
-rw-r--r--config.c42
-rw-r--r--config.h2
-rw-r--r--sockets.c2
-rw-r--r--sockets.h2
6 files changed, 61 insertions, 13 deletions
diff --git a/blabouncer.c b/blabouncer.c
index 1a781df..d96ec09 100644
--- a/blabouncer.c
+++ b/blabouncer.c
@@ -20,6 +20,8 @@
// - Comma separated channel list in JOINs/PARTs
// - Long raw strings like a 321/322/323 channel LIST, and the initial server welcome, need to be read and split properly
// - Only send some things to the requesting client (e.g. LIST replies)
+// - Normal (non-replay) log
+// - Alert when clients connect/authenticate/disconnect
//
// Example WHOIS reply:
// BOUNCER-SERVER RECEIVED: :irc.tghost.co.uk 307 blabounce l_bratch :is identified for this nick
@@ -66,6 +68,7 @@
#define MAXNICKLENGTH 64 // Randomly picked (TODO - is there an actual maximum number (ignoring the RFC preference of 9)?)
#define MAXUSERNAMELEN 64 // Randomly picked (TODO - is there an actual maximum username length?)
#define MAXREALNAMELEN 128 // Randomly picked (TODO - is there an actual maximum real name length?)
+#define MAXPORTLEN 6 // Up to 65535, so 5 characters + 1 for null
struct channel {
char name[MAXCHANLENGTH];
@@ -95,6 +98,7 @@ struct ircdstrings {
// Structure of settings either to be read from the configuration file or set/changed at runtime
struct settings {
int replayseconds;
+ char clientport[MAXPORTLEN];
};
int debugmode = 0;
@@ -998,7 +1002,7 @@ int processrawstring(int *serversockfd, int *clientsockfd, char *str, int source
}
// Where the big bouncing loop is
-void dochat(int *serversockfd, int *clientsockfd) {
+void dochat(int *serversockfd, int *clientsockfd, struct settings *settings) {
char serverbuf[MAXRCVSIZE]; // buffer for receiving data on server socket
char clientbuf[MAXRCVSIZE]; // buffer for receiving data on client socket(s)
int servernumbytes; // Number of bytes received from remote server
@@ -1048,10 +1052,6 @@ void dochat(int *serversockfd, int *clientsockfd) {
ircdstrings.ircrealname[0] = '\0';
ircdstrings.currentmsg[0] = '\0';
- // Structure of our various settings which are to either be read from the configuration file or set at runtime
- struct settings settings;
- settings.replayseconds = confreplayseconds();
-
// Read required things from configuration file
readnames(ircdstrings.ircnick, ircdstrings.ircusername, ircdstrings.ircrealname);
@@ -1126,7 +1126,7 @@ void dochat(int *serversockfd, int *clientsockfd) {
// Try to process received string (which should contain one or more server responses/commands)
// TODO - What if there were two server respones/commands and only one didn't need relaying?
- if (!processrawstring(serversockfd, clientsockfd, serverbuf, SOURCE_SERVER, fdmax, arr_clients, EXCEPT_NONE, &ircdstrings, channels, arr_authed, arr_ssl, &settings)) {
+ if (!processrawstring(serversockfd, clientsockfd, serverbuf, SOURCE_SERVER, fdmax, arr_clients, EXCEPT_NONE, &ircdstrings, channels, arr_authed, arr_ssl, settings)) {
fprintf(stderr, "Error: bouncer-server failed to process raw string.\n");
exit(1);
}
@@ -1252,7 +1252,7 @@ void dochat(int *serversockfd, int *clientsockfd) {
// Try to process received string (which should contain one or more client responses/commands)
// TODO - What if there were two server respones/commands and only one didn't need relaying?
- if (!processrawstring(serversockfd, clientsockfd, clientbuf, SOURCE_CLIENT, fdmax, arr_clients, i, &ircdstrings, channels, arr_authed, arr_ssl, &settings)) {
+ if (!processrawstring(serversockfd, clientsockfd, clientbuf, SOURCE_CLIENT, fdmax, arr_clients, i, &ircdstrings, channels, arr_authed, arr_ssl, settings)) {
fprintf(stderr, "Error: bouncer-client failed to process raw string.\n");
exit(1);
}
@@ -1276,6 +1276,11 @@ int main(int argc, char *argv[]) {
}
}
+ // Structure of our various settings which are to either be read from the configuration file or set at runtime
+ struct settings settings;
+ settings.replayseconds = confreplayseconds();
+ confclientport(settings.clientport);
+
// TODO: see if any of this can be shared (i.e. 1. avoid code duplication, and 2. see if variables can be shared between client/server sockets)
// TODO: track fdmax - kind of doing this now with arr_clients and num_clients but might be pointlessly tracking both in some places (?)
@@ -1288,9 +1293,9 @@ int main(int argc, char *argv[]) {
int serversockfd = createserversocket(argv[1], argv[2]);
// Create client socket (after server so we can use its fd number later as fdmax)
- int clientsockfd = createclientsocket(BOUNCERLISTENPORT);
+ int clientsockfd = createclientsocket(settings.clientport);
- dochat(&serversockfd, &clientsockfd);
+ dochat(&serversockfd, &clientsockfd, &settings);
printf("dochat() complete, closing socket...\n");
diff --git a/blabouncer.conf b/blabouncer.conf
index 32ad54b..e5d6613 100644
--- a/blabouncer.conf
+++ b/blabouncer.conf
@@ -13,3 +13,6 @@ replayseconds = "7200"
# Connect password clients must provided to connect
password = "bananas"
+
+# Port the bouncer should listen on
+clientport = "1234"
diff --git a/config.c b/config.c
index 7d26ca0..d652147 100644
--- a/config.c
+++ b/config.c
@@ -183,3 +183,45 @@ int checkpassword(char *password) {
printf("No password read from configuration file, quitting.\n");
exit(1);
}
+
+int confclientport(char *clientport) {
+ FILE *fp;
+ char str[MAXCHAR];
+ char* filename = "blabouncer.conf";
+
+ // Set all strings to zero-length to begin
+ clientport[0] = '\0';
+
+ fp = fopen(filename, "r");
+
+ if (fp == NULL) {
+ printf("error: could not open configuration file '%s'.\n", filename);
+ exit(1);
+ }
+
+ while (fgets(str, MAXCHAR, fp) != NULL) {
+ long int len;
+ if ((len = isconf(str, "clientport"))) {
+ getconf(str, len);
+ strncpy(clientport, str, strlen(str));
+ clientport[strlen(str)] = '\0';
+ printf("clientport is: '%s', length '%ld'.\n", clientport, strlen(clientport));
+ }
+ }
+
+ // Ensure we have everything required
+ if (!strlen(clientport)) {
+ printf("Failed to read clientport from configuration file.\n");
+ exit(1);
+ }
+
+ int portint = strtol(clientport, NULL, 10); // Convert resulting string to an integer, base 10
+ // Ensure port is valid
+ if (portint < 1 || portint > 65535) {
+ printf("Invalid clientport value in configuration file.\n");
+ exit(1);
+ }
+
+ fclose(fp);
+ return 1;
+}
diff --git a/config.h b/config.h
index 3d81d2f..156d8ad 100644
--- a/config.h
+++ b/config.h
@@ -13,4 +13,6 @@ int confreplayseconds();
int checkpassword(char *password);
+int confclientport(char *port);
+
#endif
diff --git a/sockets.c b/sockets.c
index 142be21..fd733b8 100644
--- a/sockets.c
+++ b/sockets.c
@@ -56,8 +56,6 @@ int createserversocket(char *host, char *port) {
// Create listening socket to listen for bouncer client connections
int createclientsocket(char *listenport) {
- listenport = BOUNCERLISTENPORT;
-
int listener; // listening socket descriptor
int rv; // return value for getaddrinfo (for error message)
struct addrinfo hints, *ai, *p;
diff --git a/sockets.h b/sockets.h
index 08ca457..523d609 100644
--- a/sockets.h
+++ b/sockets.h
@@ -17,8 +17,6 @@
#define BACKLOG 10 // maximum length to which the queue of pending connections for sockfd may grow
-#define BOUNCERLISTENPORT "1234" // TODO: change this to a config option!
-
// get sockaddr, IPv4 or IPv6:
void *get_in_addr(struct sockaddr *sa);