Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <arpa/inet.h>
- #include <netdb.h>
- #include <errno.h>
- #include <sys/wait.h>
- #include <signal.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netinet/in.h>
- #include <sys/ioctl.h>
- #include <net/if.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <string>
- #include <pthread.h>
- #include <iostream>
- #include <fstream>
- using namespace std;
- #define BUFF_SIZE 1024
- char current_channel[BUFF_SIZE];
- char NICK[BUFF_SIZE];
- char USER[BUFF_SIZE];
- char REALNAME[BUFF_SIZE];
- int FMESSAGE=0;
- struct dcc_thread_args
- {
- char *from;
- char *filename;
- unsigned long ip;
- char* port;
- unsigned int filesize;
- };
- struct ThreadArgs
- {
- int clntSock;
- };
- struct DCCsendStruct
- {
- char *bestand;
- };
- int get_own_ip(){
- int fd;
- struct ifreq ifr;
- fd = socket(AF_INET, SOCK_DGRAM, 0);
- ifr.ifr_addr.sa_family = AF_INET;
- strncpy(ifr.ifr_name, "eth0", IFNAMSIZ-1);
- ioctl(fd, SIOCGIFADDR, &ifr);
- close(fd);
- char *data = inet_ntoa(((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr);
- int waarde = inet_addr(data);
- return htonl(waarde);
- }
- int size(char *file_name){
- FILE *f;
- long f_size;
- try{
- f = fopen(file_name,"r");
- fseek(f,0,SEEK_END);
- f_size = ftell(f);
- rewind(f);
- return f_size;
- }
- catch (int e) {
- printf("file bestaat niet! \r\n");
- }
- return 0;
- }
- void substr(char* haystack, int start, int end, char* out) {
- int i;
- int count;
- count = 0;
- for(i=start; i<=end; i++) {
- out[count] = haystack[i];
- count++;
- }
- out[count] = '\0';
- }
- void *sendDCCBestand(void *arg) {
- struct DCCsendStruct *info = (DCCsendStruct*) arg;
- int status,s, new_soc;
- struct addrinfo hints;
- struct addrinfo *servinfo, *p;
- socklen_t sin_size;
- struct sockaddr_storage their_addr;
- memset(&hints,0,sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- hints.ai_flags = AI_PASSIVE;
- if ((status = getaddrinfo("localhost", "48990", &hints, &servinfo)) !=0){
- printf("getaddrinfo error: %s \n", gai_strerror(status));
- }else{
- for (p = servinfo; p != NULL; p = p->ai_next) {
- if ((s = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
- perror("server: socket");
- continue;
- }
- if (bind(s, p->ai_addr, p->ai_addrlen) == -1) {
- close(s);
- perror("server: bind");
- continue;
- }
- break;
- }
- if (p == NULL) {
- fprintf(stderr, "server: failed to bind\n");
- }
- freeaddrinfo(servinfo);
- if(listen(s,10)==-1){
- perror("listen");
- }
- sin_size = sizeof(their_addr);
- if((new_soc = accept(s, (struct sockaddr *) &their_addr, &sin_size)) == -1) {
- perror("accept");
- }
- printf("testing this shit");
- int nbytes;
- long b_size;
- size_t result;
- char buffer[1024];
- FILE *f;
- try {
- f = fopen(info->bestand, "r");
- fseek(f, 0, SEEK_END);
- b_size = ftell(f);
- rewind(f);
- while((result = fread(buffer,1,sizeof(buffer),f)) != 0){
- char *bufsend = buffer;
- do{
- nbytes = send(new_soc,bufsend,result,0);
- if (nbytes > 0){
- bufsend += nbytes;
- result -= nbytes;
- }else if(nbytes == -1){
- throw 1;
- }
- }while (result > 0);
- }
- fclose(f);
- printf("Het bestand is verzonden!\r\n");
- }catch (int i){
- printf("Er is een fout opgetreden tijdens het zenden van het bestand \r\n");
- }
- close(new_soc);
- close(s);
- }
- return EXIT_SUCCESS;
- }
- void *receiveDCC(void *ptr) {
- struct dcc_thread_args *args = (struct dcc_thread_args *)ptr;
- int sockfd;
- char buf[BUFF_SIZE];
- struct addrinfo hints, *servinfo, *p;
- int rv;
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- struct in_addr in;
- in.s_addr = args->ip;
- if ((rv = getaddrinfo("127.0.0.1", args->port, &hints, &servinfo)) != 0) {
- fprintf(stderr, "getaddrinfo: %s\r\n", gai_strerror(rv));
- exit(1);
- }
- for(p = servinfo; p != NULL; p = p->ai_next) {
- if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
- perror("client: socket");
- continue;
- }
- if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
- close(sockfd);
- perror("client: connect");
- continue;
- }
- break;
- }
- if (p == NULL) {
- fprintf(stderr, "client: failed to connect\n");
- exit(2);
- }
- int res;
- FILE *file;
- file = fopen(args->filename, "wb");
- do {
- memset((void*)&buf, 0, BUFF_SIZE);
- if ((res = recv(sockfd, &buf, BUFF_SIZE, 0)) == -1) {
- fprintf(stderr, "recv error: %s\r\n", gai_strerror(res));
- close(sockfd);
- exit(1);
- }
- fwrite (buf , 1 , sizeof(buf) , file );
- } while (res == BUFF_SIZE);
- fclose(file);
- printf(": Filetransfer complete: received '%s' from %s\r\n", args->filename, args->from);
- freeaddrinfo(servinfo);
- close(sockfd);
- }
- void *rv_check(void *arg){
- int tempsock = ((struct ThreadArgs *) arg) -> clntSock;
- while(1){
- char msg[BUFF_SIZE];
- char buf[BUFF_SIZE];
- char ping_check[BUFF_SIZE];
- string user_use, first_message;
- size_t found;
- memset(&buf,0,sizeof(buf));
- memset(&ping_check,0,sizeof(ping_check));
- memset(&msg,0,sizeof(msg));
- int r = recv(tempsock,buf,sizeof (buf),0);
- if(r<0){
- perror("recv error");
- }else{
- strncpy(ping_check,&buf[0],4);
- ping_check[4] ='\0';
- if(strcmp(ping_check,"PING")==0){
- buf[1]= 'O';
- int verzenden = send(tempsock,buf, strlen(buf),0);
- if(verzenden<0){
- perror("send error");
- }
- }else if(strstr(buf, "PRIVMSG") != NULL){
- printf("testing p \r\n");
- printf("%s \r\n",buf);
- if(strstr(buf, "DCC SEND") != NULL){
- printf("testing d \r\n");
- printf("%s \r\n",buf);
- int tmp_size;
- // FROM
- tmp_size = strcspn(buf, "!")-1;
- char f_from[tmp_size];
- strncpy(f_from, buf+1, tmp_size);
- f_from[tmp_size] = '\0';
- // FILENAME
- tmp_size = strcspn(strstr(buf, "SEND")+5, " ");
- char f_name[tmp_size];
- substr(strstr(buf, "SEND")+5, 0, tmp_size, f_name);
- f_name[tmp_size] = '\0';
- // LONG IP, PORT, SIZE
- unsigned long f_ip;
- char* f_port;
- unsigned int f_size;
- char* values;
- strcpy(buf, strstr(buf, f_name)+strlen(f_name)+1);
- values = strtok(buf, " ");
- f_ip = atol(values);
- values = strtok(NULL, " ");
- f_port = values;
- values = strtok(NULL, " ");
- f_size = atoi(values);
- // START DCC RECEIVE THREAD
- struct dcc_thread_args args;
- // arguments
- args.from = f_from;
- args.filename = f_name;
- args.ip = ntohl(f_ip);
- args.port = f_port;
- args.filesize = f_size;
- pthread_t dcc_thread;
- int ret_dcc;
- ret_dcc = pthread_create( &dcc_thread, NULL, receiveDCC, (void *) &args);
- pthread_join(dcc_thread, NULL);
- }
- }else{
- first_message = buf;
- found = first_message.find(":***");
- if(found!=string::npos && FMESSAGE==0){
- FMESSAGE = 1;
- strcpy(msg,"NICK ");
- strcat(msg, NICK);
- strcat(msg, "\r\n");
- strcat(msg, "USER ");
- strcat(msg, USER);
- strcat(msg, " 0 * :");
- strcat(msg, REALNAME);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- user_use = buf;
- if(user_use.find(":Nickname is already")!=-1){
- exit(1);
- }
- printf("%s \n", buf);
- }
- }
- }
- close(tempsock);
- return 0;
- }
- void *input_check(void *arg){
- int tempsock = ((struct ThreadArgs *) arg) -> clntSock;
- char command[BUFF_SIZE];
- char input[BUFF_SIZE];
- char msg[BUFF_SIZE];
- char channel[BUFF_SIZE];
- char reason[BUFF_SIZE];
- char user[BUFF_SIZE];
- char user_remove[BUFF_SIZE];
- char data[BUFF_SIZE];
- char file_name[BUFF_SIZE];
- char ip[BUFF_SIZE];
- char file_size[BUFF_SIZE];
- struct DCCsendStruct *send_arg;
- pthread_t ddc_send_data;
- while(1){
- if(fgets(input, sizeof(input),stdin)){
- strncpy(command,&input[0],7);
- if(strcmp(command,"/part #")==0){
- if(strlen(current_channel)!=0){
- strncpy(channel,&input[6],strlen(input)-7);
- strcpy(msg, "PART ");
- strcat(msg, channel);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- else{
- printf("first join a channel \n");
- }
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],7);
- if(strcmp(command,"/join #")==0){
- strncpy(channel,&input[6],strlen(input)-7);
- if(strlen(current_channel)!=0){
- strcpy(msg, "PART ");
- strcat(msg, current_channel);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- strcpy(current_channel,channel);
- strcpy(msg, "JOIN ");
- strcat(msg, channel);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],6);
- if(strcmp(command,"/names")==0){
- if(strlen(current_channel)==0){
- printf("incorrect channel or channel doesn't exists \r\n");
- }
- else{
- strcpy(msg, "NAMES ");
- strcat(msg, current_channel);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- }
- memset(&command,0,sizeof(command));
- memset(&user,0,sizeof(user));
- strncpy(command,&input[0],6);
- if(strcmp(command,"/nick ")==0){
- strncpy(user,&input[5],strlen(input)-6);
- strcpy(msg, "NICK ");
- strcat(msg, user);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- memset(&command,0,sizeof(command));
- memset(&user_remove,0,sizeof(user_remove));
- strncpy(command,&input[0],6);
- if(strcmp(command,"/kick ")==0){
- strncpy(user_remove,&input[5],strlen(input)-6);
- strcpy(msg, "KICK ");
- strcat(msg, current_channel);
- strcat(msg, " ");
- strcat(msg, user_remove);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],5);
- if(strcmp(command,"/quit")==0){
- strcpy(msg, "QUIT");
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- printf("bye bye, until next time \r\n");
- exit(1);
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],5);
- if(strcmp(command,"/list")==0){
- strcpy(msg, "LIST");
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],5);
- if(strcmp(command,"/info")==0){
- strcpy(msg, "INFO");
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],5);
- if(strcmp(command,"/away")==0){
- if(strlen(input)<=6){
- strcpy(msg, "AWAY");
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- else{
- strncpy(reason,&input[4],strlen(input)-5);
- strcpy(msg, "AWAY ");
- strcat(msg, reason);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- }
- memset(&command,0,sizeof(command));
- memset(&user,0,sizeof(user));
- memset(&ip,0,sizeof(ip));
- memset(&file_size,0,sizeof(file_size));
- strncpy(command,&input[0],9);
- if(strcmp(command,"/dcc send")==0){
- strncpy(data,&input[10],strlen(input)-11);
- string d = data;
- int location = d.find(' ');
- strncpy(user,&data[0],location);
- strncpy(file_name,&data[location+1],(strlen(data)-(location+1)));
- sprintf(ip,"%i",get_own_ip());
- sprintf(file_size,"%i",size(file_name));
- strcpy(msg, "PRIVMSG ");
- strcat(msg, user);
- strcat(msg, " :\\001");
- strcat(msg, "DCC SEND ");
- strcat(msg, file_name);
- strcat(msg, " ");
- //strcat(msg, ip);
- strcat(msg, "3232238042");
- strcat(msg, " ");
- strcat(msg, "48990");
- strcat(msg, " ");
- strcat(msg, file_size);
- strcat(msg, "\\001");
- strcat(msg, "\r\n");
- printf("%s",msg);
- if ((send_arg = (struct DCCsendStruct *) malloc(sizeof(struct DCCsendStruct))) == NULL) {
- perror("malloc() failed");
- }
- send_arg -> bestand = file_name;
- if ((pthread_create(&ddc_send_data, NULL, sendDCCBestand, (void *) send_arg)) == -1) {
- printf("pthread error");
- exit(1);
- }
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- memset(&command,0,sizeof(command));
- strncpy(command,&input[0],1);
- if(strcmp(command,"/")!=0){
- if(strlen(current_channel)!=0){
- strcpy(msg, "PRIVMSG ");
- strcat(msg, current_channel);
- strcat(msg, " :");
- strcat(msg, input);
- strcat(msg, "\r\n");
- int verzenden = send(tempsock,msg, strlen(msg),0);
- if(verzenden<0){
- perror("send error");
- }
- }
- else{
- printf("first join a channel \n");
- }
- }
- }
- memset(&user,0,sizeof(user));
- memset(&input,0,sizeof(input));
- memset(&msg,0,sizeof(msg));
- memset(&command,0,sizeof(command));
- memset(&channel,0,sizeof(channel));
- memset(&reason,0,sizeof(reason));
- }
- return 0;
- }
- int main(void){
- int s;
- int status;
- struct addrinfo hints;
- struct addrinfo *servinfo;
- pthread_t rcv_threads, input_threads;
- char server[BUFF_SIZE];
- struct ThreadArgs *threadArgs;
- memset(&hints,0,sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- printf("What server do you want to connect to? \n");
- scanf("%s",server);
- printf("what's your nickname? \n");
- scanf("%s",NICK);
- printf("what's your username? \n");
- scanf("%s",USER);
- printf("what's your real name? \n");
- scanf("%s",REALNAME);
- if ((status = getaddrinfo(server, "6667", &hints, &servinfo)) !=0){
- printf("getaddrinfo error: %s \n", gai_strerror(status));
- }else{
- s = socket(servinfo->ai_family, servinfo->ai_socktype,servinfo->ai_protocol);
- if(s<0)
- {
- perror("socket error");
- }
- else{
- bind(s,servinfo->ai_addr,servinfo->ai_addrlen);
- int c = connect(s,servinfo->ai_addr,servinfo->ai_addrlen);
- if(c<0){
- perror("connect error");
- }
- else{
- if ((threadArgs = (struct ThreadArgs *) malloc(sizeof(struct ThreadArgs))) == NULL) {
- perror("malloc() failed");
- }
- threadArgs -> clntSock = s;
- if ((pthread_create(&rcv_threads, NULL, rv_check, (void *) threadArgs)) == -1) {
- printf("pthread error");
- exit(1);
- }
- if ((pthread_create(&input_threads, NULL, input_check, (void *) threadArgs)) == -1) {
- printf("pthread error");
- exit(1);
- }
- while(1){
- }
- }
- }
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment