Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*!
- * \file server.c
- * \author Peter C. Chapin, Nathan S. Brown
- * \brief Trivial FTP client
- *
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <sys/socket.h>
- #ifndef S_SPLIT_S // Workaround for splint.
- #include <unistd.h>
- #endif
- #include "server.h"
- #define BUFFER_SIZE 128
- // ============
- // Main Program
- // ============
- void SIGCHLD_handler(int signal_number)
- {
- int status;
- while (waitpid(-1, &status, WNOHANG) > 0) ;
- }
- int main(int argc, char **argv)
- {
- int listen_handle; // Handle of accepting socket.
- unsigned short port = 69; // Default TFTP port.
- struct sockaddr_in6 server_address; // Defines listening address.
- struct sockaddr_in6 client_address; // Defines struct to hold the client address.
- int file_handle; // Handle of the file.
- int buffer[BUFFER_SIZE]; // Multipurpose buffer.
- socklen_t cli_add_len; // Holds the return value for the client address.
- pid_t child_id; // Process ID number of a child process.
- cli_add_len = sizeof(client_address);
- // Do I have an explicit port number?
- if (argc == 2)
- {
- port = atoi(argv[1]);
- }
- // Creates the server socket as an IPv4 UDP socket.
- if ((listen_handle = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
- {
- perror("Unable to create socket");
- return 1;
- }
- // Creates the structure for the server socket address
- memset(&server_address, 0, sizeof(server_address));
- server_address.sin6_family = PF_INET6;
- server_address.sin6_addr = in6addr_any;
- server_address.sin6_port = htons(port);
- if (bind(listen_handle, (struct sockaddr *) &server_address, sizeof(server_address)) < 0)
- {
- perror("Unable to bind socket");
- close(listen_handle);
- return 1;
- }
- while (1)
- {
- // Call recvfrom() to get a request datagram from the client.
- // This also gives you the client address.
- if ( recvfrom(listen_handle, buffer, BUFFER_SIZE, 0, (struct sockaddr *) &client_address, &cli_add_len) == -1 )
- {
- perror("Request was problematic");
- }
- else
- {
- // Create a child process to handle this client
- if (child_id = 0)
- {
- // Close the socket used to receive client requests.
- close(listen_handle);
- // Create a new socket for communication with this client.
- if ((listen_handle = socket(PF_INET6, SOCK_DGRAM, 0)) < 0)
- {
- perror("Unable to create socket");
- return 1;
- }
- // ?? I know that I'm supposed to not use the default port of 69 anymore,
- // correct? How do I create a new auto assigned port for the child?
- // Extract the file name from the request and open the file.
- // ?? Not sure what to do here...I know I have to somehow extract the file
- // name from the 3rd byte of the RRQ up to the first null character, I just
- // am not sure how to go about that.
- if ( (file_handle = open( file_name, O_RDONLY)) == -1 )
- {
- // File failed to open.
- perror("File failed to open.");
- }
- else
- {
- send_file(file_handle);
- // ?? I'm currently building the server.c, and then moving on to the
- // implementation of the send_file.c. I have to be honest and say that
- // I'm fairly uncomfortable and unknowing of what I'm supposed to do there.
- // When I get to that point I'll try and get as far as I can, but just a
- // heads up that I might need some help there too. Thanks Peter!
- }
- exit;
- }
- }
- }
- printf("Hello world!\n");
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement