Friday 15 July 2011

c - OSPF - Checksum not working -



c - OSPF - Checksum not working -

i'm working on project on need generate ospf packet manually. having problems getting ospf checksum right. read have maintain auth info out of calculation, , though i'm doing can't work. know function beingness used generate checksum right because utilize same 1 generate checksum ip header, , works.

*i'm sorry bad c programming, it's not main language.

void generatehello(unsigned char* packet_return,unsigned char* buff,unsigned short *ospf_packet){ ospf_packet = (unsigned short*) malloc(14*sizeof(unsigned short)); //ospf version packet_return[34] = 0x02; //message type - hello packet_return[35] = 0x01; //packet length packet_return[36] = 0x00; packet_return[37] = 0x2c; //source ospf router (ip) packet_return[38]=local_ip[0]; packet_return[39]=local_ip[1]; packet_return[40]=local_ip[2]; packet_return[41]=local_ip[3]; //area packet_return[42]=0x00; packet_return[43]=0x00; packet_return[44]=0x00; packet_return[45]=0x01; //add checksum packet_return[46]=0x00; packet_return[47]=0x00; //auth type packet_return[48]=0x00; packet_return[49]=0x00; //auth info packet_return[50]=0x00; packet_return[51]=0x00; packet_return[52]=0x00; packet_return[53]=0x00; packet_return[54]=0x00; packet_return[55]=0x00; packet_return[56]=0x00; packet_return[57]=0x00; //network mask packet_return[58]=0xff; packet_return[59]=0xff; packet_return[60]=0xff; packet_return[61]=0x00; //hello interval packet_return[62]=0x00; packet_return[63]=0x0a; //multi-topology routing packet_return[64]=0x12; //router priority packet_return[65]=0x01; //router dead interval packet_return[66]=0x00; packet_return[67]=0x00; packet_return[68]=0x00; packet_return[69]=0x28; //designated router packet_return[70]=0x00; packet_return[71]=0x00; packet_return[72]=0x00; packet_return[73]=0x00; //backup designated router packet_return[74]=0x00; packet_return[75]=0x00; packet_return[76]=0x00; packet_return[77]=0x00; //checksum packet_return[78]=0x00; packet_return[79]=0x00; //lls info length packet_return[80]=0x00; packet_return[81]=0x03; //type packet_return[82]=0x00; packet_return[83]=0x01; //length packet_return[84]=0x00; packet_return[85]=0x04; //options - lsdb resynchronization packet_return[86]=0x00; packet_return[87]=0x00; packet_return[88]=0x00; packet_return[89]=0x01; int i; int j; for(i=0,j=34;i<48;i++,j+=2) { ospf_packet[i]= htons(((packet_return[j] << 8) | packet_return[j+1])); } unsigned short ck_sum = in_cksum(ospf_packet,sizeof(unsigned short)*14); printf("checksum ospf - %.4x \n", ck_sum); packet_return[46]=ck_sum & 0xff; packet_return[47]=(ck_sum >> 8) & 0xff; }

firstly, please utilize constants or improve struct __attribute__((packed)) rather lots of array offsets.

secondly, looks fishy:

for(i=0,j=34;i<48;i++,j+=2) { ospf_packet[i]= htons(((packet_return[j] << 8) | packet_return[j+1])); }

ospf_packet 14 unsigned shorts long, per malloc. yet writing 48 unsigned shorts it. cause undefined behaviour.

also, packet_return appears char * presumably in wire order. reading out assuming shorts in wire order (fine far goes suppose), converting wire order host order (it seems) - think should ntohs not htons (yes, know same thing). not evident why doing @ all.

lastly, ospf checksum calculated on entire ospf packet except authentication field.

from rfc2328

checksum standard ip 16-bit one's complement checksum of entire ospf packet, excluding 64-bit authentication field. checksum calculated part of appropriate authentication procedure; ospf authentication types, checksum calculation omitted. see section d.4 details.

i can't see why code sums across entire packet nor how omits authentication field.

c networking network-programming checksum ospf

No comments:

Post a Comment