basic structure and client process chain done

This commit is contained in:
Afonso Franco 2024-05-10 20:34:01 +01:00
parent 77657c7078
commit 83bd6fb796
Signed by: afonso
SSH key fingerprint: SHA256:PQTRDHPH3yALEGtHXnXBp3Orfcn21pK20t0tS1kHg54
21 changed files with 313 additions and 7 deletions

View file

@ -1,4 +0,0 @@
#include <sys/stat.h>
int main(){
}

68
src/client/client.c Normal file
View file

@ -0,0 +1,68 @@
#include "client.h"
#include <stdio.h>
int send_message(unsigned int sender, unsigned int receiver) {
int pipe_to_child[2];
if (pipe(pipe_to_child) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
close(pipe_to_child[1]); // Close write end of pipe
// Redirect stdin to read from pipe_to_child
dup2(pipe_to_child[0], STDIN_FILENO);
// Execute a command, for example, a simple "cat" command
execlp("./bin/djumbai_client_send/djumbai_client_send", "djumbai_client_send", NULL);
// If execlp fails
perror("execlp");
close(pipe_to_child[0]);
exit(EXIT_FAILURE);
} else { // Parent process
close(pipe_to_child[0]); // Close read end of pipe
printf("Please enter your message (Max of %ld bytes):\n", MAX_CONTENT_SIZE);
char content[MAX_CONTENT_SIZE];
fgets(content, MAX_CONTENT_SIZE, stdin);
message msg;
if (new_message(&msg, sender, receiver, content) != 0) {
printf("Error when creating new message\n");
}
// Serialize the message
unsigned char buffer[sizeof(struct Message)];
if (serialize_message(&msg, sizeof(struct Message), buffer) == -1) {
fprintf(stderr, "Error: Serialization failed\n");
return 1;
}
write(pipe_to_child[1], buffer, sizeof(buffer));
// Close the write end of the pipe
close(pipe_to_child[1]);
// Wait for the child process to finish
wait(NULL);
}
return 0;
}
int main() {
// TODO: Client parsing to be done
unsigned int sender = getuid();
unsigned int receiver = 1000;
send_message(sender, receiver);
return 0;
}

12
src/client/client.h Normal file
View file

@ -0,0 +1,12 @@
#ifndef CLIENT_H
#define CLIENT_H
#include "../../libs/communication/communication.h"
#include "../../libs/protocol/protocol.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <unistd.h>
int send_message(unsigned int sender, unsigned int receiver);
#endif // !CLIENT_H

View file

@ -0,0 +1,3 @@
int main(){
}

View file

@ -0,0 +1,47 @@
#include "../../libs/protocol/protocol.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
int pipe_to_child[2];
if (pipe(pipe_to_child) == -1) {
perror("pipe");
exit(EXIT_FAILURE);
}
pid_t pid = fork();
if (pid == -1) {
perror("fork");
exit(EXIT_FAILURE);
}
if (pid == 0) { // Child process
close(pipe_to_child[1]); // Close write end of pipe
// Redirect stdin to read from pipe_to_child
dup2(pipe_to_child[0], STDIN_FILENO);
execlp("./bin/djumbai_enqueue/djumbai_enqueue", "djumbai_enqueue", NULL);
// If execlp fails
perror("execlp");
close(pipe_to_child[0]);
exit(EXIT_FAILURE);
} else { // Parent process
close(pipe_to_child[0]); // Close read end of pipe
unsigned char buffer[sizeof(struct Message)];
read(0, buffer, sizeof(struct Message));
write(pipe_to_child[1], buffer, sizeof(buffer));
// Close the write end of the pipe
close(pipe_to_child[1]);
// Wait for the child process to finish
wait(NULL);
}
return 0;
}

View file

@ -0,0 +1,8 @@
#include <unistd.h>
int main(){
//Make stdin the FIFO
//Read from FIFO
//Make stdout the output FIFO
//Write to another FIFO
}

View file

@ -0,0 +1,8 @@
#include <unistd.h>
int main(){
//Make stdin the FIFO
//Read from FIFO
//Make stdout the output FIFO
//Write to another FIFO
}

View file

@ -0,0 +1,30 @@
#include "djumbai_enqueue.h"
int main() {
//Change the root of the djumbai_enqueue process so it doesn't have access to anything else.
chroot("/opt/djumbai/queue/");
const char *message_queue_path =
"mailqueue"; // Replace this with the path to your FIFO
// Open the FIFO for writing
int queue_fd;
queue_fd = open(message_queue_path, O_WRONLY);
if (queue_fd == -1) {
if (errno == ENOENT) {
// FIFO does not exist
printf("FIFO '%s' does not exist. Exiting...\n", message_queue_path);
return 1;
} else {
perror("open");
return 1;
}
}
// Read message from stdin
unsigned char buffer[MESSAGE_SIZE];
read(0, buffer, MESSAGE_SIZE);
// Write message to message queue
write(queue_fd, buffer, MESSAGE_SIZE);
close(queue_fd);
}

View file

@ -0,0 +1,12 @@
#ifndef DJUMBAI_ENQUEUE_H
#define DJUMBAI_ENQUEUE_H
#include "../../libs/protocol/protocol.h"
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main();
#endif // !DJUMBAI_ENQUEUE_H

View file

@ -0,0 +1,3 @@
int main(){
}

View file