Advertisement
Guest User

Untitled

a guest
Jun 25th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.39 KB | None | 0 0
  1. #include "p2p.h"
  2.  
  3. /**
  4.  GLOBAL VARIABLES; do not uncomment
  5.  Sourced in p2p.h
  6. **/
  7. //struct p2p_peer * p2p_peers = NULL;
  8. //struct p2p_file * p2p_files = NULL;
  9. int main(int argc, char *argv[])
  10. {
  11.   if(argc == 1) {
  12.     printf("usage: ./p2p port [peer socket] [filename]\n");
  13.     exit(1);
  14.   }
  15.  
  16.   int listeningPort = atoi(argv[1]);
  17.  
  18.   if(listeningPort == 0 || listeningPort < 1025 || listeningPort > 65536) {
  19.     printf("error: use a port in the unreserved range\n");
  20.     exit(1);
  21.   } else {
  22.     printf("Listening port -> %d/udp\n", listeningPort);
  23.   }
  24.  
  25.   int countPeers = 0;
  26.   int countFiles = 0;
  27.   int loop = 0;
  28.  
  29.   p2p_peers = (struct p2p_peer *) malloc(sizeof(struct p2p_peer));
  30.   memset(p2p_peers, 0, sizeof(struct p2p_peer));
  31.  
  32.   struct p2p_peer * temp_peer = p2p_peers;
  33.   int isFirstPeer = 1;
  34.  
  35.   p2p_files = (struct p2p_file *) malloc(sizeof(struct p2p_file));
  36.   memset(p2p_files, 0, sizeof(struct p2p_file));
  37.  
  38.   struct p2p_file * temp_file = p2p_files;
  39.   int isFirstFile = 1;
  40.  
  41.   for(loop = 2; loop < argc; loop++) {
  42.     int index = containsChar(argv[loop], '/');
  43.     if(index >= 0) {
  44.       struct p2p_peer * p = createPeer(argv[loop]);
  45.       if(p != NULL) {
  46.         if(isPeerInList(p2p_peers, p) == 0) {
  47.           if(isFirstPeer == 1) {
  48.             p2p_peers = p;
  49.             temp_peer = p2p_peers;
  50.             isFirstPeer = 0;
  51.           } else {
  52.             temp_peer->next_peer = p;
  53.             temp_peer = temp_peer->next_peer;
  54.           }
  55.           countPeers++;
  56.         }
  57.       }
  58.       continue;
  59.     } else {
  60.       struct p2p_file * f = createFile(argv[loop]);
  61.       if(f != NULL) {
  62.         if(isFileInList(p2p_files, f) == 0) {
  63.           if(isFirstFile == 1) {
  64.             p2p_files = f;
  65.             temp_file = p2p_files;
  66.             isFirstFile = 0;
  67.           } else {
  68.             temp_file->next_file = f;
  69.             temp_file = temp_file->next_file;
  70.           }
  71.           countFiles++;
  72.         }
  73.       }
  74.     }
  75.   }
  76.  
  77.   if(countPeers > 0) {
  78.     temp_peer = p2p_peers;
  79.     char tempIp[INET_ADDRSTRLEN];
  80.     int tempPort;
  81.     do {
  82.       tempPort = ntohs(temp_peer->socket.sin_port);
  83.       inet_ntop(AF_INET, &(temp_peer->socket.sin_addr), tempIp, INET_ADDRSTRLEN);
  84.       printf("Loaded peer -> %s:%d\n", tempIp, tempPort);
  85.       if(temp_peer->next_peer != NULL)
  86.         temp_peer = temp_peer->next_peer;
  87.       else
  88.         temp_peer = NULL;
  89.     } while(temp_peer != NULL);
  90.   }
  91.  
  92.   if(countFiles > 0) {
  93.     temp_file = p2p_files;
  94.     do {
  95.       printf("Loaded file -> %s\n", temp_file->filename);
  96.       if(temp_file->next_file != NULL)
  97.         temp_file = temp_file->next_file;
  98.       else
  99.         temp_file = NULL;
  100.     } while(temp_file != NULL);
  101.   }
  102.  
  103.   /**
  104.    Create UDP Handler
  105.   **/
  106.  
  107.   UDPHandler udphandler;
  108.   udphandler.ret_listening_sockfd = ret_udp_sockfd;
  109.   udphandler.sockfd = udphandler.ret_listening_sockfd("0.0.0.0", argv[1]);
  110.  
  111.   /**
  112.    Spawn threads
  113.   **/
  114.  
  115.   pthread_t tids[3];
  116.  
  117.   //IOHandler iohandler;
  118.   //pthread_create(&tids[0], NULL, handle_stdin, (void *)&iohandler);
  119.  
  120.   TOHandler timeouthandler;
  121.   pthread_create(&tids[0],NULL,handle_timeouts,(void *)&timeouthandler);
  122.  
  123.   pthread_create(&tids[0],NULL,handle_p2p_client,(void *)&udphandler);
  124.  
  125.   /**
  126.    Listen from stdin
  127.   **/
  128.  
  129.   char input[40];
  130.   printf("Type !quit <enter> to quit p2p\n");
  131.   while(1) {
  132.     memset(&input, 0, 40);
  133.     getInput(input, 40);
  134.  
  135.     if(strncmp(input,"!quit",5) == 0) {
  136.       printf("Quitting...\n");
  137.       break;
  138.     } else {
  139.       if(validFilename(input) == 0) {
  140.         printf("Error: Invalid filename %s\n", input);
  141.       } else {
  142.         if(isFilenameInList(input) == 1) {
  143.           printf("Success: Requested content '%s' FOUND in the LOCAL cache!\n", input);
  144.         } else if(0) { // check content cache
  145.  
  146.         } else if(0) { // check data cache
  147.  
  148.         } else if(0) { // perform "send a request message"
  149.  
  150.         } else {
  151.           printf("Failure: Requested content '%s' NOT found in any cache or remote peer.\n", input);
  152.         }
  153.       }
  154.     }
  155.   }
  156.  
  157.   /**
  158.    Teardown of program... good bye!
  159.   **/
  160.   temp_peer = p2p_peers;
  161.   do {
  162.     free(temp_peer);
  163.     if(temp_peer->next_peer != NULL)
  164.       temp_peer = temp_peer->next_peer;
  165.     else
  166.       temp_peer = NULL;
  167.   } while(temp_peer != NULL);
  168.  
  169.   temp_file = p2p_files;
  170.   do {
  171.     free(temp_file);
  172.     if(temp_file->next_file != NULL)
  173.       temp_file = temp_file->next_file;
  174.     else
  175.       temp_file = NULL;
  176.   } while(temp_file != NULL);
  177.  
  178.   return 0;
  179. }
  180.  
  181. struct p2p_peer * createPeer(char * argument) {
  182.   int index = containsChar(argument, '/');
  183.   char * ip   = getSubstring(argument, 0, index-1);
  184.   char * port = getSubstring(argument, index+1, strlen(argument));
  185.  
  186.   struct p2p_peer * rv = (struct p2p_peer *) malloc(sizeof(struct p2p_peer));
  187.   memset(rv, 0, sizeof(struct p2p_peer));
  188.  
  189.   if(atoi(port) < 1025 || atoi(port) > 65536) {
  190.     printf("error: the argument %s must use a port in the unreserved range\n", argument);
  191.     exit(1);
  192.   } else {
  193.     rv->socket.sin_port = htons(atoi(port));
  194.   }
  195.  
  196.   if(inet_pton(AF_INET, ip, &(rv->socket.sin_addr)) != 1) {
  197.     printf("error: the argument %s must use a valid ip address\n", argument);
  198.     exit(1);
  199.   }
  200.  
  201.   return rv;
  202. }
  203.  
  204. /**
  205.  Check to see if the peer is already added to the list
  206.  Returns 0 if the peer_ip is not in the list
  207.  Returns 1 if the peer_ip IS in the list
  208.  Returns < 0 if an error has occurred
  209. **/
  210. int isPeerInList(struct p2p_peer * peers, struct p2p_peer * peer) {
  211.   struct p2p_peer * temp = peers;
  212.  
  213.   if(temp == NULL && peer != NULL)
  214.     return 0;
  215.  
  216.   if(peer == NULL)
  217.     return -1; /* peer should NOT be NULL */
  218.  
  219.   if(temp != NULL && peer != NULL) {
  220.     do {
  221.       if(peer->socket.sin_addr.s_addr == temp->socket.sin_addr.s_addr)
  222.         if(peer->socket.sin_port == temp->socket.sin_port)
  223.           return 1;
  224.       if(temp->next_peer != NULL)
  225.         temp = temp->next_peer;
  226.       else
  227.         temp = NULL;
  228.     } while(temp != NULL);
  229.     return 0;
  230.   }
  231.   return -1; /* code should not be reached */
  232. }
  233.  
  234. struct p2p_file * createFile(char * argument) {
  235.   struct p2p_file * rv = (struct p2p_file *) malloc(sizeof(struct p2p_file));
  236.   memset(rv, 0, sizeof(struct p2p_file));
  237.  
  238.   if(validFilename(argument) == 0) {
  239.     printf("error: invalid filename %s\n", argument);
  240.     exit(1);
  241.   }
  242.  
  243.   FILE * fileio;
  244.   fileio = fopen(argument, "r");
  245.   if(fileio == NULL) {
  246.     printf("error: missing a content file called %s\n", argument);
  247.     exit(1);
  248.   } else {
  249.     fclose(fileio);
  250.   }
  251.  
  252.   strcpy(rv->filename, argument);
  253.   return rv;
  254. }
  255.  
  256. /**
  257.  Check to see if the peer is already added to the list
  258.  Returns 0 if the peer_ip is not in the list
  259.  Returns 1 if the peer_ip IS in the list
  260.  Returns < 0 if an error has occurred
  261. **/
  262. int isFileInList(struct p2p_file * files, struct p2p_file * file) {
  263.   struct p2p_file * temp = files;
  264.  
  265.   if(temp == NULL && file != NULL)
  266.     return 0;
  267.  
  268.   if(file == NULL)
  269.     return -1; /* file should NOT be NULL */
  270.  
  271.   if(temp != NULL && file != NULL) {
  272.     do {
  273.       if(strcmp(temp->filename,file->filename) == 0)
  274.         return 1;
  275.       if(temp->next_file != NULL)
  276.         temp = temp->next_file;
  277.       else
  278.         temp = NULL;
  279.     } while(temp != NULL);
  280.     return 0;
  281.   }
  282.   return -1; /* code should not be reached */
  283. }
  284.  
  285. /**
  286.  Pass in a peer and get the port as an int
  287. **/
  288. int getPeerPort(struct p2p_peer * peer) {
  289.   if(peer == NULL)
  290.     return -1;
  291.   return ntohs(peer->socket.sin_port);
  292. }
  293.  
  294. /**
  295.  Pass in a peer and get the ip as a malloc'd char *
  296. **/
  297. char * getPeerIp(struct p2p_peer * peer) {
  298.   if(peer == NULL)
  299.     return NULL;
  300.  
  301.   char * rv = (char *) malloc(INET_ADDRSTRLEN);
  302.   if(inet_ntop(AF_INET, &(peer->socket.sin_addr), rv, INET_ADDRSTRLEN) == NULL)
  303.     return NULL;
  304.   else
  305.     return rv;
  306. }
  307.  
  308. /**
  309.  Helper function to read from stdin
  310. **/
  311. void getInput(char arrayInput [], int arrayLength)
  312. {
  313.   int tempInput;
  314.   int tempCounter = 0;
  315.   while((tempInput = fgetc(stdin)) != 10) {
  316.     if(arrayLength-1 > tempCounter) {
  317.       arrayInput[tempCounter] = tempInput;
  318.       tempCounter++;
  319.     }
  320.   }
  321.   arrayInput[tempCounter] = 0;
  322. }
  323.  
  324. /**
  325.  Helper function check a filename from local_content
  326. **/
  327. int isFilenameInList(const char * filename) {
  328.   struct p2p_file f;
  329.   strcpy(f.filename, filename);
  330.   int result = isFileInList(p2p_files, &f);
  331.   if(result == 0)
  332.     return 0;
  333.   else if(result == 1)
  334.     return 1;
  335.   else
  336.     return -1;
  337. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement