tod
tod

Reputation: 91

How to change the values of socketid is changed after calling function

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.

Upvotes: 0

Views: 92

Answers (2)

torek
torek

Reputation: 488519

While there is some information missing, some of what you have shown is clearly wrong and is likely to be involved in the problem. For instance:

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);

If each call to fscanf works, each will fill in one int, but buf points to a sequence of unsigned chars. Unless you have very large chars and sizeof(int) == 1 this is obviously wrong.

The same mistake is repeated at many other points, e.g., in Fill_COTP, fscanf with a %d directive is used to fill in temp, which has type unsigned short rather than int.

You could change the directives (%hhd will fill in a single char and %hd will fill in a single short; %hhu and %hu will fill in unsigned char and unsigned short). However, simply calling fscanf like this, without any error checking, is not very robust. If the contents of the input stream are not convert-able to the target type, the call will fail (fscanf will return either EOF or a short count, depending on the kind of failure, "input" vs "matching", and the point of the failure). You might want a little intermediate function that does appropriate error checking, perhaps scanning into an int after all and then range-checking the value for instance.

Upvotes: 0

Jay
Jay

Reputation: 24905

The trouble appears to be in the line:

fscanf(fptr,"%d",&(cotp->TPDU_size)); 

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.

Upvotes: 1

Related Questions