Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <pthread.h>
- #include <mysql.h>
- #include <sys/socket.h>
- #include <sys/types.h>
- #include <netinet/in.h>
- #include <string.h>
- #include <signal.h>
- /*definitions*/
- //the number of clients that can connect at once
- #define NUM_CLIENTS 10
- #define PORT 55555
- #define INPUT_SIZE 256
- #define MYSQL_SERVER "localhost"
- #define MYSQL_USER "root"
- #define MYSQL_PASSWORD "12345"
- #define DATABASE "BBS"
- struct threadData {
- int threadId;
- int connfd;
- int userId;
- //can add more here if we need it (sql connection?)
- };
- struct threadData threadDataArray[NUM_CLIENTS];
- int authenticate(struct threadData *);
- void browseTopics(struct threadData *);
- void listAll(struct threadData *);
- void listByUser(struct threadData *);
- void addPost(struct threadData *, int, int);
- void deletePost(struct threadData *);
- void addThread(struct threadData *, int);
- void terminate (int param);
- static void *clientLoop(void *threadarg){
- int taskId, connfd, menulen, errlen, n, i, quit;
- struct threadData *clientData;
- char input[INPUT_SIZE];
- char option[INPUT_SIZE];
- int authenticated = 0;
- char menuText [200]= {"Please select an option:\n1) Browse topic boards\n2) List all messages\n3) List all messages by a user\n4) Quit\n>"};
- char errText[30] = "Invalid option.\n";
- char browsing[30] = "Browsing function\n";
- char listing[30] = "Listall function\n";
- menulen = strlen(menuText);
- errlen = strlen(errText);
- clientData = (struct thread_data *) threadarg;
- taskId = clientData->threadId;
- connfd = clientData->connfd;
- //1. authenticate user
- //2. send menu
- //3. receive choice
- //4. call function that handles that choice
- //5. loop to send menu again
- //1. authenticate user
- //TODO: test authenticate function
- authenticated = authenticate(clientData);
- //printf("Authenticated %d", authenticated);
- /*
- if(authenticated) {
- send(connfd, menuText, strlen(menuText) + 1, 0);
- printf("Sending options menu...");
- } else
- {
- write(connfd, "Authentication Failed", 22);
- printf("Authetication Failed");
- }
- */
- quit= 0;
- while(!quit && authenticated)
- {
- send(connfd, menuText, strlen(menuText) + 1, 0);
- printf("Sending options menu...\n");
- fflush(stdout);
- //3. receive choice
- if ( ( n = recv (connfd, input, 12, 0) ) == 0 ) {
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- connfd = -1;
- }
- /*else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- printf("choice received %d \n", input[0]);
- sprintf(option, "Choice received %d \n", input[0]);
- send(connfd, option, strlen(option) + 1, 0);
- }*/
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>33 && input[i]<123)
- {
- input[i]=input[i];
- }
- i++;
- }
- //4. call function that handles that choice
- switch (input[0]) {
- case '1':
- //TODO: test browse topic boards function
- browseTopics(clientData);
- // send(connfd, browsing, strlen(browsing) + 1, 0);
- break;
- case '2':
- //TODO: test list all messages function
- listAll(clientData);
- // send(connfd, listing, strlen(listing) + 1, 0);
- break;
- case '3':
- //TODO: test list all messages by a user function
- listByUser(clientData);
- break;
- case '4':
- quit = 1;
- break;
- default:
- send(connfd, errText, strlen(errText) + 1, 0);
- break;
- }
- }
- }//5. loop to send menu again
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- }
- /* allows the user to log in
- returns if user is valid */
- int authenticate(struct threadData *clientData) {
- // 1) Prompts user for username
- // 2) Prompts user for password
- // 3) Connects to db
- // 4) Checks db for validity
- // 5) Returns if user is valid
- // 6) Disconnects from db
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, userId, connfd;
- struct threadData thisData = *clientData;
- char temp[INPUT_SIZE];
- char input[INPUT_SIZE];
- char username[INPUT_SIZE];
- char password[INPUT_SIZE];
- char query[256];
- for(i= 0; i<INPUT_SIZE; i++)
- {
- input[i]= 0;
- }
- connfd = clientData->connfd;
- // 1) Prompts user for username
- send(connfd, "user: ", 7, 0);
- if ( (n = recv(connfd, input, 12, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return 0;
- }
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- //strcpy(username, input);
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>33 && input[i]<123)
- {
- username[i]=input[i];
- }
- i++;
- }
- printf("username received\n");
- }
- // 2) Prompts user for password
- send(connfd, "password: ", 11, 0);
- if ( (n = recv(connfd, input, 12, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return 0;
- }
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- //strcpy(password, input);
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>33 && input[i]<123)
- {
- password[i]=input[i];
- }
- i++;
- }
- printf("password received\n");
- }
- printf("'%s'", username);
- printf("\n");
- printf("'%s'", password);
- printf("\n");
- // 3) Connects to db
- printf("connecting... %s", username);
- printf("\n");
- conn= NULL;
- conn = mysql_init(NULL);
- if(conn!=NULL)
- {
- printf("initialized the database\n");
- } else {
- printf("Error initializing database\n");
- }
- if (mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD, DATABASE, 0, NULL, 0) == NULL) {
- printf("mysql error: %s\n", mysql_error(conn));
- printf("could not find connect");
- mysql_close(conn);
- return 0;
- } else {
- printf("Connected to database\n");
- }
- // 4) Checks db for validity
- //sanitize this
- sprintf(query, "SELECT id FROM Users WHERE username LIKE '%s' AND password LIKE SHA('%s')",
- username, password);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- printf("could not find user\n");
- mysql_close(conn);
- return 0;
- } else {
- printf("Found user\n");
- }
- // 5) Returns if user is valid
- res = mysql_use_result(conn);
- if ( (row = mysql_fetch_row(res)) != NULL) {
- userId = *row[0] - 48;
- if (userId != 0) {
- thisData.userId = userId;
- printf("Authenication flag set\n");
- }
- else {
- mysql_free_result(res);
- mysql_close(conn);
- return 0;
- }
- } else {
- mysql_free_result(res);
- mysql_close(conn);
- send(connfd, "Authentication failed.\n", 24, 0);
- return 0;
- }
- *clientData = thisData;
- // 6) Disconnects from db
- mysql_free_result(res);
- mysql_close(conn);
- return 1;
- }
- /* allows the user to view the threads in each topical board */
- void browseTopics(struct threadData *clientData) {
- // 1) Connects to db
- // 2) Retrieves list of topics
- // 3) Sends menu with list to user
- // 4) Receives choice of topic
- // 5) Retrieves threads in topic
- // 6) Sends menu with thread titles to user
- // 7) Receives choice of threads
- // 8) Retrieves posts in thread
- // 9) Sends posts to user
- //10) Send menu with reply or quit to last menu (?)
- //11) Receieve menu choice
- //12) Calls addPost function if necessary
- //13) Disconnects from db
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, length, chosenTopicId, chosenThreadId, chosenPostId;
- int connfd;
- int topicIds[256], threadIds[256], postIds[256];
- char topicSubjects[256][20], threadTitles[256][20], postTitles[256][20];
- char threadAuthors[256][16], postAuthors[256][16];
- char * posts[256];
- struct threadData thisData = *clientData;
- char input[INPUT_SIZE];
- char output[INPUT_SIZE];
- char query[256];
- char temp[10];
- connfd = clientData->connfd;
- // 1) Connects to db
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD,
- DATABASE, 0, NULL, 0)) {
- printf("mysql error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- // 2) Retrieves list of topics
- sprintf(query, "SELECT id, subject FROM Topics ORDER BY id ASC;");
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- res = mysql_use_result(conn);
- // 3) Sends list of topics to user
- send (connfd, "All topics:\n", 12, 0);
- for ( i = 0; (row = mysql_fetch_row(res)) != NULL; i++) {
- temp[0] = row[0][0];
- temp[1] = row[0][1];
- topicIds[i] = atoi(temp);
- strcpy(topicSubjects[i], row[1]);
- sprintf(output, "%0d) %s\n", topicIds[i], topicSubjects[i]);
- length = strlen(output);
- send(connfd, output, length, 0);
- }
- send (connfd, "\nPlease enter the number of the topic to view\n>", 47, 0);
- // 4) Receives choice of topic
- // send(connfd, "user: ", 7, 0);
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- //TODO: check that this is valid, sanitize
- chosenTopicId = atoi(input);
- }
- // 5) Retrieves threads in topic
- sprintf(query, "SELECT Threads.title, Users.username, Threads.topic_id, Threads.id, Threads.user_id, Users.id FROM Threads, Users WHERE Threads.topic_id = '%d' AND Threads.user_id = Users.id ORDER BY Threads.id DESC;", chosenTopicId);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- row = NULL;
- mysql_free_result(res);
- res = mysql_use_result(conn);
- // 6) Sends menu with thread titles to user
- for ( i = 0; (row = mysql_fetch_row(res)) != NULL; i++ ){
- temp[0] = row[3][0];
- temp[1] = row[3][1];
- threadIds[i] = atoi(temp);
- strcpy(threadTitles[i], row[0]);
- strcpy(threadAuthors[i], row[1]);
- sprintf(output, "\n%d) %s: %s\n", threadIds[i], threadAuthors[i], threadTitles[i]);
- length = strlen(output);
- send(connfd, output, length, 0);
- }
- sprintf(output, "\nNumber of the topic, or (n)ew topic\n>");
- length = strlen(output) + 1;
- send(connfd, output, length, 0);
- // 7) Recieves choice of threads
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- if (input[0] == 'n' || input[0] == 'N')
- {
- addThread(clientData, chosenTopicId);
- mysql_free_result(res);
- mysql_close(conn);
- return;
- }
- //TODO: check that this is valid, sanitize
- chosenThreadId = atoi(input);
- }
- // 8) Retrieves posts in thread
- sprintf(query, "SELECT Posts.title, Posts.message, Posts.id, Posts.user_id, Posts.thread_id, Posts.time, Users.username, Users.id, Posts.topic_id FROM Posts, Users WHERE Posts.topic_id = %d AND Posts.thread_id = %d AND Posts.user_id = Users.id ORDER BY Posts.time ASC;", chosenTopicId, chosenThreadId);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- //printf(query);
- row = NULL;
- send (connfd, "Posts:\n\n", 8, 0);
- mysql_free_result(res);
- res = mysql_use_result(conn);
- // 9) Sends posts to user
- for ( i = 0; (row = mysql_fetch_row(res)) != NULL; i++ ){
- strcpy( postTitles[i], row[0] );
- temp[0] = row[2][0];
- temp[1] = row[2][1];
- postIds[i] = atoi(temp);
- strcpy (postAuthors[i], row[6]);
- posts[i] = malloc ( strlen(row[1]) );
- strcpy( posts[i], row[1] );
- sprintf(output, "%d) %s: %s\n--\n%s\n\n", postIds[i], postAuthors[i], postTitles[i], posts[i]);
- length = strlen(output);
- send(connfd, output, length, 0);
- }
- //10) Sends menu with reply or go to main menu
- send (connfd, "\n(r)eply, or (d)elete, or (m)ain menu\n>", 40, 0);
- //11) Receive menu choice
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- //12) Calls addPost function if necessary
- if (input[0] == 'r' || input[0] == 'R') {
- //TODO: implement post reply
- addPost(clientData, chosenTopicId, chosenThreadId);
- }else if (input[0] == 'd' || input[0] == 'D') {
- deletePost(clientData);
- }else if (input[0] == 'm' || input[0] == 'M') {
- mysql_free_result(res);
- mysql_close(conn);
- return;
- }
- //13) Disconnects from db
- mysql_free_result(res);
- mysql_close(conn);
- }
- /* lists all posts in any thread */
- void listAll(struct threadData *clientData) {
- // 1) Connects to db
- // 2) Retrieves all posts
- // 3) Sends all posts to user (descending by timestamp)
- // 4) Receives menu choice
- // 5) Calls deletePost if necessary
- // 6) Disconnects from database
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, length, chosenId, connfd;
- int topicIds[256], threadIds[256], postIds[256];
- char topicSubjects[256][20], threadTitles[256][20], postTitles[256][20];
- char threadAuthors[256][16], postAuthors[256][16];
- char * posts[256];
- struct threadData thisData = *clientData;
- char input[INPUT_SIZE];
- char output[INPUT_SIZE];
- char query[256];
- char temp[10];
- connfd = clientData->connfd;
- printf("listing all posts...\n");
- // 1) Connects to db
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD,
- DATABASE, 0, NULL, 0)) {
- printf("mysql error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- printf("retrieving results...\n");
- // 2) Retrieves posts in any thread
- sprintf(query, "SELECT Posts.title, Posts.message, Posts.id, Posts.user_id, Posts.thread_id, Posts.time, Users.username, Users.id FROM Posts, Users WHERE Posts.user_id= Users.id ORDER BY Posts.time DESC;");
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- row = NULL;
- send (connfd, "Posts:\n", 7, 0);
- res = mysql_use_result(conn);
- // 3) Sends posts to user
- for ( i = 0; (row = mysql_fetch_row(res)) != NULL; i++ ){
- strcpy( postTitles[i], row[0] );
- temp[0] = row[2][0];
- temp[1] = row[2][1];
- postIds[i] = atoi(temp);
- strcpy (postAuthors[i], row[6]);
- posts[i] = malloc ( strlen(row[1]) + 1 );
- strcpy( posts[i], row[1] );
- sprintf(output, "%d) %s: %s\n%s\n--\n\n", postIds[i], postAuthors[i], postTitles[i], posts[i]);
- length = strlen(output);
- send(connfd, output, length, 0);
- }
- send (connfd, "\n\n(d)elete, or (m)ain menu\n>", 28, 0);
- // 4) Receive menu choice
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- // 5) Calls deletePost function if necessary
- if (input[0] == 'd' || input[0] == 'D') {
- deletePost(clientData);
- }else if (input[0] == 'm' || input[0] == 'M') {
- mysql_free_result(res);
- mysql_close(conn);
- return;
- }
- // 6) Disconnects from db
- mysql_free_result(res);
- mysql_close(conn);
- }
- /* lists all posts by the specifed user */
- void listByUser(struct threadData *clientData) {
- // 1) Connects to db
- // 2) Requests a username to use
- // 3) Retrieves username
- // 4) Retrieves posts by username
- // 5) Sends posts to user
- // 6) Receives menu choice
- // 7) Calls deletePost if necessary
- // 8) Disconnects from db
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, length, chosenId, connfd;
- int topicIds[256], threadIds[256], postIds[256];
- char topicSubjects[256][20], threadTitles[256][20], postTitles[256][20];
- char threadAuthors[256][16], postAuthors[256][16];
- char * posts[256];
- struct threadData thisData = *clientData;
- char input[INPUT_SIZE];
- char output[INPUT_SIZE];
- char username[INPUT_SIZE];
- char query[256];
- char temp[10];
- connfd = clientData->connfd;
- chosenId = clientData->userId;
- // 1) Connects to db
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD,
- DATABASE, 0, NULL, 0)) {
- printf("mysql error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- fflush(stdin);
- // 2) Requests a username to use
- send (connfd, "Enter a username\n>", 18, 0);
- for(i=0; i<INPUT_SIZE; i++)
- {
- username[i]='\0';
- }
- // 3) Retrieves username
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>33 && input[i]<123)
- {
- username[i]=input[i];
- }
- i++;
- }
- username[i]='\0';
- }
- // 4) Retrieves posts by username
- sprintf(query, "SELECT Posts.title, Posts.message, Posts.id, Posts.user_id, Posts.thread_id, Posts.time, Users.username, Users.id FROM Posts, Users WHERE Users.username = '%s' AND Posts.user_id = Users.id ORDER BY Posts.time DESC;", username);
- printf(query);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- fflush(stdout);
- row = NULL;
- send (connfd, "Posts:\n", 7, 0);
- res = mysql_use_result(conn);
- // 5) Sends posts to user
- for ( i = 0; (row = mysql_fetch_row(res)) != NULL; i++ ){
- strcpy( postTitles[i], row[0] );
- temp[0] = row[2][0];
- temp[1] = row[2][1];
- postIds[i] = atoi(temp);
- strcpy (postAuthors[i], row[6]);
- posts[i] = malloc ( strlen(row[1]) );
- strcpy( posts[i], row[1] );
- sprintf(output, "%d) %s: %s\n--\n%s\n\n", postIds[i], postAuthors[i], postTitles[i], posts[i]);
- length = strlen(output);
- send(connfd, output, length, 0);
- }
- send (connfd, "\n\n(d)elete, or (m)ain menu\n>", 28, 0);
- // 6) Receive menu choice
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- // 7) Calls deletePost function if necessary
- if (input[0] == 'd' || input[0] == 'D') {
- deletePost(clientData);
- }else if (input[0] == 'm' || input[0] == 'M') {
- mysql_free_result(res);
- mysql_close(conn);
- return;
- }
- // 8) Disconnects from db
- mysql_free_result(res);
- mysql_close(conn);
- }
- /* adds a reply to a thread */
- void addPost(struct threadData *clientData, int topicId, int threadId) {
- // 1) Connects to db
- // 2) Prompts user for post
- // 3) Inserts post into thread
- // 4) Disconnects from db
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, length, chosenId, connfd, userId;
- int topicIds[256], threadIds[256], postIds[256];
- char topicSubjects[256][20], threadTitles[256][20], postTitles[256][20];
- char threadAuthors[256][16], postAuthors[256][16];
- char * posts[256];
- struct threadData thisData = *clientData;
- char input[INPUT_SIZE];
- char output[INPUT_SIZE];
- char query[256];
- char post[INPUT_SIZE];
- char title[INPUT_SIZE];
- connfd = thisData.connfd;
- userId = thisData.userId;
- // 1) Connects to db
- // 2) Prompts for post input
- send(connfd, "Enter your title\n", 17, 0);
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>31 && input[i]<123)
- {
- title[i]=input[i];
- }
- i++;
- }
- // strcpy(title, input);
- printf("title received: '%s'\n", title);
- }
- input[0] = '\0';
- send(connfd, "Enter your post\n>", 18, 0);
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>31 && input[i]<123)
- {
- post[i]=input[i];
- }
- i++;
- }
- //strcpy(post, input);
- printf("post received: '%s'\n", post);
- }
- // 3) Inserts post into db
- // 1) Connects to db
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD,
- DATABASE, 0, NULL, 0)) {
- printf("mysql error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- printf("connection established\n");
- sprintf(query, "INSERT INTO Posts (topic_id, thread_id, user_id, title, message) VALUES (%d, %d, %d, '%s', '%s');", topicId, threadId, userId, title, post);
- printf("executing query: '%s'\n", query);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- printf("query done");
- // 4) Closes database
- mysql_close(conn);
- }
- /* deletes a post from a thread */
- void deletePost(struct threadData *clientData) {
- // 1) Connect to db
- // 2) Prompt user for post
- // 3) Check if user has permissions
- // 4) Deletes post from thread
- // 5) Disconnects from db
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, length, chosenId, connfd, userId, postId, deletePostId;
- int topicIds[256], threadIds[256], postIds[256];
- char topicSubjects[256][20], threadTitles[256][20], postTitles[256][20];
- char threadAuthors[256][16], postAuthors[256][16];
- char * posts[256];
- struct threadData thisData = *clientData;
- char input[INPUT_SIZE];
- char output[INPUT_SIZE];
- char query[256];
- char post[INPUT_SIZE];
- char title[INPUT_SIZE];
- char temp[10];
- connfd = clientData->connfd;
- userId = clientData->userId;
- // 1) Connects to db
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD,
- DATABASE, 0, NULL, 0)) {
- printf("mysql error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- // 2) Prompts for post id
- send(connfd, "Enter post ID to delete:\n", 25, 0);
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0) {
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- postId= atoi(input);
- }
- // 3) Check if user has permissions
- sprintf(query, "Delete from Posts WHERE id = %d AND user_id = %d;", postId, userId);
- printf(query);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- if(mysql_affected_rows(conn) == 0)
- {
- send(connfd, "Access Denied. Post was not deleted\n\n", 37, 0);
- }else {
- printf("Post deleted successfully");
- send(connfd, "Post deleted succesfully\n\n", 27, 0);
- }
- mysql_close(conn);
- }
- /* adds a new thread to a topic */
- void addThread(struct threadData *clientData, int topicNum) {
- // 1) Connects to db
- // 2) Prompts user for thread
- // 3) Creates thread in db
- // 4) Queries db for the thread ID that was used
- // 5) Disconnects from db
- // 6) Calls addPost
- MYSQL *conn;
- MYSQL_RES *res;
- MYSQL_ROW row;
- int i, n, length, topicId, connfd, userId, threadId;
- int topicIds[256], threadIds[256], postIds[256];
- char topicSubjects[256][20], threadTitles[256][20], postTitles[256][20];
- char threadAuthors[256][16], postAuthors[256][16];
- char * posts[256];
- struct threadData thisData = *clientData;
- char input[INPUT_SIZE];
- char output[INPUT_SIZE];
- char query[256];
- char * post;
- char title[INPUT_SIZE];
- connfd = thisData.connfd;
- userId = thisData.userId;
- topicId = topicNum;
- // 1) Connects to db
- conn = mysql_init(NULL);
- if (!mysql_real_connect(conn, MYSQL_SERVER, MYSQL_USER, MYSQL_PASSWORD,
- DATABASE, 0, NULL, 0)) {
- printf("mysql error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- // 2) Prompts for post input
- send(connfd, "Enter a thread title\n>", 23, 0);
- if ( (n = recv(connfd, input, INPUT_SIZE, 0)) == 0){
- //connection closed
- shutdown(connfd, SHUT_RDWR);
- close(connfd);
- clientData->connfd = -1;
- return;
- }
- else {
- if(strlen(input)>0) input[strlen(input)-1]='\0';
- i=0;
- while(input[i]!='\0')
- {
- if(input[i]>33 && input[i]<123)
- {
- title[i]=input[i];
- }
- i++;
- }
- }
- // 3) Creates thread in db
- sprintf(query, "INSERT INTO Threads (topic_id, user_id, title) VALUE (%d, %d, '%s');", topicId, userId, input);
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- // 4) Queries db for the thread ID that was used
- sprintf(query, "SELECT id FROM Threads WHERE Threads.user_id = %d ORDER BY Threads.id DESC LIMIT 1;");
- if (mysql_query(conn, query)) {
- printf("query error: %s\n", mysql_error(conn));
- mysql_close(conn);
- return;
- }
- row = NULL;
- res = mysql_use_result(conn);
- if ( (row = mysql_fetch_row(res) ) != NULL)
- {
- threadId = *row[0] - 0x30;
- }
- // 5) Disconnects from db
- mysql_free_result(res);
- mysql_close(conn);
- // 6) Calls addPost
- addPost(clientData, topicId, threadId);
- }
- /*entry point of server*/
- int main(int argc, char ** argv){
- /*variables*/
- int i, maxi, maxfd, listenfd, connfd, sockfd, nready;
- pthread_t threads[NUM_CLIENTS];
- ssize_t n;
- fd_set rset, allset;
- struct sockaddr_in cliaddr, servaddr;
- void (*prev_fn)(int);
- //Handle kill signal
- prev_fn = signal (SIGINT, terminate);
- printf("starting server...\n");
- /*prepare port*/
- listenfd = socket (AF_INET, SOCK_STREAM, 0);
- bzero (&servaddr, sizeof(servaddr));
- servaddr.sin_family = AF_INET;
- servaddr.sin_port = htons(PORT);
- servaddr.sin_addr.s_addr = htonl (INADDR_ANY);
- if(bind(listenfd, (struct sockaddr *) &servaddr, sizeof(servaddr))==-1)
- {
- perror ("on bind");
- exit (-1);
- }
- listen(listenfd, 1);
- printf("waiting for clients\n");
- /*initialize*/
- for (i = 0; i < NUM_CLIENTS; i++){
- threadDataArray[i].connfd = -1;
- }
- int sizeofCliaddr = sizeof(cliaddr);
- /*main loop*/
- for ( ; ; ) {
- if ( (connfd = accept(listenfd, (struct sockaddr *) &cliaddr, &sizeofCliaddr)) > 0 ){
- printf("client accepted\n");
- //new client connection
- //we are going to save where we put it in a struct array
- //then spawn a thread to deal with the new struct
- for (i = 0; i < NUM_CLIENTS; i++) {
- if (threadDataArray[i].connfd < 0) {
- printf("creating thread...\n");
- threadDataArray[i].connfd = connfd;
- threadDataArray[i].threadId = i;
- int rc = pthread_create(&threads[i], NULL, &clientLoop, &threadDataArray[i]);
- if (rc)
- {
- printf("thread create failed. return code is %d\n", rc);
- }
- //send(connfd, "user: ", 7);
- //clientLoop(&threadDataArray[i]);
- break;
- }
- }
- }
- }
- }
- void terminate (int param)
- {
- printf("terminating server...");
- char *cmd = "killall bbs.out";
- system(cmd);
- exit(1);
- }
Add Comment
Please, Sign In to add comment