Thread: program not printing mac addr correctly from ethernet header

  1. #1
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266

    program not printing mac addr correctly from ethernet header

    I can post more code if need be... I have a procedure that prints the fields of ethernet frames and ip headers. I have an issue with the src mac addr being printed incorrectly and incompletely. What am I doing wrong? The part in red is the trouble code.

    Code:
     1#include "sniffer.h"
     2
     3void print_headers(struct ethhdr * ethhdr, struct ip * iphdr){
     4
     5  char ipstr_src[INET_ADDRSTRLEN];
     6  char ipstr_dst[INET_ADDRSTRLEN];
     7  char macstr_dst[ETH_ALEN],  macstr_src[ETH_ALEN];
     8
     9
    10  struct ether_addr * ether_addr;
    11
    12  printf("\n"
    13         "[Ethernet Header]\n"
    14         "Destination MAC: %s \n"
    15         "Source MAC: %s \n"
    16         "Protocol: %u \n\t",
    17         ether_ntoa_r((struct ether_addr *)&(ethhdr->h_dest), macstr_dst),
    18         ether_ntoa_r((struct ether_addr *)&(ethhdr->h_source), macstr_src),
    19         ethhdr->h_proto
    20         );
    21
    22  printf(
    23         "[IP Header]\n\t"
    24         "Header Length: %u\n\t"
    25         "Version: %u\n\t"
    26         "TOS: %u \n\t"
    27         "Len: %u \n\t"
    28         "ID: %u \n\t"
    29         "Fragment Offset: %u\n\t"
    30         "TTL: %u \n\t"
    31         "Protocol: %u \n\t"
    32         "Checksum: %u \n\t"
    33         "Source: %s \n\t"
    34         "Destination: %s\n\t",
    35         iphdr->ip_hl, iphdr->ip_v,
    36         iphdr->ip_tos, iphdr->ip_len, iphdr->ip_id, iphdr->ip_off,
    37         iphdr->ip_ttl, iphdr->ip_p, iphdr->ip_sum,
    38         inet_ntop(AF_INET, &iphdr->ip_src, ipstr_src, INET_ADDRSTRLEN),
    39         inet_ntop(AF_INET, &iphdr->ip_dst, ipstr_dst, INET_ADDRSTRLEN)
    40         );
    41
    42
    43}

  2. #2
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by Syscal View Post
    Code:
    char macstr_dst[ETH_ALEN],  macstr_src[ETH_ALEN];
    These are only long enough to hold the binary value, not the string value.

    Since the format is hexadecimal with colons in between (and including the '\0' at the end of the string), they should be exactly three times as long,
    Code:
    char macstr_dst[3*ETH_ALEN],  macstr_src[3*ETH_ALEN];

  3. #3
    C lover
    Join Date
    Oct 2007
    Location
    Virginia
    Posts
    266
    Works perfectly now, thanks!! Hadn't considered that as the first of the two mac addresses was printing correctly. Expected output below:

    Code:
    codeblox@Lubuntu-pc:~/Programming/C/Network/Sniffer/Raw_Socket$ sudo ./sniffer -i eth0
    
    00 90 27 2d e7 5e 50 e5 49 5b f8 87 08 00 45 00 00 28 76 5f 40 00 80 06 01 93 c0 a8 00 c7 c0 a8 00 c6 64 55 00 16 7e fb db c4 bf b2 81 dd 50 10 3e e3 ed 57 00 00 00 00 00 00 00 00
    
    [Ethernet Header]
    Destination MAC: 0:90:27:2d:e7:5e
    Source MAC: 50:e5:49:5b:f8:87
    Protocol: 8
            [IP Header]
            Header Length: 5
            Version: 4
            TOS: 0
            Len: 10240
            ID: 24438
            Fragment Offset: 64
            TTL: 128
            Protocol: 6
            Checksum: 37633
            Source: 192.168.0.199
            Destination: 192.168.0.198

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char** within struct not printing correctly.
    By aj74 in forum C Programming
    Replies: 1
    Last Post: 11-12-2011, 11:11 AM
  2. printing matrices correctly
    By maxzoran in forum C Programming
    Replies: 3
    Last Post: 07-20-2010, 01:04 PM
  3. a program for Ethernet
    By arian in forum Networking/Device Communication
    Replies: 5
    Last Post: 07-29-2005, 03:48 PM
  4. windows header files not linking correctly
    By skorman00 in forum Windows Programming
    Replies: 2
    Last Post: 04-13-2005, 11:14 AM
  5. Printing a pattern correctly???
    By Basia in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 04:34 PM