unsigned char far * first_call(unsigned size) { retry: if ((Rtail == Rhead) && Frame_Count) { goto no_buffer; } if (Rhead < Rtail) { if ((Rtail - Rhead) > (size + 2)) { buffer_ok: PACKET_SIZE(Rhead) = size; return(RING_BUFFER + Rhead + 2); } else { goto no_buffer; } } else if ((MAX_RING_BUFFER - Rhead) > (size + 2)) { goto buffer_ok; } else { PACKET_SIZE(Rhead) = 0; Rhead = 0; goto retry; } no_buffer: return(NULL); // Bufer Full }
size_t readBaseband(int fd, void *buffer, unsigned short size) { LOG(LOGLEVEL_STATUS, "Reading %u bytes from flash...\n", size); writePacket(fd, 0x803, &size, sizeof(size)); void *temp = malloc(PACKET_SIZE(size)); size_t length = readPacket(fd, DEFAULT_TIMEOUT, temp, PACKET_SIZE(size)); void *ret = verifyPacket(temp, length); if (ret) { CmdPacket *packet = (CmdPacket *) temp; LOG(LOGLEVEL_DEBUG, "Read returns %d bytes:\n", packet->dataSize); LOGDO(LOGLEVEL_DEBUG, printBuffer(ret, packet->dataSize)); if (packet->dataSize <= size) { memcpy(buffer, ret, packet->dataSize); } else { LOG(LOGLEVEL_WARN, "Warning: The returned data does not fit into the buffer!\n"); memcpy(buffer, ret, size); } free(temp); return packet->dataSize; } else { LOG(LOGLEVEL_DEBUG, "Read returns: ERROR!\n"); free(temp); return 0; } }
void check_buffer() { if (driver_type == SLIP_DRIVER) { union REGS ireg, oreg; ireg.h.ah = 6; ireg.x.cx = _DS; ireg.x.dx = (unsigned)RING_BUFFER; loop: //rprintf("BEFORE SLIP"); int86(packet_int, &ireg, &oreg); //rprintf("AFTER SLIP"); if (oreg.x.ax) { statistics.lanInPackets++; rcv_ip(); goto loop; } } else { while (Frame_Count || Tick_Count) { if (Frame_Count) { if (!PACKET_SIZE(Rtail)) Rtail = 0; packet_buffer = RING_BUFFER + Rtail + 2; rcv_packet(); Rtail += (PACKET_SIZE(Rtail) + 2); disable(); Frame_Count--; enable(); } if (Tick_Count) { tmr_service(); } } } }
void eraseBaseband(int fd, unsigned int begin, unsigned int end) { // correct the end address as the boot loader does, but still give it the // 'wrong' value unsigned int end2 = end; if (begin == 0xa0020000) end = 0xa0310000; LOG(LOGLEVEL_INFO, "Erasing flash range 0x%08x-0x%08x...\n", begin, end); unsigned int base = end - begin; if (base == 0) base = 1; // no div by 0, rather wrong values ErasePacket packet = { .begin = begin, .end = end2 }; writePacket(fd, 0x805, &packet, ERASE_PACKET_SIZE); char buffer[PACKET_SIZE(sizeof(unsigned short))]; size_t length = readPacket(fd, WRITE_TIMEOUT, buffer, sizeof(buffer)); unsigned short *job = verifyPacket(buffer, length); LOG(LOGLEVEL_DEBUG, "Erase returns: %d\n", job ? *job : 0); if (job) { EraseStatusReplyPacket *reply; int previous = -1; do { writePacket(fd, 0x806, job, sizeof(*job)); char buffer2[PACKET_SIZE(ERASE_STATUS_REPLY_PACKET_SIZE)]; length = readPacket(fd, DEFAULT_TIMEOUT, buffer2, sizeof(buffer2)); reply = verifyPacket(buffer2, length); if (reply) { LOG(LOGLEVEL_DEBUG, "Erase status returns: done=%s current=0x%08x w00=%d\n", reply->done ? "yes" : "no", reply->current, reply->w00); if (reply->current >= begin && reply->current <= end) { int percent = (reply->current - begin) * 100 / base; if (percent != previous) { LOG(LOGLEVEL_INFO, "Current progress: %u%%\n", percent); previous = percent; } } else if (reply->current == 0xa0000000 && reply->done) { LOG(LOGLEVEL_ERROR, "Looks like the erase command failed due to an invalid secpack.\n"); } else { LOG(LOGLEVEL_INFO, "Current position: 0x%08x\n", reply->current); } } } while (reply && !reply->done); } else { LOG(LOGLEVEL_ERROR, "Erase command failed!\n"); } }
// here the second parameter is the length of msg of packet! void sendPacketPiecesToDatalink(char *packet, size_t length, int choose_link) { //printf("send packet pieces to data link at %s\n", nodeinfo.nodename); size_t maxPacketLength = linkinfo[choose_link].mtu - PACKET_HEADER_SIZE; NL_PACKET *tempPacket = (NL_PACKET *) packet; size_t tempLength = length; char *str = tempPacket->msg; //printf("src = %d, des = %d, linkinfo[choose_link].mtu = %d, PACKET_HEADER_SIZE = %d\n", tempPacket->src, tempPacket->dest, linkinfo[choose_link].mtu, PACKET_HEADER_SIZE); NL_PACKET piecePacket; piecePacket.src = tempPacket->src; piecePacket.dest = tempPacket->dest; piecePacket.kind = tempPacket->kind; piecePacket.seqno = tempPacket->seqno; piecePacket.hopcount = tempPacket->hopcount; piecePacket.pieceNumber = tempPacket->pieceNumber; piecePacket.pieceEnd = tempPacket->pieceEnd; piecePacket.src_packet_length = tempPacket->src_packet_length; piecePacket.checksum = tempPacket->checksum; piecePacket.trans_time = tempPacket->trans_time; piecePacket.is_resent = tempPacket->is_resent; while (tempLength > maxPacketLength) { //printf("packet remains %d bytes\n", tempLength); piecePacket.length = maxPacketLength; memcpy(piecePacket.msg, str, maxPacketLength); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); //("piece %d down_to_datalink\n", piecePacket.pieceNumber); str = str + maxPacketLength; piecePacket.pieceNumber = piecePacket.pieceNumber + 1; tempLength = tempLength - maxPacketLength; } piecePacket.pieceEnd = 1; piecePacket.length = tempLength; //printf("last piece contains %d bytes, pieceNumber = %d\n\n", tempLength ,piecePacket.pieceNumber); memcpy(piecePacket.msg, str, tempLength); ////("Required link is provided link = %d\n", choose_link); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); //("last piece %d down_to_datalink\n", piecePacket.pieceNumber); ////("Provided link = %d sent! \n", choose_link); }
/* down_to_network() RECEIVES NEW MESSAGES FROM THE APPLICATION LAYER AND PREPARES THEM FOR TRANSMISSION TO OTHER NODES. */ static EVENT_HANDLER(down_to_network) { //printf("down_to_network\n"); NL_PACKET p; p.length = sizeof(p.msg); //printf("p.length = %d, MAX_MESSAGE_SIZE = %d", p.length, MAX_MESSAGE_SIZE); CHECK(CNET_read_application(&p.dest, p.msg, &p.length)); CNET_disable_application(p.dest); p.src = nodeinfo.address; p.kind = NL_DATA; //p.hopcount = 0; memset(p.traveled_hops, -1, MAXHOPS*sizeof(int)); p.traveled_hops[0] = nodeinfo.nodenumber; p.traveled_hops_count = 1; p.trans_time = 0; p.seqno = NL_nextpackettosend(p.dest); p.pieceNumber = 0; p.pieceEnd = 0; p.checksum = CNET_ccitt((unsigned char *) (p.msg), p.length); lastPacket = &p; update_last_packet(&p); printf("Packet generated at host %d, des %d\n\n", nodeinfo.address, p.dest); flood3((char *) &p, PACKET_SIZE(p), 0, 0); }
/* down_to_network() RECEIVES NEW MESSAGES FROM THE APPLICATION LAYER AND PREPARES THEM FOR TRANSMISSION TO OTHER NODES. */ static EVENT_HANDLER( down_to_network) { NL_PACKET p; p.length = sizeof(p.msg); CHECK(CNET_read_application(&p.dest, p.msg, &p.length)); CNET_disable_application(p.dest); p.src = nodeinfo.address; p.kind = NL_DATA; p.seqno = NL_nextpackettosend(p.dest); p.hopcount = 0; p.pieceNumber = 0; p.pieceEnd = 0; p.src_packet_length = (int) p.length; p.checksum = CNET_ccitt((unsigned char *) (p.msg), p.src_packet_length); p.trans_time = 0; p.is_resent = 0; //lastPacket = &p; printf( "packet generated, src = %d, des = %d, seqno = %d, send_length = %d, checksum = %d\n\n", nodeinfo.address, p.dest, p.seqno, p.length, p.checksum); flood((char *) &p, PACKET_SIZE(p), 0, 0); update_last_packet(&p); }
void update_last_packet(NL_PACKET *last) { int index = find_address(last->dest); //NL_PACKET * lastsend = &(NL_table[index].lastpacket); memcpy(&(NL_table[index].lastpacket), last, PACKET_SIZE((*last))); //free(temp); }
void CutupProtocol::CreateEmptyPacket( ByteBuffer& data ) const { PCP_PACKET pPacket = CreatePacket(0, 0, CPCMD_NO_DATA, NULL, 0); DWORD dwPacketSize = PACKET_SIZE(pPacket); data.Alloc(dwPacketSize); memcpy(data, pPacket, dwPacketSize); FreePacket(pPacket); }
void secPack(int fd, void *secpack) { LOG(LOGLEVEL_INFO, "Sending secpack...\n"); writePacket(fd, 0x204, secpack, 0x800); char buffer[PACKET_SIZE(SECPACK_REPLY_PACKET_SIZE)]; size_t rlength = readPacket(fd, WRITE_TIMEOUT, buffer, sizeof(buffer)); SecpackReplyPacket *reply = verifyPacket(buffer, rlength); LOG(LOGLEVEL_DEBUG, "Secpack returns: unknown1=%d unknown2=0x%x\n", reply ? reply->unknown1 : 0, reply ? reply->unknown2 : 0); }
void seekBaseband(int fd, unsigned int offset) { LOG(LOGLEVEL_INFO, "Seeking to 0x%08x...\n", offset); writePacket(fd, 0x802, &offset, sizeof(offset)); char buffer[PACKET_SIZE(SEEK_REPLY_PACKET_SIZE)]; size_t length = readPacket(fd, DEFAULT_TIMEOUT, buffer, sizeof(buffer)); LOG(LOGLEVEL_DEBUG, "Seek returns:\n"); LOGDO(LOGLEVEL_DEBUG, printBuffer(verifyPacket(buffer, length), SEEK_REPLY_PACKET_SIZE)); }
static void writePacket(int fd, short cmd, void *data, size_t length) { char *buffer = malloc(PACKET_SIZE(length)); //memset(buffer, 0, PACKET_SIZE(length)); CmdPacket *packet = (CmdPacket *) buffer; packet->w02 = 2; packet->cmd = cmd; packet->dataSize = length; memcpy(buffer + CMD_PACKET_SIZE, data, length); CmdPacketEnd *end = (CmdPacketEnd *) (buffer + CMD_PACKET_SIZE + length); end->checksum = checksum(packet, data); end->w03 = 3; LOG(LOGLEVEL_TRACE, "Writing packet:\n"); LOGDO(LOGLEVEL_TRACE, printBuffer(buffer, PACKET_SIZE(length))); write(fd, buffer, PACKET_SIZE(length)); free(buffer); }
void piece_to_flood(char *packet, size_t mtu_from_src_to_dest) { NL_PACKET *tempPacket = (NL_PACKET *) packet; size_t maxPieceLength = mtu_from_src_to_dest - PACKET_HEADER_SIZE; size_t tempLength = tempPacket->length; char *str = tempPacket->msg; NL_PACKET piecePacket; piecePacket.src = tempPacket->src; piecePacket.dest = tempPacket->dest; piecePacket.kind = tempPacket->kind; piecePacket.seqno = tempPacket->seqno; piecePacket.hopcount = tempPacket->hopcount; piecePacket.pieceStartPosition = 0; piecePacket.pieceEnd = 0; piecePacket.src_packet_length = tempPacket->src_packet_length; piecePacket.checksum = tempPacket->checksum; piecePacket.trans_time = tempPacket->trans_time; piecePacket.is_resent = tempPacket->is_resent; while (tempLength > maxPieceLength) { piecePacket.length = maxPieceLength; memcpy(piecePacket.msg, str, maxPieceLength); piecePacket.piece_checksum = CNET_crc32( (unsigned char *) (piecePacket.msg), piecePacket.length); flood((char *) &piecePacket, PACKET_SIZE(piecePacket), 0, 0); str = str + maxPieceLength; piecePacket.pieceStartPosition = piecePacket.pieceStartPosition + maxPieceLength; tempLength = tempLength - maxPieceLength; } piecePacket.pieceEnd = 1; piecePacket.length = tempLength; memcpy(piecePacket.msg, str, tempLength); piecePacket.piece_checksum = CNET_crc32( (unsigned char *) (piecePacket.msg), piecePacket.length); flood((char *) &piecePacket, PACKET_SIZE(piecePacket), 0, 0); }
void endSecPack(int fd) { LOG(LOGLEVEL_INFO, "Ending secpack...\n"); unsigned short unknown = 0; writePacket(fd, 0x205, &unknown, sizeof(unknown)); char buffer[PACKET_SIZE(sizeof(unsigned short))]; size_t rlength = readPacket(fd, DEFAULT_TIMEOUT, buffer, sizeof(buffer)); unsigned short *ret = verifyPacket(buffer, rlength); LOG(LOGLEVEL_DEBUG, "End secpack returns: %d\n", ret ? *ret : 0); }
void second_call(void) { int spl; Rhead += (PACKET_SIZE(Rhead) + 2); spl = splnet(); Frame_Count++; splx(spl); }
void writeBaseband(int fd, void *data, size_t length) { LOG(LOGLEVEL_STATUS, "Writing %lu bytes to flash...\n", length); writePacket(fd, 0x804, data, length); char buffer[PACKET_SIZE(sizeof(unsigned short))]; size_t rlength = readPacket(fd, WRITE_TIMEOUT, buffer, sizeof(buffer)); unsigned short *ret = verifyPacket(buffer, rlength); if (ret && *ret) { LOG(LOGLEVEL_ERROR, "Write returns error: %d\n", *ret); } }
// here the second parameter is the length of msg of packet! void sendPacketPiecesToDatalink(char *packet, size_t length, int choose_link) { size_t maxPacketLength = linkinfo[choose_link].mtu - PACKET_HEADER_SIZE; NL_PACKET *tempPacket = (NL_PACKET *) packet; size_t tempLength = length; char *str = tempPacket->msg; NL_PACKET piecePacket; piecePacket.src = tempPacket->src; piecePacket.dest = tempPacket->dest; piecePacket.kind = tempPacket->kind; piecePacket.seqno = tempPacket->seqno; //piecePacket.hopcount = tempPacket->hopcount; piecePacket.trans_time = tempPacket->trans_time; piecePacket.traveled_hops_count = tempPacket->traveled_hops_count; memcpy(piecePacket.traveled_hops,tempPacket->traveled_hops, MAXHOPS); piecePacket.pieceEnd = tempPacket->pieceEnd; piecePacket.pieceNumber = tempPacket->pieceNumber; piecePacket.checksum = tempPacket->checksum; while (tempLength > maxPacketLength) { piecePacket.length = maxPacketLength; memcpy(piecePacket.msg, str, maxPacketLength); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); str = str + maxPacketLength; piecePacket.pieceNumber = piecePacket.pieceNumber + 1; tempLength = tempLength - maxPacketLength; } piecePacket.pieceEnd = 1; piecePacket.length = tempLength; memcpy(piecePacket.msg, str, tempLength); CHECK(down_to_datalink(choose_link, (char *) &piecePacket, PACKET_SIZE(piecePacket))); }
void setBaudRate(int fd, unsigned int speed) { LOG(LOGLEVEL_INFO, "Increasing baud rate to %dbps...\n", speed); writePacket(fd, 0x82, &speed, 4); char buffer[PACKET_SIZE(sizeof(unsigned int))]; int length = readPacket(fd, DEFAULT_TIMEOUT, buffer, sizeof(buffer)); unsigned int *setSpeed = (unsigned int *) verifyPacket(buffer, length); LOG(LOGLEVEL_DEBUG, "Set baudrate returns: %d, %s\n", setSpeed ? *setSpeed : 0, (speed == (setSpeed ? *setSpeed : 0)) ? "ok" : "NOT ok"); //LOGDO(LOGLEVEL_DEBUG, printBuffer(buffer, length)); struct termios options; tcgetattr(fd, &options); cfsetspeed(&options, speed); tcsetattr(fd, TCSANOW, &options); }
void prepareFlash(int fd) { LOG(LOGLEVEL_INFO, "Preparing flash access...\n"); short param = 0; writePacket(fd, 0x84, ¶m, sizeof(param)); char buffer[PACKET_SIZE(CFI1_PACKET_SIZE)]; size_t length = readPacket(fd, DEFAULT_TIMEOUT, buffer, sizeof(buffer)); LOG(LOGLEVEL_DEBUG, "CFI Stage 1 returns:\n"); LOGDO(LOGLEVEL_DEBUG, printBuffer(verifyPacket(buffer, length), CFI1_PACKET_SIZE)); writePacket(fd, 0x85, NULL, 0); length = readPacket(fd, DEFAULT_TIMEOUT, buffer, sizeof(buffer)); short *unknown = verifyPacket(buffer, length); LOG(LOGLEVEL_DEBUG, "CFI Stage 2 returns: %d\n", unknown ? *unknown : 0); //LOGDO(LOGLEVEL_DEBUG, printBuffer(buffer, length)); }
static void down_to_network(char* msg, int length, CnetAddr dest) { /* modified to take stuff form my transport layer instead of application layer -- Sumit*/ NL_PACKET p; p.length = length; memcpy(&(p.msg[0]),msg,length); p.dest = dest; p.src = nodeinfo.address; p.kind = NL_DATA; p.hopsleft = maxhops(p.dest); p.hopstaken = 1; p.timesent = nodeinfo.time_in_usec; p.seqno = nextpackettosend(p.dest); selective_flood((char *)&p, PACKET_SIZE(p), whichlink(p.dest)); }
VersionPacket getBootVersion(int fd) { LOG(LOGLEVEL_INFO, "Getting bootloader version...\n"); writePacket(fd, 0x801, NULL, 0); char buffer[PACKET_SIZE(VERSION_PACKET_SIZE)]; size_t length = readPacket(fd, 5, buffer, sizeof(buffer)); VersionPacket *packet = (VersionPacket *) verifyPacket(buffer, length); if (packet) { LOG(LOGLEVEL_DEBUG, "Got bootloader version: %d.%d\n", packet->major, packet->minor); //LOGDO(LOGLEVEL_DEBUG, printBuffer(buffer, length)); return *packet; } VersionPacket zeroBoot; memset(&zeroBoot, 0, VERSION_PACKET_SIZE); return zeroBoot; }
BOOL CutupProtocol::AddRecvPacket( const LPBYTE pData, DWORD dwSize, COMM_NAME fromComm, CPGUID* pCpguid /*= NULL*/ ) { if (NULL == pData || 0 == dwSize) return FALSE; PCP_PACKET pPacket = (PCP_PACKET) pData; //if (pPacket->header.size == 0 && pPacket->header.index == 0) return TRUE; DWORD dwPacketSize = PACKET_SIZE(pPacket); if (dwPacketSize != dwSize) { errorLog(_T("invalid packetsize %u %u"), dwPacketSize, dwSize); return FALSE; } BYTE cmd = pPacket->header.cmd; const CPGUID& fromGuid = pPacket->header.guid; CPSERIAL serial = pPacket->header.serial; //返回 包中含带的cpguid if (NULL != pCpguid) *pCpguid = fromGuid; switch (cmd) { case CPCMD_ABORT_SEND: AbortSending(fromGuid, serial); break; case CPCMD_RESEND: if (pPacket->header.size == sizeof(DWORD)) { DWORD dwReceivedSize = *(LPDWORD)pPacket->data; ResendMsg(fromGuid, serial, pPacket->header.index, dwReceivedSize); } break; case CPCMD_DATA_MORE: case CPCMD_DATA_END: return HandleDataPacket(pPacket, fromComm); break; case CPCMD_NO_DATA: break; } return TRUE; }
/* up_to_network() IS CALLED FROM THE DATA LINK LAYER (BELOW) TO ACCEPT A PACKET FOR THIS NODE, OR TO RE-ROUTE IT TO THE INTENDED DESTINATION. */ int up_to_network(char *packet, size_t length, int arrived_on_link) { NL_PACKET *p = (NL_PACKET *) packet; if(p->src == nodeinfo.address){ printf ("drop a packet at %d, src = %d, des = %d, seqno = %d\n", nodeinfo.address, p->src, p->dest, p->seqno); return 0; } //printf("up to network at %d (from %d to %d)\n", nodeinfo.address, p->src, p->dest); ++p->hopcount; /* took 1 hop to get here */ mtu = linkinfo[arrived_on_link].mtu; p->trans_time += ((CnetTime)8000 * 1000 * mtu / linkinfo[arrived_on_link].bandwidth + linkinfo[arrived_on_link].propagationdelay)*100/mtu; ////("me = %d, dest = %d =======\n", nodeinfo.address, p->dest); /* IS THIS PACKET IS FOR ME? */ if (p->dest == nodeinfo.address) { switch (p->kind) { case NL_DATA: if (p->seqno == NL_packetexpected(p->src)) { length = p->length; memcpy(rb[p->src], (char *) p->msg, length); rb[p->src] = rb[p->src] + length; //packet_length[p->src] += length; if (p->pieceEnd) { CnetAddr tmpaddr; length = p->pieceNumber * (linkinfo[arrived_on_link].mtu - PACKET_HEADER_SIZE) + p->length; //length = packet_length[p->src]; //packet_length[p->src] = 0; int p_checksum = p->checksum; int checksum = CNET_ccitt( (unsigned char *) (receiveBuffer[p->src]), p->src_packet_length); if(p->is_resent){ printf("%d received a resent packet, src = %d, des = %d, seqno = %d, send_length = %d,receive_length = %d \n", nodeinfo.address, p->src, p->dest, p->seqno, p->src_packet_length, length); }else{ printf("%d received a packet, src = %d, des = %d, seqno = %d, send_length = %d,receive_length = %d \n", nodeinfo.address, p->src, p->dest, p->seqno, p->src_packet_length, length); } printf("last_piece_trans_time = %d, hopcount = %d\n", p->trans_time, p->hopcount); printf("src_checksum = %d calc_checksum = %d, ", p_checksum, checksum); if (p_checksum != checksum) { /***************************send back error ack**************/ printf("error packet\n\n"); NL_savehopcount(p->src, p->trans_time, arrived_on_link); tmpaddr = p->src; /* swap src and dest addresses */ p->src = p->dest; p->dest = tmpaddr; if(p->is_resent == 1) p->kind = NL_ERR_ACK_RESENT; else p->kind = NL_ERR_ACK; p->hopcount = 0; p->pieceNumber = 0; p->pieceEnd = 0; p->length = 0; p->src_packet_length = 0; p->checksum = 0; p->trans_time = 0; flood3(packet, PACKET_HEADER_SIZE, arrived_on_link, 0); rb[p->src] = &receiveBuffer[p->src][0]; //printf("\n"); return 0; /***************************end******************************/ } /* if (CNET_write_application(receiveBuffer[p->src], &length) == -1) { //source node should send this msg again //printf("===\ncnet_errno = %d\n===\n", cnet_errno); //printf("error count: %d\n", ++ err_count); if (cnet_errno == ER_CORRUPTFRAME) { printf("corrupt packet\n"); //printf("\nWarning: frame corrupted\n==\n"); } else if (cnet_errno == ER_MISSINGMSG) { printf("unordered packet\n"); //printf("\nWarning: frame missed\n\n"); } else if(cnet_errno == ER_BADSIZE){ printf("loss packet\n"); } } */ CHECK(CNET_write_application(receiveBuffer[p->src], &length)); printf("correct packet\n", p->dest, p->seqno, p->src); rb[p->src] = &receiveBuffer[p->src][0]; inc_NL_packetexpected(p->src); NL_savehopcount(p->src, p->trans_time, arrived_on_link); tmpaddr = p->src; /* swap src and dest addresses */ p->src = p->dest; p->dest = tmpaddr; p->kind = NL_ACK; p->hopcount = 0; p->pieceNumber = 0; p->pieceEnd = 0; p->length = 0; p->src_packet_length = 0; p->checksum = 0; p->trans_time = 0; p->is_resent = 0; flood3(packet, PACKET_HEADER_SIZE, arrived_on_link, 0); printf("\n"); } } break; case NL_ACK: if (p->seqno == NL_ackexpected(p->src)) { ////("ACK come!\n"); inc_NL_ackexpected(p->src); NL_savehopcount(p->src, p->trans_time, arrived_on_link); NL_set_has_resent(p->src, 0); CHECK(CNET_enable_application(p->src)); } break; case NL_ERR_ACK: printf("NL_ERR_ACK!\n"); if (p->seqno == NL_ackexpected(p->src)){ if(NL_get_has_resent(p->src) == 0) { NL_savehopcount(p->src, p->trans_time, arrived_on_link); NL_PACKET * packettoresend = get_last_packet(p->src); printf("src = %d, des = %d, seqno = %d, send_length = %d, checksum = %d\n", packettoresend->src, packettoresend->dest, packettoresend->seqno, packettoresend->length, packettoresend->checksum); printf("resend it\n"); int len = PACKET_HEADER_SIZE + packettoresend->length; packettoresend->is_resent = 1; NL_set_has_resent(p->src, 1); NL_inc_resent_times(p->src); // for debug flood3((char *) packettoresend, len, 0, 0); } else{ printf("this packet has already been resent, dont resent it again\n"); } } else{ printf("this packet has already been correct received, dont resent it again\n"); } printf("\n"); break; case NL_ERR_ACK_RESENT: printf("NL_ERR_ACK_RESENT!\n"); if (p->seqno == NL_ackexpected(p->src)) { NL_savehopcount(p->src, p->trans_time, arrived_on_link); NL_PACKET * packettoresend = get_last_packet(p->src); printf("resend a resent packet, src = %d, des = %d, seqno = %d, send_length = %d, checksum = %d\n", packettoresend->src, packettoresend->dest, packettoresend->seqno, packettoresend->length, packettoresend->checksum); printf("this packet has been resent %d times before this time\n", NL_get_resent_times(p->src)); //for debug NL_inc_resent_times(p->src); // for debug int len = PACKET_HEADER_SIZE + packettoresend->length; packettoresend->is_resent = 1; flood3((char *) packettoresend, len, 0, 0); } else{ printf("this packet has already been correct received, dont resent it again\n"); } printf("\n"); break; default: //("it's nothing!====================\n"); break; } } /* THIS PACKET IS FOR SOMEONE ELSE */ else { // //("hopcount = %d\n", p->hopcount); // //("MAXHOPS = %d\n", MAXHOPS); if (p->hopcount < MAXHOPS) { /* if not too many hops... */ ////("other's frame!\n"); //("piece for another node arrives\n"); length = p->length; memcpy(rb[p->src], (char *) p->msg, length); rb[p->src] = rb[p->src] + length; //packet_length[p->src] += length; if (p->pieceEnd) { //printf("last piece for another node arrives\n"); length = p->pieceNumber * (linkinfo[arrived_on_link].mtu - PACKET_HEADER_SIZE) + p->length; //length = packet_length[p->src]; //packet_length[p->src] = 0; NL_savehopcount(p->src, p->trans_time, arrived_on_link); NL_PACKET wholePacket; wholePacket.src = p->src; wholePacket.dest = p->dest; wholePacket.kind = p->kind; wholePacket.seqno = p->seqno; wholePacket.hopcount = p->hopcount; wholePacket.pieceNumber = 0; wholePacket.pieceEnd = 0; wholePacket.length = length; wholePacket.src_packet_length = p->src_packet_length; wholePacket.checksum = p->checksum; wholePacket.trans_time = p->trans_time; wholePacket.is_resent = p->is_resent; memcpy(wholePacket.msg, receiveBuffer[p->src], length); // //("contents of msg forwarding is \n %s\n", receiveBuffer[p->src]); flood3((char *) &wholePacket, PACKET_SIZE(wholePacket), 0, arrived_on_link); rb[p->src] = &receiveBuffer[p->src][0]; } } else { /* silently drop */; //free(&p->msg); //free(p); } } return (0); }
BOOL CutupProtocol::HandleDataPacket( const PCP_PACKET pPacket, COMM_NAME fromComm ) { BYTE cmd = pPacket->header.cmd; const CPGUID& fromGuid = pPacket->header.guid; CPSERIAL serial = pPacket->header.serial; DWORD packetIndex = pPacket->header.index; //根据guid查找对应的serialMsgMap GuidRecvMap::iterator guidIter = m_guidRecvMap.find(fromGuid); if (guidIter == m_guidRecvMap.end()) { SerialMsgMap temp; std::pair<GuidRecvMap::iterator, bool> res = m_guidRecvMap.insert(GuidRecvMap::value_type(fromGuid, temp)); if (res.second) guidIter = res.first; } if (guidIter == m_guidRecvMap.end()) return FALSE; //查找或创建该序列号serial对应的消息结构 SerialMsgMap& serialMsgMap = guidIter->second; SerialMsgMap::iterator serialIter = serialMsgMap.find(serial); if (serialIter == serialMsgMap.end()) { RECV_MSG temp; std::pair<SerialMsgMap::iterator, bool> res = serialMsgMap.insert(SerialMsgMap::value_type(serial, temp)); if (res.second) { serialIter = res.first; _time64(&serialIter->second.begintime); serialIter->second.dwDataSize = 0; serialIter->second.dwExpectedIndex = 0; serialIter->second.iErrorCountPerPacket = 0; } } if (serialIter == serialMsgMap.end()) return FALSE; //检查消息序列号的正确性 if (pPacket->header.index != serialIter->second.dwExpectedIndex) { errorLog(_T("serial:%u. expected index:%u. recv index:%u"), serial, serialIter->second.packetList.size(), pPacket->header.index); serialIter->second.iErrorCountPerPacket++; if (serialIter->second.iErrorCountPerPacket > 5) { debugLog(_T("request abort %u"), serial); AddCmdPacket(fromGuid, fromComm, serial, CPCMD_ABORT_SEND, 0, NULL, 0, TRUE); } else { debugLog(_T("request resend %u %u %u"), serial, serialIter->second.dwExpectedIndex, serialIter->second.dwDataSize); AddCmdPacket(fromGuid, fromComm, serial, CPCMD_RESEND, serialIter->second.dwExpectedIndex, (const LPBYTE)&serialIter->second.dwDataSize, sizeof(serialIter->second.dwDataSize), TRUE); } return TRUE; } //调整数据参数 serialIter->second.dwExpectedIndex++; serialIter->second.dwDataSize += pPacket->header.size; serialIter->second.iErrorCountPerPacket = 0; serialIter->second.lastCommname = fromComm; //将消息复制并追加到packetList末尾 DWORD dwPacketSize = PACKET_SIZE(pPacket); PCP_PACKET pClone = (PCP_PACKET) malloc(dwPacketSize); memcpy(pClone, pPacket, dwPacketSize); serialIter->second.packetList.push_back(pClone); //检查是否还有数据 if (CPCMD_DATA_END == cmd) { //消息数据包已收齐,做合并处理 ByteBuffer byteData; MergePackets(serialIter->second.packetList, byteData); m_recvQueueSection.Enter(); { RECV_BYTE_MSG msg; msg.fromCPGuid = fromGuid; msg.byteData = byteData; m_recvQueue.push_back(msg); } m_recvQueueSection.Leave(); ::ReleaseSemaphore(m_hRecvSemaphore, 1, NULL); //清理消息 FreePackets(serialIter->second.packetList); serialMsgMap.erase(serialIter); } return TRUE; }
/* up_to_network() IS CALLED FROM THE DATA LINK LAYER (BELOW) TO ACCEPT A PACKET FOR THIS NODE, OR TO RE-ROUTE IT TO THE INTENDED DESTINATION. */ int up_to_network(char *packet, size_t length, int arrived_on_link) { printf("up to network at hop %s \n", nodeinfo.nodename); NL_PACKET *p = (NL_PACKET *) packet; //if(p->traveled_hops_count < 1 || p->traveled_hops_count > MAXHOPS) //return 0; //++p->hopcount; /* took 1 hop to get here */ if(p->traveled_hops_count > 0 && p->traveled_hops_count<= MAXHOPS){ is_traveled_hop = 0; for(i=0; i<p->traveled_hops_count; ++i){ if(p->traveled_hops[i] == nodeinfo.nodenumber){ is_traveled_hop = 1; break; } } if(is_traveled_hop != 1){ printf("seqno %d pieceNumber %d src %d des %d current_hop %d\n", p->seqno, p->pieceNumber, p->src, p->dest, nodeinfo.address); printf("p->traveled_hops_count %d\n", p->traveled_hops_count); p->traveled_hops[p->traveled_hops_count++] = nodeinfo.nodenumber; mtu = linkinfo[arrived_on_link].mtu; p->trans_time += ((CnetTime)8000000 * mtu / linkinfo[arrived_on_link].bandwidth + linkinfo[arrived_on_link].propagationdelay)/mtu; //p->trans_time += linkinfo[arrived_on_link].costperframe; } else{ printf("seqno %d pieceNumber %d src %d des %d current_hop %d. This hop has been traveled\n", p->seqno, p->pieceNumber, p->src, p->dest, nodeinfo.address); printf("\n"); /*free(&p->traveled_hops); free(&p->msg); free(p); */ return 0; } } /* IS THIS PACKET IS FOR ME? */ if (p->dest == nodeinfo.address) { switch (p->kind) { case NL_DATA: if (p->seqno == NL_packetexpected(p->src)) { length = p->length; memcpy(rb[p->src], (char *) p->msg, length); rb[p->src] = rb[p->src] + length; packet_length[p->src] += length; printf("This piece traveled %d hops\n", p->traveled_hops_count); if (p->pieceEnd) { CnetAddr tmpaddr; //length = p->pieceNumber * (linkinfo[arrived_on_link].mtu // - PACKET_HEADER_SIZE) + p->length; length = packet_length[p->src]; int p_checksum = p->checksum; int checksum = CNET_ccitt( (unsigned char *) (receiveBuffer[p->src]), (int) length); if (p_checksum != checksum) { /***************************send back error ack**************/ //NL_savehopcount(p->src, p->hopcount, arrived_on_link); NL_savehopcount(p->src, p->trans_time, arrived_on_link); tmpaddr = p->src; /* swap src and dest addresses */ p->src = p->dest; p->dest = tmpaddr; p->kind = NL_ERR_ACK; //p->hopcount = 0; p->traveled_hops_count = 0; memset(p->traveled_hops, -1, MAXHOPS*sizeof(int)); p->trans_time = 0; p->length = 0; flood3(packet, PACKET_HEADER_SIZE, arrived_on_link, 0); return 0; /***************************end******************************/ } if (CNET_write_application(receiveBuffer[p->src], &length) == -1) { //source node should send this msg again if (cnet_errno == ER_CORRUPTFRAME) { printf("Warning: host %d received a corrupt packet (seqno = %d) from %d\n", nodeinfo.address, p->seqno, p->src); } else if (cnet_errno == ER_MISSINGMSG) { printf("Warning: host %d received a unordered packet (seqno = %d) from %d\n", nodeinfo.address, p->seqno, p->src); }else if (cnet_errno == ER_BADSIZE){ printf("Warning: host %d received a loss packet (seqno = %d) from %d\n", nodeinfo.address, p->seqno, p->src); } } else { printf("host %d received a correct packet (seqno = %d) from %d\n", nodeinfo.address, p->seqno, p->src); } rb[p->src] = &receiveBuffer[p->src][0]; packet_length[p->src] = 0; inc_NL_packetexpected(p->src); //NL_savehopcount(p->src, p->hopcount, arrived_on_link); NL_savehopcount(p->src, p->trans_time, arrived_on_link); //send a NL_ACK back to source host tmpaddr = p->src; /* swap src and dest addresses */ p->src = p->dest; p->dest = tmpaddr; p->kind = NL_ACK; //p->hopcount = 0; p->traveled_hops_count = 0; memset(p->traveled_hops, -1, MAXHOPS*sizeof(int)); p->trans_time = 0; p->length = 0; packet_length[p->src] = 0; //printf("right frame! up to app\n"); flood3(packet, PACKET_HEADER_SIZE, arrived_on_link, 0); //printf("send ack\n"); } } break; case NL_ACK: if (p->seqno == NL_ackexpected(p->src)) { //printf("ACK come!\n"); inc_NL_ackexpected(p->src); //NL_savehopcount(p->src, p->hopcount, arrived_on_link); NL_savehopcount(p->src, p->trans_time, arrived_on_link); CHECK(CNET_enable_application(p->src)); } break; case NL_ERR_ACK: printf("ERROR ACK!\n"); if (p->seqno == NL_ackexpected(p->src)) { //NL_savehopcount(p->src, p->hopcount, arrived_on_link); NL_savehopcount(p->src, p->trans_time, arrived_on_link); printf("packet %d (to %d) error!\n", p->seqno, p->src); NL_PACKET * packettoresend = get_last_packet(p->src); printf("length = %d seq = %d\n", packettoresend->length, packettoresend->seqno); int a = packettoresend->checksum; int b = CNET_ccitt((unsigned char *) (packettoresend->msg), (int) (packettoresend->length)); if (a == b) { printf("ok!\n"); } else printf("wrong!\n"); int len = PACKET_HEADER_SIZE + packettoresend->length; flood3((char *) packettoresend, len, 0, 0); } break; default: ; //printf("it's nothing!====================\n"); } } /* THIS PACKET IS FOR SOMEONE ELSE */ else { // printf("hopcount = %d\n", p->hopcount); // printf("MAXHOPS = %d\n", MAXHOPS); //if (p->hopcount < MAXHOPS) { /* if not too many hops... */ //printf("other's frame!\n"); //printf("piece for another node arrives\n"); length = p->length; memcpy(rb[p->src], (char *) p->msg, length); rb[p->src] = rb[p->src] + length; packet_length[p->src] += length; if (p->pieceEnd) { //printf("last piece for another node arrives\n"); //length = p->pieceNumber * (linkinfo[arrived_on_link].mtu // - PACKET_HEADER_SIZE) + p->length; length = packet_length[p->src]; //NL_savehopcount(p->src, p->hopcount, arrived_on_link); NL_savehopcount(p->src, p->trans_time, arrived_on_link); NL_PACKET wholePacket; wholePacket.src = p->src; wholePacket.dest = p->dest; wholePacket.kind = p->kind; wholePacket.seqno = p->seqno; //wholePacket.hopcount = p->hopcount; wholePacket.trans_time = p->trans_time; wholePacket.traveled_hops_count = p->traveled_hops_count; memcpy(wholePacket.traveled_hops,p->traveled_hops, MAXHOPS); wholePacket.pieceEnd = 0; wholePacket.pieceNumber = 0; wholePacket.length = length; packet_length[p->src] = 0; memcpy(wholePacket.msg, receiveBuffer[p->src], length); flood3((char *) &wholePacket, PACKET_SIZE(wholePacket), 0, arrived_on_link); rb[p->src] = &receiveBuffer[p->src][0]; } //} else // printf("drop\n"); /* silently drop */; } printf("\n"); return (0); }