I am using following api to initialize sockfd at client side:(sockfd=3)
if ((sockfd = socket(p->ai_family, p->ai_socktype,p->ai_protocol)) == -1) {
perror("client: socket");
continue;
}
& initializing my TPKT_Buff to {3,0,0,0} value by using function :
if(Fill_TPKT(PStack,TPKT_Buff) != 0)
{
printf("Error while filling TPKT Buffer");
return 1;
}printf("tpkt/2_Buff%x %x\n",TPKT_Buff[0],TPKT_Buff[1]);printf("sockfd=%d\n",sockfd);
But, after calling function :
if(Fill_COTP(PStack,&cotp) != 0)
{
printf("Error while filling COTP Structure!");
return 1;
}
my socfd & TPKT_Buff values changed to zero TPKT_Buff={0,0,0,0} & sockfd=0 :
printf("sockfd=%d\n",sockfd);
printf("TPKT/2_Buff=%x %x\n",TPKT_Buff[0],TPKT_Buff[1]);
Definitions of functions Fill_COTP & Fill_TPKT are as follows :
int Fill_TPKT(FILE *fptr,unsigned char *buf)
{
fseek(fptr,14,SEEK_SET);
fscanf(fptr,"%d",buf+0);
fseek(fptr,15,SEEK_CUR);
fscanf(fptr,"%d",buf+1);
return 0;
}
int Fill_COTP(FILE *fptr, COTP *cotp)
{
unsigned short temp;
fseek(fptr,13,SEEK_CUR);
fscanf(fptr,"%d",&temp);
cotp->Destination_Ref[1] = temp;
cotp->Destination_Ref[0] = temp>>8;
printf("%x %x\n",cotp->Destination_Ref[0],cotp->Destination_Ref[1]);
fseek(fptr,13,SEEK_CUR);
fscanf(fptr,"%d",&temp);
cotp->Source_Ref[1] = temp;
cotp->Source_Ref[0] = temp>>8;
printf("%x %x\n",cotp->Source_Ref[0],cotp->Source_Ref[1]);
fseek(fptr,14,SEEK_CUR);
fscanf(fptr,"%d",&temp);
cotp->Source_Tsap[1] = temp;
cotp->Source_Tsap[0] = temp>>8;
printf("%x %x\n",cotp->Source_Tsap[0],cotp->Source_Tsap[1]);
fseek(fptr,14,SEEK_CUR);
fscanf(fptr,"%d",&temp);
cotp->Destination_Tsap[1] = temp;
cotp->Destination_Tsap[0] = temp>>8;
printf("%x %x\n",cotp->Destination_Tsap[0],cotp->Destination_Tsap[1]);
fseek(fptr,17,SEEK_CUR);
fscanf(fptr,"%d",&(cotp->TPDU_size));
printf("%x\n",cotp->TPDU_size);
return 0;
}
Here PStack is a file pointer.
I am not getting why my sockfd & TPKT_Buff values changing to zero even I am not using these values in my function Fill_COTP();
Please give some suggestion.
Definition of COTP is:
typedef struct
{
unsigned char PDU_type;
unsigned char Destination_Ref[2];
unsigned char Source_Ref[2];
unsigned char Source_Tsap[2];
unsigned char Destination_Tsap[2];
unsigned char TPDU_size;
} COTP;
There is no relation between sockfd & TPKT_Buff.
The trouble appears to be in the line:
Your TPCU_size is
unsigned char TPDU_size;which is only 1 byte (assuming this to be the size of ‘char’) in size, but you are trying put 4 bytes (assuming that to be the size of ‘int’) into it during fscanf, thereby potentially overwriting the memory around it.