//send data ,and save the left data static int __tcpnode_send_with_save(struct nd_tcp_node *node, void *msg_buf, size_t datalen) { ENTER_FUNC() ; int ret = node->sock_write((nd_handle)node,msg_buf,datalen) ; if(-1==ret ) { if(node->sys_error!=ESOCKETTIMEOUT){ LEAVE_FUNC(); return -1 ; } else { ret = ndlbuf_write(&(node->send_buffer),(void*)msg_buf,datalen,EBUF_SPECIFIED) ; if(ret == datalen) { node->myerrno = NDERR_SUCCESS ; } } } else if(ret==datalen) { LEAVE_FUNC(); return (int)datalen ; } else { int wlen ; char *padd = (char*) msg_buf ; padd += ret ; wlen = ndlbuf_write(&(node->send_buffer),padd,datalen-ret,EBUF_SPECIFIED) ; if (wlen > 0) { ret = wlen + ret; } } LEAVE_FUNC(); return ret ; }
//connect remote host int nd_tcpnode_connect(const char *host, int port, struct nd_tcp_node *node, struct nd_proxy_info *proxy) { ENTER_FUNC() nd_assert(node); nd_assert(host); node->sys_error = 0; node->last_push = nd_time(); ndlbuf_reset(&(node->recv_buffer)); /* buffer store data recv from net */ ndlbuf_reset(&(node->send_buffer)); /* buffer store data send from net */ if (proxy && proxy->proxy_type != ND_PROXY_NOPROXY) { node->fd = nd_proxy_connect(host, port, &(node->remote_addr), proxy, 0); } else { node->fd = nd_socket_tcp_connect(host, (short)port, &(node->remote_addr)); } if (node->fd <= 0) { node->myerrno = NDERR_OPENFILE; LEAVE_FUNC(); return -1; } TCPNODE_SET_OK(node); nd_socket_nonblock(node->fd, 1); _set_ndtcp_conn_dft_option(node->fd); node->start_time = nd_time(); if (node->remote_addr.sin_family == AF_INET6) { node->is_ipv6 = 1; } LEAVE_FUNC(); return 0; }
//#include "nd_crypt/nd_crypt.h" int _sys_socket_write(struct nd_tcp_node *node,void *data , size_t len) { ENTER_FUNC(); int ret ; ret = (int) send(node->fd, data, len, 0) ; if(ret > 0) { //char md5[33]; //ndfprintf(stderr, "!!!!!----- send data %d, return length =%d md5 = %s\n", len, ret, MD5Crypt32(data, ret, md5)); node->send_len += ret; node->last_push = nd_time(); } else if(-1==ret) { node->sys_error = nd_socket_last_error() ; if (node->sys_error == ESOCKETTIMEOUT) { node->myerrno = NDERR_WOULD_BLOCK; } else { node->myerrno = NDERR_IO ; } } else if(ret == 0 ) { node->myerrno = NDERR_WOULD_BLOCK; } LEAVE_FUNC(); return ret ; };
int nd_tcpnode_close(struct nd_tcp_node *node, int force) { ENTER_FUNC() //nd_assert(0); nd_assert(node); node->status = ETS_DEAD; if (node->fd == 0) { LEAVE_FUNC(); return 0; } nd_socket_close(node->fd); node->fd = 0; LEAVE_FUNC(); return 0; }
static int __tcpnode_push_and_send(struct nd_tcp_node *node, void *msg_buf, size_t datalen, int is_write_all) { ENTER_FUNC(); signed int ret =0; nd_netbuf_t *pbuf = &(node->send_buffer); size_t length_in_buff = ndlbuf_datalen(pbuf); if (length_in_buff) { int flushlen = node->sock_write((nd_handle)node, ndlbuf_data(pbuf), length_in_buff); if (flushlen > 0) { nd_assert(flushlen <= length_in_buff); ndlbuf_sub_data(pbuf, (size_t)flushlen); } length_in_buff = ndlbuf_datalen(pbuf); } if (length_in_buff == 0) { ret = __tcpnode_send_with_save(node, msg_buf, datalen); } else { size_t space_len = ndlbuf_free_capacity(pbuf); if(is_write_all && space_len < datalen) { node->myerrno = NDERR_WOULD_BLOCK; ret = -1; } else { ret = ndlbuf_write(pbuf, (void*)msg_buf, datalen, EBUF_SPECIFIED); } } LEAVE_FUNC(); return ret ; }
/* install message handle*/ int nd_msgentry_install(nd_netui_handle handle, nd_usermsg_func func, ndmsgid_t maxid, ndmsgid_t minid,int level, const char *name) { struct nd_msg_entry_node * node ; int ret = -1; ENTER_FUNC() ; node = _nd_msgentry_get_node(handle, maxid, minid) ; if (node) { if (node->is_script && node->entry) { free(node->entry); } node->entry = func ; node->level = level ; node->is_script = 0; #if 1 if (name && name[0]) { int len = (int)ndstrlen(name) + 1; if (len > (int) sizeof(node->name)) { len = sizeof(node->name); } ndstrncpy(node->name, name, len); } #endif ret = 0 ; } else { nd_object_seterror(handle, NDERR_NOSOURCE) ; } LEAVE_FUNC(); return ret ; }
int nd_msgentry_is_handled(nd_handle handle, ndmsgid_t maxid, ndmsgid_t minid) { struct nd_msg_entry_node * node; ENTER_FUNC(); node = _nd_msgentry_get_node(handle, maxid, minid); LEAVE_FUNC(); if (!node) { return 0; } return node->entry ? 1:0; }
const char * nd_msgentry_get_name(nd_netui_handle handle, ndmsgid_t maxid, ndmsgid_t minid) { #if 1 struct nd_msg_entry_node * node ; ENTER_FUNC() ; node = _nd_msgentry_get_node(handle, maxid, minid) ; LEAVE_FUNC(); return node ? node->name : NULL; #else return NULL ; #endif }
nd_usermsg_func nd_msgentry_get_func(nd_netui_handle handle, ndmsgid_t maxid, ndmsgid_t minid) { struct nd_msg_entry_node * node ; ENTER_FUNC() ; node = _nd_msgentry_get_node(handle, maxid, minid) ; LEAVE_FUNC(); if (!node) { return NULL; } if (node->is_script) { return NULL; } return node->entry ; }
void interp_string(char *str) { bsinterp_state state = bsst_none; bsinterp_state new_state; char c; ENTER_FUNC(); while ((c = *(str++)) != 0) { switch (c) { case '(': new_state = bsst_open_list; break; case ')': new_state = bsst_close_list; break; case ' ': case '\t': new_state = bsst_space; break; case '\n': new_state = bsst_new_line; break; case '0' ... '9': case 'A' ... 'Z': case 'a' ... 'z': case '-': case '_': case '+': case '*': case '.': case '/': new_state = bsst_lexem; break; default: new_state = bsst_none; break; } if (state != new_state) { } } LEAVE_FUNC(); }
int nd_msgentry_script_install(nd_handle handle, const char*script, ndmsgid_t maxid, ndmsgid_t minid, int level) { struct nd_msg_entry_node * node; int ret = -1; ENTER_FUNC(); node = _nd_msgentry_get_node(handle, maxid, minid); if (script && script[0] && node) { int len = (int)ndstrlen(script) + 1; if (node->is_script && node->entry) { free(node->entry); } node->entry = (nd_usermsg_func)malloc(len); ndstrncpy((char*)node->entry, script, len); node->level = level; node->is_script = 1; ret = 0; } else { nd_object_seterror(handle, NDERR_NOSOURCE); } LEAVE_FUNC(); return ret; }
//************************************************************************* // HandleInDebuggerFault() // //************************************************************************* ULONG HandleInDebuggerFault(FRAME* ptr,ULONG address) { PEPROCESS tsk; ENTER_FUNC(); DPRINT((0,"HandleInDebuggerFault(): ###### page fault @ %.8X while inside debugger, eip: %x\n",address, ptr->eip)); // fault in this page fault handler if(bInPageFaultHandler) { DPRINT((0,"HandleInDebuggerFault(): ###### page fault @ %.8X while in page fault handler\n",address)); DPRINT((0,"!!! machine is halted !!!\n")); __asm__ __volatile__ ("hlt"); LEAVE_FUNC(); return 0; } bInPageFaultHandler = TRUE; // when we come here from DebuggerShell() we live on a different stack // so the current task is different as well tsk = IoGetCurrentProcess(); DPRINT((0,"%.8X (%.4X:%.8X %.8X %s %s %s task=%.8X )\n", address, ptr->cs, ptr->eip, ptr->eflags, (ptr->error_code&1)?"PLP":"NP", (ptr->error_code&2)?"WRITE":"READ", (ptr->error_code&4)?"USER-MODE":"KERNEL-MODE", (ULONG)tsk)); if(!bInPrintk) { DPRINT((0,"HandleInDebuggerFault(): unexpected pagefault in command handler!\n",address)); } else { DPRINT((0,"HandleInDebuggerFault(): unexpected pagefault in command handler while in PrintkCallback()!\n",address)); } if(tsk) { PULONG pPGD; PULONG pPTE; pPGD = ADDR_TO_PDE(address); DPRINT((0,"PGD for %.8X @ %.8X = %.8X\n",address,(ULONG)pPGD,(ULONG)(*pPGD) )); if(pPGD && (*pPGD)&_PAGE_PRESENT) { // not large page if(!((*pPGD)&_PAGE_4M)) { pPTE = ADDR_TO_PTE(address); if(pPTE) { DPRINT((0,"PTE for %.8X @ %.8X = %.8X\n",address,(ULONG)pPTE,(ULONG)(*pPTE) )); } } } } IntelStackWalk(ptr->eip,CurrentEBP,ulRealStackPtr); DPRINT((0,"!!! machine is halted !!!\n")); __asm__ __volatile__ ("hlt"); LEAVE_FUNC(); return 2; }
//************************************************************************* // InitPICE() // //************************************************************************* BOOLEAN InitPICE(void) { ULONG ulHandleScancode=0,ulHandleKbdEvent=0; ARGS Args; KIRQL Dirql; KAFFINITY Affinity; ULONG ulAddr; ENTER_FUNC(); DPRINT((0,"InitPICE(): trace step 0.5\n")); KeyboardIRQL = HalGetInterruptVector(Internal, 0, 0, KEYBOARD_IRQ, &Dirql, &Affinity); DPRINT((0,"KeyboardIRQL: %x\n", KeyboardIRQL)); DPRINT((0,"InitPICE(): trace step 1\n")); // enable monochrome passthrough on BX type chipset EnablePassThrough(); DPRINT((0,"InitPICE(): trace step 2\n")); // now load all symbol files described in /etc/pice.conf if(!LoadSymbolsFromConfig(FALSE)) { DPRINT((0,"InitPICE: LoadSymbolsFromConfig() failed\n")); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 3\n")); // init the output console // this might be one of the following depending setup // a) monochrome card // b) serial terminal (TODO) if(!ConsoleInit()) { DPRINT((0,"InitPICE: ConsoleInit() failed\n")); UnloadSymbols(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 4\n")); // print the initial screen template PrintTemplate(); /* DPRINT((0,"InitPICE(): trace step 5\n")); // ask the user if he wants to abort the debugger load if(!CheckLoadAbort()) { Print(OUTPUT_WINDOW,"pICE: ABORT (abort by user)\n"); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } */ DPRINT((0,"InitPICE(): trace step 6\n")); // load the file /boot/System.map. // !!! It must be consistent with the current kernel at all cost!!! if(!LoadExports()) { Print(OUTPUT_WINDOW,"pICE: failed to load exports\n"); Print(OUTPUT_WINDOW,"press any key to continue...\n"); while(!GetKeyPolled()); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 7\n")); ScanExports("_KernelAddressSpace", &ulAddr); my_init_mm = (PMADDRESS_SPACE) ulAddr; DPRINT((0,"init_mm %x @ %x\n",&my_init_mm,my_init_mm)); if(!my_init_mm) { Print(OUTPUT_WINDOW,"pICE: ABORT (initial memory map not found)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); DbgPrint("pICE: ABORT (initial memory map not found)\n"); DbgPrint("pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 7.1\n")); ScanExports("_ModuleListHead",&ulAddr); pModuleListHead = (LIST_ENTRY*)ulAddr; DPRINT((0,"pModuleListHead @ %X\n",pModuleListHead)); if(!pModuleListHead) { Print(OUTPUT_WINDOW,"pICE: ABORT (pModuleListHead not found)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 7.2\n")); ScanExports("_PsProcessListHead",&ulAddr); pPsProcessListHead = (LIST_ENTRY*)ulAddr; DPRINT((0,"pPsProcessListHead @ %X\n",pPsProcessListHead)); if(!pPsProcessListHead) { Print(OUTPUT_WINDOW,"pICE: ABORT (PsProcessListHead not found)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 8\n")); // end of the kernel /* ScanExports("_end",(PULONG)&kernel_end); if(!kernel_end) { Print(OUTPUT_WINDOW,"pICE: ABORT (kernel size is unknown)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadExports(); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } */ DPRINT((0,"InitPICE(): trace step 9\n")); // the loaded module list ScanExports("_NameSpaceRoot", &ulAddr); pNameSpaceRoot = (PDIRECTORY_OBJECT *)ulAddr; DPRINT((0,"pNameSpaceRoot @ %X\n",pNameSpaceRoot)); if(!pNameSpaceRoot) { Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't retreive name space root)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadExports(); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 10\n")); // setup a linked list for use in module parsing routines. if(!InitModuleList(&pdebug_module_head, 100)) { Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't initialize kernel module list)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); FreeModuleList( pdebug_module_head ); while(!GetKeyPolled()); UnloadExports(); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } pdebug_module_tail = pdebug_module_head; DPRINT((0,"InitPICE(): trace step 11\n")); // do a sanity check on exports if(!SanityCheckExports()) { Print(OUTPUT_WINDOW,"pICE: ABORT (exports are conflicting with kernel symbols)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadExports(); UnloadSymbols(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 12\n")); DPRINT((0,"InitPICE(): trace step 13\n")); // patch the keyboard driver if(!PatchKeyboardDriver()) { Print(OUTPUT_WINDOW,"pICE: ABORT (couldn't patch keyboard driver)\n"); Print(OUTPUT_WINDOW,"pICE: press any key to continue...\n"); while(!GetKeyPolled()); UnloadSymbols(); UnloadExports(); ConsoleShutdown(); LEAVE_FUNC(); return FALSE; } DPRINT((0,"InitPICE(): trace step 14\n")); // partial init of shadow registers CurrentCS = GLOBAL_CODE_SEGMENT; CurrentEIP = (ULONG)RealIsr; CurrentDS = CurrentSS = GLOBAL_DATA_SEGMENT; __asm__("\n\t \ mov %%esp,%%eax\n\t \ mov %%eax,_CurrentESP\n\t \ ":::"eax"); // display version and symbol information Ver(NULL); // disable HW breakpoints __asm__("\n\t \ xorl %%eax,%%eax\n\t \ mov %%eax,%%dr6\n\t \ mov %%eax,%%dr7\n\t \ mov %%dr0,%%eax\n\t \ mov %%dr1,%%eax\n\t \ mov %%dr2,%%eax\n\t \ mov %%dr3,%%eax" :::"eax" ); DPRINT((0,"InitPICE(): trace step 15\n")); TakeIdtSnapshot(); DPRINT((0,"InitPICE(): trace step 16\n")); // install all hooks InstallTraceHook(); InstallGlobalKeyboardHook(); InstallSyscallHook(); InstallInt3Hook(); InstallDblFltHook(); InstallGPFaultHook(); InstallIntEHook(); InstallPrintkHook(); DPRINT((0,"InitPICE(): trace step 16\n")); if(ulDoInitialBreak) { DPRINT((0,"about to do initial break...\n")); // simulate an initial break __asm__("\n\t \ pushfl\n\t \ pushl %cs\n\t \ pushl $initialreturnpoint\n\t \ pushl $" STR(REASON_CTRLF) "\n\t \ jmp NewInt31Handler\n\t \ initialreturnpoint:"); } else {