// ------------------------------------------------------------------------- // ethernet.c // // 21.12.2003, J. Ahrensfeld // // ------------------------------------------------------------------------- #include "libsys.h" #include "ethernet.h" #include "ipv4.h" #include "string.h" #define IPV4_HEADER_SIZE 20 // ------------------------------------------------------------------------- // Testing - not the final API uint8_t *IPV4_create(packet_t *pPacket, uint16_t proto, uint16_t len, uint8_t *inet_addr_src, uint8_t *inet_addr_dst) { IPV4 *pIPv4_hdr; pIPv4_hdr = (IPV4*)Eth_pkt_buffer_get(pPacket); memset(pIPv4_hdr, 0, sizeof(IPV4)); memcpy(pIPv4_hdr->inet_addr_src, inet_addr_src, 4); memcpy(pIPv4_hdr->inet_addr_dst, inet_addr_dst, 4); pIPv4_hdr->ver_ihl = 0x45; pIPv4_hdr->tos = 0x00; pIPv4_hdr->len = sizeof(IPV4) + len; pIPv4_hdr->id = 0x1234; pIPv4_hdr->flag_foff = 0; //IPV4_FLAG_DF; pIPv4_hdr->ttl = 0x10; pIPv4_hdr->proto = proto; pIPv4_hdr->hdr_chksum = 0x0000; pIPv4_hdr->hdr_chksum = ~IPv4ChkSum((uint16_t*)pIPv4_hdr, 0, sizeof(IPV4)); // Send it return Eth_pkt_append(pPacket, sizeof(IPV4)); } // ------------------------------------------------------------------------- uint16_t IPv4ChkSum(uint16_t *pBuf, uint16_t acc, uint16_t len) { while(len > 1) { acc += *pBuf; if(acc < *pBuf) acc++; ++pBuf; len -= 2; } /* add up any odd byte */ if(len == 1) { acc += htons(((uint16_t)(*(uint8_t *)pBuf)) << 8); if(acc < htons(((uint16_t)(*(uint8_t *)pBuf)) << 8)) acc++; } return acc; }