void initRTC() { spinlockRelease(&timeLock); KernelThreadParams rtcPars; memset(&rtcPars, 0, sizeof(KernelThreadParams)); rtcPars.stackSize = 0x4000; rtcPars.name = "RTC reader daemon"; CreateKernelThread(rtcThread, &rtcPars, NULL); };
void ipreasmInit() { spinlockRelease(&reasmLock); KernelThreadParams params; memset(¶ms, 0, sizeof(KernelThreadParams)); params.stackSize = DEFAULT_STACK_SIZE; params.name = "IP Reassembler"; params.flags = THREAD_WAITING; reasmHandle = CreateKernelThread(ipreasmThread, ¶ms, NULL); };
/* Initializer of NAT manager. */ static BOOL nmInitialize(__NAT_MANAGER* pMgr) { BOOL bResult = FALSE; BUG_ON(NULL == pMgr); /* Create radix tree object. */ pMgr->pTree = CreateRadixTree(); if (NULL == pMgr->pTree) { goto __TERMINAL; } /* Create the mutex object to protect NAT entry list. */ pMgr->lock = CreateMutex(); if (NULL == pMgr->lock) { goto __TERMINAL; } /* Initialize NAT entry list. */ pMgr->entryList.pPrev = pMgr->entryList.pNext = &pMgr->entryList; /* Create background thread of NAT. */ pMgr->hMainThread = CreateKernelThread( 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_NORMAL, NatMainThread, NULL, NULL, NAT_MAIN_THREAD_NAME); if (NULL == pMgr->hMainThread) { goto __TERMINAL; } bResult = TRUE; __TERMINAL: return bResult; }
/* Initializer of PPPoE manager object. */ static BOOL PPPoEInitialize(__PPPOE_MANAGER* pMgr) { BOOL bResult = FALSE; BUG_ON(NULL == pMgr); #if defined(__CFG_SYS_SMP) __INIT_SPIN_LOCK(pMgr->spin_lock, "pppoe"); #endif /* * Main thread of PPP over Ethernet functions. * All PPPoE related functions are bounced to this kernel thread * to process. * It's priority is higher than normal priority level,but lower than * the main thread of tcp/ip. * The priority level of kernel threads in network subsystem in HelloX, * is arranged as following rule: * Kernel thread's priority level in device driver <= * Kernel thread's priority level in datalink layer <= * Kernel thread's priority level in network layer <= * Kernel thread's priority level in transportation layer. */ pMgr->hMainThread = CreateKernelThread( 0, KERNEL_THREAD_STATUS_READY, PRIORITY_LEVEL_HIGH_3, pppoeMainThread, NULL, NULL, PPPOE_MAIN_THREAD_NAME); if (NULL == pMgr->hMainThread) { goto __TERMINAL; } bResult = TRUE; __TERMINAL: return bResult; }
static int tcpsock_connect(Socket *sock, const struct sockaddr *addr, size_t size) { TCPSocket *tcpsock = (TCPSocket*) sock; if (addr->sa_family != sock->domain) { ERRNO = EAFNOSUPPORT; return -1; }; if (size != INET_SOCKADDR_LEN) { ERRNO = EAFNOSUPPORT; return -1; }; semWait(&tcpsock->lock); if (tcpsock->state == TCP_CONNECTING) { semSignal(&tcpsock->lock); ERRNO = EALREADY; return -1; }; if (tcpsock->state != TCP_CLOSED) { semSignal(&tcpsock->lock); ERRNO = EISCONN; return -1; }; uint16_t srcport, dstport; if (sock->domain == AF_INET) { const struct sockaddr_in *inaddr = (const struct sockaddr_in*) addr; struct sockaddr_in *inname = (struct sockaddr_in*) &tcpsock->sockname; if (inname->sin_family == AF_UNSPEC) { inname->sin_family = AF_INET; getDefaultAddr4(&inname->sin_addr, &inaddr->sin_addr, sock->ifname); inname->sin_port = AllocPort(); srcport = inname->sin_port; } else { srcport = inname->sin_port; }; dstport = inaddr->sin_port; } else { const struct sockaddr_in6 *inaddr = (const struct sockaddr_in6*) addr; struct sockaddr_in6 *inname = (struct sockaddr_in6*) &tcpsock->sockname; if (inname->sin6_family == AF_UNSPEC) { inname->sin6_family = AF_INET6; getDefaultAddr6(&inname->sin6_addr, &inaddr->sin6_addr, sock->ifname); inname->sin6_port = AllocPort(); srcport = inname->sin6_port; } else { srcport = inname->sin6_port; } dstport = inaddr->sin6_port; }; memcpy(&tcpsock->peername, addr, INET_SOCKADDR_LEN); tcpsock->state = TCP_CONNECTING; tcpsock->nextSeqNo = (uint32_t) getRandom(); tcpsock->currentOut = CreateOutbound(&tcpsock->sockname, &tcpsock->peername, 0); tcpsock->expectedAck = tcpsock->nextSeqNo; TCPSegment *syn = tcpsock->currentOut->segment; syn->srcport = srcport; syn->dstport = dstport; syn->seqno = htonl(tcpsock->nextSeqNo-1); syn->dataOffsetNS = 0x50; syn->flags = TCP_SYN; syn->winsz = htons(TCP_BUFFER_SIZE); ChecksumOutbound(tcpsock->currentOut); KernelThreadParams pars; memset(&pars, 0, sizeof(KernelThreadParams)); pars.stackSize = DEFAULT_STACK_SIZE; pars.name = "TCP Thread"; tcpsock->thread = CreateKernelThread(tcpThread, &pars, tcpsock); semSignal(&tcpsock->lock); uint8_t bitmap = 0; Semaphore *semConn = &tcpsock->semConnected; if (semPoll(1, &semConn, &bitmap, SEM_W_FILE(sock->fp->oflags), 0) == 0) { // we caught an interrupt before the connection was completed ERRNO = EINPROGRESS; return -1; } else { // report the error synchronously if there is one; otherwise success if (tcpsock->sockErr == 0) { return 0; } else { ERRNO = tcpsock->sockErr; return -1; }; }; };
static Socket* tcpsock_accept(Socket *sock, struct sockaddr *addr, size_t *addrlenptr) { TCPSocket *tcpsock = (TCPSocket*) sock; if (tcpsock->state != TCP_LISTENING) { ERRNO = EINVAL; return NULL; }; if (addrlenptr != NULL) { if ((*addrlenptr) < INET_SOCKADDR_LEN) { ERRNO = EINVAL; return NULL; }; }; int count = semWaitGen(&tcpsock->semConnWaiting, 1, SEM_W_FILE(sock->fp->oflags), sock->options[GSO_RCVTIMEO]); if (count < 0) { ERRNO = -count; return NULL; }; // a connection is pending, create the socket. // TODO: there is a race condition where we remove the pending connection from the list, // and another SYN arrives before we add the socket to the list, causing another connection // request to be created. We must work on that somehow. Socket *client = CreateTCPSocket(); client->domain = sock->domain; client->type = SOCK_STREAM; client->proto = IPPROTO_TCP; memset(client->options, 0, 8*GSO_COUNT); TCPSocket *tcpclient = (TCPSocket*) client; semWait(&tcpsock->lock); TCPPending *pend = tcpsock->firstPending; tcpsock->firstPending = pend->next; semSignal(&tcpsock->lock); semWait(&tcpclient->lock); memcpy(&tcpclient->sockname, &pend->local, sizeof(struct sockaddr)); memcpy(&tcpclient->peername, &pend->peer, sizeof(struct sockaddr)); tcpclient->state = TCP_ESTABLISHED; tcpclient->nextSeqNo = (uint32_t) getRandom(); tcpclient->nextAckNo = pend->ackno; tcpclient->currentOut = CreateOutbound(&tcpclient->sockname, &tcpclient->peername, 0); tcpclient->expectedAck = tcpclient->nextSeqNo; TCPSegment *syn = tcpclient->currentOut->segment; if (sock->domain == AF_INET) { syn->srcport = ((struct sockaddr_in*)&pend->local)->sin_port; syn->dstport = ((struct sockaddr_in*)&pend->peer)->sin_port; } else { syn->srcport = ((struct sockaddr_in6*)&pend->local)->sin6_port; syn->dstport = ((struct sockaddr_in6*)&pend->peer)->sin6_port; }; syn->seqno = htonl(tcpclient->nextSeqNo-1); syn->ackno = htonl(pend->ackno); syn->dataOffsetNS = 0x50; syn->flags = TCP_SYN | TCP_ACK; syn->winsz = htons(TCP_BUFFER_SIZE); ChecksumOutbound(tcpclient->currentOut); KernelThreadParams pars; memset(&pars, 0, sizeof(KernelThreadParams)); pars.stackSize = DEFAULT_STACK_SIZE; pars.name = "TCP Thread"; tcpclient->thread = CreateKernelThread(tcpThread, &pars, tcpclient); semSignal(&tcpclient->lock); semSignal(&tcpclient->semConnected); if (addrlenptr != NULL) *addrlenptr = INET_SOCKADDR_LEN; if (addr != NULL) { memcpy(addr, &pend->peer, INET_SOCKADDR_LEN); }; kfree(pend); return client; };
DWORD Fibonacci(LPVOID lpParam) { //LPSTR lpszParam = (LPSTR)lpParam; __CMD_PARA_OBJ* pCmdParaObj = (__CMD_PARA_OBJ*)lpParam; __FIBONACCI_CONTROL_BLOCK ControlBlock[5] = {0}; HANDLE hThread[5] = {NULL}; CHAR Buffer[12]; DWORD dwCounter; DWORD dwIndex,i; PrintLine("Fibonacci application running..."); GotoHome(); ChangeLine(); if(NULL == pCmdParaObj || pCmdParaObj->byParameterNum < 2) { return 0; } dwCounter = 0; for(i = 0;i < 5;i ++) { dwIndex = 0; while(pCmdParaObj->Parameter[1][dwCounter]) { Buffer[dwIndex] = pCmdParaObj->Parameter[1][dwCounter]; dwIndex ++; dwCounter ++; } Buffer[dwIndex] = 0; Str2Hex(Buffer,&ControlBlock[i].dwInitNum); //Convert the parameter to integer. if(pCmdParaObj->Parameter[1][dwCounter]) { break; } } i = 5; for(i;i > 0;i --) { hThread[i - 1] = CreateKernelThread( 0, //Stack size,use default. KERNEL_THREAD_STATUS_READY, //Status. PRIORITY_LEVEL_NORMAL, CalculateThread, //Start routine. (LPVOID)&ControlBlock[i - 1], NULL, "FIBONACCI"); if(NULL == hThread[i - 1]) //Failed to create kernel thread. { PrintLine("Create kernel thread failed."); break; } } // //Waiting for the kernel thread to over. // WaitForThisObject(hThread[0]); WaitForThisObject(hThread[1]); WaitForThisObject(hThread[2]); WaitForThisObject(hThread[3]); WaitForThisObject(hThread[4]); // //Now,we have calculated the fibonacci number,print them out. // for(i = 0;i < 5;i ++) { Int2Str(ControlBlock[i].dwInitNum,Buffer); PrintStr(Buffer); PrintStr("'s result is: "); Int2Str(ControlBlock[i].dwResult,Buffer); PrintStr(Buffer); GotoHome(); ChangeLine(); } // //Close the kernel thread. // DestroyKernelThread(hThread[0]); DestroyKernelThread(hThread[1]); DestroyKernelThread(hThread[2]); DestroyKernelThread(hThread[3]); DestroyKernelThread(hThread[4]); return 1L; }