packet capturing tex file

Asked by sajidtariq

hi every one i have taken a code of packet capturing from net and i am saving that information in a text file
but the problem is that even i have close the file using close() function but it is still created and i think there are not
packets in the network but is is still created i delete it and it is again created. any idea please tell me
here is the part where i have implemented filing. (i have run this code on terminal )

void packetHandler(u_char *userData, const struct pcap_pkthdr* pkthdr, const u_char* packet)
 {

FILE *f1;

   const struct ether_header* ethernetHeader;
 const struct ip* ipHeader;
 const struct udphdr* udpHeader;
        const struct tcphdr*tcpHeader;
 char sourceIp[INET_ADDRSTRLEN];
 char destIp[INET_ADDRSTRLEN];
        char * transport_protocol;
        char sp[5] ,dp[5];
  int sourcePort,destPort;
 u_char *data;
 int dataLength = 0;
        char dataStr[100]="";
        char filedata[500]="";

 ethernetHeader = (struct ether_header*)packet;
 if (ntohs(ethernetHeader->ether_type) == ETHERTYPE_IP)
                {
  ipHeader = (struct ip*)(packet + sizeof(struct ether_header));
  inet_ntop(AF_INET, &(ipHeader->ip_src), sourceIp, INET_ADDRSTRLEN);
  inet_ntop(AF_INET, &(ipHeader->ip_dst), destIp, INET_ADDRSTRLEN);

  if (ipHeader->ip_p == IPPROTO_UDP)
                 {

                   transport_protocol = "UDP";
     udpHeader = (struct udphdr*)(packet + sizeof(struct ether_header) + sizeof(struct ip));
     sourcePort = ntohs(udpHeader->source);
                   sprintf(sp,"%d",ntohs(udpHeader->source));
     destPort = ntohs(udpHeader->dest);
                   data = (u_char*)(packet + sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct udphdr));
     dataLength = pkthdr->len - (sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct udphdr));

             }
               else if (ipHeader->ip_p == IPPROTO_TCP)
                 {
                  transport_protocol = "TCP";
                  tcpHeader = (struct tcphdr*)(packet + sizeof(struct ether_header) + sizeof(struct ip));
    sourcePort = ntohs(tcpHeader->source);
    destPort = ntohs(tcpHeader->dest);
    data = (u_char*)(packet + sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct tcphdr));
    dataLength = pkthdr->len - (sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct tcphdr));

             }

        }

sprintf(sp,"%d",sourcePort);
sprintf(dp,"%d",destPort);

f1=fopen("info.txt","a");
fputs(sourceIp,f1);
fputs("\n",f1);

fputs(destIp,f1);
fputs(" ",f1);

fputs(sp,f1);
fputs(" ",f1);

fputs(dp,f1);
fputs(" ",f1);

fputs("\n",f1);

fclose(f1);

} //end function

thanks in adcance

Question information

Language:
English Edit question
Status:
Answered
For:
Ubuntu build-essential Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
mycae (mycae) said :
#1

Your description of the codes behaviour is as it should be. FILE *f=fopen("blah.txt","a"); fclose(f); will always create the file blah.txt in the working directory, if it does not exist..

try "man fopen" from the terminal

Can you help with this problem?

Provide an answer of your own, or ask sajidtariq for more information if necessary.

To post a message you must log in.