Forum Discussion
Altera_Forum
Honored Contributor
12 years agoHi,
The ethernet transmit frame after changing the original code to ether type IP is: unsigned char tx_frame[1024] = { 0x00,0x00, // for 32-bit alignment 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // destination address (broadcast) 0x01,0x60,0x6E,0x11,0x02,0x0F, // source address 0x08,0x00, // length or type of the payload data '\0' // payload data (ended with termination character) }; According to some internet source: // The packet length# define PCKT_LEN 1024 // The IP header's structure struct ipheader { unsigned char iph_ihl:5, iph_ver:4; unsigned char iph_tos; unsigned short int iph_len; unsigned short int iph_ident; unsigned char iph_flag; unsigned short int iph_offset; unsigned char iph_ttl; unsigned char iph_protocol; unsigned short int iph_chksum; unsigned int iph_sourceip; unsigned int iph_destip; }; // UDP header's structure struct udpheader { unsigned short int udph_srcport; unsigned short int udph_destport; unsigned short int udph_len; unsigned short int udph_chksum; }; // The checksum unsigned short csum(unsigned short *buf, int nwords) { // unsigned long sum; for(sum=0; nwords>0; nwords--) sum += *buf++; sum = (sum >> 16) + (sum &0xffff); sum += (sum >> 16); return (unsigned short)(~sum); } char buffer[PCKT_LEN]; // Our own headers' structures struct ipheader *ip; struct udpheader *udp; // Fabricate the IP header or we can use the // standard header structures but assign our own values. ip->iph_ihl = 5; ip->iph_ver = 4; ip->iph_tos = 16; // Low delay ip->iph_len = sizeof(struct ipheader) + sizeof(struct udpheader); //ip->iph_ident = htons(54321); ip->iph_ident = 54321; ip->iph_ttl = 64; // hops ip->iph_protocol = 17; // UDP // Source IP address, can use spoofed address here!!! ip->iph_sourceip = 0xa074; // The destination IP address ip->iph_destip = 0xa073; // Fabricate the UDP header. Source port number, redundant udp->udph_srcport = 0x80; // Destination port number udp->udph_destport = 0x70; udp->udph_len = sizeof(struct udpheader); // Calculate the checksum for integrity ip->iph_chksum = csum((unsigned short *)buffer, sizeof(struct ipheader) + sizeof(struct udpheader)); memcpy(buffer,&ip, sizeof(ip)); memcpy((buffer+ sizeof(ip)),&udp, sizeof(udp)); Now I am wondering how I can add this IP and UDP header to the raw data. I mean there is no any socket number or something like that. So where I have to map the variable buffer now? Is it to change some parameters here(below) in original code? If yes, how? If not what is alternative? // Create transmit sgdma descriptor alt_avalon_sgdma_construct_mem_to_stream_desc( &tx_descriptor, &tx_descriptor_end, tx_frame, 1480, 0, 1, 1, 0 ); // Set up non-blocking transfer of sgdma transmit descriptor alt_avalon_sgdma_do_async_transfer( sgdma_tx_dev, &tx_descriptor ); // Wait until transmit descriptor transfer is complete while (alt_avalon_sgdma_check_descriptor_status(&tx_descriptor) != 0) ; } return 0; }