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.