Chat system: simplify connect logic.

Simplify the logic around making connections in the
chat system by delegating to utility functions that
return early on failure.

Signed-off-by: Dan Cross <patchdev@fat-dragon.org>
This commit is contained in:
Dan Cross 2018-10-08 14:13:12 +00:00 committed by Andrew Pamment
parent d6826137dd
commit b28e003945
2 changed files with 61 additions and 67 deletions

View File

@ -1,6 +1,8 @@
#ifndef __BBS_H__
#define __BBS_H__
#include <sys/types.h>
#include <time.h>
#include <termios.h>
#ifdef __APPLE__

View File

@ -239,9 +239,61 @@ void append_screenbuffer(char *buffer) {
row_at = 0;
}
void chat_system(struct user_record *user) {
struct sockaddr_in servaddr;
int chat_connect_ipv6(int *socketp) {
struct sockaddr_in6 servaddr6;
int chat_socket;
char buffer[513];
memset(&servaddr6, 0, sizeof(struct sockaddr_in6));
if (inet_pton(AF_INET6, conf.mgchat_server, &servaddr6.sin6_addr) != 1) {
if (hostname_to_ip6(conf.mgchat_server, buffer)) {
return 0;
}
if (!inet_pton(AF_INET6, buffer, &servaddr6.sin6_addr)) {
return 0;
}
}
servaddr6.sin6_family = AF_INET6;
servaddr6.sin6_port = htons(conf.mgchat_port);
if ((chat_socket = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
return 0;
}
if (connect(chat_socket, (struct sockaddr *)&servaddr6, sizeof(servaddr6)) < 0) {
return 0;
}
*socketp = chat_socket;
return 1;
}
int chat_connect_ipv4(int *socketp) {
struct sockaddr_in servaddr;
int chat_socket;
char buffer[513];
memset(&servaddr, 0, sizeof(struct sockaddr_in));
if (inet_pton(AF_INET, conf.mgchat_server, &servaddr.sin_addr) != 1) {
if (hostname_to_ip(conf.mgchat_server, buffer)) {
return 0;
}
if (!inet_pton(AF_INET, buffer, &servaddr.sin_addr)) {
return 0;
}
}
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(conf.mgchat_port);
if ((chat_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return 0;
}
if (connect(chat_socket, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
return 0;
}
*socketp = chat_socket;
return 1;
}
void chat_system(struct user_record *user) {
fd_set fds;
int t;
int ret;
@ -250,7 +302,6 @@ void chat_system(struct user_record *user) {
int len;
char c;
char buffer2[256];
char buffer[513];
char outputbuffer[513];
char readbuffer[1024];
char message[1024];
@ -290,74 +341,16 @@ void chat_system(struct user_record *user) {
s_putstring("\e[24;1H");
if (conf.ipv6) {
memset(&servaddr6, 0, sizeof(struct sockaddr_in6));
if (inet_pton(AF_INET6, conf.mgchat_server, &servaddr6.sin6_addr) != 1) {
if (!hostname_to_ip6(conf.mgchat_server, buffer)) {
if (!inet_pton(AF_INET6, buffer, &servaddr6.sin6_addr)) {
chat_connected = 0;
} else {
servaddr6.sin6_family = AF_INET6;
servaddr6.sin6_port = htons(conf.mgchat_port);
if ((chat_socket = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
chat_connected = 0;
} else {
if (connect(chat_socket, (struct sockaddr *)&servaddr6, sizeof(servaddr6)) < 0) {
chat_connected = 0;
} else {
chat_connected = 1;
}
}
}
} else {
chat_connected = 0;
}
} else {
servaddr6.sin6_family = AF_INET6;
servaddr6.sin6_port = htons(conf.mgchat_port);
if ((chat_socket = socket(AF_INET6, SOCK_STREAM, 0)) < 0) {
chat_connected = 0;
} else {
if (connect(chat_socket, (struct sockaddr *)&servaddr6, sizeof(servaddr6)) < 0) {
chat_connected = 0;
} else {
chat_connected = 1;
}
}
}
chat_connected = chat_connect_ipv6(&chat_socket);
}
if (!chat_connected) {
memset(&servaddr, 0, sizeof(struct sockaddr_in));
if (inet_pton(AF_INET, conf.mgchat_server, &servaddr.sin_addr) != 1) {
if (!hostname_to_ip(conf.mgchat_server, buffer)) {
if (!inet_pton(AF_INET, buffer, &servaddr.sin_addr)) {
return;
} else {
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(conf.mgchat_port);
if ((chat_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return;
}
if (connect(chat_socket, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
return;
}
}
} else {
return;
}
} else {
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(conf.mgchat_port);
if ((chat_socket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
return;
}
if (connect(chat_socket, (struct sockaddr *)&servaddr, sizeof(servaddr)) < 0) {
return;
}
}
chat_connected = chat_connect_ipv4(&chat_socket);
}
memset(buffer, 0, 513);
if (!chat_connected) {
return;
}
screenbuffer = (struct character_t ***)malloc(sizeof(struct character_t **) * 23);
for (i = 0; i < 23; i++) {
@ -488,7 +481,6 @@ void chat_system(struct user_record *user) {
append_screenbuffer(outputbuffer);
do_update = 1;
memset(buffer, 0, 513);
buffer_at = 0;
}
}