OpenCores
URL https://opencores.org/ocsvn/1g_ethernet_dpi/1g_ethernet_dpi/trunk

Subversion Repositories 1g_ethernet_dpi

[/] [1g_ethernet_dpi/] [trunk/] [sw/] [dev/] [test_main/] [src/] [net/] [arp.c] - Blame information for rev 4

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 4 kuzmi4
 
2
#include <stdio.h>
3
 
4
#include "xil_types.h"
5
#include "xil_io.h"
6
 
7
#include "xil_lib.h" // xil_malloc, xil_free
8
 
9
#include "eth.h"
10
#include "arp.h"
11
 
12
// net-if 
13
net_if_t *ifp;
14
 
15
// ??
16
void eth_arp_init(net_if_t *ip_net_if)
17
{
18
    ifp = ip_net_if;
19
    // Final
20
}
21
// simple arp req-resp logic:
22
void eth_arp(char *iv_data)
23
{
24
    // extract Eth-Type
25
    char *ethin = iv_data;
26
    u16 etype = ETH_TYPE_GET(ethin);
27
    // chk ARP-proto
28
    if (etype != ntohs(ETH_ARP)) {
29
        return;
30
    }
31
 
32
    // chk ipkt
33
    arp_hdr_t *in = (arp_hdr_t *)(iv_data + ETH_HDR_SZ);
34
    if (in->ar_pro != ARP_IPv4) {
35
        return;
36
    }
37
    if (in->ar_op != ARP_REQ) {
38
        return;
39
    }
40
    if (in->ar_tpa != ifp->ip_addr) {
41
        return;
42
    }
43
 
44
    // alloc/init opkt
45
    char *ethout = xil_malloc(ARP_SZ); // <-
46
    arp_hdr_t *out = (arp_hdr_t *)(ethout + ETH_HDR_SZ);
47
    if (!ethout) { // xil_malloc-ERR
48
        return;
49
    }
50
    xil_memset(ethout, 0, ARP_SZ);
51
 
52
    // fill opkt
53
    xil_memmove(ethout + ETH_DST_OFST, ethin + ETH_SRC_OFST, 6);
54
    xil_memmove(ethout + ETH_SRC_OFST, ifp->mac_addr, 6);
55
    ETH_TYPE_SET(ethout, ntohs(ETH_ARP));
56
 
57
    out->ar_hd = ARP_HW_ETH;
58
    out->ar_pro = ARP_IPv4;
59
    out->ar_hln = 6;
60
    out->ar_pln = 4;
61
    out->ar_op = ARP_RESP;
62
 
63
    xil_memmove((char *)out->ar_sha, (char *)ifp->mac_addr, 6);
64
    out->ar_spa = in->ar_tpa;
65
    xil_memmove((char *)out->ar_tha, (char *)in->ar_sha, 6);
66
    out->ar_tpa = in->ar_spa;
67
 
68
    // tx
69
    ifp->net_raw_send(ethout, ETH_HDR_SZ+sizeof(arp_hdr_t));
70
 
71
    // Final
72
    xil_free(ethout); // ->
73
}

powered by: WebSVN 2.1.0

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