void send_frames(int link){ //printf("Send frames called for link : %d\n", link); size_t len = sizeof(FRAME); CnetTime timeout; FRAME *f = queue_remove(links[link].sender, &len); switch(f->payload.kind){ case DL_DATA : if(!links[link].ack_received[f->payload.A]) { CHECK(CNET_write_physical(link, (char*)f, &len)); timeout = (len*((CnetTime)8000000)/linkinfo[link].bandwidth + linkinfo[link].propagationdelay); start_timer(link, timeout); queue_add(links[link].sender, f, len); } else { if(queue_nitems(links[link].sender) > 0) send_frames(link); } break; case DL_ACK : CHECK(CNET_write_physical(link, (char*)f, &len)); timeout = (len*((CnetTime)8000000)/linkinfo[link].bandwidth + linkinfo[link].propagationdelay); start_timer(link, timeout); break; case RT_DATA: //printf("RT packet sending on link : %d\n", link); CHECK(CNET_write_physical(link, (char*)f, &len)); timeout = (len*((CnetTime)8000000)/linkinfo[link].bandwidth + linkinfo[link].propagationdelay); start_timer(link, timeout); break; } free(f); }
static void transmit_frame(){ if(strcmp(nodeinfo.nodename,"Dehradun")==0 && flag_sen == 1 && count < 1000 ){ char *frame=(char *)malloc(64*sizeof(char)); size_t length=64; int i=0; if(SN==0){ sprintf(frame, "%dmessage %d is %s",SN, count+1, messages[0]); } else{ sprintf(frame, "%dmessage %d is %s",SN, count+1, messages[1]); } printf("\nsending %u bytes of ' %s ': ", length, frame); for(i=1;i<9;i++){ check[i]=frame[i]; } CHECK(CNET_write_physical(1, frame, &length)); id=CNET_start_timer(EV_TIMER1,3600,0); flag_sen=0; } else if(strcmp(nodeinfo.nodename,"KGP")==0 && flag_rec == 1){ char *ack=(char *)malloc(48*sizeof(char)); size_t length=48; sprintf(ack, "%d",RN); CHECK(CNET_write_physical(1, ack, &length)); flag_rec =0; printf("\n Acknowledgement send "); } }
static void transmit_frame(FRAME frame) { size_t frameLength; if (frame.kind == DL_DATA) // DATA FRAME { // SET THE TIMER PRIOR TO SENDING printf("DATA TRANSMITTED\nTO:\t%s\n", nodenames[frame.destNode]); set_timer(frame); } else // ACK FRAME { // NO TIMER, SIMPLY SEND THE FRAME OUT printf("\nACK TRANSMITTED\n"); } printf("VIA LINK:\t%d\nSEQ NO:\t%d\n\n", frame.link, frame.seq); // CALCULATE THE CHECKSUM AND ADD TO THE FRAME frame.checksum = 0; frameLength = FRAME_SIZE(frame); frame.checksum = CNET_ccitt((unsigned char *)&frame, frameLength); // SEND FRAME VIA PHYSICAL LAYER CHECK(CNET_write_physical(frame.link, (char *)&frame, &frameLength)); }
//----------------------------------------------------------------------------- // flush a queue void flush_datalink_queue(CnetEvent ev, CnetTimerID t1, CnetData data) { int current_link = (int) data; if (queue_nitems(output_queues[current_link]) > 0) { // take a first datalink frame size_t containter_len; DTG_CONTAINER * dtg_container = queue_remove(output_queues[current_link], &containter_len); // write datalink frame to the link DL_FRAME frame; size_t datagram_length = dtg_container->len; frame.checksum = dtg_container->checksum; memcpy(&frame.data, dtg_container->data, datagram_length); size_t frame_length = datagram_length + DL_FRAME_HEADER_SIZE; int link = dtg_container->link; CHECK(CNET_write_physical(link, (char *)&frame, &frame_length)); //compute timeout for the link double bandwidth = linkinfo[link].bandwidth; CnetTime timeout = 1+8000005.0*(frame_length / bandwidth); datalink_timers[link] = CNET_start_timer(EV_DATALINK_FLUSH, timeout, current_link); free(dtg_container); } else { datalink_timers[current_link] = NULLTIMER; } }
/// Write a frame to the given Ethernet link. /// void dll_eth_write(struct dll_eth_state *state, CnetNICaddr dest, const char *data, uint16_t length) { if (!data || length == 0) return; struct eth_frame frame; // Set the destination and source address. memcpy(frame.dest, dest, sizeof(CnetNICaddr)); memcpy(frame.src, linkinfo[state->link].nicaddr, sizeof(CnetNICaddr)); // Set the length of the payload. memcpy(frame.type, &length, sizeof(length)); // Copy the payload into the frame. memcpy(frame.data, data, length); // Calculate the number of bytes to send. size_t frame_length = length + ETH_HEADER_LENGTH; if (frame_length < ETH_MINFRAME) frame_length = ETH_MINFRAME; CHECK(CNET_write_physical(state->link, &frame, &frame_length)); }
void forward_frames(int link){ size_t len = sizeof(FRAME); FRAME *f = queue_remove(links[link].forwarding_queue, &len); CHECK(CNET_write_physical(link, (char*)f, &len)); CnetTime timeout = (len*((CnetTime)8000000)/linkinfo[link].bandwidth + linkinfo[link].propagationdelay); start_timer(link, timeout); free(f); }
/**************************************************************************** Write length bytes of the packet to the data link layer on a link. ****************************************************************************/ int down_to_datalink(int link, char *packet, int length) { if (!linkinfo[link].linkup) { return 0; } #ifndef LOSSIE CHECK(CNET_write_physical_reliable(link, (char *)packet, &length)); #else CHECK(CNET_write_physical(link, (char *)packet, &length)); #endif return(0); }
static void send_unreliable(CnetEvent ev, CnetTimer timer, CnetData data) { char frame[256]; int length; sprintf(frame, "message %d is %s", ++count, fruit[random()%NFRUITS]); length = strlen(frame) + 1; printf("sending %d bytes, checksum=%6d\n\n", length, checksum_internet((unsigned short *)frame,length)); CHECK(CNET_write_physical(1, frame, &length)); }
static void ack_send(int sequence, int type) { ACK_FRAME f; f.check = 0; if(type == SREJ) f.sequence = -sequence; else f.sequence = sequence; size_t size = sizeof(ACK_FRAME); f.check = CNET_ccitt((unsigned char *) &f, size); printf("ACK transmitted\n"); CHECK(CNET_write_physical(1, (char *) &f, &size)); }
int ethernet_send(CnetNICaddr to, unsigned short type, char *payload, size_t length) { EthernetHeader header; memcpy(header.destination, to, LEN_NICADDR); memcpy(header.source, linkinfo[1].nicaddr, LEN_NICADDR); header.type = type; size_t len = length + ETHERNET_HEADER_SIZE; len = len < MINIMUM_ETHERNET_PACKET_SIZE ? MINIMUM_ETHERNET_PACKET_SIZE : len; char packet[len]; memcpy(packet, (char *) &header, ETHERNET_HEADER_SIZE); memcpy(packet + ETHERNET_HEADER_SIZE, payload, length); return CNET_write_physical(1, (char *) &packet, &len); }
static EVENT_HANDLER(application_ready) { CnetAddr destaddr; char buffer[MAX_MESSAGE_SIZE]; size_t length; /* Firstly, indicate the maximum sized message we are willing to receive */ length = sizeof(buffer); /* Accept the message from the Application Layer. We will be informed of the message's destination address and length and buffer will be filled in. */ CHECK(CNET_read_application(&destaddr, buffer, &length)); printf("\tI have a message of %4d bytes for address %d\n", length, (int)destaddr); CHECK(CNET_write_physical(1, buffer, &length)); }
/* * Reply arp request */ void arp_request(ARP_PACKET *arp_packet) { assert(arp_packet != NULL); int link = LAN_LINK; ETHER_PACKET packet; uint16_t code = 0; size_t len = 0; code = htons(ARP_CODE); memcpy(packet.dest, arp_packet->dest_mac, sizeof(CnetNICaddr)); memcpy(packet.sour, arp_packet->sour_mac, sizeof(CnetNICaddr)); memcpy(packet.type, &code, sizeof(packet.type)); memcpy(packet.payload, arp_packet, sizeof(ARP_PACKET)); len = sizeof(packet); CHECK(CNET_write_physical(link, &packet, &len)); }
static void frame_send(MESSAGE *message, int length, int sequence) { if(length > DATA_SIZE_MAX) length = DATA_SIZE_MAX; FRAME_DATA f; f.sequence = sequence; f.length = length; f.checksum = 0; memcpy(&f.message, message, length); f.checksum = CNET_ccitt((unsigned char *) &f, CHECK_BYTES); printf("DATA transmitted, frame sequence = %d\n", sequence); t_window[sequence] = f; size_t size = DATA_FRAME_SIZE(f); CHECK(CNET_write_physical(1, (char *) &f, &size)); }
static int down_to_datalink(int link, char *packet, int length) { /* changed this to unreliable Suimt */ CHECK(CNET_write_physical(link, (char *)packet, &length)); return(0); }