var/www/smartcan/bin/domocan-libs.h
// Build IPv4 UDP pseudo-header and call checksum function.
Build IPv4 UDP pseudo-header and call checksum function.
Build IPv4 UDP pseudo - header and call checksum function .
unsigned short int
udp4_checksum (struct ip iphdr, struct udphdr udphdr, unsigned char *payload, int payloadlen) {
  char buf[IP_MAXPACKET];
  char *ptr;
  int chksumlen = 0;
  int i;
  ptr = &buf[0];  
  memcpy (ptr, &iphdr.ip_src.s_addr, sizeof (iphdr.ip_src.s_addr));
  ptr += sizeof (iphdr.ip_src.s_addr);
  chksumlen += sizeof (iphdr.ip_src.s_addr);
  memcpy (ptr, &iphdr.ip_dst.s_addr, sizeof (iphdr.ip_dst.s_addr));
  ptr += sizeof (iphdr.ip_dst.s_addr);
  chksumlen += sizeof (iphdr.ip_dst.s_addr);
  *ptr = 0; ptr++;
  chksumlen += 1;
  memcpy (ptr, &iphdr.ip_p, sizeof (iphdr.ip_p));
  ptr += sizeof (iphdr.ip_p);
  chksumlen += sizeof (iphdr.ip_p);
  memcpy (ptr, &udphdr.len, sizeof (udphdr.len));
  ptr += sizeof (udphdr.len);
  chksumlen += sizeof (udphdr.len);
  memcpy (ptr, &udphdr.source, sizeof (udphdr.source));
  ptr += sizeof (udphdr.source);
  chksumlen += sizeof (udphdr.source);
  memcpy (ptr, &udphdr.dest, sizeof (udphdr.dest));
  ptr += sizeof (udphdr.dest);
  chksumlen += sizeof (udphdr.dest);
  memcpy (ptr, &udphdr.len, sizeof (udphdr.len));
  ptr += sizeof (udphdr.len);
  chksumlen += sizeof (udphdr.len);
  *ptr = 0; ptr++;
  *ptr = 0; ptr++;
  chksumlen += 2;
  memcpy (ptr, payload, payloadlen);
  ptr += payloadlen;
  chksumlen += payloadlen;
  for (i=0; i<payloadlen%2; i++, ptr++) {
    *ptr = 0;
    ptr++;
    chksumlen++;
  } 
  return checksum ((unsigned short int *) buf, chksumlen);
}
unsigned short int udp4_checksum ( struct ip iphdr , struct udphdr udphdr , unsigned char * payload , int payloadlen ) { char buf [ IP_MAXPACKET ] ; char * ptr ; int chksumlen = 0 ; int i ; ptr = & buf [ 0 ] ; memcpy ( ptr , & iphdr . ip_src . s_addr , sizeof ( iphdr . ip_src . s_addr ) ) ; ptr += sizeof ( iphdr . ip_src . s_addr ) ; chksumlen += sizeof ( iphdr . ip_src . s_addr ) ; memcpy ( ptr , & iphdr . ip_dst . s_addr , sizeof ( iphdr . ip_dst . s_addr ) ) ; ptr += sizeof ( iphdr . ip_dst . s_addr ) ; chksumlen += sizeof ( iphdr . ip_dst . s_addr ) ; * ptr = 0 ; ptr ++ ; chksumlen += 1 ; memcpy ( ptr , & iphdr . ip_p , sizeof ( iphdr . ip_p ) ) ; ptr += sizeof ( iphdr . ip_p ) ; chksumlen += sizeof ( iphdr . ip_p ) ; memcpy ( ptr , & udphdr . len , sizeof ( udphdr . len ) ) ; ptr += sizeof ( udphdr . len ) ; chksumlen += sizeof ( udphdr . len ) ; memcpy ( ptr , & udphdr . source , sizeof ( udphdr . source ) ) ; ptr += sizeof ( udphdr . source ) ; chksumlen += sizeof ( udphdr . source ) ; memcpy ( ptr , & udphdr . dest , sizeof ( udphdr . dest ) ) ; ptr += sizeof ( udphdr . dest ) ; chksumlen += sizeof ( udphdr . dest ) ; memcpy ( ptr , & udphdr . len , sizeof ( udphdr . len ) ) ; ptr += sizeof ( udphdr . len ) ; chksumlen += sizeof ( udphdr . len ) ; * ptr = 0 ; ptr ++ ; * ptr = 0 ; ptr ++ ; chksumlen += 2 ; memcpy ( ptr , payload , payloadlen ) ; ptr += payloadlen ; chksumlen += payloadlen ; for ( i = 0 ; i < payloadlen % 2 ; i ++ , ptr ++ ) { * ptr = 0 ; ptr ++ ; chksumlen ++ ; } return checksum ( ( unsigned short int * ) buf , chksumlen ) ; }
Build IPv4 UDP pseudo-header and call checksum function.
Build IPv4 UDP pseudo - header and call checksum function .
// ptr points to beginning of buffer buf // Copy source IP address into buf (32 bits) // Copy destination IP address into buf (32 bits) // Copy zero field to buf (8 bits) // Copy transport layer protocol to buf (8 bits) // Copy UDP length to buf (16 bits) // Copy UDP source port to buf (16 bits) // Copy UDP destination port to buf (16 bits) // Copy UDP length again to buf (16 bits) // Copy UDP checksum to buf (16 bits) // Zero, since we don't know it yet // Copy payload to buf // Pad to the next 16-bit boundary // END FOR