int runDaemon(int debug) { char packetPtr[PACKETLEN]; size_t packetSize; struct sockaddr_in sa; if (checkOtherProcess()) return (EXIT_FAILURE); signal(SIGTERM, sigHandler); signal(SIGINT, sigHandler); signal(SIGUSR1, sigHandler); if (!debug) { daemon(1, 1); if (savePid()) return EXIT_FAILURE; } fprintf(stderr, "Daemon started\n"); initConnection(&sa); while (1) { setClient(acceptClient(&sa)); bzero(packetPtr, PACKETLEN); getPacket(packetPtr, &packetSize); handlePacket(packetPtr, packetSize); setClient(-1); } setSock(-1); return EXIT_SUCCESS; }
void push(void){ char fname[2048]; char fnamep1[2048]; char fnamep2[2048]; char fnamep3[2048]; char fnamep4[2048]; char readBuffer[4096]; FILE* file; struct stat st; int p1, p2, p3, p4, hashIndex, sock1, sock2, sock1set, sock2set, index; int totalRead = 0; printf("\nFilename:\n\n> "); scanf("%s", fname); if(access(fname, F_OK) == -1){ printf("Invalid Filename!\n"); return; } strcpy(fnamep1, "."); strcpy(fnamep2, "."); strcpy(fnamep3, "."); strcpy(fnamep4, "."); strcat(fnamep1, fname); strcat(fnamep2, fname); strcat(fnamep3, fname); strcat(fnamep4, fname); strcat(fnamep1, ".1"); strcat(fnamep2, ".2"); strcat(fnamep3, ".3"); strcat(fnamep4, ".4"); stat(fname, &st); int size = st.st_size; file = fopen(fname, "r"); md5sum(file, &p1, &p2, &p3, &p4); fclose(file); file = fopen(fname, "r"); hashIndex = (p1 ^ p2 ^ p3 ^ p4) % 4; //send first block sock1set = 0; sock2set = 0; for(index=0;index<4;index++){ if(serverConf[hashIndex][index][0] == 1 || serverConf[hashIndex][index][1] == 1){ if(sock1set < 1){ setSock(&sock1, index, fnamep1); sock1set = 1; } else if(sock2set < 1){ setSock(&sock2, index, fnamep1); sock2set = 1; } } } if(&sock1 == NULL || &sock2 == NULL){ printf("Invalid user credentials!\n"); fclose(file); return; } while(totalRead < size/4){ unsigned char readbuf[1]; int numread; int staticread = fread(readbuf, sizeof(unsigned char), 1, file); unsigned char* bufptr; totalRead += staticread; //send to first server numread = staticread; //if(numread < 0)break; bufptr = readbuf; do{ int numsent = send(sock1, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); //send to second server numread = staticread; bufptr = readbuf; do{ int numsent = send(sock2, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); } shutdown(sock1, SHUT_WR); shutdown(sock2, SHUT_WR); recv(sock1, NULL, 0, 0); recv(sock2, NULL, 0, 0); close(sock1); close(sock2); //send second block sock1set = 0; sock2set = 0; for(index=0;index<4;index++){ if(serverConf[hashIndex][index][0] == 2 || serverConf[hashIndex][index][1] == 2){ if(sock1set < 1){ setSock(&sock1, index, fnamep2); sock1set = 1; } else if(sock2set < 1){ setSock(&sock2, index, fnamep2); sock2set = 1; } } } if(&sock1 == NULL || &sock2 == NULL){ printf("Invalid user credentials!\n"); fclose(file); return; } while(totalRead < size/2){ unsigned char readbuf[1]; int numread; int staticread = fread(readbuf, sizeof(unsigned char), 1, file); unsigned char* bufptr; totalRead += staticread; //send to first server numread = staticread; bufptr = readbuf; do{ int numsent = send(sock1, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); //send to second server numread = staticread; bufptr = readbuf; do{ int numsent = send(sock2, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); } shutdown(sock1, SHUT_WR); shutdown(sock2, SHUT_WR); recv(sock1, NULL, 0, 0); recv(sock2, NULL, 0, 0); close(sock1); close(sock2); //send third block sock1set = 0; sock2set = 0; for(index=0;index<4;index++){ if(serverConf[hashIndex][index][0] == 3 || serverConf[hashIndex][index][1] == 3){ if(sock1set < 1){ setSock(&sock1, index, fnamep3); sock1set = 1; } else if(sock2set < 1){ setSock(&sock2, index, fnamep3); sock2set = 1; } } } if(&sock1 == NULL || &sock2 == NULL){ printf("Invalid user credentials!\n"); fclose(file); return; } while(totalRead < (size*3)/4){ unsigned char readbuf[1]; int numread; int staticread = fread(readbuf, sizeof(unsigned char), 1, file); unsigned char* bufptr; totalRead += staticread; //send to first server numread = staticread; if(numread < 0)break; bufptr = readbuf; do{ int numsent = send(sock1, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); //send to second server numread = staticread; if(numread < 0)break; bufptr = readbuf; do{ int numsent = send(sock2, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); } shutdown(sock1, SHUT_WR); shutdown(sock2, SHUT_WR); recv(sock1, NULL, 0, 0); recv(sock2, NULL, 0, 0); close(sock1); close(sock2); //send fourth block sock1set = 0; sock2set = 0; for(index=0;index<4;index++){ if(serverConf[hashIndex][index][0] == 4 || serverConf[hashIndex][index][1] == 4){ if(sock1set < 1){ setSock(&sock1, index, fnamep4); sock1set = 1; } else if(sock2set < 1){ setSock(&sock2, index, fnamep4); sock2set = 1; } } } if(&sock1 == NULL || &sock2 == NULL){ printf("Invalid user credentials!\n"); fclose(file); return; } while(totalRead < size){ unsigned char readbuf[1]; int numread; int staticread = fread(readbuf, sizeof(unsigned char), 1, file); unsigned char* bufptr; totalRead += staticread; //send to first server numread = staticread; if(numread < 0)break; bufptr = readbuf; do{ int numsent = send(sock1, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); //send to second server numread = staticread; if(numread < 0)break; bufptr = readbuf; do{ int numsent = send(sock2, bufptr, numread, MSG_NOSIGNAL); if(numsent < 0){ break; } bufptr += numsent; numread -= numsent; }while(numread > 0); } shutdown(sock1, SHUT_WR); shutdown(sock2, SHUT_WR); recv(sock1, NULL, 0, 0); recv(sock2, NULL, 0, 0); close(sock1); close(sock2); fclose(file); }