|
|
@ -30,29 +30,72 @@ uint32_t htonl(uint32_t val)
|
|
|
|
return (htons(val>>16) | (uint32_t)htons(val&0x0000FFFF)<<16);
|
|
|
|
return (htons(val>>16) | (uint32_t)htons(val&0x0000FFFF)<<16);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static uint16_t generateChecksum(uint16_t sum, netIpHeader *data, uint16_t len)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
uint16_t t;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint32_t *dataptr;
|
|
|
|
|
|
|
|
uint32_t *last_byte;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
dataptr = (uint32_t *) data;
|
|
|
|
|
|
|
|
last_byte = (uint32_t *) data + len - 1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
while(dataptr < last_byte) { // At least two more bytes
|
|
|
|
|
|
|
|
t = (dataptr[0] << 8) + dataptr[1];
|
|
|
|
|
|
|
|
sum += t;
|
|
|
|
|
|
|
|
if(sum < t) {
|
|
|
|
|
|
|
|
sum++; // carry
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
dataptr += 2;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if(dataptr == last_byte) {
|
|
|
|
|
|
|
|
t = (dataptr[0] << 8) + 0;
|
|
|
|
|
|
|
|
sum += t;
|
|
|
|
|
|
|
|
if(sum < t) {
|
|
|
|
|
|
|
|
sum++; // carry
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Return sum in host byte order.
|
|
|
|
|
|
|
|
return sum;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t ipChecksum(netIpHeader *data, uint16_t len)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
return htons(generateChecksum(0, (netIpHeader *)data, len));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t netChecksum(netIpHeader *data, uint16_t len)
|
|
|
|
uint16_t netChecksum(netIpHeader *data, uint16_t len)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
register uint32_t sum = 0;
|
|
|
|
uint16_t sum;
|
|
|
|
|
|
|
|
sum = generateChecksum(0, &data[len], len);
|
|
|
|
|
|
|
|
SPrintln_P(PSTR("uip_ipchksum: sum 0x"));
|
|
|
|
|
|
|
|
Serial.println((long int)sum, HEX);
|
|
|
|
|
|
|
|
return (sum == 0) ? 0xffff : htons(sum);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* register uint32_t sum = 0;
|
|
|
|
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
for (;;) {
|
|
|
|
if (len < 2)
|
|
|
|
if (len < 2)
|
|
|
|
break;
|
|
|
|
break;
|
|
|
|
//sum += *((uint16_t *)data)++;
|
|
|
|
//sum += *((uint16_t *)data)++;
|
|
|
|
sum += *((uint16_t *)data);
|
|
|
|
sum += *((uint16_t *)data);
|
|
|
|
data+=2;
|
|
|
|
data+=2;
|
|
|
|
len -= 2;
|
|
|
|
len -= 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (len)
|
|
|
|
|
|
|
|
sum += *(uint8_t *) data;
|
|
|
|
if (len)
|
|
|
|
|
|
|
|
sum += *(uint8_t *) data;
|
|
|
|
while ((len = (uint16_t) (sum >> 16)) != 0)
|
|
|
|
|
|
|
|
sum = (uint16_t) sum + len;
|
|
|
|
while ((len = (uint16_t) (sum >> 16)) != 0)
|
|
|
|
|
|
|
|
sum = (uint16_t) sum + len;
|
|
|
|
|
|
|
|
#if NET_DEBUG > 6
|
|
|
|
Serial.print("Checksum: ");
|
|
|
|
Serial.print("Checksum: ");
|
|
|
|
Serial.println((uint16_t) sum ^ 0xFFFF, HEX);
|
|
|
|
Serial.println((uint16_t) ~sum, HEX);
|
|
|
|
|
|
|
|
#endif
|
|
|
|
return (uint16_t) sum ^ 0xFFFF;
|
|
|
|
return (uint16_t) ~sum;
|
|
|
|
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void netPrintEthAddr(struct netEthAddr* ethaddr)
|
|
|
|
void netPrintEthAddr(struct netEthAddr* ethaddr)
|
|
|
@ -97,7 +140,7 @@ void netPrintEthHeader(struct netEthHeader* eth_hdr)
|
|
|
|
void netPrintIpHeader(struct netIpHeader* ipheader)
|
|
|
|
void netPrintIpHeader(struct netIpHeader* ipheader)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
SPrint_P(PSTR("IP Header\r\n"));
|
|
|
|
SPrint_P(PSTR("IP Header\r\n"));
|
|
|
|
SPrint_P(PSTR("Ver : %d\r\n", (ipheader->vhl)>>4);
|
|
|
|
SPrint_P(PSTR("Ver : %d\r\n", (ipheader->vhl)>>4);
|
|
|
|
SPrint_P(PSTR("Length : %d\r\n", htons(ipheader->len));
|
|
|
|
SPrint_P(PSTR("Length : %d\r\n", htons(ipheader->len));
|
|
|
|
if(ipheader->proto == IP_PROTO_ICMP)
|
|
|
|
if(ipheader->proto == IP_PROTO_ICMP)
|
|
|
|
SPrint_P(PSTR("Protocol: ICMP\r\n"));
|
|
|
|
SPrint_P(PSTR("Protocol: ICMP\r\n"));
|
|
|
@ -107,9 +150,9 @@ void netPrintIpHeader(struct netIpHeader* ipheader)
|
|
|
|
SPrint_P(PSTR("Protocol: UDP\r\n"));
|
|
|
|
SPrint_P(PSTR("Protocol: UDP\r\n"));
|
|
|
|
else
|
|
|
|
else
|
|
|
|
SPrint_P(PSTR("Protocol: %d\r\n", ipheader->proto);
|
|
|
|
SPrint_P(PSTR("Protocol: %d\r\n", ipheader->proto);
|
|
|
|
|
|
|
|
|
|
|
|
SPrint_P(PSTR("SourceIP: ")); netPrintIPAddr(htonl(ipheader->srcipaddr)); Serial.println();
|
|
|
|
SPrint_P(PSTR("SourceIP: ")); netPrintIPAddr(htonl(ipheader->srcipaddr)); Serial.println();
|
|
|
|
SPrint_P(PSTR("Dest IP: ")); netPrintIPAddr(htonl(ipheader->destipaddr)); Serial.println();
|
|
|
|
SPrint_P(PSTR("Dest IP: ")); netPrintIPAddr(htonl(ipheader->destipaddr)); Serial.println();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void netPrintTcpHeader(struct netTcpHeader* tcpheader)
|
|
|
|
void netPrintTcpHeader(struct netTcpHeader* tcpheader)
|
|
|
|