Click here to Skip to main content
14,603,733 members
Rate this:
Please Sign up or sign in to vote.
See more:
Have this error from main() Need help! :(


#include "stdafx.h"
#include <pcap.h>


/* 4 bytes IP address */
typedef struct ip_address{
    u_char byte1;
    u_char byte2;
    u_char byte3;
    u_char byte4;
}ip_address;

/* IPv4 header */
typedef struct ip_header{
    u_char  ver_ihl;        // Version (4 bits) + Internet header length (4 bits)
    u_char  tos;            // Type of service 
    u_short tlen;           // Total length 
    u_short identification; // Identification
    u_short flags_fo;       // Flags (3 bits) + Fragment offset (13 bits)
    u_char  ttl;            // Time to live
    u_char  proto;          // Protocol
    u_short crc;            // Header checksum
    ip_address  saddr;      // Source address
    ip_address  daddr;      // Destination address
    u_int   op_pad;         // Option + Padding
}ip_header;

/* UDP header*/
typedef struct udp_header{
    u_short sport;          // Source port
    u_short dport;          // Destination port
    u_short len;            // Datagram length
    u_short crc;            // Checksum
}udp_header;

/* prototype of the packet handler */
void packet_handler(u_char *param, const struct pcap_pkthdr *header, const u_char *pkt_data);


main()
{
pcap_if_t *alldevs;
pcap_if_t *d;
int inum;
int i=0;
pcap_t *adhandle;
char errbuf[PCAP_ERRBUF_SIZE];
u_int netmask;
char packet_filter[] = "ip and udp";
struct bpf_program fcode;

    /* Retrieve the device list */
    if (pcap_findalldevs_ex(PCAP_SRC_IF_STRING, NULL, &alldevs, errbuf) == -1)
    {
        fprintf(stderr,"Error in pcap_findalldevs: %s\n", errbuf);
        exit(1);
    }
    
    /* Print the list */
    for(d=alldevs; d; d=d->next)
    {
        printf("%d. %s", ++i, d->name);
        if (d->description)
            printf(" (%s)\n", d->description);
        else
            printf(" (No description available)\n");
    }

    if(i==0)
    {
        printf("\nNo interfaces found! Make sure WinPcap is installed.\n");
        return -1;
    }
Posted
Updated 24-May-20 12:32pm
v4
Comments
   
In what line of code? No matter, locate this line and actually declare the variable with appropriate type.
—SA
Volynsky Alex 6-May-13 3:28am
   
Do not see the class declaration pcap_if_t....
Try to add a class declaration before the main function...
For example:

class pcap_if_t;


P.S. main should return int
Rate this:
Please Sign up or sign in to vote.

Solution 1

Quote:
main()

should be
int main()
   
Rate this:
Please Sign up or sign in to vote.

Solution 2

The error indicates the missing return type of the main() function. Because main() must return an int, use:
int main()
{
    // ...
    return 0;
}
   
Comments
decayer23 6-May-13 3:38am
   
This is the error i had after putting int main()

error LNK2019: unresolved external symbol "void __cdecl packet_handler(unsigned char *,struct pcap_pkthdr const *,unsigned char const *)" (?packet_handler@@YAXPAEPBUpcap_pkthdr@@PBE@Z) referenced in function _main
Jochen Arndt 6-May-13 3:46am
   
That is another error which has been introduced later (my answer is for the initial question). You should not edit your questions in such a way that the context to answers may be lost.

However, the error is from the linker telling you that it can not find the packet_handler() function in object files (created by compiling source files) or a library.
PrafullaVedante 6-May-13 3:48am
   
add .lib file inwhich packet_handler function is located in linker path .....

For visual studio you should add .lib file at Project Settings-->Linker--->Input
decayer23 6-May-13 3:57am
   
sorry for that, its my first time posting and do you know what kind of .lib file should i add in? I have wpcap.lib and packet.lib in the linker input project already.
Jochen Arndt 6-May-13 4:15am
   
It is a handler function that must be implemented by you. Your code looks like those from http://www.winpcap.org/docs/docs_41b5/html/group__wpcap__tut3.html. So just add the function as done there.
decayer23 6-May-13 4:24am
   
Thanks. Got it working! :D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100