error writing to a text file
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(
void save_in_file(char *sourceIp,
char* application_
int main()
{
char *dev;
pcap_t *descr;
char errbuf[
dev = pcap_lookupdev(
if (dev == NULL)
{
printf ("pcap_lookupdev() failed:",errbuf);
return 1;
}
descr = pcap_open_live( dev ,BUFSIZ, 0, -1, errbuf);
if (descr == NULL)
{
return 1;
}
if (pcap_loop(descr, 0, packetHandler, NULL) < 0)
{
printf(
return 1;
}
return 0;
} //end main
void packetHandler(
{
FILE *f1;
struct hostent *hp;
const struct ether_header* ethernetHeader;
const struct ip* ipHeader;
const struct udphdr* udpHeader;
const struct tcphdr*tcpHeader;
char sourceIp[
char destIp[
char * transport_protocol;
char sp[5];
char dp[5];
char dl[10];
char hl[10];
int sourcePort,
u_char *data;
int dataLength = 0;
char dataStr[100]="";
char* app =NULL ;
ethernetHeader = (struct ether_header*
if (ntohs(
{
ipHeader = (struct ip*)(packet + sizeof(struct ether_header));
inet_
inet_
if (ipHeader->ip_p == IPPROTO_UDP)
{
udpHeader = (struct udphdr*)(packet + sizeof(struct ether_header) + sizeof(struct ip));
sourcePort = ntohs(udpHeader
// d = ntohs(udpHeader
dataLength = pkthdr->len - (sizeof(struct ether_header) + sizeof(struct ip) + sizeof(struct udphdr));
}
else if (ipHeader->ip_p == IPPROTO_TCP)
{
sourcePort = ntohs(tcpHeader
destPort = ntohs(tcpHeader
// d = ntohs(tcpHeader
app= application_
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_
}
}
sprintf(
sprintf(
sprintf(
sprintf(
save_in_
printf(app);
} //end function
//=====
void save_in_
{
FILE *f1;
f1=fopen(
fputs("\n",f1);
fprintf(
fprintf(
fprintf(
fprintf(
fprintf(
fprintf(
fprintf(
fprintf(
fclose(f1);
}
char* application_
{
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_
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
- Assignee:
- No assignee Edit question
- Last query:
- Last reply:
Can you help with this problem?
Provide an answer of your own, or ask sajidtariq for more information if necessary.