|
1 |
| -nothing here! |
| 1 | +//This code creates a TCP server that listens on port 9999 and accepts incoming connections. |
| 2 | +//When a client connects, it creates a new thread to handle the client. |
| 3 | +//The client can send commands to the server, such as "ping", "exit", "upload <file_name>", and "exec <file_name>". |
| 4 | +//The server responds accordingly, uploading files to the "uploads/" directory and executing files in that directory. |
| 5 | +// |
| 6 | +// NOTE: |
| 7 | +//This code uses the POSIX socket API and the pthread library for threading. |
| 8 | +//It also uses the system function to execute files, which can be a security risk if not used carefully. |
| 9 | +//Additionally, this code does not handle errors as robustly as it could, and it does not implement any authentication or authorization mechanisms. |
| 10 | + |
| 11 | + |
| 12 | +Note that this code uses the POSIX socket API and the pthread library for threading. It also uses the system function to execute files, which can be a security risk if not used carefully. Additionally, this code does not handle errors as robustly as it could, and it does not implement any authentication or authorization mechanisms. |
| 13 | + |
| 14 | +#include <stdio.h> |
| 15 | +#include <stdlib.h> |
| 16 | +#include <string.h> |
| 17 | +#include <sys/socket.h> |
| 18 | +#include <netinet/in.h> |
| 19 | +#include <arpa/inet.h> |
| 20 | +#include <pthread.h> |
| 21 | +#include <unistd.h> |
| 22 | +#include <sys/types.h> |
| 23 | +#include <sys/stat.h> |
| 24 | +#include <fcntl.h> |
| 25 | + |
| 26 | +#define UPLOAD_DIR "uploads/" |
| 27 | +#define PORT 9999 |
| 28 | +#define BUFFER_SIZE 1024 |
| 29 | + |
| 30 | +void* handle_client(void* arg); |
| 31 | +void create_directory(const char* dir); |
| 32 | + |
| 33 | +int main() { |
| 34 | + int server_fd, new_socket; |
| 35 | + struct sockaddr_in address; |
| 36 | + int opt = 1; |
| 37 | + int addrlen = sizeof(address); |
| 38 | + pthread_t thread; |
| 39 | + |
| 40 | + // Create upload directory if it doesn't exist |
| 41 | + create_directory(UPLOAD_DIR); |
| 42 | + |
| 43 | + // Create socket |
| 44 | + if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { |
| 45 | + perror("socket failed"); |
| 46 | + exit(EXIT_FAILURE); |
| 47 | + } |
| 48 | + |
| 49 | + // Set address and port number for the server |
| 50 | + address.sin_family = AF_INET; |
| 51 | + address.sin_addr.s_addr = INADDR_ANY; |
| 52 | + address.sin_port = htons(PORT); |
| 53 | + |
| 54 | + // Forcefully attaching socket to the port 9999 |
| 55 | + if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, &opt, sizeof(opt))) { |
| 56 | + perror("setsockopt"); |
| 57 | + exit(EXIT_FAILURE); |
| 58 | + } |
| 59 | + |
| 60 | + // Bind the socket to the address and port |
| 61 | + if (bind(server_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { |
| 62 | + perror("bind failed"); |
| 63 | + exit(EXIT_FAILURE); |
| 64 | + } |
| 65 | + |
| 66 | + // Listen for incoming connections |
| 67 | + if (listen(server_fd, 5) < 0) { |
| 68 | + perror("listen"); |
| 69 | + exit(EXIT_FAILURE); |
| 70 | + } |
| 71 | + |
| 72 | + printf("Listening on port %d...\n", PORT); |
| 73 | + |
| 74 | + while (1) { |
| 75 | + // Accept incoming connection |
| 76 | + if ((new_socket = accept(server_fd, (struct sockaddr*)&address, (socklen_t*)&addrlen)) < 0) { |
| 77 | + perror("accept"); |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + printf("Accepted connection from IP address %s and port %d...\n", inet_ntoa(address.sin_addr), ntohs(address.sin_port)); |
| 82 | + |
| 83 | + // Create a new thread to handle the client |
| 84 | + pthread_create(&thread, NULL, handle_client, &new_socket); |
| 85 | + } |
| 86 | + |
| 87 | + return 0; |
| 88 | +} |
| 89 | + |
| 90 | +void* handle_client(void* arg) { |
| 91 | + int new_socket = *((int*)arg); |
| 92 | + char buffer[BUFFER_SIZE]; |
| 93 | + char command[BUFFER_SIZE]; |
| 94 | + char file_name[BUFFER_SIZE]; |
| 95 | + int file_size; |
| 96 | + FILE* file; |
| 97 | + int received; |
| 98 | + |
| 99 | + while (1) { |
| 100 | + // Receive command from client |
| 101 | + recv(new_socket, command, BUFFER_SIZE, 0); |
| 102 | + command[strcspn(command, "\n")] = 0; // Remove newline character |
| 103 | + |
| 104 | + if (strcasecmp(command, "ping") == 0) { |
| 105 | + send(new_socket, "Running", 7, 0); |
| 106 | + } |
| 107 | + else if (strcasecmp(command, "exit") == 0) { |
| 108 | + break; |
| 109 | + } |
| 110 | + else if (strncmp(command, "upload ", 7) == 0) { |
| 111 | + // Receive file size from client |
| 112 | + recv(new_socket, buffer, BUFFER_SIZE, 0); |
| 113 | + file_size = atoi(buffer); |
| 114 | + |
| 115 | + // Send ready signal to client |
| 116 | + send(new_socket, "Ready to receive file", 20, 0); |
| 117 | + |
| 118 | + // Extract file name from command |
| 119 | + strncpy(file_name, command + 7, BUFFER_SIZE - 7); |
| 120 | + file_name[BUFFER_SIZE - 7] = 0; // Ensure null termination |
| 121 | + |
| 122 | + // Create file |
| 123 | + file = fopen(UPLOAD_DIR file_name, "wb"); |
| 124 | + if (file == NULL) { |
| 125 | + perror("fopen"); |
| 126 | + continue; |
| 127 | + } |
| 128 | + |
| 129 | + received = 0; |
| 130 | + while (received < file_size) { |
| 131 | + // Receive file data from client |
| 132 | + recv(new_socket, buffer, BUFFER_SIZE, 0); |
| 133 | + fwrite(buffer, 1, strlen(buffer), file); |
| 134 | + received += strlen(buffer); |
| 135 | + } |
| 136 | + |
| 137 | + fclose(file); |
| 138 | + send(new_socket, "File uploaded", 12, 0); |
| 139 | + } |
| 140 | + else if (strncmp(command, "exec ", 5) == 0) { |
| 141 | + // Extract file name from command |
| 142 | + strncpy(file_name, command + 5, BUFFER_SIZE - 5); |
| 143 | + file_name[BUFFER_SIZE - 5] = 0; // Ensure null termination |
| 144 | + |
| 145 | + // Check if file exists |
| 146 | + if (access(UPLOAD_DIR file_name, F_OK) != -1) { |
| 147 | + // Execute file |
| 148 | + char cmd[256]; |
| 149 | + sprintf(cmd, "%s%s", UPLOAD_DIR, file_name); |
| 150 | + system(cmd); |
| 151 | + send(new_socket, "Execution started", 15, 0); |
| 152 | + } |
| 153 | + else { |
| 154 | + send(new_socket, "File not found", 13, 0); |
| 155 | + } |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + close(new_socket); |
| 160 | + return NULL; |
| 161 | +} |
| 162 | + |
| 163 | +void create_directory(const char* dir) { |
| 164 | + if (mkdir(dir, 0777) == -1) { |
| 165 | + if (errno != EEXIST) { |
| 166 | + perror("mkdir"); |
| 167 | + exit(EXIT_FAILURE); |
| 168 | + } |
| 169 | + } |
| 170 | +} |
0 commit comments