#define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include "RSA-Chat.h" #include "text.c" int listen_mode=1;int send_mode=1; int stop_flag=0; struct receive_input{ int sockfd; EVP_PKEY *key; struct addrinfo* result; }; void* receive_f(void* _i){ pthread_setcanceltype(PTHREAD_CANCEL_DEFERRED, NULL); struct receive_input* input = (struct receive_input*)_i; if(input->sockfd<0||!listen_mode){ return NULL; } char buffer[BUFFER]={0}; while(!stop_flag){ ssize_t received = recvfrom(input->sockfd, buffer, sizeof(buffer), 0,NULL, NULL); char* output = malloc(received); int size = text_decrypt(input->key,buffer,output); if(size < 0){ return NULL; } output = realloc(output,size); printf("\033[32m%s\033[0m\n",output); free(output); } return NULL; } enum command_type{help=0,receive,send_c,quit,unknown}; enum command_type command(char* _i){ if(strcmp(_i,"h")==0||strcmp(_i,"help")==0||strcmp(_i,"?")==0){ return help; }else if(strcmp(_i,"r")==0||strcmp(_i,"recv")==0){ return receive; }else if(strcmp(_i,"s")==0||strcmp(_i,"send")==0){ return send_c; }else if(strcmp(_i,"q")==0||strcmp(_i,"quit")==0||strcmp(_i,"exit")==0){ return quit; }else{ return unknown; } } int main(int argn,char** argc){ if(argn==1){ printf("Useage: %s \n",argc[0]); printf("Useage: %s --offline \n",argc[0]); return 0; } FILE* peer_key_file = fopen(argc[2],"r"); EVP_PKEY* local_key = EVP_PKEY_new();char* local_key_c = malloc(BUFFER); EVP_PKEY* peer_key = EVP_PKEY_new();char* peer_key_c = malloc(BUFFER); int peer_bits=0;int local_bits=0; if(peer_key_file==NULL){ printf("Peer public key read failed ,send mode will be disabled.\n"); send_mode=0; }else{ size_t peer_key_file_size = fread(peer_key_c,1,BUFFER,peer_key_file); peer_key_c = realloc(peer_key_c,peer_key_file_size); BIO* peer_bio = BIO_new_mem_buf(peer_key_c,peer_key_file_size); PEM_read_bio_PUBKEY(peer_bio,&peer_key,NULL,NULL); peer_bits = EVP_PKEY_bits(peer_key); fclose(peer_key_file); } FILE* local_key_file = fopen("local.key","r"); if(local_key_file==NULL){ printf("Local private key read failed ,listen/receive mode will be disabled.\n"); listen_mode=0; }else{ size_t local_key_file_size = fread(local_key_c,1,BUFFER,local_key_file); local_key_c = realloc(local_key_c,local_key_file_size); BIO* local_bio = BIO_new_mem_buf(local_key_c,local_key_file_size); PEM_read_bio_PrivateKey(local_bio,&local_key,NULL,NULL); local_bits = EVP_PKEY_bits(local_key); fclose(local_key_file); } free(local_key_c);free(peer_key_c); int sockfd = -1;struct addrinfo* result; if(strcmp(argc[1],"--offline")!=0){ sockfd = socket(AF_INET6,SOCK_DGRAM,0); if(sockfd < 0){ perror("Socket creation failed"); return -1; } int no = 0; if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no))) { perror("Setsockopt IPV6_V6ONLY failed"); close(sockfd); return -1; } unsigned short int port = atoi(argc[1]); if(port>65535){ perror("Wrong port number"); return -1; } struct sockaddr_in6 serverAddr; serverAddr.sin6_family = AF_INET6; serverAddr.sin6_port = htons(port); serverAddr.sin6_addr = in6addr_any; if (bind(sockfd, &serverAddr, sizeof(serverAddr))) { perror("bind failed"); close(sockfd); return 1; } struct addrinfo hints={0}; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; printf("UDP socket opened on port %u\n",port); if(send_mode){ char* buffer = malloc(BUFFER); printf("Please enter :\n"); retry: memset(buffer,0,strlen(buffer)); char peer_port[10]={0}; scanf("%s%s",buffer,peer_port); if (getaddrinfo(buffer,peer_port, &hints, &result)) { perror("Wrong IP or PORT , please try again:\n$ "); goto retry; } memset(buffer,0,BUFFER); free(buffer); } } pthread_t receive_t;struct receive_input receive_i = {sockfd, local_key, result}; pthread_create(&receive_t,NULL,receive_f,&receive_i); char* buffer = malloc(BUFFER); char* ioput = malloc(BUFFER); char input[BUFFER]={0}; while(1){ memset(input,0,BUFFER); scanf("%s",input); switch(command(input)){ case receive: if(!listen_mode){ printf("Receive mode is disabled\n"); break; } printf("Please paste base64 code:\n"); scanf("%s",ioput); base64_decode(ioput,buffer,NULL); memset(ioput,0,BUFFER); int size = text_decrypt(local_key,buffer,ioput); memset(buffer,0,BUFFER); if(size < 0){ perror("Decrypt failed."); }else{ printf("%s\n",ioput); } memset(ioput,0,BUFFER); break; case send_c: if(!send_mode){ printf("Send mode is disabled\n"); break; } printf("Please enter text:\n"); scanf("%s",ioput); if(strlen(ioput)>(peer_bits/8-48)){ perror("Text too long for key of peer.\n"); continue; } size = text_encrypt(peer_key,ioput,buffer); memset(ioput,0,BUFFER); if(size < 0){ perror("Encrypt failed."); }else{ if(sockfd>0){ ssize_t sent = sendto(sockfd, buffer, size, 0,result->ai_addr, result->ai_addrlen); if (sent < 0){ perror("sendto failed"); } } base64_encode(buffer,size,ioput); memset(buffer,0,BUFFER); printf("%s\n",ioput); memset(ioput,0,BUFFER); } break; case quit: stop_flag = 1; close(sockfd); EVP_PKEY_free(local_key); EVP_PKEY_free(peer_key); pthread_cancel(receive_t); pthread_join(receive_t,NULL); return 0; case help: printf("Useage:\n send/s | Send text to remote or stdout with base64.\n recv/r | Decrypt base64 and output into stdout.\n help/h/? | Show this page.\n"); break; default: printf("Useage:\n send/s | Send text to remote or stdout with base64.\n recv/r | Decrypt base64 and output into stdout.\n help/h/? | Show this page.\n"); } } stop_flag = 1; close(sockfd); EVP_PKEY_free(local_key); EVP_PKEY_free(peer_key); pthread_cancel(receive_t); pthread_join(receive_t,NULL); return 0; }