void logger::writeLog(string const& data) { lock.lock(); fstream file(filePath + SUFFIX_LOG, ios::out | ios::in | ios::binary); if (file.is_open()) { //写入checksum uint32_t checksum = calChecksum(0, data); file.seekp(0, ios::end); file.write((char*)(&checksum), 4); //写入size uint32_t size = data.size(); file.write((char*)(&size), 4); //写入data file.write(data.c_str(), sizeof(char) * size); //更新xChecksum uint32_t xChecksum_tmp; file.seekp(0, ios::beg); file.read((char*)&xChecksum_tmp, 4); xChecksum_tmp = calChecksum(xChecksum_tmp, data); file.seekg(0, ios::beg); file.write((char*)&xChecksum_tmp, 4); file.close(); } lock.unlock(); }
void Calendar::parseNetworkResponse(bool success, QString *data) { assert(data); engageBufferLock("accessing status and timer"); StatusCode oldStatus = _status; _nfyTimer.stop(); releaseBufferLock("releasing status and timer"); if (!success) { // In the event of a download error, set the calendar to Offline. Logger::instance()->add(CLASSNAME, this, "Error fetching update"); setStatus(Offline); if (oldStatus != Offline) emit formatNotRecognized(this); } else { Logger::instance()->add(CLASSNAME, this, "Parsing ICS data..."); ICSParser parser(*data); // Check ICS validity first if (!parser.holdsValidICS()) { Logger::instance()->add(CLASSNAME, this, "Downloaded data appears to be invalid ICS"); setStatus(Offline); } else { // Only repopulate the AptCache if the calendar changed if (calChecksum() != parser.checksum()) repopulateCache(parser); if (status() == Online) { // This will re-enable the timer and, if the cache was repopulated, // trigger the first batch of notifications. sendNotifications(); } } } Logger::instance()->add(CLASSNAME, this, "Finished updating"); }
int main(int argc, char *argv[]) { int n = 0, i = 0, len = 0, reuseaddr; int ch; struct sockaddr_in serv_addr, clnt_addr; struct protocol1 header; int protocol = 0; struct protocol2_2 proto2_2; struct protocol2_1 proto2_1; int listenfd, clntaddrlen, connfd; pid_t pid; struct sigaction sa; //FILE *stdIn = freopen(NULL, "rb", stdin); //FILE *stdOut = freopen(NULL, "wb", stdout); if(argc != 5) { printf("usage : %s -h <ip of server> -p <port>", argv[0]); return 1; } if((listenfd = socket(PF_INET, SOCK_STREAM, 0)) < 0) { bail("socket", -1); return 1; } if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, &reuseaddr, sizeof(int)) == -1) { bail("setsockopt", listenfd); return 1; } bzero((char *)&serv_addr, sizeof(serv_addr)); serv_addr.sin_family = AF_INET; inet_pton(AF_INET, argv[2], &serv_addr.sin_addr); serv_addr.sin_port = htons(atoi(argv[4])); if(bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0) { bail("bind", listenfd); return 1; } if(listen(listenfd, 5) < 0) { bail("listen", listenfd); return 1; } /* Set up the signal handler */ sa.sa_handler = wait_for_child; sigemptyset(&sa.sa_mask); sa.sa_flags = SA_RESTART; if (sigaction(SIGCHLD, &sa, NULL) == -1) { bail("sigaction", listenfd); return 1; } srand(time(NULL)); while(1) { clntaddrlen = sizeof(clnt_addr); if((connfd = accept(listenfd, (struct sockaddr *)&clnt_addr, &clntaddrlen)) < 0) { bail("accept", listenfd); continue; } // for a new server process to service this client pid = fork(); if(pid==-1) { bail("fork", connfd); continue; } else if(pid > 0) { close(connfd); continue; } // send phase1 msg header.op = 0; header.proto = 0; header.trans_id = rand(); header.checksum = calChecksum(header); header.trans_id = htonl(header.trans_id); header.checksum = htons(header.checksum); n = write(connfd, (char *)&header, sizeof(header)); if(n < 0) {bail("write phase 1", connfd); continue;} // receive phase1 msg memset(&header, '0', sizeof(header)); n = read(connfd,(char *)&header, sizeof(header)); if(n < 0) {bail("read phase 1", connfd); continue;} header.checksum = ntohs(header.checksum); header.trans_id = ntohl(header.trans_id); protocol = header.proto; if(isCorrupt(header, connfd)==1) {bail("corrupt", connfd); continue;} //receive phase2 msg and send handled msg if(protocol==PROTOCOL2_1) { unsigned size = 0; unsigned totalback = 0; unsigned lastch = '\0'; char recvBuff[SIZE+3]; memset(recvBuff, '\0', SIZE+3); memset(proto2_1.string, '\0', SIZE+3); while((n = read(connfd, recvBuff, SIZE+3)) > 0) { //printf("%s\n", recvBuff); i = 0, size = 0; //printf("here0\n"); while(i < n) { ch = recvBuff[i++]; //printf("%c", ch); if(ch == '\\') { totalback++; //printf("%d\n", totalback); if(ch!=lastch && totalback%2==0) { proto2_1.string[size++] = ch; proto2_1.string[size++] = ch; lastch = ch; //printf("%d ", size); } //printf("here1\n"); } else if(ch =='0' && (totalback%2)==1){ proto2_1.string[size++] = '\\'; proto2_1.string[size++] = '0'; totalback--; //printf("here\n"); //printf("here2 %d\n", totalback); break; } else if(ch != lastch) { proto2_1.string[size++] = ch; lastch = ch; //printf("%d ", size); //printf("here3\n"); } } //printf("here4\n"); //printf("%d\n") //printf("\n%d",size); //proto2_1.string[size++] = '\\'; //proto2_1.string[size++] = '0'; n = write(connfd, proto2_1.string, size); if(n < 0) {bail("wirte phase 2", connfd); continue;} memset(recvBuff, '\0', SIZE+3); memset(proto2_1.string, '\0', SIZE+3); } } else if(protocol==PROTOCOL2_2) { memset(proto2_2.string, '\0', SIZE); char recvBuff[SIZE+3]; memset(recvBuff , '\0' , SIZE); int recvSize = 0;// = proto2_2.length; int sendSize = 0; int counter = 0; char lastch='\0', ch; while((n = read(connfd, (char *)&proto2_2, 4)) > 0){ proto2_2.length = ntohl(proto2_2.length); recvSize = proto2_2.length; while(recvSize > 0) { n = read(connfd, recvBuff, SIZE); while(counter < n){ ch = recvBuff[counter++]; if(ch != lastch){ proto2_2.string[sendSize++] = ch; lastch = ch; } } memset(recvBuff, '\0', SIZE); recvSize = recvSize - counter; counter = 0; } proto2_2.length = htonl(sendSize); n = write(connfd, &proto2_2, sendSize+4); sendSize = 0; memset(proto2_2.string, '\0', SIZE); } } else { bail("wrong protocol",connfd); continue; } //h = gethostbyaddr((const char *)&clnt_addr.sin_addr.s_addr, sizeof(clnt_addr.sin_addr.s_addr), AF_INET); //printf("server connected to %s (%s) \n", h->h_name, inet_ntoa(clnt_addr.sin_addr)); //printf("connection terminated.\n"); close(listenfd); exit(0); } //fclose(stdIn); //fclose(stdOut); return 0; }
// Function Specification // // Name: boot_main // // Description: boot main will test SRAM, copy main application image from // main memory to SRAM, validate checksum and call ssx_boot. // // End Function Specification void main() { uint32_t l_rc = 0; uint32_t l_gpe0_start_addr = 0; uint32_t l_gpe1_start_addr = 0; uint32_t l_gpe_size = 0; uint8_t* l_gpe0_src_ptr = 0; uint8_t* l_gpe1_src_ptr = 0; // set checkpoint to boot test SRAM WRITE_TO_SPRG0(BOOT_TEST_SRAM_CHKPOINT); #ifndef VPO // This is ifdef'd out b/c it takes too long to run in VPO // Test SRAM l_rc = boot_test_sram(); #endif // If failed to test SRAM, write failed return code to SPRG1 and halt if(0 != l_rc) { WRITE_TO_SPRG1_AND_HALT(l_rc); } // set imageHdr_t pointer to point to boot image header to get to boot // image size. This way we can get to main application image header. imageHdr_t *l_hdrPtr = (imageHdr_t *)(G_bootImageHdr.start_addr + G_bootImageHdr.image_size); // set checkpoint to boot load main application image to SRAM WRITE_TO_SPRG0(BOOT_LOAD_IMAGE_CHKPOINT); // Load main application image to SRAM including main application header l_rc = boot_load_405(l_hdrPtr); // If failed to load image, write failed return code to SPRG1 and halt if(0 != l_rc) { WRITE_TO_SPRG1_AND_HALT(l_rc); } // set checkpoint to load gpe0 into SRAM WRITE_TO_SPRG0(BOOT_LOAD_GPE0_CHKPOINT); // Load GPE0 l_gpe0_start_addr = (uint32_t) (((uint32_t) l_hdrPtr) + l_hdrPtr->image_size); l_gpe0_src_ptr = (uint8_t*) (((uint32_t)l_hdrPtr) + l_hdrPtr->image_size); l_rc = boot_load_gpe0(l_gpe0_start_addr, l_hdrPtr->gpe0_size, l_gpe0_src_ptr); if(0 != l_rc) { WRITE_TO_SPRG1_AND_HALT(l_rc); } WRITE_TO_SPRG0(BOOT_LOAD_GPE1_CHKPOINT); // Load GPE1 l_gpe1_start_addr = l_gpe0_start_addr + l_hdrPtr->gpe0_size; l_gpe1_src_ptr = (uint8_t*) (((uint32_t)l_gpe0_src_ptr) + l_hdrPtr->gpe0_size); l_rc = boot_load_gpe1(l_gpe1_start_addr, l_hdrPtr->gpe1_size, l_gpe1_src_ptr); if(0 != l_rc) { WRITE_TO_SPRG1_AND_HALT(l_rc); } //========================================== // Calculate checksums and verify they match //========================================== // set checkpoint to calculate checksum WRITE_TO_SPRG0(BOOT_CALCULTE_CHKSUM_CHKPOINT_405); // Calculate checksum for 405 SRAM uint32_t l_checksum = calChecksum(l_hdrPtr->start_addr, l_hdrPtr->image_size, false); // If checksum does not match, store bad checksum into SPRG1 and halt if(l_checksum != l_hdrPtr->checksum) { WRITE_TO_SPRG1_AND_HALT(l_checksum); } WRITE_TO_SPRG0(BOOT_CALCULTE_CHKSUM_CHKPOINT_GPE0); // Calculate checksum for GPE0 SRAM l_checksum = calChecksum(SRAM_START_ADDRESS_GPE0, l_hdrPtr->gpe0_size, true); // If checksum does not match, store bad checksum into SPRG1 and halt if(l_checksum != l_hdrPtr->gpe0_checksum) { WRITE_TO_SPRG1_AND_HALT(l_checksum); } WRITE_TO_SPRG0(BOOT_CALCULTE_CHKSUM_CHKPOINT_GPE1); // Calculate checksum for GPE1 SRAM l_checksum = calChecksum(SRAM_START_ADDRESS_GPE1, l_hdrPtr->gpe1_size, true); // If checksum does not match, store bad checksum into SPRG1 and halt if(l_checksum != l_hdrPtr->gpe1_checksum) { WRITE_TO_SPRG1_AND_HALT(l_checksum); } // set checkpoint to get nest frequency WRITE_TO_SPRG0(BOOT_GET_NEST_FREQ_CHKPOINT); // set checkpoint to call to SSX_BOOT WRITE_TO_SPRG0(BOOT_SSX_BOOT_CALL_CHKPOINT); // create function pointer pointing to main application header entry point // address. void (*execute_ssx_boot)(void) = (void (*)(void))l_hdrPtr->ep_addr; (*execute_ssx_boot)(); // set checkpoint to return from ssx_boot. This should never happen so // halt at this point. WRITE_TO_SPRG0(BOOT_SSX_RETURNED_CHKPOINT); WRITE_TO_SPRG1_AND_HALT(l_hdrPtr->ep_addr); }