void app_dns_sd(char *arg, struct pico_ip4 address) { char *hostname; char *nxt = arg; uint64_t starttime = 0; int once = 0; if (!nxt) { exit(255); } nxt = cpy_arg(&hostname, nxt); if(!hostname) { exit(255); } if(!nxt) { printf("Not enough args supplied!\n"); exit(255); } nxt = cpy_arg(&service_name, nxt); if(!service_name) { exit(255); } printf("\nStarting DNS Service Discovery module...\n"); if (pico_dns_sd_init(hostname, address, &dns_sd_init_callback, NULL) != 0) { printf("Initialisation returned with Error!\n"); exit(255); } printf("\nTry reinitialising DNS-SD\n"); if (pico_dns_sd_init(hostname, address, &dns_sd_init_callback, NULL)) { printf("Initialisation returned with Error!\n"); exit(255); } printf("DONE - Re-initialising DNS-SD module.\n"); starttime = PICO_TIME_MS(); printf("Starting time: %d\n", starttime); while(1) { pico_stack_tick(); usleep(2000); if (((PICO_TIME_MS() - starttime) > SECONDS * 1000) && fully_initialized && !once) { printf("\nTry reinitialising DNS-SD (a second time)\n"); if (pico_dns_sd_init(hostname, address, &dns_sd_init_callback, NULL)) { printf("Initialisation returned with Error!\n"); exit(255); } once = 1; printf("DONE - Re-initialising mDNS module. (a second time)\n"); } } }
END_TEST START_TEST(tc_pico_mld_timer_expired) { struct mld_timer *t,*s; t = PICO_ZALLOC(sizeof(struct mld_timer)); t->stopped = MLD_TIMER_STOPPED; t->type = 0; pico_string_to_ipv6("AAAA::1", t->mcast_link.addr); pico_string_to_ipv6("AAAA::1", t->mcast_group.addr); //void function, just check for side effects pico_mld_timer_expired(NULL, (void *)t); pico_tree_insert(&MLDTimers, t); s = PICO_ZALLOC(sizeof(struct mld_timer)); memcpy(s,t,sizeof(struct mld_timer)); // t will be freed next test pico_mld_timer_expired(NULL, (void *)t); /* will be freed */ s->stopped++; s->start = PICO_TIME_MS()*2; s->type++; pico_tree_insert(&MLDTimers, s); t = PICO_ZALLOC(sizeof(struct mld_timer)); memcpy(t,s,sizeof(struct mld_timer)); // s will be freed next test pico_mld_timer_expired(NULL, (void *)s); /* s will be freed */ t->mld_callback = mock_callback; pico_mld_timer_expired(NULL, (void *)t); /* t will be freed */ }
/* Get some random bytes from the generator. */ int pico_rand_bytes(uint8_t* buffer, int count) { int remaining_bytes = 0; uint8_t* block_buffer = 0; uint8_t* seed_buffer = 0; int blocks_done = 0; int seed_size = 0; if ((count > 0) && (count < (2 * 1024 * 1024))) { if ((PICO_TIME_MS() - pico_rand_generator.last_reseed_time >= PICO_RAND_MINIMUM_RESEED_MS) && (1 >= PICO_RAND_MINIMUM_RESEED_ENTR)) { /* FIXME to check 'size' of pool 0 */ seed_buffer = PICO_ZALLOC (sizeof (uint8_t) * PICO_RAND_POOL_COUNT * PICO_RAND_HASH_SIZE); /* Assume that we use every pool hash... */ if (NULL == seed_buffer) { return 0; } seed_size = pico_rand_extract_seed (seed_buffer, sizeof (uint8_t) * PICO_RAND_POOL_COUNT * PICO_RAND_HASH_SIZE); pico_rand_reseed (seed_buffer, seed_size); PICO_FREE (seed_buffer); } /* Get random blocks until we have our data. */ for (remaining_bytes = count; remaining_bytes > 0; remaining_bytes -= PICO_RAND_ENCRYPT_BLOCK_SIZE) { if (remaining_bytes / PICO_RAND_ENCRYPT_BLOCK_SIZE > 0) { /* At least one full block remaining? Can copy directly without overflowing. */ pico_rand_generate_block(buffer + (PICO_RAND_ENCRYPT_BLOCK_SIZE * blocks_done++), PICO_RAND_ENCRYPT_BLOCK_SIZE); /* TODO check! */ } else { /* This'll only be necessary for the last block, and only if requested byte count != multiple of block size */ block_buffer = PICO_ZALLOC(PICO_RAND_ENCRYPT_BLOCK_SIZE); if (NULL != block_buffer) { pico_rand_generate_block(block_buffer, PICO_RAND_ENCRYPT_BLOCK_SIZE); /* Copy required part of block to output */ memcpy (buffer + (blocks_done * PICO_RAND_ENCRYPT_BLOCK_SIZE), block_buffer, remaining_bytes); PICO_FREE (block_buffer); } else { return 0; } } } return 1; } else { /* Not allowed to produce more than 1MiB of data at once */ return 0; } }
void wget_callback(uint16_t ev, uint16_t conn) { static char data[1024 * 128]; /* Buffer: 128kb */ static uint32_t _length = 0u; static uint32_t _length_tot = 0u; static uint32_t start_time = 0u; static int fd = -1; if(ev & EV_HTTP_CON) { printf("Connected to the download server\n"); start_time = PICO_TIME_MS(); pico_http_client_sendHeader(conn, NULL, HTTP_HEADER_DEFAULT); _length = 0u; } if(ev & EV_HTTP_REQ) { struct pico_http_header *header = pico_http_client_readHeader(conn); printf("Received header from server...\n"); printf("Server response : %d\n", header->responseCode); printf("Location : %s\n", header->location); printf("Transfer-Encoding : %d\n", header->transferCoding); printf("Size/Chunk : %d\n", header->contentLengthOrChunk); fd = http_open_file(); } if(ev & EV_HTTP_BODY) { int len; struct pico_http_header *header = pico_http_client_readHeader(conn); printf("Reading data... len=%d\n", _length_tot + _length); if (_length + 1024 >= sizeof(data)) { http_save_file(fd, data, _length); _length_tot += _length; _length = 0u; } /* Read from buffer */ while((len = pico_http_client_readData(conn, data + _length, 1024)) && len > 0) { _length += (uint32_t)len; } if(header->contentLengthOrChunk == _length) ev = EV_HTTP_CLOSE; } if(ev & EV_HTTP_CLOSE) { struct pico_http_header *header = pico_http_client_readHeader(conn); int len; uint32_t speed; printf("Connection was closed...\n"); printf("Reading remaining data, if any ...\n"); if(!header) { printf("No header received\n"); pico_http_client_close(conn); } /* first save any open read bytes */ http_save_file(fd, data, _length); _length_tot += _length; _length = 0u; while((len = pico_http_client_readData(conn, data + _length, 1000u)) && len > 0) { _length += (uint32_t)len; } printf("Read a total data of : %d bytes \n", _length_tot); if(header->transferCoding == HTTP_TRANSFER_CHUNKED) { if(header->contentLengthOrChunk) { printf("Last chunk data not fully read !\n"); exit(1); } else { printf("Transfer ended with a zero chunk! OK !\n"); } } else { if(header->contentLengthOrChunk == (_length + _length_tot)) { printf("Received the full : %d \n", _length + _length_tot); } else { printf("Received %d , waiting for %d\n", _length + _length_tot, header->contentLengthOrChunk); } } if (!url_filename) { printf("Failed to get local filename\n"); } len = http_save_file(fd, data, _length); http_close_file(fd); if ((len < 0) || ((uint32_t)len < _length)) { printf("Failed to save file: %s\n", strerror(errno)); } speed = _length_tot / (PICO_TIME_MS() - start_time) * 8; printf("Download speed: %d kbps\n", speed); pico_http_client_close(conn); pico_free(url_filename); } if(ev & EV_HTTP_ERROR) { printf("Connection error (probably dns failed : check the routing table), trying to close the client...\n"); pico_http_client_close(conn); } if(ev & EV_HTTP_DNS) { printf("The DNS query was successful ... \n"); } }