int main(int argc, char const *argv[]) { FILE *fp; int i = 0; char line[MAX_LAST_NAME_SIZE]; clock_t start, end; double cpuTimeUsed1, cpuTimeUsed2; /* check file opening*/ fp = fopen("./dictionary/words.txt", "r"); if (fp == NULL){ printf("cannot open the file\n"); return 0; } /* create hash table. table size is a prime number*/ int tableSize = 42737; hashTable *ht = createHashTable(tableSize); printf("hash table size (prime number) : %d\n", tableSize); /* build the lastNameEntry */ lastNameEntry *pHead, *lne; pHead = (lastNameEntry *) malloc(sizeof(lastNameEntry)); printf("size of entry : %lu bytes\n", sizeof(lastNameEntry)); lne = pHead; lne->pNext = NULL; start = clock(); while (fgets(line, sizeof(line), fp)){ while(line[i] != '\0'){ i++; } line[i-1] = '\0'; i = 0; appendHash(line, ht); } end = clock(); cpuTimeUsed1 = ((double) (end - start)) / CLOCKS_PER_SEC; /* input lastName */ char input[INPUT_SIZE][MAX_LAST_NAME_SIZE] = {"uninvolved","zyxel","whiteshank", "odontomous", "pungoteague", "reweighted", "xiphisternal", "yakattalo"}; /* compute the execution time */ int j; lne = pHead; start = clock(); for(j = 0; j < INPUT_SIZE; j++){ findNameHash(input[j], ht); lne = pHead; } end = clock(); cpuTimeUsed2 = ((double) (end - start)) / CLOCKS_PER_SEC; printf("execution time of appendHash() : %lf\n", cpuTimeUsed1); printf("execution time of findNameHash() : %lf\n", cpuTimeUsed2); /* release the resource */ free(pHead); fclose(fp); return 0; }
/*****************child process encrypt udp connection*******************/ void udp_vpn(int MODE, int Port, char *ip, int port, int pipefd) { struct sockaddr_in sin, sout;//standard in and out sockets address struct ifreq ifr;//virtual nic card socklen_t soutlen; int fd, s, l, i, ready = 0; int status; fd_set fdset; char buf[BUFSIZE], hash[HASHLEN], totalbuf[BUFSIZE]; //set up virtual nic card if ((fd = open("/dev/net/tun", O_RDWR)) < 0) PERROR("open error!!!"); memset(&ifr, 0, sizeof(ifr)); ifr.ifr_flags = IFF_TUN; strncpy(ifr.ifr_name, "toto%d", IFNAMSIZ); if (ioctl(fd, TUNSETIFF, (void *)&ifr) < 0) PERROR("ioctl"); printf("Allocated interface %s. Configure and use it\n", ifr.ifr_name); //create udp socket s = socket(PF_INET, SOCK_DGRAM, 0); sin.sin_family = AF_INET; sin.sin_addr.s_addr = htonl(INADDR_ANY); sin.sin_port = htons(Port); //bind step if (bind(s, (struct sockaddr *)&sin, sizeof(sin)) < 0) PERROR("bind error!!!"); soutlen = sizeof(sout); if (MODE == CLIENT) { sout.sin_family = AF_INET; sout.sin_port = htons(port); inet_aton(ip, &sout.sin_addr); } while (1) { //read the key l = read(pipefd, buf, sizeof(buf)); if (l > 0) { if (buf[0] == 'a') { //disconnect exit(0); } else if (buf[0]=='k') { //getting key memcpy(key, buf + 1, KEYSIZE); ready = 1; } } if (!ready) { sleep(1); continue; } //fd from nic card, s from socket FD_ZERO(&fdset); FD_SET(fd, &fdset); FD_SET(s, &fdset); if (select(fd+s+1, &fdset,NULL,NULL,NULL) < 0) PERROR("select");//select which file descriptor is using //virtual nic card if (FD_ISSET(fd, &fdset)) { if (DEBUG) printf("SENT: "); l = read(fd, buf, BUFSIZE); if (l < 0) PERROR("read"); if (DEBUG) { printf("\nplain payload: "); for(i = 0; i < l; i++) printf("%02x", *(buf+i)); printf("\n"); } genIV(IV); //encrypt, hash, append, and send to client if (do_crypt(key, IV, buf, &l, 1)) { if (DEBUG) { printf("\nencrypted payload: "); for(i = 0; i < l; i++) printf("%02x", *(buf+i)); printf("\n"); } memcpy(totalbuf, IV, IVSIZE); memcpy(totalbuf+IVSIZE, buf, BUFSIZE-IVSIZE); l+=IVSIZE; appendHash(totalbuf, &l); if (DEBUG) { printf("\nfinal payload: "); for(i = 0; i < l; i++) printf("%02x", *(buf+i)); printf("\n"); } //send to client if (sendto(s, totalbuf, l, 0, (struct sockaddr *)&sout, soutlen) < 0) PERROR("sendto"); } else { printf("encrypt error\n"); } } //socket else { if (DEBUG) printf("\n RECEIVED"); l = recvfrom(s, buf, BUFSIZE, 0, (struct sockaddr *)&sout, &soutlen); if (DEBUG) { printf("\n(encrypted) payload: "); for(i = 0; i < l; i++) printf("%02x", *(buf+i)); putchar(10); } //check reliability if (checkHash(buf, &l)) { printf("HASH mismatch.\n"); } else { memcpy(IV, buf, IVSIZE); if (DEBUG) { printf("\nhash checked payload: "); for(i = 0; i < l; i++) printf("%02x", *(buf+i)); putchar(10); } l-=IVSIZE; //decrypt and send to virtual nic card if (do_crypt(key, IV, buf+IVSIZE, &l, 0)) { if (DEBUG) { printf("\nfinal plain payload: "); for(i = 0; i < l; i++) printf("%02x", *(buf+i)); putchar(10); } if (write(fd, buf+IVSIZE, l) < 0) PERROR("write"); } else { printf("decrypt error\n"); } } } } }