Used Users instead of IP addresses

master
Aadhavan Srinivasan 2 years ago
parent 3b71854b62
commit 84a2a79b89

@ -15,8 +15,12 @@
#define BUFFER_SIZE 10000 #define BUFFER_SIZE 10000
#define MAX_CONNECTIONS 100 #define MAX_CONNECTIONS 100
User* create_user_list(char* filename); User** create_user_list(char* filename);
void sigint_handler(int dummy); void sigint_handler(int dummy);
User* fetch_user(char* username);
User** users;
int num_users;
bool stop_running = false; bool stop_running = false;
@ -29,10 +33,11 @@ int main() {
fd_set read_fd_set; fd_set read_fd_set;
int conn_sockets[MAX_CONNECTIONS] = {-1}; int conn_sockets[MAX_CONNECTIONS] = {-1};
char* dest_address[MAX_CONNECTIONS] = {NULL}; User* dest_users[MAX_CONNECTIONS] = {NULL};
FD_ZERO(&read_fd_set); FD_ZERO(&read_fd_set);
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
User* users = create_user_list("user_file.txt"); users = create_user_list("user_file.txt");
num_users = num_of_lines("user_file.txt");
struct sockaddr addr_struct; struct sockaddr addr_struct;
int server_sock = create_local(4,'T',"127.0.0.1",30000,&addr_struct); int server_sock = create_local(4,'T',"127.0.0.1",30000,&addr_struct);
@ -67,19 +72,15 @@ int main() {
if (num_bytes_read <= 0) { if (num_bytes_read <= 0) {
close(conn_sockets[i]); close(conn_sockets[i]);
conn_sockets[i] = 0; conn_sockets[i] = 0;
dest_address[i] = NULL; dest_users[i] = NULL;
} else { } else {
if (dest_address[i] == NULL) { if (dest_users[i] == NULL) {
dest_address[i] = fetch_address(buffer); dest_users[i] = fetch_user(user_string(buffer));
if (dest_address[i] == NULL) { if (dest_users[i] == NULL) {
printf("Invalid message format\n"); printf("Invalid message format or User\n");
exit(241); exit(241);
}
if (check_ip_ver(dest_address[i]) == -1) {
printf("Invalid address: %s\n",dest_address[i]);
exit(242);
} else { } else {
printf("Valid IPv%d addres: %s\n",check_ip_ver(dest_address[i]),dest_address[i]); printf("Message intended for %s\n",dest_users[i]->username);
exit(0); exit(0);
} }
@ -119,7 +120,7 @@ int main() {
} }
User* create_user_list(char* filename) { User** create_user_list(char* filename) {
/* Structure of user file - <username> <password> on every line */ /* Structure of user file - <username> <password> on every line */
@ -127,13 +128,13 @@ User* create_user_list(char* filename) {
char* file_str = file_to_string(filename); char* file_str = file_to_string(filename);
char* token = malloc(sizeof(char) * 30); char* token = malloc(sizeof(char) * 30);
User* users = calloc(num_users, sizeof(User)); User** users = malloc (num_users * sizeof(User*));
token = strtok(file_str," \r\n"); token = strtok(file_str," \r\n");
for (int i=0;i<num_users;i++) { for (int i=0;i<num_users-1;i++) {
users[i].username = token; users[i]->username = strdup(token);
token = strtok(NULL," \r\n"); token = strtok(NULL," \r\n");
users[i].password = token; users[i]->password = strdup(token);
token = strtok(NULL," \r\n"); token = strtok(NULL," \r\n");
} }
@ -144,3 +145,17 @@ User* create_user_list(char* filename) {
void sigint_handler(int dummy) { void sigint_handler(int dummy) {
stop_running = true; stop_running = true;
} }
User* fetch_user(char* username) {
if (username == NULL) {
return NULL;
}
for (int i=0;i<num_users;i++) {
if (strcmp(users[i]->username, username) == 0) {
return users[i];
}
}
return NULL;
}

Loading…
Cancel
Save