/* * * Function: start * Description: 启动子系统服务名 * Input: * pstClient - 通讯接口字 * pczName - 子系统服务名 * OutPut: * * Return: * -1 - FAIL * 0 - SUCC * Other: * */ int CommandServer::start( apr_socket_t *pstClient, char *pczName ) { int iEmptyIdx = 0, iShmOffset; pid_t tChild; char aczRsp[100]; apr_size_t tLen; if( getRunningIndex( pczName ) != -1 ) { LOG4C(( LOG_WARN, "%s 子系统已经在运行\n", pczName )); sprintf( aczRsp, "< %-11.11s>子系统已经在运行", pczName ); tLen = strlen( aczRsp ); apr_socket_send( pstClient, aczRsp, &tLen ); return -1; } if( (iEmptyIdx = getEmptyIndex()) == -1 ) { LOG4C(( LOG_WARN, "无足够的管理器运行 %s 子系统\n", pczName )); sprintf( aczRsp, "无足够的管理器运行 %s 子系统", pczName ); tLen = strlen( aczRsp ); apr_socket_send( pstClient, aczRsp, &tLen ); return -1; } iShmOffset = iEmptyIdx*m_pstConfig->m_iMaxChild; if( !fileExistAndExec( "nlmanager" ) ) { LOG4C(( LOG_WARN, "nlmanager 不存在, 请检查系统\n" )); sprintf( aczRsp, "系统错误, 请检查" ); tLen = strlen( aczRsp ); apr_socket_send( pstClient, aczRsp, &tLen ); return -1; } if( (tChild = runAnyManager( pczName, iEmptyIdx )) == -1 ) { LOG4C(( LOG_WARN, "运行 nlmanager 失败, 请检查系统\n" )); sprintf( aczRsp, "运行 nlmanager 失败, 请检查系统" ); tLen = strlen( aczRsp ); apr_socket_send( pstClient, aczRsp, &tLen ); return -1; } LOG4C(( LOG_INFO, "%s 子系统已经开始运行\n", pczName )); sprintf( aczRsp, "< %-11.11s>子系统启动成功|Start Success", pczName ); tLen = strlen( aczRsp ); apr_socket_send( pstClient, aczRsp, &tLen ); saveBusiManager( iEmptyIdx, pczName, tChild ); LOG4C(( LOG_INFO, "%s 子系统已经开始运行, 等待下一次运行\n", pczName )); return 0; }
u08 Ctransport::tx(u08 _handle, u08 nodeId, u08* pDat, u08 numBytes, u08** ppRsp) { u08 i; u08 ret; switch (_handle) { //======================================================================== // Create a new transaction - get first available handle/index case handleEmpty: i = getEmptyIndex(); if (i < maxTransactions) { txList[i].used = true; txList[i].nodeId = nodeId; memcpy(txList[i].txDat, pDat, numBytes); memset(txList[i].rxDat, 0, sizeof(txList[i].rxDat)); txList[i].len = numBytes; //txList[index].rsp = Rsp; txList[i].done = false; txList[i].cntTx = 0; txList[i].timeout = false; } else { i = handleEmpty; } ret = i; break; //======================================================================== // Reset the handle case handleDone: ret = handleEmpty; break; //======================================================================== // Respond with existing transaction in Queue default: if (_handle < maxTransactions) { if (txList[_handle].timeout) { txList[_handle].timeout = false; txList[_handle].used = false; ret = handleEmpty; } else if (txList[_handle].done) { txList[_handle].used = false; *ppRsp = (u08*) &txList[_handle].rxDat[0]; ret = handleDone; } else { ret = _handle; } } else { ret = handleEmpty; } break; } return ret; }
bool IcmpWrapper::receive(void) { if(rawSock->fd == -1) { // ICMP socket closed? quit, no data return false; } //----------------------- // receive the data // recvfrom will receive only one ICMP packet, even if there are more than 1 packets waiting in socket struct sockaddr_in src_addr; socklen_t addrlen = sizeof(struct sockaddr); ssize_t res = recvfrom(rawSock->fd, recvBfr, RECV_BFR_SIZE, MSG_DONTWAIT, (struct sockaddr *) &src_addr, &addrlen); if(res == -1) { // if recvfrom failed, no data if(errno != EAGAIN && errno != EWOULDBLOCK) { Debug::out(LOG_ERROR, "IcmpWrapper::receive() - recvfrom() failed, errno: %d", errno); } return false; } else if (res == 0) { Debug::out(LOG_ERROR, "IcmpWrapper::receive() - recvfrom() returned 0"); return false; } // res now contains length of ICMP packet (header + data) Debug::out(LOG_DEBUG, "IcmpWrapper::receive() %d bytes from %s", res, inet_ntoa(src_addr.sin_addr)); //----------------------- // parse response to the right structs int i = getEmptyIndex(); // now find space for the datagram if(i == -1) { // no space? fail, but return that we were able to receive data Debug::out(LOG_DEBUG, "IcmpWrapper::receive() - dgram_getEmpty() returned -1"); return true; } TStingDgram *d = &dgrams[i]; d->clear(); d->time = Utils::getCurrentMs(); //------------- // fill IP header d->data[0] = 0x45; // IP ver, IHL Utils::storeWord(d->data + 2, 20 + res); // data[2 .. 3] = TOTAL LENGTH = IP header lenght (20) + ICMP header & data length (res) d->data[8] = 128; // TTL d->data[9] = ICMP; // protocol Utils::storeDword(d->data + 12, ntohl(src_addr.sin_addr.s_addr)); // data[12 .. 15] - source IP Utils::storeDword(d->data + 16, localIp); // data[16 .. 19] - destination IP WORD checksum = TRawSocks::checksum((WORD *) d->data, 20); Utils::storeWord(d->data + 10, checksum); // calculate chekcsum, store to data[10 .. 11] //------------- // fill IP_DGRAM header Utils::storeWord(d->data + 30, res); // data[30 .. 31] - pkt_length - length of IP packet data block (res) Utils::storeDword(d->data + 32, 128); // data[32 .. 35] - timeout - timeout of packet life //------------- // now append ICMP packet int rest = MIN(STING_DGRAM_MAXSIZE - 48 - 2, res); // we can store only (STING_DGRAM_MAXSIZE - 48 - 2) = 462 bytes of ICMP packets to fit 512 B memcpy(d->data + 48, recvBfr, rest); // copy whole ICMP packet beyond the IP_DGRAM structure Utils::storeWord(d->data + 52, rawSockHeads.echoId); // fake this ECHO ID, because linux replaced the ECHO ID when sending ECHO packet //-------------- // epilogue - update stuff, unlock mutex, success! d->count = 48 + rest; // update how many bytes this gram contains all together icmpDataCount = calcDataByteCountTotal(); // update icmpDataCount Debug::out(LOG_DEBUG, "IcmpWrapper::receive() - icmpDataCount is now %d bytes", icmpDataCount); return true; }