Thread: Help using inet_ntoa()

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    Help using inet_ntoa()

    I have been copying code from a book to understand it better.

    Heres the code:

    Code:
    #include <pcap.h>
    #include "hacking.h"
    #include "hacking-network.h"
    
    void pcap_fatal(const char *, const char *);
    void decode_ethernet(const u_char *);
    void decode_ip(const u_char *);
    u_int decode_tcp(const u_char *);
    
    void caught_packet(u_char *, const struct pcap_pkthdr *, const u_char *);
    
    int main() {
      struct pcap_pkthdr cap_header;
      const u_char *packet, *pkt_data;
      char errbuf[PCAP_ERRBUF_SIZE];
      char *device;
      pcap_t *pcap_handle;
    
      device = pcap_lookupdev(errbuf);
      if(device == NULL)
        pcap_fatal("pcap_lookupdev", errbuf);
    
      printf("Sniffing on device %s\n", device);
    
      pcap_handle = pcap_open_live(device, 4096, 1, 0, errbuf);
      if(pcap_handle == NULL)
        pcap_fatal("pcap_open_live", errbuf);
    
      pcap_loop(pcap_handle, 3, caught_packet, NULL);
    
      pcap_close(pcap_handle);
    }
    
    void caught_packet(u_char *user_args, const struct pcap_pkthdr *cap_header, const u_char *packet) {
      int tcp_header_length, total_header_size, pkt_data_len;
      u_char *pkt_data;
    
      printf("==== Got a %d byte packet ====\n", cap_header->len);
    
      decode_ethernet(packet);
      decode_ip(packet+ETHER_HDR_LEN);
      /*tcp_header_length = decode_tcp(packet+ETHER_HDR_LEN+sizeof(struct ip_hdr));
    
      total_headder_size = ETHER_HDR_LEN+sizeof(struct ip_hdr)+tcp_header_length;
      pkt_data = (u_char *)packet + total_header_size;
    
      pkt_data_len = cap_header->len - total_header_size;
      if(pkt_data_len > 0) {
        printf("\t\t\t%u bytes of packet data\n", pkt_data_len);
        dump(pkt_data, pkt_data_len);
      }
      else
        printf("\t\t\tNo Packet Data\n");
      */
    }
    
    void pcap_fatal(const char *failed_in, const char *errbuf) {
      printf("Fatal Error in %s: %s\n", failed_in, errbuf);
    }
    
    
    void decode_ethernet(const u_char *header_start) {
      int i;
      const struct ether_hdr *ethernet_header;
    
      ethernet_header = (const struct ether_hdr *)header_start;
      printf("[[  Layer 2 :: Ethernet Header  ]]\n");
      printf("[  Source: %02x", ethernet_header->ether_src_addr[0]);
      for(i=1; i < ETHER_ADDR_LEN; i++)
        printf(":%02x", ethernet_header->ether_src_addr[i]);
    
      printf("\tDest: %02x", ethernet_header->ether_dest_addr[0]);
      for(i=1; i < ETHER_ADDR_LEN; i++)
        printf(":%02x", ethernet_header->ether_dest_addr[i]);
      printf("\tType: %hu ]\n", ethernet_header->ether_type);
    }
    
    void decode_ip(const u_char *header_start) {
      const struct ip_hdr *ip_header;
    
      ip_header = (const struct ip_hdr *)header_start;
      printf("\t((   Layer 3 ::: IP Header   ))\n");
      printf("\t( Source: %s\t", inet_ntoa(ip_header->ip_src_addr));
      printf("Dest: %s )\n", inet_ntoa(ip_header->ip_dest_addr));
      printf("\t( Type: %u\t", (u_int) ip_header->ip_type);
      printf("ID: %hu\tLength: %hu )\n", ntohs(ip_header->ip_id), ntohs(ip_header->ip_len));
    }
    The code is from a book called hacking the art of exploitation and its used for sniffing network packets.

    The problem is every time i try to compile it i get this error:

    decode_sniff.c:83:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]
    decode_sniff.c:84:3: warning: format ‘%s’ expects argument of type ‘char *’, but argument 2 has type ‘int’ [-Wformat]

    The program runs and prints the mac addresses and enters the decode_ip function and prints the first printf statement "Layer 3 ::: IP Header" but then just ends.

    It seems to work all right if i use %d or %c but then im not printing in ip format!

    Any help would be appreciated

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Given the source of your code... I wouldn't be holding my breath for a lot of help on this one.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    maybe you aren't including the file that defines inet_ntoa so it is defaulting to 'int'. you probably need "netinet/in.h".

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Read this: Announcements - C Programming. Pay extra close attention to rule #6.

    mods: Should this thread be locked/deleted?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Look, i just reading the book to better understand it. I;ve no intention of using the code in a illegitimate way. It's actually a pretty good book for learning network programming!

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    Quote Originally Posted by dmh2000 View Post
    maybe you aren't including the file that defines inet_ntoa so it is defaulting to 'int'. you probably need "netinet/in.h".
    Thanks for the reply, i tried it but still the same problem!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcpy and inet_ntoa
    By AdamOhio76 in forum C Programming
    Replies: 1
    Last Post: 05-09-2011, 06:38 PM
  2. inet_ntoa to strings
    By TomChesley in forum Networking/Device Communication
    Replies: 7
    Last Post: 02-24-2009, 05:01 PM
  3. Replies: 1
    Last Post: 04-05-2003, 08:39 AM
  4. inet_ntoa() or not?
    By PhilB in forum C Programming
    Replies: 1
    Last Post: 05-01-2002, 04:02 PM