void icmpEchoRequest(icmpip_hdr* packet) { uint32_t tempIp; // change type to reply packet->icmp.type = ICMP_TYPE_ECHOREPLY; // recalculate checksum packet->icmp.icmpchksum = 0; packet->icmp.icmpchksum = netChecksum((uint8_t*)&packet->icmp, htons(packet->ip.len)-IP_HEADER_LEN); // return to sender tempIp = packet->ip.destipaddr; packet->ip.destipaddr = packet->ip.srcipaddr; packet->ip.srcipaddr = tempIp; // add ethernet routing arpIpOut((struct netEthIpHeader*)(((uint8_t*)packet)-ETH_HEADER_LEN), 0); // debugging #if NET_DEBUG >= 2 icmpPrintHeader(packet); //debugPrintHexTable(htons(packet->ip.len), (u08*)packet); #endif // send it (packet->ip.len+ETH_HEADER_LEN nicSend(htons(packet->ip.len)+ETH_HEADER_LEN, (((uint8_t*)packet)-ETH_HEADER_LEN)); }
void ipSend(uint32_t dstIp, uint8_t protocol, uint16_t len, uint8_t* data) { // make pointer to ethernet/IP header struct netEthIpHeader* ethIpHeader; // move data pointer to make room for headers data -= ETH_HEADER_LEN+IP_HEADER_LEN; ethIpHeader = (struct netEthIpHeader*)data; // debugPrintHexTable(len+ETH_HEADER_LEN+IP_HEADER_LEN, data); // adjust length to add IP header len += IP_HEADER_LEN; // fill IP header ethIpHeader->ip.destipaddr = HTONL(dstIp); ethIpHeader->ip.srcipaddr = HTONL(IpMyConfig.ip); ethIpHeader->ip.proto = protocol; ethIpHeader->ip.len = htons(len); ethIpHeader->ip.vhl = 0x45; ethIpHeader->ip.tos = 0; ethIpHeader->ip.ipid = 0; ethIpHeader->ip.ipoffset = 0; ethIpHeader->ip.ttl = IP_TIME_TO_LIVE; ethIpHeader->ip.ipchksum = 0; // calculate and apply IP checksum // DO THIS ONLY AFTER ALL CHANGES HAVE BEEN MADE TO IP HEADER ethIpHeader->ip.ipchksum = netChecksum(ðIpHeader->ip, IP_HEADER_LEN); // add ethernet routing // check if we need to send to gateway if( (dstIp & IpMyConfig.netmask) == (IpMyConfig.ip & IpMyConfig.netmask) ) { arpIpOut(ethIpHeader,0); // local send //rprintf("Sending IP packet on local net\r\n"); } else { arpIpOut(ethIpHeader,IpMyConfig.gateway); // gateway send //rprintf("Sending IP packet to gateway\r\n"); } // adjust length to add ethernet header len += ETH_HEADER_LEN; // debug //debugPrintHexTable(ETH_HEADER_LEN, &data[0]); //debugPrintHexTable(len-ETH_HEADER_LEN, &data[ETH_HEADER_LEN]); // send it nicSend(len, data); }