Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <errno.h>
- #include <sys/types.h>
- #include <sys/wait.h>
- #include <sys/socket.h>
- #include <arpa/inet.h>
- #define DFL_PORT 8006
- #define BACKLOG 10
- #define BUFLEN 120
- void make_server_addr(struct sockaddr_in * addr, unsigned short int port) {
- addr->sin_family = AF_INET;
- addr->sin_addr.s_addr = htonl(INADDR_ANY);
- addr->sin_port = htons(port);
- memset(&(addr->sin_zero), '\0', 8);
- }
- void out(int client_fd, char * buf, size_t len) {
- size_t sent;
- while (len > 0) {
- sent = write(client_fd, buf, len);
- if (sent < 0) {
- perror("out: write");
- close(client_fd);
- exit(EXIT_FAILURE);
- }
- len -= sent;
- }
- }
- void ls_cmd(int client_fd, char* buf){
- pid_t pid;
- char ls_cmd[] = "/bin/ls";
- char * ls_args[] = {"ls", "-lh", 0};
- int retval;
- printf("ls_cmd buf %s\n", buf);
- if((pid = fork()) < 0){
- perror("ls_cmd: fork");
- close(client_fd);
- exit(EXIT_FAILURE);
- }else if(!pid){
- /* child */
- if(dup2(client_fd, 1) < 0 ){
- perror("ls_cmd: dup2");
- close(client_fd);
- exit(EXIT_FAILURE);
- }
- if(execv(ls_cmd, ls_args)){
- perror("ls_cmd: execv");
- close(client_fd);
- exit(EXIT_FAILURE);
- }
- }else{
- waitpid(pid, &retval, 0);
- if(retval != EXIT_SUCCESS){
- out(client_fd, "Internal server error.\n", 24);
- close(client_fd);
- exit(EXIT_FAILURE);
- }
- }
- }
- void get_cmd(int client_fd, char* buf){
- int isfileopen = 0;
- int filesize;
- int readcount;
- char* filename = malloc(sizeof(char)*120);
- if(filename == NULL){
- perror("get_cmd malloc");
- }
- strncpy(filename, strchr(buf, ' ')+1, strlen(buf));
- printf("file name %s_\n", filename);
- FILE *fp;
- fp = fopen(filename, "rb");
- if(!fp){
- buf = "Error opening file\n";
- out(client_fd, buf, strlen(buf)+1);
- perror("get_cmd file open");
- }else{
- isfileopen = 1;
- while(1){
- if(feof(fp)){
- printf("done sending file, last slice length: %d\n", readcount);
- break;
- }
- readcount = fread(buf, 1, BUFLEN, fp);
- out(client_fd, buf, readcount);
- }
- }
- if(isfileopen){
- fclose(fp);
- }
- free(filename);
- }
- void serve(int client_fd,char* buf) {
- char* error = malloc(120);
- size_t readcount;
- while ( (readcount = read(client_fd, buf, BUFLEN)) > 0) {
- printf("buf %c_ %s strlen(buf) %d_ \n", buf[0] , buf, readcount);
- if(buf[0] == 'g'){
- printf("serve: file %s_\n", buf);
- get_cmd(client_fd, buf);
- }else if(buf[0] == 'l'){
- puts("Serve: ls");
- ls_cmd(client_fd, buf);
- }else{
- int i;
- printf(error, "Unknown command: %c, %d, %s_\n", buf[0], buf[0], buf);
- sprintf(error, "Unknown command: %c, %d, %s_\n\0", buf[0], buf[0], buf);
- out(client_fd, error, strlen(error)+1);
- }
- }
- close(client_fd);
- free(error);
- }
- int main(int argc, char ** argv) {
- char buf[BUFLEN];
- int listen_fd;
- struct sockaddr_in client_addr;
- socklen_t client_addr_len = sizeof(struct sockaddr_in);
- struct sockaddr_in local_address;
- int on = 1;
- int client_fd;
- pid_t pid;
- /* make a listening socket */
- listen_fd = socket(PF_INET, SOCK_STREAM, 0);
- if ( listen_fd < 0 ) {
- perror("main: socket");
- exit(EXIT_FAILURE);
- }
- make_server_addr(&local_address, DFL_PORT);
- if( bind(listen_fd, (struct sockaddr *)&local_address,
- sizeof(local_address)) ){
- perror("main: bind");
- exit(EXIT_FAILURE);
- }
- if( listen(listen_fd, BACKLOG) ){
- perror("main: listen");
- exit(EXIT_FAILURE);
- }
- if( setsockopt(listen_fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) ){
- perror("main: setsockopt:");
- exit(EXIT_FAILURE);
- }
- puts("Started listening");
- while(1){
- client_fd = accept(listen_fd, (struct sockaddr *)&client_addr,
- &client_addr_len );
- if (client_fd < 0 ) {
- perror("main: accept");
- exit(EXIT_FAILURE);
- }
- printf("Client connected!\n");
- if( (pid = fork()) < 0 ) {
- perror("myserver: fork");
- exit(EXIT_FAILURE);
- } else if(!pid) {
- /* child */
- close(listen_fd);
- serve(client_fd, buf);
- printf("Client disconnected!\n");
- exit(EXIT_SUCCESS);
- } else {
- /* parent*/
- close(client_fd);
- }
- }
- close(listen_fd);
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement