Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
6048_Labs/BaseImages/Server/server.c
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
70 lines (57 sloc)
1.71 KB
This file contains 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
#include <stdio.h> | |
#include <unistd.h> | |
#include <stdlib.h> | |
#include <sys/types.h> | |
#include <sys/socket.h> | |
#include <sys/stat.h> | |
#include <arpa/inet.h> | |
#include <errno.h> | |
#include <fcntl.h> | |
#include <string.h> | |
//https://stackoverflow.com/questions/5194666/disable-randomization-of-memory-addresses/30385370#30385370 | |
#include <sys/personality.h> | |
#include <signal.h> | |
int processConnection(int fd, int argc, char* argv[]) { | |
close(0); | |
close(1); | |
close(2); | |
dup2(fd, 0); | |
dup2(fd, 1); | |
dup2(fd, 2); | |
if(getenv("RUN_ALSR")) { | |
printf("Running with ALSR turned off\n"); | |
int out = personality(ADDR_NO_RANDOMIZE); | |
fflush(stdout); | |
} | |
execvp(argv[1], &argv[1]); | |
} | |
int main(int argc, char* argv[]) { | |
if (argc < 2) { | |
printf("Please provide the application to run on connection"); | |
exit(-1); | |
} | |
struct sockaddr_in address; | |
address.sin_family = AF_INET; | |
address.sin_port = htons(1337); | |
address.sin_addr.s_addr = INADDR_ANY; | |
int sock = socket(AF_INET, SOCK_STREAM, 0); | |
int status = bind(sock, (struct sockaddr *) &address, sizeof(address)); | |
if (status == -1) { | |
printf("Failed to bind socket, error is: %s\n", strerror(errno)); | |
exit(-2); | |
} | |
listen(sock, 0); | |
printf("Server started, listening on 0.0.0.0:1337, connect using the client app!\n"); | |
while (1) { | |
int resultfd = accept(sock, NULL, NULL); | |
int pid = fork(); | |
if (pid == 0) | |
return processConnection(resultfd, argc, argv); | |
else { | |
signal(SIGCHLD, SIG_IGN); //Dont give fuck about Zombie Children. | |
printf("Received connection as FD %d\n", resultfd); | |
close(resultfd); | |
} | |
} | |
return 0; | |
} |