Advertisement
Guest User

Untitled

a guest
Mar 1st, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.18 KB | None | 0 0
  1. /*!
  2.  * \file server.c
  3.  * \author Peter C. Chapin, Nathan S. Brown
  4.  * \brief Trivial FTP client
  5.  *
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11.  
  12. #include <arpa/inet.h>
  13. #include <netdb.h>
  14. #include <sys/socket.h>
  15. #ifndef S_SPLIT_S     // Workaround for splint.
  16. #include <unistd.h>
  17. #endif
  18.  
  19. #include "server.h"
  20. #define BUFFER_SIZE 128
  21.  
  22. // ============
  23. // Main Program
  24. // ============
  25.  
  26. void SIGCHLD_handler(int signal_number)
  27. {
  28.     int status;
  29.     while (waitpid(-1, &status, WNOHANG) > 0) ;
  30. }
  31.  
  32. int main(int argc, char **argv)
  33. {
  34.     int                 listen_handle;       // Handle of accepting socket.
  35.     unsigned short      port = 69;           // Default TFTP port.
  36.     struct sockaddr_in6 server_address;      // Defines listening address.
  37.     struct sockaddr_in6 client_address;      // Defines struct to hold the client address.
  38.     int                 file_handle;         // Handle of the file.
  39.     int                 buffer[BUFFER_SIZE]; // Multipurpose buffer.
  40.     socklen_t           cli_add_len;         // Holds the return value for the client address.
  41.     pid_t               child_id;            // Process ID number of a child process.
  42.  
  43.     cli_add_len = sizeof(client_address);
  44.  
  45.     // Do I have an explicit port number?
  46.     if (argc == 2)
  47.     {
  48.         port = atoi(argv[1]);
  49.     }
  50.  
  51.     // Creates the server socket as an IPv4 UDP socket.
  52.     if ((listen_handle = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
  53.     {
  54.         perror("Unable to create socket");
  55.         return 1;
  56.     }
  57.  
  58.     // Creates the structure for the server socket address
  59.     memset(&server_address, 0, sizeof(server_address));
  60.     server_address.sin6_family      = PF_INET6;
  61.     server_address.sin6_addr = in6addr_any;
  62.     server_address.sin6_port        = htons(port);
  63.  
  64.     if (bind(listen_handle, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
  65.     {
  66.         perror("Unable to bind socket");
  67.         close(listen_handle);
  68.         return 1;
  69.     }
  70.  
  71.     while (1)
  72.     {
  73.         // Call recvfrom() to get a request datagram from the client.
  74.         // This also gives you the client address.
  75.         if ( recvfrom(listen_handle, buffer, BUFFER_SIZE, 0, (struct sockaddr *) &client_address, &cli_add_len) == -1 )
  76.         {
  77.             perror("Request was problematic");
  78.         }
  79.         else
  80.         {
  81.             // Create a child process to handle this client
  82.             if (child_id = 0)
  83.             {
  84.                 // Close the socket used to receive client requests.
  85.                 close(listen_handle);
  86.                 // Create a new socket for communication with this client.
  87.                 if ((listen_handle = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
  88.                 {
  89.                     perror("Unable to create socket");
  90.                     return 1;
  91.                 }
  92.                 // ?? I know that I'm supposed to not use the default port of 69 anymore,
  93.                 // correct? How do I create a new auto assigned port for the child?
  94.  
  95.                 // Extract the file name from the request and open the file.
  96.  
  97.                 // ?? Not sure what to do here...I know I have to somehow extract the file
  98.                 // name from the 3rd byte of the RRQ up to the first null character, I just
  99.                 // am not sure how to go about that.
  100.                 if ( (file_handle = open( file_name, O_RDONLY)) == -1 )
  101.                 {
  102.                     // File failed to open.
  103.                     perror("File failed to open.");
  104.                 }
  105.                 else
  106.                 {
  107.                     send_file(file_handle);
  108.                     // ?? I'm currently building the server.c, and then moving on to the
  109.                     // implementation of the send_file.c. I have to be honest and say that
  110.                     // I'm fairly uncomfortable and unknowing of what I'm supposed to do there.
  111.                     // When I get to that point I'll try and get as far as I can, but just a
  112.                     // heads up that I might need some help there too. Thanks Peter!
  113.                 }
  114.                 exit;
  115.             }
  116.         }
  117.     }
  118.  
  119.     printf("Hello world!\n");
  120.     return EXIT_SUCCESS;
  121. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement