void task_signal(Task *task, Signal signal) { /* Set destionation signal. Unblock the task if the target signal is in mask. */ interrupts_disable(); task_line(""); log_hex(signal); log_string(" --> "); log_string(task->name); log_string(" {mask="); log_hex(task->sig_mask); log_string(", rec="); log_hex(task->sig_rec); log_string("} ==> rec="); task->sig_rec |= signal; log_hex(task->sig_rec); if (task->sig_mask & signal) { log_string(", unblock, prio:"); list_remove_node((Node *) task); list_enqueue(&ready_tasks, (Node *) task); if (running_task->node.ln_pri < task->node.ln_pri) { log_string("hi"); port_reschedule(); } else { /* An unneeded context switch is optimized away. */ log_string("lo"); interrupts_enable(); } } else { log_string(", no unblock."); interrupts_enable(); } }
void unbound_stat_free_lite(void *ptr, const char* file, int line, const char* func) { void* real; size_t orig = 0; if(!ptr) return; real = ptr-lite_pad-sizeof(size_t); if(memcmp(real, lite_pre, lite_pad) != 0) { log_err("free(): prefix failed %s:%d %s", file, line, func); log_hex("prefix here", real, lite_pad); log_hex(" should be", lite_pre, lite_pad); fatal_exit("alloc assertion failed"); } memmove(&orig, real+lite_pad, sizeof(size_t)); if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){ log_err("free(): suffix failed %s:%d %s", file, line, func); log_err("alloc size is %d", (int)orig); log_hex("suffix here", real+lite_pad+orig+sizeof(size_t), lite_pad); log_hex(" should be", lite_post, lite_pad); fatal_exit("alloc assertion failed"); } memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */ free(real); }
void log_phex(LogType type, const void *data, unsigned long length, unsigned char padding) { if(length <= 1024) log_hex(type, data, length, padding); else { char buffer[80]; log_hex(type, data, 1024-32, padding); log_message(type, " ... truncated ..."); build_hex_line((const uint8 *)data,length,length-16,buffer,padding); log_message(type, "%s", buffer); } }
/** * Check protected memory region. Memory compare. Exit on error. * @param lock: which lock to check. * @param func: location we are now (when failure is detected). * @param file: location we are now (when failure is detected). * @param line: location we are now (when failure is detected). */ static void prot_check(struct checked_lock* lock, const char* func, const char* file, int line) { struct protected_area* p = lock->prot; while(p) { if(memcmp(p->hold, p->region, p->size) != 0) { log_hex("memory prev", p->hold, p->size); log_hex("memory here", p->region, p->size); lock_error(lock, func, file, line, "protected area modified"); } p = p->next; } }
void log_raw_packet(LogType type, uint16 seq, const BasePacket *p) { if(!is_log_enabled(type)) return; char buffer[196]; p->build_raw_header_dump(buffer, seq); log_message(type,buffer); log_hex(type,(const char *)p->pBuffer,p->size); }
void log_packet(LogType type, const BasePacket *p) { if(!is_log_enabled(type)) return; char buffer[80]; p->build_header_dump(buffer); log_message(type,"%s", buffer); log_hex(type,(const char *)p->pBuffer,p->size); }
void log_packet_mob(LogType type, Mob *who, const BasePacket *p) { if(!who->IsLoggingEnabled()) return; //could prolly put this in the macro, but it feels even dirtier than prototyping this in common char buffer[80]; p->build_header_dump(buffer); log_message(type,"[%s] %s: %s", log_type_info[type].name, who->GetName(), buffer); log_hex(type,(const char *)p->pBuffer,p->size); }
void *unbound_stat_realloc_lite(void *ptr, size_t size, const char* file, int line, const char* func) { /* always free and realloc (no growing) */ void* real, *newa; size_t orig = 0; if(!ptr) { /* like malloc() */ return unbound_stat_malloc_lite(size, file, line, func); } if(!size) { /* like free() */ unbound_stat_free_lite(ptr, file, line, func); return NULL; } /* change allocation size and copy */ real = ptr-lite_pad-sizeof(size_t); if(memcmp(real, lite_pre, lite_pad) != 0) { log_err("realloc(): prefix failed %s:%d %s", file, line, func); log_hex("prefix here", real, lite_pad); log_hex(" should be", lite_pre, lite_pad); fatal_exit("alloc assertion failed"); } memmove(&orig, real+lite_pad, sizeof(size_t)); if(memcmp(real+lite_pad+orig+sizeof(size_t), lite_post, lite_pad)!=0){ log_err("realloc(): suffix failed %s:%d %s", file, line, func); log_err("alloc size is %d", (int)orig); log_hex("suffix here", real+lite_pad+orig+sizeof(size_t), lite_pad); log_hex(" should be", lite_post, lite_pad); fatal_exit("alloc assertion failed"); } /* new alloc and copy over */ newa = unbound_stat_malloc_lite(size, file, line, func); if(!newa) return NULL; if(orig < size) memmove(newa, ptr, orig); else memmove(newa, ptr, size); memset(real, 0xdd, orig+lite_pad*2+sizeof(size_t)); /* mark it */ free(real); return newa; }
/* Why are satisfied signal bits cleared? Because there is no need to clear it manually afterwards then. */ Signal task_wait(Signal mask) { Signal ret; interrupts_disable(); task_line("{mask="); log_hex(mask); log_string(", rec="); log_hex(running_task->sig_rec); log_string("} ==> "); if (!(mask & running_task->sig_rec)) { running_task->sig_mask = mask; list_remove_node((Node *) running_task); list_enqueue(&waiting_tasks, (Node *) running_task); log_string("blocked..."); port_reschedule(); /* Interrupts may occur here. */ interrupts_disable(); task_line("...unblocked "); log_string("{mask="); log_hex(mask); log_string("("); log_hex(running_task->sig_mask); log_string(")"); log_string(", rec="); log_hex(running_task->sig_rec); log_string("} ==> "); } ret = running_task->sig_rec & mask; running_task->sig_rec &= ~mask; running_task->sig_mask = 0; log_string("{rec="); log_hex(running_task->sig_rec); log_string("}, return "); log_hex(ret); interrupts_enable(); return ret; }
/** see if buffers contain the same packet */ static int test_buffers(sldns_buffer* pkt, sldns_buffer* out) { /* check binary same */ if(sldns_buffer_limit(pkt) == sldns_buffer_limit(out) && memcmp(sldns_buffer_begin(pkt), sldns_buffer_begin(out), sldns_buffer_limit(pkt)) == 0) { if(vbmp) printf("binary the same (length=%u)\n", (unsigned)sldns_buffer_limit(pkt)); return 1; } if(vbmp) { size_t sz = 16; size_t count; size_t lim = sldns_buffer_limit(out); if(sldns_buffer_limit(pkt) < lim) lim = sldns_buffer_limit(pkt); for(count=0; count<lim; count+=sz) { size_t rem = sz; if(lim-count < sz) rem = lim-count; if(memcmp(sldns_buffer_at(pkt, count), sldns_buffer_at(out, count), rem) == 0) { log_info("same %d %d", (int)count, (int)rem); log_hex("same: ", sldns_buffer_at(pkt, count), rem); } else { log_info("diff %d %d", (int)count, (int)rem); log_hex("difp: ", sldns_buffer_at(pkt, count), rem); log_hex("difo: ", sldns_buffer_at(out, count), rem); } } } /* check if it 'means the same' */ if(vbmp) { char* s1, *s2; log_buf(0, "orig in hex", pkt); log_buf(0, "unbound out in hex", out); printf("\npacket from unbound (%d):\n", (int)sldns_buffer_limit(out)); s1 = sldns_wire2str_pkt(sldns_buffer_begin(out), sldns_buffer_limit(out)); printf("%s\n", s1?s1:"null"); free(s1); printf("\npacket original (%d):\n", (int)sldns_buffer_limit(pkt)); s2 = sldns_wire2str_pkt(sldns_buffer_begin(pkt), sldns_buffer_limit(pkt)); printf("%s\n", s2?s2:"null"); free(s2); printf("\n"); } /* if it had two EDNS sections, skip comparison */ if(1) { char* s = sldns_wire2str_pkt(sldns_buffer_begin(pkt), sldns_buffer_limit(pkt)); char* e1 = strstr(s, "; EDNS:"); if(e1 && strstr(e1+4, "; EDNS:")) { free(s); return 0; } free(s); } /* compare packets */ unit_assert(match_all(sldns_buffer_begin(pkt), sldns_buffer_limit(pkt), sldns_buffer_begin(out), sldns_buffer_limit(out), 1, matches_nolocation)); return 0; }
/** see if buffers contain the same packet */ static int test_buffers(ldns_buffer* pkt, ldns_buffer* out) { ldns_pkt* p1=0, *p2=0; ldns_status s1, s2; /* check binary same */ if(ldns_buffer_limit(pkt) == ldns_buffer_limit(out) && memcmp(ldns_buffer_begin(pkt), ldns_buffer_begin(out), ldns_buffer_limit(pkt)) == 0) { if(vbmp) printf("binary the same (length=%u)\n", (unsigned)ldns_buffer_limit(pkt)); return 1; } if(vbmp) { size_t sz = 16; size_t count; size_t lim = ldns_buffer_limit(out); if(ldns_buffer_limit(pkt) < lim) lim = ldns_buffer_limit(pkt); for(count=0; count<lim; count+=sz) { size_t rem = sz; if(lim-count < sz) rem = lim-count; if(memcmp(ldns_buffer_at(pkt, count), ldns_buffer_at(out, count), rem) == 0) { log_info("same %d %d", (int)count, (int)rem); log_hex("same: ", ldns_buffer_at(pkt, count), rem); } else { log_info("diff %d %d", (int)count, (int)rem); log_hex("difp: ", ldns_buffer_at(pkt, count), rem); log_hex("difo: ", ldns_buffer_at(out, count), rem); } } } /* check if it 'means the same' */ s1 = ldns_buffer2pkt_wire(&p1, pkt); s2 = ldns_buffer2pkt_wire(&p2, out); if(vbmp) { log_buf(0, "orig in hex", pkt); log_buf(0, "unbound out in hex", out); printf("\npacket from unbound (%d):\n", (int)ldns_buffer_limit(out)); ldns_pkt_print(stdout, p2); printf("\npacket original (%d):\n", (int)ldns_buffer_limit(pkt)); ldns_pkt_print(stdout, p1); printf("\n"); } if(s1 != s2) { /* oops! */ printf("input ldns parse: %s, output ldns parse: %s.\n", ldns_get_errorstr_by_id(s1), ldns_get_errorstr_by_id(s2)); unit_assert(0); } /* compare packets */ unit_assert(match_all(p1, p2)); ldns_pkt_free(p1); ldns_pkt_free(p2); return 0; }
void SteamWorks::Logging::log_hex(SteamWorks::Logging::LoggerStream& log, const char* p, uint32_t len) { log_hex(log, (const unsigned char *)p, len); }
void log_hex_mob(LogType type, Mob *who, const char *data, uint32 length, uint8 padding) { if(!who->IsLoggingEnabled()) return; //could prolly put this in the macro, but it feels even dirtier than prototyping this in common log_hex(type,data,length,padding); }
int main(int argc, char **argv) { int i; int ret; char *pfile = NULL; message_t * ll_head; lw_skey_seed_t lw_skey_seed; lw_dnonce_t devnonce; lw_anonce_t appnonce; lw_netid_t netid; uint8_t jappskey[LW_KEY_LEN]; uint8_t jnwkskey[LW_KEY_LEN]; memset(&config, 0, sizeof(config_t)); // for(i=0; i<argc; i++){ // log_puts(LOG_NORMAL, "arg%d %s", i, argv[i]); // } logflag = lw_log(LW_LOG_ON); while ((i = getopt (argc, argv, "hc:")) != -1) { switch (i) { case 'h': usage(basename(argv[0])); return 0; case 'c': pfile = optarg; log_puts(LOG_NORMAL, "File name: %s", pfile); if( access( pfile, F_OK ) != -1 ) { // file exists log_puts(LOG_NORMAL, "Found configuration file"); }else{ // file doesn't exist log_puts(LOG_FATAL, "Can't open %s", pfile); return -1; } break; default: log_puts(LOG_FATAL, "PARAMETER ERROR"); usage(basename(argv[0])); return -1; } } ret = config_parse(pfile, &config); if(ret < 0){ log_puts(LOG_NORMAL, "Configuration parse error(%d)", ret); return -1; } if(lw_set_band(config.band) < 0){ log_puts(LOG_NORMAL, "Band error"); } lw_parse_key_t pkey; if(config.flag&CFLAG_NWKSKEY){ pkey.nwkskey = config.nwkskey; pkey.flag.bits.nwkskey = 1; } if(config.flag&CFLAG_APPSKEY){ pkey.appskey = config.appskey; pkey.flag.bits.appskey = 1; } if(config.flag&CFLAG_APPKEY){ pkey.appkey = config.appkey; pkey.flag.bits.appkey = 1; } /** try to parse join request/accept message */ if(config.flag&CFLAG_JOINR){ if(0==lw_parse(config.joinr, config.joinr_size, &pkey, 0)){ } } if(config.flag&CFLAG_JOINA){ /** If get join request and accept is parsed, then try to generate new key with JION transaction, the new generated key will be used to parse message */ if(0==lw_parse(config.joina, config.joina_size, &pkey, 0)){ if( 0==lw_get_devnonce(&devnonce) && 0==lw_get_appnonce(&appnonce) && 0==lw_get_netid(&netid) ){ lw_skey_seed.aeskey = config.appkey; lw_skey_seed.anonce = appnonce; lw_skey_seed.dnonce = devnonce; lw_skey_seed.netid = netid; lw_get_skeys(jnwkskey, jappskey, &lw_skey_seed); log_line(); log_hex(LOG_NORMAL, jnwkskey, LW_KEY_LEN, "J-NWKSKEY:\t"); log_hex(LOG_NORMAL, jappskey, LW_KEY_LEN, "J-APPSKEY:\t"); if(config.joinkey){ /** Overwrite default session keys */ pkey.nwkskey = jnwkskey; pkey.flag.bits.nwkskey = 1; pkey.appskey = jappskey; pkey.flag.bits.appskey = 1; log_puts(LOG_WARN, "Force use session keys get from join request"); } }else{ log_puts(LOG_WARN, "Can't get DEVNONCE/APPNONCE/NETID"); } } } /** parse all data message */ ll_head = config.message; while(ll_head != NULL){ ret = lw_parse(ll_head->buf, ll_head->len, &pkey, 0); if(ret < 0){ log_puts(LOG_ERROR, "DATA MESSAGE PARSE error(%d)", ret); } ll_head = ll_head->next; } /** parse command list */ ll_head = config.maccmd; while(ll_head != NULL){ if(logflag){ log_line(); } /** buf[0] -> MHDR, buf[1] ~ buf[n] -> maccmd */ ret = lw_maccmd(ll_head->buf[0], ll_head->buf+1, ll_head->len-1); if(ret < 0){ log_puts(LOG_ERROR, "MACCMD error(%d)", ret); } ll_head = ll_head->next; } config_free(&config); return 0; }