Crazy Struct

This is a discussion on Crazy Struct within the Linux Programming forums, part of the Platform Specific Boards category; Hi all, I'm coding a little exercise program to send raw ethernet packet over localnet. Code following: Code: #include <stdio.h> ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    2

    Crazy Struct

    Hi all,

    I'm coding a little exercise program to send raw ethernet packet over localnet. Code following:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <sys/types.h>
    #include <sys/socket.h>
    #include <linux/if_ether.h>
    #include <linux/if_arp.h>
    #include <linux/sockios.h>
    
    #define ETH_LEN 46
    
    struct _arp_hdr {
        __be16 ar_hrd; /* hardware address */
        __be16 ar_pro; /* protocol address */
        unsigned char ar_hln; /*length of hardware address */
        unsigned char ar_pln; /*length of protocol address */
        __be16 ar_op; /* ARP opcode (command) */
        
        unsigned char ar_sha[ETH_ALEN]; /* sender hardware address */
        unsigned char ar_sip[4]; /* sender ip address */
        unsigned char ar_tha[ETH_ALEN]; /* target hardware address */
        unsigned char ar_tip[4]; /* target IP address */
    };
    
    struct eth_frame {
    	struct ethhdr eth_hdr;
    	struct _arp_hdr arp_hdr;
    	u_char padding[18];
    };
    
    int main(int argc, char **argv)
    {
    	void *buffer = (void *) malloc(ETH_LEN*sizeof(char));
    	int length;
    	int sock;
    	if((sock = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL))) == -1) {
    		fprintf(stderr, "Error creating socket.\n");
    		exit(1);
    	}
    	while(1) {
    		if((length = recvfrom(sock, buffer, ETH_LEN, 0, NULL, NULL)) == -1) {
    			fprintf(stderr, "Error receiving packets.\n");
    			exit(3);
    		} else {
    			fprintf(stderr, "Arp Packet Received\n");
    			struct eth_frame *eth_pkt = (struct eth_frame *) buffer;
    			fprintf(stderr, "Sender Address: %02X:%02X:%02X:%02X:%02X:%02X, ", 
    						eth_pkt->eth_hdr.h_source[0], 
    						eth_pkt->eth_hdr.h_source[1], 
    						eth_pkt->eth_hdr.h_source[2], 
    						eth_pkt->eth_hdr.h_source[3], 
    						eth_pkt->eth_hdr.h_source[4], 
    						eth_pkt->eth_hdr.h_source[5]);
    			fprintf(stderr, "Sender IP: %d.%d.%d.%d\n", 
    						eth_pkt->arp_hdr.ar_sip[0], 
    						eth_pkt->arp_hdr.ar_sip[1], 
    						eth_pkt->arp_hdr.ar_sip[2]. 
    						eth_pkt->arp_hdr.ar_sip[3]);
    			exit(0);
    		}
    	}
    	return 0;
    }
    The problem arises adding the fprintf of "Sender IP...". If I build the project the compiler tells me the error:

    error: request for member ‘eth_pkt’ in something not a structure or union

    But how can it possible? If I comment this line of code the program runs correctly and the fprintf line "Sender Address" is almost the same and doesn't give similar error message. Where is the problem and the difference between the two fprintf lines?

    Thanks in advance and excuse me for my english.
    Last edited by Salem; 08-28-2010 at 07:29 AM. Reason: Folded long lines

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,451
    Well, after sip[2], you have a dot, not a comma.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    2
    I want to kill myself!

    Thanks very much Salem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. casting struct to struct
    By klmdb in forum C Programming
    Replies: 6
    Last Post: 08-14-2010, 02:29 PM
  2. I'm donating my vector lib.
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 06-24-2010, 06:10 PM
  3. help assignment due tomorrow
    By wildiv in forum C Programming
    Replies: 6
    Last Post: 01-27-2010, 07:38 PM
  4. Replies: 1
    Last Post: 12-03-2008, 02:10 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 09:24 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21