Guest User

Untitled

a guest
Apr 20th, 2018
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.51 KB | None | 0 0
  1.  
  2. #include <unistd.h>
  3. #include <arpa/inet.h>
  4. #include <netdb.h>
  5. #include <errno.h>
  6. #include <sys/wait.h>
  7. #include <signal.h>
  8.  
  9.  
  10. #include <sys/types.h>
  11. #include <sys/socket.h>
  12. #include <netinet/in.h>
  13. #include <sys/ioctl.h>
  14. #include <net/if.h>
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <string>
  19. #include <pthread.h>
  20. #include <iostream>
  21. #include <fstream>
  22. using namespace std;
  23.  
  24. #define BUFF_SIZE 1024
  25.  
  26. char current_channel[BUFF_SIZE];
  27. char NICK[BUFF_SIZE];
  28. char USER[BUFF_SIZE];
  29. char REALNAME[BUFF_SIZE];
  30. int FMESSAGE=0;
  31.  
  32.  
  33. struct dcc_thread_args
  34.  {
  35.     char *from;
  36.     char *filename;
  37.     unsigned long ip;
  38.     char* port;
  39.     unsigned int filesize;
  40. };
  41.  
  42. struct ThreadArgs
  43. {
  44.     int clntSock;
  45. };
  46.  
  47. struct DCCsendStruct
  48. {
  49.     char *bestand;
  50. };
  51.  
  52. int get_own_ip(){
  53.      int fd;
  54.      struct ifreq ifr;
  55.      fd = socket(AF_INET, SOCK_DGRAM, 0);
  56.      ifr.ifr_addr.sa_family = AF_INET;
  57.      strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
  58.      ioctl(fd, SIOCGIFADDR, &ifr);
  59.      close(fd);
  60.  
  61.      char *data = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
  62.      int waarde = inet_addr(data);
  63.      return htonl(waarde);
  64. }
  65.  
  66. int size(char *file_name){
  67.     FILE *f;
  68.     long f_size;
  69.     try{
  70.         f = fopen(file_name,"r");
  71.         fseek(f,0,SEEK_END);
  72.         f_size = ftell(f);
  73.         rewind(f);
  74.         return f_size;
  75.     }
  76.     catch (int e) {
  77.         printf("file bestaat niet! \r\n");
  78.     }
  79. return 0;
  80. }
  81.  
  82. void substr(char* haystack, int start, int end, char* out) {
  83.     int i;
  84.     int count;
  85.     count = 0;
  86.  
  87.     for(i=start; i<=end; i++) {
  88.         out[count] = haystack[i];
  89.         count++;
  90.     }
  91.     out[count] = '\0';
  92. }
  93.  
  94. void *sendDCCBestand(void *arg) {
  95.     struct DCCsendStruct *info = (DCCsendStruct*) arg;
  96.     int status,s, new_soc;
  97.     struct addrinfo hints;
  98.     struct addrinfo *servinfo, *p;
  99.     socklen_t sin_size;
  100.     struct sockaddr_storage their_addr;
  101.     memset(&hints,0,sizeof hints);
  102.  
  103.  
  104.     hints.ai_family = AF_UNSPEC;
  105.     hints.ai_socktype = SOCK_STREAM;
  106.     hints.ai_flags = AI_PASSIVE;
  107.     if ((status = getaddrinfo("localhost", "48990", &hints, &servinfo)) !=0){
  108.             printf("getaddrinfo error: %s \n", gai_strerror(status));
  109.         }else{
  110.             for (p = servinfo; p != NULL; p = p->ai_next) {
  111.                 if ((s = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
  112.                     perror("server: socket");
  113.                     continue;
  114.                 }
  115.                 if (bind(s, p->ai_addr, p->ai_addrlen) == -1) {
  116.                     close(s);
  117.                     perror("server: bind");
  118.                     continue;
  119.                 }
  120.                 break;
  121.             }
  122.             if (p == NULL) {
  123.                 fprintf(stderr, "server: failed to bind\n");
  124.             }
  125.             freeaddrinfo(servinfo);
  126.             if(listen(s,10)==-1){
  127.                 perror("listen");
  128.             }
  129.             sin_size = sizeof(their_addr);
  130.  
  131.             if((new_soc = accept(s, (struct sockaddr *) &their_addr, &sin_size)) == -1) {
  132.                 perror("accept");
  133.             }
  134.             printf("testing this shit");
  135.             int nbytes;
  136.             long b_size;
  137.             size_t result;
  138.             char buffer[1024];
  139.             FILE *f;
  140.             try {
  141.                 f = fopen(info->bestand, "r");
  142.                 fseek(f, 0, SEEK_END);
  143.                 b_size = ftell(f);
  144.                 rewind(f);
  145.  
  146.                 while((result = fread(buffer,1,sizeof(buffer),f)) != 0){
  147.                     char *bufsend = buffer;
  148.                     do{
  149.                         nbytes = send(new_soc,bufsend,result,0);
  150.                         if (nbytes > 0){
  151.                             bufsend += nbytes;
  152.                             result -= nbytes;
  153.                         }else if(nbytes == -1){
  154.                         throw 1;
  155.                         }
  156.                     }while (result > 0);
  157.                 }
  158.                 fclose(f);
  159.                 printf("Het bestand is verzonden!\r\n");
  160.             }catch (int i){
  161.                 printf("Er is een fout opgetreden tijdens het zenden van het bestand \r\n");
  162.             }
  163.             close(new_soc);
  164.             close(s);
  165.  
  166.         }
  167.     return EXIT_SUCCESS;
  168. }
  169.  
  170. void *receiveDCC(void *ptr) {
  171.     struct dcc_thread_args *args = (struct dcc_thread_args *)ptr;
  172.  
  173.     int sockfd;
  174.     char buf[BUFF_SIZE];
  175.     struct addrinfo hints, *servinfo, *p;
  176.     int rv;
  177.  
  178.     memset(&hints, 0, sizeof hints);
  179.     hints.ai_family = AF_UNSPEC;
  180.     hints.ai_socktype = SOCK_STREAM;
  181.  
  182.     struct in_addr in;
  183.     in.s_addr = args->ip;
  184.     if ((rv = getaddrinfo("127.0.0.1", args->port, &hints, &servinfo)) != 0) {
  185.         fprintf(stderr, "getaddrinfo: %s\r\n", gai_strerror(rv));
  186.         exit(1);
  187.     }
  188.  
  189.     for(p = servinfo; p != NULL; p = p->ai_next) {
  190.         if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
  191.             perror("client: socket");
  192.             continue;
  193.         }
  194.  
  195.         if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
  196.             close(sockfd);
  197.             perror("client: connect");
  198.             continue;
  199.         }
  200.  
  201.         break;
  202.     }
  203.  
  204.     if (p == NULL) {
  205.         fprintf(stderr, "client: failed to connect\n");
  206.         exit(2);
  207.     }
  208.     int res;
  209.     FILE *file;
  210.     file = fopen(args->filename, "wb");
  211.  
  212.     do {
  213.         memset((void*)&buf, 0, BUFF_SIZE);
  214.  
  215.         if ((res = recv(sockfd, &buf, BUFF_SIZE, 0)) == -1) {
  216.             fprintf(stderr, "recv error: %s\r\n", gai_strerror(res));
  217.             close(sockfd);
  218.             exit(1);
  219.         }
  220.  
  221.         fwrite (buf , 1 , sizeof(buf) , file );
  222.  
  223.     } while (res == BUFF_SIZE);
  224.  
  225.     fclose(file);
  226.     printf(": Filetransfer complete: received '%s' from %s\r\n", args->filename, args->from);
  227.     freeaddrinfo(servinfo);
  228.     close(sockfd);
  229. }
  230.  
  231. void *rv_check(void *arg){
  232.     int tempsock = ((struct ThreadArgs *) arg) -> clntSock;
  233.     while(1){
  234.         char msg[BUFF_SIZE];
  235.         char buf[BUFF_SIZE];
  236.         char ping_check[BUFF_SIZE];
  237.         string user_use, first_message;
  238.         size_t found;
  239.         memset(&buf,0,sizeof(buf));
  240.         memset(&ping_check,0,sizeof(ping_check));
  241.         memset(&msg,0,sizeof(msg));
  242.                 int r = recv(tempsock,buf,sizeof (buf),0);
  243.         if(r<0){
  244.             perror("recv error");
  245.         }else{
  246.             strncpy(ping_check,&buf[0],4);
  247.             ping_check[4] ='\0';
  248.             if(strcmp(ping_check,"PING")==0){
  249.                 buf[1]= 'O';
  250.                 int verzenden = send(tempsock,buf, strlen(buf),0);
  251.                 if(verzenden<0){
  252.                     perror("send error");
  253.                 }
  254.             }else if(strstr(buf, "PRIVMSG") != NULL){
  255.                 printf("testing p \r\n");
  256.                 printf("%s \r\n",buf);
  257.                 if(strstr(buf, "DCC SEND") != NULL){
  258.                     printf("testing d \r\n");
  259.                     printf("%s \r\n",buf);
  260.                     int tmp_size;
  261.                     // FROM
  262.                     tmp_size = strcspn(buf, "!")-1;
  263.                     char f_from[tmp_size];
  264.                     strncpy(f_from, buf+1, tmp_size);
  265.                     f_from[tmp_size] = '\0';
  266.  
  267.                     // FILENAME
  268.                     tmp_size = strcspn(strstr(buf, "SEND")+5, " ");
  269.                     char f_name[tmp_size];
  270.                     substr(strstr(buf, "SEND")+5, 0, tmp_size, f_name);
  271.                     f_name[tmp_size] = '\0';
  272.  
  273.                     // LONG IP, PORT, SIZE
  274.                     unsigned long f_ip;
  275.                     char* f_port;
  276.                     unsigned int f_size;
  277.  
  278.                     char* values;
  279.  
  280.                     strcpy(buf, strstr(buf, f_name)+strlen(f_name)+1);
  281.  
  282.                     values = strtok(buf, " ");
  283.                     f_ip = atol(values);
  284.                     values = strtok(NULL, " ");
  285.                     f_port = values;
  286.                     values = strtok(NULL, " ");
  287.                     f_size = atoi(values);
  288.  
  289.                     // START DCC RECEIVE THREAD
  290.                     struct dcc_thread_args args;
  291.  
  292.                     // arguments
  293.                     args.from = f_from;
  294.                     args.filename = f_name;
  295.                     args.ip = ntohl(f_ip);
  296.                     args.port = f_port;
  297.                     args.filesize = f_size;
  298.  
  299.                     pthread_t dcc_thread;
  300.  
  301.                     int ret_dcc;
  302.  
  303.                     ret_dcc = pthread_create( &dcc_thread, NULL, receiveDCC, (void *) &args);
  304.                     pthread_join(dcc_thread, NULL);
  305.                     }
  306.             }else{
  307.  
  308.                 first_message = buf;
  309.                 found = first_message.find(":***");
  310.                 if(found!=string::npos && FMESSAGE==0){
  311.                     FMESSAGE = 1;
  312.                     strcpy(msg,"NICK ");
  313.                     strcat(msg, NICK);
  314.                     strcat(msg, "\r\n");
  315.                     strcat(msg, "USER ");
  316.                     strcat(msg, USER);
  317.                     strcat(msg, " 0 * :");
  318.                     strcat(msg, REALNAME);
  319.                     strcat(msg, "\r\n");
  320.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  321.                     if(verzenden<0){
  322.                         perror("send error");
  323.                     }
  324.                 }
  325.                 user_use = buf;
  326.                 if(user_use.find(":Nickname is already")!=-1){
  327.                     exit(1);
  328.                 }
  329.  
  330.                 printf("%s \n", buf);
  331.             }
  332.         }
  333.     }
  334.     close(tempsock);
  335.     return 0;
  336. }
  337.  
  338. void *input_check(void *arg){
  339.     int tempsock = ((struct ThreadArgs *) arg) -> clntSock;
  340.     char command[BUFF_SIZE];
  341.     char input[BUFF_SIZE];
  342.     char msg[BUFF_SIZE];
  343.     char channel[BUFF_SIZE];
  344.     char reason[BUFF_SIZE];
  345.     char user[BUFF_SIZE];
  346.     char user_remove[BUFF_SIZE];
  347.     char data[BUFF_SIZE];
  348.     char file_name[BUFF_SIZE];
  349.     char ip[BUFF_SIZE];
  350.     char file_size[BUFF_SIZE];
  351.     struct DCCsendStruct *send_arg;
  352.     pthread_t ddc_send_data;
  353.     while(1){
  354.         if(fgets(input, sizeof(input),stdin)){
  355.             strncpy(command,&input[0],7);
  356.             if(strcmp(command,"/part #")==0){
  357.                 if(strlen(current_channel)!=0){
  358.                     strncpy(channel,&input[6],strlen(input)-7);
  359.                     strcpy(msg, "PART ");
  360.                     strcat(msg, channel);
  361.                     strcat(msg, "\r\n");
  362.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  363.                     if(verzenden<0){
  364.                         perror("send error");
  365.                     }
  366.                 }
  367.                 else{
  368.                     printf("first join a channel \n");
  369.                 }
  370.             }
  371.             memset(&command,0,sizeof(command));
  372.             strncpy(command,&input[0],7);
  373.             if(strcmp(command,"/join #")==0){
  374.                 strncpy(channel,&input[6],strlen(input)-7);
  375.                 if(strlen(current_channel)!=0){
  376.                     strcpy(msg, "PART ");
  377.                     strcat(msg, current_channel);
  378.                     strcat(msg, "\r\n");
  379.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  380.                     if(verzenden<0){
  381.                         perror("send error");
  382.                     }
  383.                 }
  384.                 strcpy(current_channel,channel);
  385.                 strcpy(msg, "JOIN ");
  386.                 strcat(msg, channel);
  387.                 strcat(msg, "\r\n");
  388.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  389.                 if(verzenden<0){
  390.                     perror("send error");
  391.                 }
  392.  
  393.             }
  394.             memset(&command,0,sizeof(command));
  395.             strncpy(command,&input[0],6);
  396.             if(strcmp(command,"/names")==0){
  397.                 if(strlen(current_channel)==0){
  398.                 printf("incorrect channel or channel doesn't exists \r\n");
  399.                 }
  400.                 else{
  401.                     strcpy(msg, "NAMES ");
  402.                     strcat(msg, current_channel);
  403.                     strcat(msg, "\r\n");
  404.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  405.                     if(verzenden<0){
  406.                         perror("send error");
  407.                     }
  408.                 }
  409.             }
  410.             memset(&command,0,sizeof(command));
  411.             memset(&user,0,sizeof(user));
  412.             strncpy(command,&input[0],6);
  413.             if(strcmp(command,"/nick ")==0){
  414.                 strncpy(user,&input[5],strlen(input)-6);
  415.                 strcpy(msg, "NICK ");
  416.                 strcat(msg, user);
  417.                 strcat(msg, "\r\n");
  418.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  419.                 if(verzenden<0){
  420.                     perror("send error");
  421.                 }
  422.             }
  423.             memset(&command,0,sizeof(command));
  424.             memset(&user_remove,0,sizeof(user_remove));
  425.             strncpy(command,&input[0],6);
  426.             if(strcmp(command,"/kick ")==0){
  427.                 strncpy(user_remove,&input[5],strlen(input)-6);
  428.                 strcpy(msg, "KICK ");
  429.                 strcat(msg, current_channel);
  430.                 strcat(msg, " ");
  431.                 strcat(msg, user_remove);
  432.                 strcat(msg, "\r\n");
  433.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  434.                 if(verzenden<0){
  435.                     perror("send error");
  436.                 }
  437.             }
  438.             memset(&command,0,sizeof(command));
  439.             strncpy(command,&input[0],5);
  440.             if(strcmp(command,"/quit")==0){
  441.                 strcpy(msg, "QUIT");
  442.                 strcat(msg, "\r\n");
  443.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  444.                 if(verzenden<0){
  445.                     perror("send error");
  446.                 }
  447.                 printf("bye bye, until next time \r\n");
  448.                 exit(1);
  449.             }
  450.             memset(&command,0,sizeof(command));
  451.             strncpy(command,&input[0],5);
  452.             if(strcmp(command,"/list")==0){
  453.                 strcpy(msg, "LIST");
  454.                 strcat(msg, "\r\n");
  455.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  456.                 if(verzenden<0){
  457.                     perror("send error");
  458.                 }
  459.             }
  460.             memset(&command,0,sizeof(command));
  461.             strncpy(command,&input[0],5);
  462.             if(strcmp(command,"/info")==0){
  463.                 strcpy(msg, "INFO");
  464.                 strcat(msg, "\r\n");
  465.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  466.                 if(verzenden<0){
  467.                     perror("send error");
  468.                 }
  469.             }
  470.             memset(&command,0,sizeof(command));
  471.             strncpy(command,&input[0],5);
  472.             if(strcmp(command,"/away")==0){
  473.                 if(strlen(input)<=6){
  474.                     strcpy(msg, "AWAY");
  475.                     strcat(msg, "\r\n");
  476.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  477.                     if(verzenden<0){
  478.                         perror("send error");
  479.                     }
  480.                 }
  481.                 else{
  482.                     strncpy(reason,&input[4],strlen(input)-5);
  483.                     strcpy(msg, "AWAY ");
  484.                     strcat(msg, reason);
  485.                     strcat(msg, "\r\n");
  486.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  487.                     if(verzenden<0){
  488.                         perror("send error");
  489.                     }
  490.                 }
  491.             }
  492.             memset(&command,0,sizeof(command));
  493.             memset(&user,0,sizeof(user));
  494.             memset(&ip,0,sizeof(ip));
  495.             memset(&file_size,0,sizeof(file_size));
  496.             strncpy(command,&input[0],9);
  497.             if(strcmp(command,"/dcc send")==0){
  498.                 strncpy(data,&input[10],strlen(input)-11);
  499.                 string d = data;
  500.                 int location = d.find(' ');
  501.                 strncpy(user,&data[0],location);
  502.                 strncpy(file_name,&data[location+1],(strlen(data)-(location+1)));
  503.                 sprintf(ip,"%i",get_own_ip());
  504.                 sprintf(file_size,"%i",size(file_name));
  505.                 strcpy(msg, "PRIVMSG ");
  506.                 strcat(msg, user);
  507.                 strcat(msg, " :\\001");
  508.                 strcat(msg, "DCC SEND ");
  509.                 strcat(msg, file_name);
  510.                 strcat(msg, " ");
  511.                 //strcat(msg, ip);
  512.                 strcat(msg, "3232238042");
  513.                 strcat(msg, " ");
  514.                 strcat(msg, "48990");
  515.                 strcat(msg, " ");
  516.                 strcat(msg, file_size);
  517.                 strcat(msg, "\\001");
  518.                 strcat(msg, "\r\n");
  519.                 printf("%s",msg);
  520.                 if ((send_arg = (struct DCCsendStruct *) malloc(sizeof(struct DCCsendStruct))) == NULL) {
  521.                             perror("malloc() failed");
  522.                 }
  523.                 send_arg -> bestand = file_name;
  524.                 if ((pthread_create(&ddc_send_data, NULL, sendDCCBestand, (void *) send_arg)) == -1) {
  525.                     printf("pthread error");
  526.                     exit(1);
  527.                 }
  528.                 int verzenden = send(tempsock,msg, strlen(msg),0);
  529.                 if(verzenden<0){
  530.                     perror("send error");
  531.                 }
  532.             }
  533.             memset(&command,0,sizeof(command));
  534.             strncpy(command,&input[0],1);
  535.             if(strcmp(command,"/")!=0){
  536.                 if(strlen(current_channel)!=0){
  537.                     strcpy(msg, "PRIVMSG ");
  538.                     strcat(msg, current_channel);
  539.                     strcat(msg, " :");
  540.                     strcat(msg, input);
  541.                     strcat(msg, "\r\n");
  542.                     int verzenden = send(tempsock,msg, strlen(msg),0);
  543.                     if(verzenden<0){
  544.                         perror("send error");
  545.                     }
  546.                 }
  547.                 else{
  548.                     printf("first join a channel \n");
  549.                 }
  550.             }
  551.         }
  552.         memset(&user,0,sizeof(user));
  553.         memset(&input,0,sizeof(input));
  554.         memset(&msg,0,sizeof(msg));
  555.         memset(&command,0,sizeof(command));
  556.         memset(&channel,0,sizeof(channel));
  557.         memset(&reason,0,sizeof(reason));
  558.     }
  559.     return 0;
  560. }
  561.  
  562. int main(void){
  563.     int s;
  564.     int status;
  565.     struct addrinfo hints;
  566.     struct addrinfo *servinfo;
  567.     pthread_t rcv_threads, input_threads;
  568.     char server[BUFF_SIZE];
  569.     struct ThreadArgs *threadArgs;
  570.     memset(&hints,0,sizeof hints);
  571.  
  572.     hints.ai_family = AF_UNSPEC;
  573.     hints.ai_socktype = SOCK_STREAM;
  574.  
  575.     printf("What server do you want to connect to? \n");
  576.     scanf("%s",server);
  577.     printf("what's your nickname? \n");
  578.     scanf("%s",NICK);
  579.     printf("what's your username? \n");
  580.     scanf("%s",USER);
  581.     printf("what's your real name? \n");
  582.     scanf("%s",REALNAME);
  583.  
  584.     if ((status = getaddrinfo(server, "6667", &hints, &servinfo)) !=0){
  585.             printf("getaddrinfo error: %s \n", gai_strerror(status));
  586.         }else{
  587.  
  588.             s = socket(servinfo->ai_family, servinfo->ai_socktype,servinfo->ai_protocol);
  589.             if(s<0)
  590.             {
  591.                 perror("socket error");
  592.             }
  593.             else{
  594.                 bind(s,servinfo->ai_addr,servinfo->ai_addrlen);
  595.                 int c = connect(s,servinfo->ai_addr,servinfo->ai_addrlen);
  596.  
  597.                 if(c<0){
  598.                     perror("connect error");
  599.                 }
  600.                 else{
  601.                     if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) == NULL) {
  602.                                 perror("malloc() failed");
  603.                     }
  604.                     threadArgs -> clntSock = s;
  605.                     if ((pthread_create(&rcv_threads, NULL, rv_check, (void *) threadArgs)) == -1) {
  606.                         printf("pthread error");
  607.                         exit(1);
  608.                     }
  609.                     if ((pthread_create(&input_threads, NULL, input_check, (void *) threadArgs)) == -1) {
  610.                         printf("pthread error");
  611.                         exit(1);
  612.                     }
  613.                     while(1){
  614.  
  615.                     }
  616.                 }
  617.             }
  618.  
  619.         }
  620.     return EXIT_SUCCESS;
  621. }
Add Comment
Please, Sign In to add comment