error writing to a text file

Asked by sajidtariq

HEllO every one this is my program

#include <stdio.h>
#include <stdlib.h>
#include <pcap.h>
#include <net/ethernet.h>
#include <netinet/ip.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netinet/udp.h>
#include <arpa/inet.h>
#include <string.h>
#include <time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>

void packetHandler(u_char *userData, const struct pcap_pkthdr* pkthdr, const u_char* packet);
void save_in_file(char *sourceIp,char*destIp,char sp[],char dp[],char *transport_protocol,char dl[],char hl[],char *app);
char* application_layer_protocol(int d);

int main()
{
        char *dev;
 pcap_t *descr;
 char errbuf[PCAP_ERRBUF_SIZE];
        dev = pcap_lookupdev(errbuf);
 if (dev == NULL)
               {
          printf ("pcap_lookupdev() failed:",errbuf);
  return 1;
        }
        descr = pcap_open_live( dev ,BUFSIZ, 0, -1, errbuf);
 if (descr == NULL)
               {
                printf("pcap_open_live() failed:");
  return 1;
        }

 if (pcap_loop(descr, 0, packetHandler, NULL) < 0)
               {
  printf("pcap_loop() failed: ");
  return 1;
}

 return 0;
} //end main

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

FILE *f1;
struct hostent *hp;
   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];
 char dp[5];
 char dl[10];
 char hl[10];
        int sourcePort,destPort;
   u_char *data;
 int dataLength = 0;
        char dataStr[100]="";
        char* app =NULL ;

 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);
                   destPort = ntohs(udpHeader->dest);

                 // d = ntohs(udpHeader->dest);

                   app= application_layer_protocol(destPort);

                   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));
                  // hp=application_name(destIp ,destPort);

             }
               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);
                 // d = ntohs(tcpHeader->dest);
     app= application_layer_protocol(destPort);

    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));
                 // hp=application_name(destIp,destPort);
             }

        }
sprintf(sp,"%d",sourcePort);
sprintf(dp,"%d",destPort);
sprintf(dl,"%d",dataLength);
sprintf(hl,"%d",pkthdr->len);
save_in_file(sourceIp,destIp,sp,dp,transport_protocol,dl,hl,app);
printf(app);

} //end function
//=============================================================================
void save_in_file(char*sourceIp,char*destIp,char sp[],char dp[],char *transport_protocol,char dl[],char hl[],char *app)
{
FILE *f1;
f1=fopen("p.txt","a");
fputs("\n",f1);
fprintf(f1,"%-20s",sourceIp);
fprintf(f1,"%-20s",destIp);
fprintf(f1,"%-15s",app);
fprintf(f1,"%-15s",transport_protocol);
fprintf(f1,"%-15s",sp);
fprintf(f1,"%-15s",dp);
fprintf(f1,"%-15s",dl);
fprintf(f1,"%-15s",hl);
fclose(f1);

}
char* application_layer_protocol(int d)
{
p
char* app_proto =NULL;
if(d==80)
app_proto = "abc";
else if(d==443)
app_proto = "def";
else
app_proto = "NONE";

return &(app_proto[0]);
}

i this program i am capturing packets and extracting some info and writing it in to a file. every thing is going good but the problem is that when i tried to write the application layer protocol for which i have called a function
  char* application_layer_protocol(int d)
i have simply send a dest port which is an integer and want it to return http ,https or none as defined in the function
but the problem is that it it does not find http or https it returns none (which is correct ) but if it finds http it return a garbage value
(col) or some display time return pcap_openlive _fail() etc
please help me why it is happening as it is a simple c function returning an array of charcter
thanks alot
please help me

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

If you need to debug your program, you will need to do it yourself. I recommend using GDB, which is an excellent tool for debugging.

Secondly, dumping an entire program reuqires someone to parse the logic, and this is basically what you should be doing yourself -- so you are in effect, asking someone else to do your work for you. If you reduce the program down to a core component, where you don't understand the behaviour of a few lines, then people are more likely to help.

Finally, if you are looking for someone to answer your questions, this is probably the wrong forum -- C programming questions are probably best addressed to a c mailing list/newsgroup like comp.lang.c

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.