This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/utils/magichat/main.c

372 lines
14 KiB
C
Raw Normal View History

2017-03-20 01:37:56 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
2018-01-19 07:13:06 +00:00
#if defined(__OpenBSD__)
# include <sys/select.h>
#endif
2017-03-20 01:37:56 +00:00
#include "../../deps/jsmn/jsmn.h"
struct chat_msg {
char nick[16];
char bbstag[16];
char msg[256];
};
struct client {
char bbstag[16];
char nick[16];
int fd;
};
struct client **clients;
int client_count = 0;
typedef enum { START, KEY, PRINT, SKIP, STOP } parse_state;
static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
return 0;
}
return -1;
}
int main(int argc, char **argv) {
int port;
2018-01-19 11:42:06 +00:00
int server_socket, server_socket6;
2017-03-20 01:37:56 +00:00
struct sockaddr_in server, client;
2018-01-19 11:42:06 +00:00
struct sockaddr_in6 server6, client6;
2017-03-20 01:37:56 +00:00
fd_set master, read_fds;
int fdmax;
int c;
int new_fd;
struct chat_msg msg;
int i, j, k;
char buffer[1024];
char buf[1024];
2017-09-29 12:12:11 +00:00
char motd[256];
2017-03-20 01:37:56 +00:00
jsmn_parser parser;
2017-03-20 02:33:25 +00:00
jsmntok_t tokens[8];
2017-03-20 01:37:56 +00:00
int r;
int nbytes;
2017-09-29 12:12:11 +00:00
FILE *fptr;
2018-01-19 11:42:06 +00:00
int ipv6 = 0;
int on = 1;
2017-03-20 01:37:56 +00:00
if (argc < 2) {
2018-01-19 11:42:06 +00:00
printf("Usage: magichat [port] [ipv6(true/false)]\n");
2017-03-20 01:37:56 +00:00
return 0;
}
2018-01-19 11:42:06 +00:00
if (argc > 2 && strcasecmp(argv[2], "true") == 0) {
ipv6 = 1;
}
2017-03-20 01:37:56 +00:00
port = atoi(argv[1]);
if (port <= 1024 && port > 65535) {
printf("Invalid port number, must be between 1024 - 65535\n");
return 0;
}
2018-01-19 11:42:06 +00:00
FD_ZERO(&master);
if (ipv6) {
server_socket6 = socket(AF_INET6, SOCK_STREAM, 0);
if (server_socket6 == -1) {
fprintf(stderr, "Couldn't create socket (ipv6)..\n");
exit(1);
}
if (setsockopt(server_socket6, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
exit(1);
}
if (setsockopt(server_socket6, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, sizeof(on)) < 0) {
fprintf(stderr, "setsockopt(IPV6_V6ONLY) failed");
}
server6.sin6_family = AF_INET6;
server6.sin6_addr = in6addr_any;
server6.sin6_port = htons(port);
if (bind(server_socket6, (struct sockaddr *)&server6, sizeof(server6)) < 0) {
perror("Bind Failed, Error\n");
exit(1);
}
listen(server_socket6, 3);
FD_SET(server_socket6, &master);
}
2017-03-20 01:37:56 +00:00
server_socket = socket(AF_INET, SOCK_STREAM, 0);
if (server_socket == -1) {
fprintf(stderr, "Couldn't create socket..\n");
exit(1);
}
2018-01-19 11:42:06 +00:00
if (setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, (char *)&on, sizeof(on)) < 0) {
fprintf(stderr, "setsockopt(SO_REUSEADDR) failed");
exit(1);
}
2017-03-20 01:37:56 +00:00
server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(port);
if (bind(server_socket, (struct sockaddr *)&server, sizeof(server)) < 0) {
perror("Bind Failed, Error\n");
exit(1);
}
listen(server_socket, 3);
FD_SET(server_socket, &master);
2018-01-19 11:42:06 +00:00
if (ipv6) {
if (server_socket6 > server_socket) {
fdmax = server_socket6;
} else {
fdmax = server_socket;
}
} else {
fdmax = server_socket;
}
2017-03-20 01:37:56 +00:00
while (1) {
read_fds = master;
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(-1);
}
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) {
if (i == server_socket) {
2018-01-19 11:42:06 +00:00
c = sizeof(struct sockaddr_in);
2017-03-20 01:37:56 +00:00
new_fd = accept(server_socket, (struct sockaddr *)&client, (socklen_t *)&c);
if (new_fd == -1) {
perror("accept");
} else {
if (client_count == 0) {
clients = (struct client **)malloc(sizeof(struct client *));
} else {
clients = (struct client **)realloc(clients, sizeof(struct client *) * (client_count + 1));
}
if (!clients) {
fprintf(stderr, "Out of memory!\n");
return -1;
}
clients[client_count] = (struct client *)malloc(sizeof(struct client));
if (!clients[client_count]) {
fprintf(stderr, "Out of memory!\n");
return -1;
}
sprintf(clients[client_count]->bbstag, "UNKNOWN");
sprintf(clients[client_count]->nick, "UNKNOWN");
clients[client_count]->fd = new_fd;
client_count++;
FD_SET(new_fd, &master);
if (new_fd > fdmax) {
fdmax = new_fd;
}
2018-01-19 11:42:06 +00:00
}
} else if (ipv6 && i == server_socket6) {
c = sizeof(struct sockaddr_in6);
new_fd = accept(server_socket6, (struct sockaddr *)&client6, (socklen_t *)&c);
if (new_fd == -1) {
perror("accept");
} else {
if (client_count == 0) {
clients = (struct client **)malloc(sizeof(struct client *));
} else {
clients = (struct client **)realloc(clients, sizeof(struct client *) * (client_count + 1));
}
if (!clients) {
fprintf(stderr, "Out of memory!\n");
return -1;
}
clients[client_count] = (struct client *)malloc(sizeof(struct client));
if (!clients[client_count]) {
fprintf(stderr, "Out of memory!\n");
return -1;
}
sprintf(clients[client_count]->bbstag, "UNKNOWN");
sprintf(clients[client_count]->nick, "UNKNOWN");
clients[client_count]->fd = new_fd;
client_count++;
FD_SET(new_fd, &master);
if (new_fd > fdmax) {
fdmax = new_fd;
}
}
2017-03-20 01:37:56 +00:00
} else {
if ((nbytes = recv(i, buf, sizeof buf, 0)) <= 0) {
for (k=0;k<client_count;k++) {
if (clients[k]->fd == i) {
if (strcmp(clients[k]->nick, "UNKNOWN") != 0) {
2017-09-29 14:10:44 +00:00
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s (%s) has left the chat\" }\n", clients[k]->nick, clients[k]->bbstag);
2017-03-20 01:37:56 +00:00
for (j=0;j<=fdmax;j++) {
if (FD_ISSET(j, &master)) {
2018-01-19 11:42:06 +00:00
if (ipv6) {
if (j != server_socket && j != server_socket6 && j != clients[k]->fd) {
if (send(j, buffer, strlen(buffer), 0) == -1) {
perror("send");
}
2017-03-20 01:37:56 +00:00
}
2018-01-19 11:42:06 +00:00
} else {
if (j != server_socket && j != clients[k]->fd) {
if (send(j, buffer, strlen(buffer), 0) == -1) {
perror("send");
}
}
2017-03-20 01:37:56 +00:00
}
}
}
}
free(clients[k]);
for (j=k;j<client_count-1;j++) {
clients[j] = clients[j+1];
}
client_count--;
if (client_count == 0) {
free(clients);
} else {
clients = realloc(clients, sizeof(struct client) * (client_count));
}
}
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
// we got some data from a client
2017-03-20 03:26:28 +00:00
jsmn_init(&parser);
2017-03-20 01:37:56 +00:00
r = jsmn_parse(&parser, buf, nbytes, tokens, sizeof(tokens)/sizeof(tokens[0]));
if (r < 0) {
continue;
}
if (r < 1 || tokens[0].type != JSMN_OBJECT) {
continue;
}
for (j = 1; j < r; j++) {
if (jsoneq(buf, &tokens[j], "bbs") == 0) {
2017-09-29 10:54:32 +00:00
snprintf(msg.bbstag, 16, "%.*s", tokens[j+1].end-tokens[j+1].start, buf + tokens[j+1].start);
2017-03-20 01:37:56 +00:00
j++;
}
if (jsoneq(buf, &tokens[j], "nick") == 0) {
2017-09-29 10:54:32 +00:00
snprintf(msg.nick, 16, "%.*s", tokens[j+1].end-tokens[j+1].start, buf + tokens[j+1].start);
2017-03-20 01:37:56 +00:00
j++;
}
if (jsoneq(buf, &tokens[j], "msg") == 0) {
2017-09-29 10:54:32 +00:00
snprintf(msg.msg, 256, "%.*s", tokens[j+1].end-tokens[j+1].start, buf + tokens[j+1].start);
2017-03-20 01:37:56 +00:00
j++;
}
}
if (strcmp(msg.msg, "LOGIN") == 0) {
for (j=0;j<client_count;j++) {
if (clients[j]->fd == i) {
2017-09-30 08:57:18 +00:00
if (strcmp(clients[j]->nick, "UNKNOWN") == 0) {
strncpy(clients[j]->bbstag, msg.bbstag, 16);
strncpy(clients[j]->nick, msg.nick, 16);
clients[j]->bbstag[15] = '\0';
clients[j]->nick[15] = '\0';
2017-03-20 01:37:56 +00:00
2017-09-30 08:57:18 +00:00
for(k = 0; k < client_count; k++) {
if (i != clients[k]->fd && strcmp(clients[k]->nick, "UNKNOWN") != 0) {
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s (%s) has joined the chat\" }\n", clients[j]->nick, clients[j]->bbstag);
if (send(clients[k]->fd, buffer, strlen(buffer), 0) == -1) {
perror("send");
}
2017-09-29 12:24:54 +00:00
}
2017-09-30 08:57:18 +00:00
}
fptr = fopen("motd.txt", "r");
if (fptr) {
2017-09-29 12:12:11 +00:00
fgets(motd, 256, fptr);
2017-09-30 08:57:18 +00:00
while (!feof(fptr)) {
if (motd[strlen(motd) - 1] == '\n') {
motd[strlen(motd) - 1] = '\0';
}
if (strlen(motd) == 0) {
sprintf(motd, " ");
}
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"%s\" }\n", motd);
if (send(i, buffer, strlen(buffer), 0) == -1) {
perror("send");
}
fgets(motd, 256, fptr);
}
fclose(fptr);
2017-09-29 12:12:11 +00:00
}
}
2017-09-30 08:57:18 +00:00
break;
}
}
} else if (strcmp(msg.msg, "USERS") == 0) {
for (j=0;j<client_count;j++) {
if (strcmp(clients[j]->nick, "UNKNOWN") != 0) {
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"(%s)[%s] is online.\" }\n", clients[j]->bbstag, clients[j]->nick);
if (send(i, buffer, strlen(buffer), 0) == -1) {
perror("send");
}
}
}
2017-10-02 04:22:37 +00:00
} else if (strcmp(msg.msg, "PING") == 0) {
snprintf(buffer, 1024, "{\"bbs\": \"SYSTEM\", \"nick\": \"SYSTEM\", \"msg\": \"PONG\" }\n");
if (send(i, buffer, strlen(buffer), 0) == -1) {
perror("send");
}
2017-03-20 01:37:56 +00:00
} else {
for (j=0;j<client_count;j++) {
if (clients[j]->fd == i) {
if (strcmp(clients[j]->nick, "UNKNOWN") != 0) {
2017-03-20 02:36:02 +00:00
for(k = 0; k < client_count; k++) {
2017-03-20 01:37:56 +00:00
if (i != clients[k]->fd && strcmp(clients[k]->nick, "UNKNOWN") != 0) {
2017-03-20 03:35:04 +00:00
if (send(clients[k]->fd, buf, nbytes, 0) == -1) {
2017-03-20 01:37:56 +00:00
perror("send");
}
}
}
}
break;
}
}
}
}
}
}
}
}
2017-09-29 10:54:32 +00:00
}