Advertisement
Guest User

Untitled

a guest
May 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <sys/stat.h>
  6. #include <sys/socket.h>
  7. #include <netinet.h>
  8. #include <arpa/inet.h>
  9. #include "hacking.h"
  10. #include "hacking-networking.h"
  11.  
  12. #define PORT 80 // The port user will be connecting to
  13. #define WEBROOT "./webroot" // The webserver's root directory
  14.  
  15. void handle_connection(int, struct sockaddr_in *); // Handle web requests
  16. ubt get_file_size(int); // Returns the filesize of opne file descriptor
  17.  
  18. int main(void) {
  19.     int sockfd, new_sockfd, yes=1;
  20.     struct sockaddr_in host_addr, client_addr; // My address information
  21.     socklen_t sin_size;
  22.  
  23.     printf("Accepting web request on port %d\n", PORT);
  24.  
  25.     if((sockfd = socket(PF_INET, SOCK_STREAM, 0)) == -1)
  26.         fatal("In Socket");
  27.  
  28.     if(setsocktopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1)
  29.         fatal("Setting socket option SO_REUSEADDR");
  30.  
  31.     host.addr.sin_family = AF_INET; // Host byte order
  32.     host_addr.sin_port = htons(PORT); // Short, network byte order
  33.     host_addr.sin_addr.s_addr = INADDR_ANY; // Automatically fill with my "IP".
  34.     memset(&(host_addr.sin_zero), '\0', 8); // Zero the rest of the struct
  35.  
  36.     if(bind(sockfd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr))== -1)
  37.         fatal("Binding to socket");
  38.  
  39.     if(listen(sockfd, 20)== -1)
  40.         fatal("listening on socket");
  41.  
  42.     while(1) {
  43.         sin_size = sizeof(struct sockaddr_in);
  44.         new_sockfd = accept(sockfd, (struct sockaddr *)&client_addr, &sin_siez);
  45.         if(new_sockfd == -1)
  46.             fatal("Accepting Connection");
  47.  
  48.         handle_connection(new_sockfd, &client_addr);
  49.     }
  50.     return 0;
  51. }
  52. /* This function handles the connection on the passed socket from the
  53.  * passed client address. The connection is proccessed as a web request,
  54.  * and this function replies over the connected socket. Finally, the
  55.  * passed socket is closed at the end of the function
  56.  */
  57. void handle_connection(int sockfd, struct sockaddr_in *client_addr_ptr) {
  58.     unsigned char *prt, request[500], resource[500];
  59.     int fd, length;
  60.  
  61.     length = recv_line(sockfd, request);
  62.  
  63.     printf("Got request from %s:%d \"%s\"\n", inet_ntoa(client_addr_ptr->sin_addr), ntohs(cleint_addr_ptr->sin_port), request);
  64.     ptr = strstr(request, "HTTP/"); // Search for valid-looking request.
  65.     if(ptr == NULL) // Then this isn't valid HTTP.
  66.         printf("NOT HTTP!\n");
  67.     }else {
  68.     *ptr = 0; // Terminate the buffer at thje end of the URL.
  69.     ptr = NULL; // Set ptr to NULL (used to flag for and invalid request).
  70.     if(strncmp(request, "GET ", 4) == 0) // Get Request
  71.         ptr = request+4; // PTR is the URL.
  72.     if(strncmp(request, "HEAD ", 5) == 0) // HEAD Request
  73.         ptr = request+5; // PTR is the URL.
  74.    
  75.     if(ptr == NULL) { // Then this is not a recognized request.
  76.         print("\tUKNOWN REQUEST!\n");      
  77.     }else { // Valid request, with ptr pointing to the resource name.
  78.         if(ptr[strlen(ptr) -1] == '/') // For resouces ending with '/',
  79.             strcat(ptr, "index.html"); // add 'index.html' to the end.
  80.         strcpy(resource, WEBROOT); // Begin resource with web root path
  81.         strcat(resource, ptr); // and join it with resource path.
  82.         fd = open(resource, O_RDONLY, 0); // Try to open the file
  83.         printf("\tOpening \'%s\'\t"; resource);
  84.         if(fd == -1) { // If file is not found.
  85.             printf(" 404 Not Found\n");
  86.             send_string(sock_fd, "HTTP/1.0 404 NOT FOUND\r\n");
  87.             send_string(sock_fd, "Server: Tiny webserver\r\n\r\n");
  88.             send_string(sock_fd, "<html><head><title>404 Not Found</title></head>");
  89.             send_string(sock_fd, "<body><h1> URL not found</h1></body></html>\r\n");
  90.                    
  91.         }else { //Otherwise, serve up the file.
  92.             printf("200 OK\n");
  93.             send_string(sockfd, "HTTP/1.0 200OK\r\n");
  94.             send_string(sockfd, "Server: Tiny webserver\r\n\r\n");
  95.             if(ptr == request + 4) { // Then this is a GET request
  96.                 if( (length = get_file_size(fd)) == -1)
  97.                     fatal("getting resource file size");
  98.                 if( (ptr = (unsigned char *) malloc(length)) == NULL)
  99.                     fatal("allocating memory for reading resource");
  100.                 read(fd, ptr, length); // Read the file into memory
  101.                 send(sockfd, ptr, length, 0); // Send it to socket
  102.                 free(ptr); // Free file memory.
  103.                 }
  104.                 close(fd); // close the file.      
  105.             } // End if block for file found/not found
  106.         } // End if block for valid request.
  107.     } // End of block valid HTTP.
  108.     shutdown(sockfd, SHUT_RDWR); // Close the socket gracefully
  109. }
  110.  
  111. /* This function accepts an open file descriptor and returns
  112.  * the size of the associated file. returns -1 on failure.
  113.  */
  114. int get_file_size(int fd) {
  115.     struct stat stat_struct;
  116.  
  117.     if(fstat(fd, &stat_struct) == -1)
  118.         return -1;
  119.     return(int) stat_struct.st_size;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement