Author: heer Date: Sat Nov 7 13:13:53 2009 New Revision: 1559 Log: OS X Compatibility I This is a first attempt to improve PISA's portability. Specific changes: * Added definitions for BYTE_ORDER and BIG_ENDIAN * Use RFC 791 compliant ip header (available on Linux and OS X) * Use RFC 793 compliant tcp header (available on Linux and OS X) * Use RFC 768 compliant udp header (available on Linux and OS X) Modified: trunk/libpisa/global.h trunk/libpisa/nat.c trunk/libpisa/nat.h trunk/pisacd/cdtun.c trunk/pisasd/sdtun.c Modified: trunk/libpisa/global.h ============================================================================== --- trunk/libpisa/global.h Fri Nov 6 18:06:48 2009 (r1558) +++ trunk/libpisa/global.h Sat Nov 7 13:13:53 2009 (r1559) @@ -114,6 +114,19 @@ #define FALSE 0 #endif +/** + * __BYTE_ORDER is not available on _APPLE_ + */ +#ifdef __APPLE__ + #ifndef __BYTE_ORDER + #define __BYTE_ORDER BYTE_ORDER + #endif + #ifndef __BIG_ENDIAN + #define __BIG_ENDIAN BIG_ENDIAN + #endif +#endif + + #if __BYTE_ORDER == __BIG_ENDIAN #define hton64(i) (i) #define ntoh64(i) (i) Modified: trunk/libpisa/nat.c ============================================================================== --- trunk/libpisa/nat.c Fri Nov 6 18:06:48 2009 (r1558) +++ trunk/libpisa/nat.c Sat Nov 7 13:13:53 2009 (r1559) @@ -11,6 +11,9 @@ */ #include <netinet/ip.h> + +/* Use BSD style packet headers for tcp and udp */ +#define __FAVOR_BSD #include <netinet/udp.h> #include <netinet/tcp.h> @@ -25,16 +28,16 @@ * @param after pointer to the new IPv4 address. Points to memory outside * the packet buffer. */ -void pisa_nat_apply(struct iphdr *ip, struct in_addr *before, struct in_addr *after) +void pisa_nat_apply(struct ip *ip, struct in_addr *before, struct in_addr *after) { /* Only the first fragment can contain the TCP or UDP header */ - if ((ntohs(ip->frag_off) & IP_OFFMASK) == 0) { + if ((ntohs(ip->ip_off) & IP_OFFMASK) == 0) { pisa_csum16 *csum = NULL; - if (ip->protocol == IPPROTO_TCP) - csum = &(((struct tcphdr *)(ip+1))->check); - else if (ip->protocol == IPPROTO_UDP) - csum = &(((struct udphdr *)(ip+1))->check); + if (ip->ip_p == IPPROTO_TCP) + csum = &(((struct tcphdr *)(ip+1))->th_sum); + else if (ip->ip_p == IPPROTO_UDP) + csum = &(((struct udphdr *)(ip+1))->uh_sum); /* If we have a protocol that requires a checksum update, * csum points to the checksum in the packet. Instead of @@ -46,7 +49,7 @@ } /* The IP header has to be updated in every case. */ - pisa_csum_replace4(&ip->check, before->s_addr, after->s_addr); + pisa_csum_replace4(&ip->ip_sum, before->s_addr, after->s_addr); /* Overwrite the IP address in the packet only after we don't need the * old value anymore. */ @@ -167,7 +170,7 @@ * @param packet tun-encapsulated packet * @return the IPv4 header or NULL if it's not an IPv4 packet */ -struct iphdr *pisa_nat_get_iphdr(char *packet) +struct ip *pisa_nat_get_iphdr(char *packet) { /* We skip 6 bytes reserved for the MAC. Then we check the next 4 * bytes if they are a valid tun-device header. @@ -179,7 +182,7 @@ packet += 6; if (packet[0] == 0 && packet[1] == 0 && packet[2] == 8 && packet[3] == 0) - return (struct iphdr *)(packet + 4); + return (struct ip *)(packet + 4); return NULL; } Modified: trunk/libpisa/nat.h ============================================================================== --- trunk/libpisa/nat.h Fri Nov 6 18:06:48 2009 (r1558) +++ trunk/libpisa/nat.h Sat Nov 7 13:13:53 2009 (r1559) @@ -49,7 +49,7 @@ pisa_nat_mapping *pisa_nat_mapping_find_by_mac(pisa_nat_list *natlist, pisa_mac *mac); void pisa_nat_update_connection(pisa_nat_list *natlist, pisa_nat_mapping *map, pisa_conmgr_entry *entry); -struct iphdr *pisa_nat_get_iphdr(char *packet); -void pisa_nat_apply(struct iphdr *ip, struct in_addr *old_addr, struct in_addr *new_addr); +struct ip *pisa_nat_get_iphdr(char *packet); +void pisa_nat_apply(struct ip *ip, struct in_addr *old_addr, struct in_addr *new_addr); #endif /* PISA_NAT_H */ Modified: trunk/pisacd/cdtun.c ============================================================================== --- trunk/pisacd/cdtun.c Fri Nov 6 18:06:48 2009 (r1558) +++ trunk/pisacd/cdtun.c Sat Nov 7 13:13:53 2009 (r1559) @@ -36,7 +36,7 @@ unsigned int addrlen; struct sockaddr_in6 from; pisa_nat_mapping *map; - struct iphdr *hdr; + struct ip *hdr; struct in_addr *srcaddr; pisa_conmgr_entry *entry; @@ -65,7 +65,7 @@ return; /* Apply NAT if needed */ - srcaddr = (struct in_addr *)&hdr->saddr; + srcaddr = (struct in_addr *)&hdr->ip_src; map = pisa_nat_mapping_find_by_remote(cd_ctx.natlist, &from.sin6_addr, srcaddr); if (map != NULL) pisa_nat_apply(hdr, srcaddr, &map->local_private); @@ -100,7 +100,7 @@ { char buffer[MAX_PACKET_BUFFER_TUN]; ssize_t len; - struct iphdr *hdr; + struct ip *hdr; struct in_addr *dst; pisa_conmgr_entry *entry; pisa_nat_mapping *map; @@ -117,10 +117,10 @@ return; } - pisa_arp_from_ipv4((struct in_addr *)&hdr->saddr, mac); + pisa_arp_from_ipv4((struct in_addr *)&hdr->ip_src, mac); /* Apply NAT if needed */ - dst = (struct in_addr *)&hdr->daddr; + dst = (struct in_addr *)&hdr->ip_dst; map = pisa_nat_mapping_find_by_local_private(cd_ctx.natlist, dst); if (map != NULL) { /* found a matching NAT entry, change the destination IPv4 */ Modified: trunk/pisasd/sdtun.c ============================================================================== --- trunk/pisasd/sdtun.c Fri Nov 6 18:06:48 2009 (r1558) +++ trunk/pisasd/sdtun.c Sat Nov 7 13:13:53 2009 (r1559) @@ -45,7 +45,7 @@ pisa_conmgr_entry *entry; pisa_nat_mapping *map; pisa_mac *mac = (pisa_mac *)buffer; - struct iphdr *hdr; + struct ip *hdr; struct in_addr *srcaddr; addrlen = sizeof(from); @@ -70,7 +70,7 @@ /* Check for IPv4 in the payload, otherwise drop it */ if ((hdr = pisa_nat_get_iphdr(buffer)) == NULL) return; - srcaddr = (struct in_addr *)&hdr->saddr; + srcaddr = (struct in_addr *)&hdr->ip_src; /* Update the timeout for the connection */ entry->heartbeat_flag = 1; @@ -134,7 +134,7 @@ { char buffer[MAX_PACKET_BUFFER_TUN]; ssize_t len; - struct iphdr *hdr; + struct ip *hdr; struct in_addr *dst; pisa_conmgr_entry *entry; pisa_nat_mapping *map; @@ -154,7 +154,7 @@ } /* Apply NAT if needed */ - dst = (struct in_addr *)&hdr->daddr; + dst = (struct in_addr *)&hdr->ip_dst; map = pisa_nat_mapping_find_by_local_private(sd_ctx.natlist, dst); if (map != NULL) { /* found a matching NAT entry, change the destination IPv4 */