123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687 |
- #include <stdio.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <signal.h>
- #include <string.h>
- #include <dirent.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <fcntl.h>
- #include <dirent.h>
- #include <assert.h>
- #include "Server.h"
- int getMailCounter()
- {
- FILE* test = fopen("mailcounter", "r+");
- if(test == NULL)
- {
- if(creat("mailcounter", S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
- {
- return 0;
- }
- test = fopen("mailcounter", "r+");
- if(test == NULL)
- {
- return 0;
- }
- }
-
- int counter;
- if(fread(&counter, sizeof(int), 1, test) == 0)
- {
- counter = -1;
- }
- counter++;
-
- rewind(test);
- if(fwrite(&counter, sizeof(int), 1, test) == 0)
- {
- return 0;
- }
-
- if(fflush(test) == EOF)
- {
- return 0;
- }
- if(fclose(test) == EOF)
- {
- return 0;
- }
-
- return counter;
- }
- Server server;
- void interruptHandler(int signal)
- {
- serverRemove(&server);
- exit(EXIT_SUCCESS);
- }
- void ensureBufferSize(char** data, int* size, int minSize)
- {
- if(minSize >= *size)
- {
- int newSize = *size;
- while(newSize <= minSize)
- {
- newSize *= 2;
- }
-
- printf("Resize from %d to %d\n", *size, newSize);
-
- char* newData = malloc(sizeof(char) * newSize);
- memcpy(newData, *data, *size);
-
- *size = newSize;
- free(*data);
- *data = newData;
- }
- }
- void packageSendFailAnswer(int clientSocket)
- {
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 0);
- streamWriteChars(&out, "ERR\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
- }
- int createDirectory(char* path)
- {
- DIR* dir = opendir(path);
- if(dir == NULL)
- {
- if(mkdir(path, S_ISUID | S_ISGID | S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == -1)
- {
- perror("create dir fail");
- return -1;
- }
- dir = opendir(path);
- if(dir == NULL)
- {
- perror("open dir fail");
- return -1;
- }
- }
- if(closedir(dir))
- {
- perror("close dir fail");
- return -1;
- }
- if(chdir(path) == -1)
- {
- perror("change dir fail");
- return -1;
- }
- return 0;
- }
- void storeMail(char* sender, char* receiver, char* subject, char* message)
- {
- printf("sender: %s\n", sender);
- printf("receiver: %s\n", receiver);
- printf("subject: %s\n", subject);
- printf("message: %s\n", message);
-
- if(createDirectory(receiver) == -1)
- {
- return;
- }
-
- int id = getMailCounter();
-
- if(createDirectory(sender) == -1)
- {
- return;
- }
-
- char buffer[12];
- snprintf(buffer, 12, "%d", id);
-
- if(createDirectory(buffer) == -1)
- {
- return;
- }
-
- if(creat(subject, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH) == -1)
- {
- return;
- }
- FILE* file = fopen(subject, "w");
- if(file == NULL)
- {
- perror("cant open file");
- return;
- }
- if(fputs(message, file) == EOF)
- {
- perror("cant write to file");
- return;
- }
- if(fflush(file) == EOF)
- {
- perror("cant flush the file");
- return;
- }
- if(fclose(file) == EOF)
- {
- perror("cant close");
- return;
- }
- }
- int packageSend(int client, int clientSocket, Stream* in)
- {
- char c;
- int i;
- const char* error = "invalid SEND package\n";
-
- // sender
- i = 0;
- char sender[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- sender[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- sender[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(sender) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- // receiver
- i = 0;
- char receiver[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- receiver[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- receiver[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(receiver) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- // subject
- i = 0;
- char subject[81];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- subject[i] = '\0';
- break;
- }
- else
- {
- if(i == 80)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- subject[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(subject) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- // message
- i = 0;
- int buffer = 16;
- char* message = malloc(sizeof(char) * buffer);
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- char n1;
- if(streamGetChar(in, &n1) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(message);
- return 0;
- }
- char n2;
- if(streamGetChar(in, &n2) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(message);
- return 0;
- }
-
- if(n1 == '.' && n2 == '\n')
- {
- ensureBufferSize(&message, &buffer, i);
- message[i] = '\0';
- break;
- }
- else
- {
- ensureBufferSize(&message, &buffer, i + 2);
- message[i] = c;
- i++;
- message[i] = n1;
- i++;
- message[i] = n2;
- i++;
- continue;
- }
- }
- ensureBufferSize(&message, &buffer, i);
- message[i] = c;
- i++;
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- free(message);
- return 0;
- }
- }
-
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- storeMail(sender, receiver, subject, message);
- assert(chdir(workingPath) == 0);
- }
-
- free(message);
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 0);
- streamWriteChars(&out, "OK\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- void sendMailList(char* user, Stream* out)
- {
- DIR* dir = opendir(user);
- if(dir == NULL)
- {
- streamWriteInt(out, 0);
- return;
- }
- assert(chdir(user) == 0);
-
- int counterPos = streamGetPosition(out);
- streamWriteInt(out, 0);
- int counter = 0;
-
- struct dirent* pDir = readdir(dir);
- while(pDir != NULL)
- {
- if(strcmp(pDir->d_name, ".") != 0 && strcmp(pDir->d_name, "..") != 0)
- {
- DIR* innerdir = opendir(pDir->d_name);
- if(innerdir != NULL)
- {
- assert(chdir(pDir->d_name) == 0);
-
- struct dirent* idDir = readdir(innerdir);
- while(idDir != NULL)
- {
- if(strcmp(idDir->d_name, ".") != 0 && strcmp(idDir->d_name, "..") != 0)
- {
- counter++;
- DIR* innerIdDir = opendir(idDir->d_name);
- if(innerIdDir != NULL)
- {
- struct dirent* mails = readdir(innerIdDir);
- while(mails != NULL)
- {
- if(strcmp(mails->d_name, ".") != 0 && strcmp(mails->d_name, "..") != 0)
- {
- streamWriteChars(out, idDir->d_name);
- streamWriteChars(out, " - ");
- streamWriteChars(out, mails->d_name);
- streamWriteChar(out, '\n');
- }
- mails = readdir(innerIdDir);
- }
- closedir(innerIdDir);
- }
- }
- idDir = readdir(innerdir);
- }
-
- assert(chdir("../") == 0);
- closedir(innerdir);
- }
- }
- pDir = readdir(dir);
- }
-
- int current = streamGetPosition(out);
- streamSetPosition(out, counterPos);
- streamWriteInt(out, counter);
- streamSetPosition(out, current);
-
- closedir(dir);
- }
- int packageList(int client, int clientSocket, Stream* in)
- {
- char c;
- int i;
- const char* error = "invalid LIST package\n";
-
- // user
- i = 0;
- char user[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- user[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- user[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(user) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 1);
- char workingPath[256];
- if(getcwd(workingPath, 256) != NULL)
- {
- sendMailList(user, &out);
- assert(chdir(workingPath) == 0);
- }
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- int packageRead(int client, int clientSocket, Stream* in)
- {
- char c;
- int i;
- const char* error = "invalid READ package\n";
-
- // user
- i = 0;
- char user[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- user[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- user[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(user) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- int number;
- if(streamGetInt(in, &number) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- if(streamGetChar(in, &c) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- else if(c != '\n')
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- printf("user: %s\n", user);
- printf("number: %d\n", number);
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 0);
- streamWriteChars(&out, "OK\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
-
- return 0;
- }
- int packageDelete(int client, int clientSocket, Stream* in)
- {
- char c;
- int i;
- const char* error = "invalid DELETE package\n";
-
- // user
- i = 0;
- char user[9];
- while(1)
- {
- if(streamGetChar(in, &c) != -1)
- {
- if(c == '\n')
- {
- user[i] = '\0';
- break;
- }
- else
- {
- if(i == 8)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- user[i] = c;
- i++;
- }
- }
- else
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- }
- if(strlen(user) == 0)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- int number;
- if(streamGetInt(in, &number) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- if(streamGetChar(in, &c) == -1)
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
- else if(c != '\n')
- {
- printf("%s", error);
- packageSendFailAnswer(clientSocket);
- return 0;
- }
-
- printf("user: %s\n", user);
- printf("number: %d\n", number);
- Stream out;
- streamInit(&out, 16);
- streamWriteInt(&out, 0);
- streamWriteChars(&out, "OK\n");
- serverSend(clientSocket, &out);
- streamRemove(&out);
- return 0;
- }
- int packageQuit(int client, int clientSocket, Stream* in)
- {
- printf("%d wants to disconnect ...\n", client);
- return 1;
- }
- int main(int argc, char** argv)
- {
- if(argc < 2)
- {
- printf("Usage: %s directory\n", argv[0]);
- return EXIT_FAILURE;
- }
- serverInitDefaults(&server);
-
- signal(SIGINT, interruptHandler);
- signal(SIGKILL, interruptHandler);
-
- if(serverInit(&server, 3, 6543, argv[1]) == -1)
- {
- return EXIT_FAILURE;
- }
-
- serverRegisterHandler(&server, packageSend);
- serverRegisterHandler(&server, packageList);
- serverRegisterHandler(&server, packageRead);
- serverRegisterHandler(&server, packageDelete);
- serverRegisterHandler(&server, packageQuit);
-
- serverWaitForConnection(&server);
-
- serverRemove(&server);
- return EXIT_SUCCESS;
- }
|