|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
#include "message.h"
|
|
|
|
#include "user.h"
|
|
|
|
#include "easysock.h"
|
|
|
|
#include "file_helpers.h"
|
|
|
|
#include "message_helpers.h"
|
|
|
|
|
|
|
|
#define BUFFER_SIZE 10000
|
|
|
|
#define MAX_CONNECTIONS 100
|
|
|
|
|
|
|
|
int num_of_lines(char* filename);
|
|
|
|
char* file_to_string(char* filename);
|
|
|
|
User* create_user_list(char* filename);
|
|
|
|
|
|
|
|
int main() {
|
|
|
|
struct sockaddr temp_addr;
|
|
|
|
socklen_t temp_addrlen;
|
|
|
|
|
|
|
|
fd_set read_fd_set;
|
|
|
|
int conn_sockets[MAX_CONNECTIONS] = {-1};
|
|
|
|
char* dest_address[MAX_CONNECTIONS] = {NULL};
|
|
|
|
FD_ZERO(&read_fd_set);
|
|
|
|
char buffer[BUFFER_SIZE];
|
|
|
|
User* users = create_user_list("user_file.txt");
|
|
|
|
|
|
|
|
struct sockaddr addr_struct;
|
|
|
|
int server_sock = create_local(4,'T',"127.0.0.1",30000,&addr_struct);
|
|
|
|
conn_sockets[0] = server_sock;
|
|
|
|
|
|
|
|
assert(listen(server_sock,MAX_CONNECTIONS) == 0);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
FD_ZERO(&read_fd_set);
|
|
|
|
for (int i=0; i < MAX_CONNECTIONS; i++) {
|
|
|
|
if (conn_sockets[i] > 0) {
|
|
|
|
FD_SET(conn_sockets[i],&read_fd_set);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int num_conns = select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL);
|
|
|
|
|
|
|
|
if (num_conns > 0) {
|
|
|
|
if (FD_ISSET(conn_sockets[0],&read_fd_set)) {
|
|
|
|
int client_sock = accept(conn_sockets[0],&temp_addr,&temp_addrlen);
|
|
|
|
for (int i=0;i < MAX_CONNECTIONS; i++) {
|
|
|
|
if (conn_sockets[i] <= 0) {
|
|
|
|
conn_sockets[i] = client_sock;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i=1; i < MAX_CONNECTIONS; i++) {
|
|
|
|
if (FD_ISSET(conn_sockets[i],&read_fd_set)) {
|
|
|
|
int num_bytes_read = recv(conn_sockets[i],buffer,sizeof(buffer),0);
|
|
|
|
if (num_bytes_read <= 0) {
|
|
|
|
close(conn_sockets[i]);
|
|
|
|
conn_sockets[i] = 0;
|
|
|
|
dest_address[i] = NULL;
|
|
|
|
} else {
|
|
|
|
if (dest_address[i] == NULL) {
|
|
|
|
dest_address[i] = fetch_address(buffer);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// for (int i=0;i<num_bytes_read;i++) {
|
|
|
|
// printf("%c",buffer[i]);
|
|
|
|
// }
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Message message = new_message("Hello, this is a text message",users[0],users[1]);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// printf("Message was: %s\nSentfrom: %s\nSent to: %s\nWith password: %s\nSent at: %s\n",message.text, message.sender.username, message.recipient.username, message.recipient.password, asctime(&message.timeinfo));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
User* create_user_list(char* filename) {
|
|
|
|
|
|
|
|
/* Structure of user file - <username> <password> on every line */
|
|
|
|
|
|
|
|
int num_users = num_of_lines(filename);
|
|
|
|
char* file_str = file_to_string(filename);
|
|
|
|
|
|
|
|
char* token = malloc(sizeof(char) * 30);
|
|
|
|
User* users = calloc(num_users, sizeof(User));
|
|
|
|
|
|
|
|
token = strtok(file_str," \r\n");
|
|
|
|
for (int i=0;i<num_users;i++) {
|
|
|
|
users[i].username = token;
|
|
|
|
token = strtok(NULL," \r\n");
|
|
|
|
users[i].password = token;
|
|
|
|
token = strtok(NULL," \r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
return users;
|
|
|
|
|
|
|
|
}
|