OpenCores
URL https://opencores.org/ocsvn/minsoc/minsoc/trunk

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [utils/] [contributions/] [eth_transf_linux/] [eth1_mac_recv.c] - Blame information for rev 160

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 40 rfajardo
#include <stdio.h>
2
 
3
#include <string.h>
4
 
5
//packet socket
6
#include <sys/socket.h>
7
#include <netpacket/packet.h>
8
#include <net/ethernet.h>
9
 
10
//protocol
11
#include <linux/if_ether.h>
12
 
13
//netdevice stuff
14
#include <sys/ioctl.h>
15
#include <net/if.h>
16
 
17
//file open stuff
18
#include <sys/types.h>
19
#include <sys/stat.h>
20
#include <fcntl.h>
21
 
22
//arp stuff
23
//#include <linux/if_arp.h>
24
 
25
#define MAC_ADDR_LEN 6
26
typedef unsigned char MacAddress[MAC_ADDR_LEN];
27
 
28
int main()
29
{
30
    int socket_id, new_sock, iRet = -1;
31
    int addrlen, bytesread, nfound =0;
32
 
33
    int i = 0;
34
 
35
    MacAddress localMac = {0x00, 0x00, 0xC0, 0x41, 0x36, 0xD3};
36
//    MacAddress localMac = {0xD3, 0x36, 0x41, 0xC0, 0x00, 0x00};
37
 
38
    char buf[256];
39
 
40
    struct sockaddr_ll my_addr;
41
 
42
    struct ifreq ethreq;
43
 
44
    int if_index;
45
 
46
    //create packet socket from type sock_dgram where headers are automatically thrown out
47
    if ( ( socket_id = socket(PF_PACKET, SOCK_DGRAM, htons(ETH_P_ALL) ) ) < 0  )
48
    {
49
        perror("socket");
50
        exit(1);
51
    }
52
    else
53
    {
54
        printf("Socket has been created: socket_number %d\n", socket_id);
55
    }
56
 
57
 
58
    //GET ethreq for if "eth1"
59
    strncpy(ethreq.ifr_name,"eth1",IFNAMSIZ);
60
    ioctl(socket_id, SIOCGIFFLAGS, &ethreq);
61
    //SET promisc mode for if ethreq
62
//    ethreq.ifr_flags |= IFF_PROMISC;
63
//    ioctl(socket_id, SIOCSIFFLAGS, &ethreq);
64
    //request index
65
    ioctl(socket_id, SIOCGIFINDEX, &ethreq);
66
 
67
    if_index = ethreq.ifr_ifindex;
68
 
69
    printf("This is the index of the interface: %d\n", if_index );
70
 
71
    memset(&my_addr, '0', sizeof(my_addr) );
72
 
73
    my_addr.sll_family = AF_PACKET;
74
    my_addr.sll_protocol = htons(ETH_P_ALL);  //defaults to socket protocol
75
    my_addr.sll_ifindex = if_index;
76
//    my_addr.sll_hatype = htons(ARPHRD_ETHER);
77
//    my_addr.sll_pkttype = PACKET_OTHERHOST;
78
    my_addr.sll_halen = 6;
79
    memcpy( &(my_addr.sll_addr), localMac, MAC_ADDR_LEN );
80
 
81
     //request hw_addres
82
    ioctl(socket_id, SIOCGIFHWADDR, &ethreq);
83
 
84
    printf("This is the address of my card: %d\n", my_addr.sll_addr[5] );
85
 
86
    //bind to interface goten from ioctl SIOCGIFHWADDR directive (otherwise all packets are recved)
87
    if ( bind( socket_id, (struct sockaddr *) &my_addr, sizeof(my_addr) ) )
88
    {
89
        perror("bind");
90
        exit(1);
91
    }
92
 
93
    struct sockaddr_ll from;
94
    int fromlen;
95
 
96
    fromlen = sizeof(from);
97
 
98
    for (;;)
99
    {
100
        iRet = recvfrom(socket_id, buf, 256, 0, &from, &fromlen);
101
        if ( iRet == -1 )
102
        {
103
            perror("recvfrom");
104
            exit(1);
105
        }
106
        else
107
        {
108
            printf("Received %d bytes of data.\n", iRet);
109
            printf("This is the received data:\n");
110
            for ( i = 0; i < iRet; i++)
111
                printf("Byte %d: %X\n", i, (int)buf[i]);
112
            printf("End of transmission!\n");
113
        }
114
    }
115
 
116
    return 0;
117
}
118
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.