packet: move some common code out of wrapper.c and util.c
This commit is contained in:
@@ -52,43 +52,6 @@ int create_tun( const char* name )
|
||||
return fd;
|
||||
}
|
||||
|
||||
|
||||
// shamelessly copied from:
|
||||
// http://www.roman10.net/how-to-calculate-iptcpudp-checksumpart-2-implementation/
|
||||
unsigned short compute_checksum(unsigned short *addr, unsigned int count) {
|
||||
|
||||
unsigned long sum = 0;
|
||||
|
||||
while (count > 1) {
|
||||
sum += * addr++;
|
||||
count -= 2;
|
||||
}
|
||||
|
||||
//if any bytes left, pad the bytes and add
|
||||
if(count > 0) {
|
||||
sum += ((*addr)&htons(0xFF00));
|
||||
}
|
||||
|
||||
//Fold sum to 16 bits: add carrier to result
|
||||
while (sum>>16) {
|
||||
sum = (sum & 0xffff) + (sum >> 16);
|
||||
}
|
||||
|
||||
//one's complement
|
||||
sum = ~sum;
|
||||
|
||||
return ((unsigned short)sum);
|
||||
|
||||
}
|
||||
|
||||
void compute_ip_checksum(struct iphdr* pkt)
|
||||
{
|
||||
pkt->check = 0x0000;
|
||||
pkt->check = compute_checksum( (unsigned short*) pkt, pkt->ihl * 4 );
|
||||
}
|
||||
|
||||
|
||||
|
||||
int link_set_up( char *link_name, int state )
|
||||
{
|
||||
int fd = socket( PF_INET, SOCK_DGRAM, IPPROTO_IP );
|
||||
|
Reference in New Issue
Block a user