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

Subversion Repositories minsoc

[/] [minsoc/] [trunk/] [utils/] [contributions/] [eth_transf_linux/] [eth1_mac_snd.c] - Blame information for rev 88

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 extMac = {0x55, 0x47, 0x34, 0x22, 0x88, 0x92};
37
//    MacAddress extMac = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
38
 
39
    char buf[256];
40
 
41
    struct sockaddr_ll my_addr;
42
 
43
    struct ifreq ethreq;
44
 
45
    int if_index;
46
 
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
    if_index = ethreq.ifr_ifindex;
67
 
68
    printf("This is the index of the interface: %d\n", if_index );
69
 
70
    memset(&my_addr, '0', sizeof(my_addr) );
71
 
72
    my_addr.sll_family = AF_PACKET;
73
    my_addr.sll_protocol = htons(ETH_P_ALL);  //defaults to socket protocol
74
    my_addr.sll_ifindex = if_index;
75
//    my_addr.sll_hatype = htons(ARPHRD_ETHER);
76
//    my_addr.sll_pkttype = PACKET_OTHERHOST;
77
    my_addr.sll_halen = 6;
78
    memcpy( &(my_addr.sll_addr), localMac, MAC_ADDR_LEN );
79
 
80
     //request hw_addres
81
    ioctl(socket_id, SIOCGIFHWADDR, &ethreq);
82
 
83
    printf("This is the address of my card: %d\n", my_addr.sll_addr[5] );
84
 
85
    if ( bind( socket_id, (struct sockaddr *) &my_addr, sizeof(my_addr) ) )
86
    {
87
        perror("bind");
88
        exit(1);
89
    }
90
 
91
    struct sockaddr_ll addr_to;
92
    int addr_toLen;
93
 
94
    addr_toLen = sizeof(addr_to);
95
 
96
    memset(&addr_to, '0', sizeof(addr_to) );
97
 
98
    addr_to.sll_family = AF_PACKET;
99
    addr_to.sll_ifindex = if_index;
100
    addr_to.sll_halen = 6;
101
    memcpy( &(addr_to.sll_addr), extMac, MAC_ADDR_LEN );
102
 
103
    for (i=0; i<256 ; i++ )
104
            buf[i] = 0;
105
 
106
    //first 2 bytes are gathered with length and are ignored
107
    buf[0] = 0xAA;
108
    buf[1] = 0xAA;
109
    //now it gets to fpga: send opcode 0xBA8
110
    buf[2] = 0xBA;
111
    buf[3] = 0x87;
112
    //opcode sent
113
    buf[4] = 0xAA;
114
    buf[5] = 0xAA;
115
    buf[6] = 0xAA;
116
    buf[7] = 0xAA;
117
    buf[8] = 0xAA;
118
    buf[9] = 0xAA;
119
    buf[10] = 0xAA;
120
    buf[11] = 0xAA;
121
 
122
//    for (;;)
123
//    {
124
        iRet = sendto(socket_id, buf, 46, 0, (struct sockaddr *) &addr_to, addr_toLen);
125
        if ( iRet == -1 )
126
        {
127
            perror("sendto");
128
            exit(1);
129
        }
130
        else
131
        {
132
//            printf("%s\n", buf);
133
              printf("Data sent!\nExiting...\n");
134
        }
135
//    }
136
 
137
    return 0;
138
}
139
 

powered by: WebSVN 2.1.0

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