Last active
August 9, 2019 20:00
-
-
Save jcward/8d10a5b98863d800d76a4f50b0047690 to your computer and use it in GitHub Desktop.
A C socket server that runs a command and returns the response, socket input is ignored
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// A simple C socket server cobbled of examples that runs a command | |
// and returns the response over the socket, socket input is ignored. | |
// MIT LICENSE / NO WARRANTY OR GUARANTEE OF FITNESS FOR ANY PURPOSE | |
// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! | |
// Please note and fill in the defines below | |
// ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! ! | |
#define CMD "date" // The command to run | |
#define BUFSIZE 4096 // CMD stdout buffer, max length | |
#define PORT 9550 // The port to listen on | |
#define SLEEP_MS 500 // Intentional rate limiting / DDOS | |
#include <unistd.h> | |
#include <stdio.h> | |
#include <sys/socket.h> | |
#include <stdlib.h> | |
#include <netinet/in.h> | |
#include <string.h> | |
void start_listener(); | |
int main(int argc, char const *argv[]) | |
{ | |
start_listener(); | |
return 0; | |
} | |
char buf[BUFSIZE]; | |
char *cmd = CMD; | |
char* run_command(void) | |
{ | |
FILE *fp; | |
if ((fp = popen(cmd, "r")) == NULL) { | |
printf("Error opening pipe!\n"); | |
return NULL; | |
} | |
int idx = 0; | |
char ch; | |
while (EOF != (ch = fgetc(fp))) { | |
buf[idx++] = ch; | |
if (idx >= BUFSIZE) return NULL; // cmd buffer overflow | |
} | |
if (pclose(fp)) { | |
printf("Command not found or exited with error status\n"); | |
return NULL; | |
} | |
return buf; | |
} | |
void start_listener() | |
{ | |
int server_fd, new_socket, valread; | |
struct sockaddr_in address; | |
int opt = 1; | |
int addrlen = sizeof(address); | |
// Creating socket file descriptor | |
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == 0) { | |
perror("socket failed"); | |
exit(EXIT_FAILURE); | |
} | |
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR | SO_REUSEPORT, | |
&opt, sizeof(opt))) | |
{ | |
perror("setsockopt"); | |
exit(EXIT_FAILURE); | |
} | |
address.sin_family = AF_INET; | |
address.sin_addr.s_addr = INADDR_ANY; | |
address.sin_port = htons( PORT ); | |
// Forcefully attaching socket to the port 8080 | |
if (bind(server_fd, (struct sockaddr *)&address, | |
sizeof(address))<0) | |
{ | |
perror("bind failed"); | |
exit(EXIT_FAILURE); | |
} | |
if (listen(server_fd, 3) < 0) | |
{ | |
perror("listen"); | |
exit(EXIT_FAILURE); | |
} | |
while (1) { | |
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, | |
(socklen_t*)&addrlen))<0) | |
{ | |
perror("accept"); | |
exit(EXIT_FAILURE); | |
} | |
char* result = run_command(); | |
if (result) { | |
send(new_socket , result , strlen(result) , 0 ); | |
printf("Result sent\n"); | |
} | |
close(new_socket); | |
usleep(SLEEP_MS * 1000); // Throttle connections by SLEEP_MS milliseconds | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment