/* Calculate a checksum over a chain of pbufs (without pseudo-header, much like inet_chksum only pbufs are used). */
u16_t inet_chksum_pbuf(struct pbuf *p)
/* Calculate a checksum over a chain of pbufs (without pseudo-header, much like inet_chksum only pbufs are used). */
u16_t inet_chksum_pbuf(struct pbuf *p)
{
  u32_t acc;
  struct pbuf *q;
  u8_t swapped;
  acc = 0;
  swapped = 0;
  for(q = p; q != NULL; q = q->next) {
    acc += LWIP_CHKSUM(q->payload, q->len);
    while ((acc >> 16) != 0) {
      acc = (acc & 0xffffUL) + (acc >> 16);
    }
    if (q->len % 2 != 0) {
      swapped = 1 - swapped;
      acc = (acc & 0x00ffUL << 8) | (acc & 0xff00UL >> 8);
    }
  }
  if (swapped) {
    acc = ((acc & 0x00ffUL) << 8) | ((acc & 0xff00UL) >> 8);
  }
  return (u16_t)~(acc & 0xffffUL);
}