Author: tjansen Date: Thu Nov 12 13:08:21 2009 New Revision: 1653 Log: Merged checksum.h into nat.c checksum.h was only used by nat.c. Deleted: trunk/libpisa/checksum.h Modified: trunk/libpisa/nat.c Modified: trunk/libpisa/nat.c ============================================================================== --- trunk/libpisa/nat.c Thu Nov 12 12:59:29 2009 (r1652) +++ trunk/libpisa/nat.c Thu Nov 12 13:08:21 2009 (r1653) @@ -16,10 +16,47 @@ #include <netinet/udp.h> #include <netinet/tcp.h> -#include "checksum.h" #include "nat.h" /** + * Fold a 32-bit checksum into a 16-bit checksum. + * @param checksum checksum that will be folded + * @return folded checksum (16 bit) + */ +static inline uint16_t pisa_csum_fold(uint32_t checksum) +{ + while (checksum >> 16) + checksum = (checksum & 0xffff) + (checksum >> 16); + return checksum; +} + +/** + * Unfold a 16-bit checksum into a 32-bit checksum. + * @param checksum checksum that will be unfolded + * @return unfolded checksum (32 bit) + */ +static inline uint32_t pisa_csum_unfold(uint16_t checksum) +{ + return (uint32_t) checksum; +} + +/** + * Replace 4 bytes in a checksum. + * @param addr pointer to the current checksum. Will be updated to the new + * checksum when the functions returns. + * @param before 4 bytes that were used to calculate the old checksum and + * should be replaced. + * @param after 4 bytes that will become part of the new checksum. + */ +static inline void pisa_csum_replace4(uint16_t *checksum, uint32_t before, uint32_t after) +{ + uint32_t checksum_new = pisa_csum_unfold(~*checksum); + checksum_new += ((~before) & 0xffff) + ((~before) >> 16); + checksum_new += (after & 0xffff) + (after >> 16); + *checksum = ~pisa_csum_fold(checksum_new); +} + +/** * Apply NAT mapping to a given IPv4 packet. * @param ip pointer to the IPv4 header of the packet * @param before pointer to the old IPv4 address in the packet. Can be