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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [redboot/] [v2_0/] [src/] [net/] [arp.c] - Blame information for rev 773

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

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      net/arp.c
4
//
5
//      Stand-alone ARP support for RedBoot
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):    gthomas
45
// Contributors: gthomas
46
// Date:         2000-07-14
47
// Purpose:      
48
// Description:  
49
//              
50
// This code is part of RedBoot (tm).
51
//
52
//####DESCRIPTIONEND####
53
//
54
//==========================================================================
55
 
56
#include <net/net.h>
57
 
58
static struct {
59
    int      waiting;
60
    char     *eth;
61
    char     *ip;
62
} arp_req;
63
 
64
/*
65
 * Handle incoming ARP packets.
66
 */
67
void
68
__arp_handler(pktbuf_t *pkt)
69
{
70
    arp_header_t *arp = pkt->arp_hdr;
71
    int hw_type, protocol;
72
 
73
    /*
74
     * Only handle ethernet hardware and IP protocol.
75
     */
76
    protocol = ntohs(arp->protocol);
77
    hw_type = ntohs(arp->hw_type);
78
    if ((hw_type == ARP_HW_ETHER) && (protocol == ETH_TYPE_IP)) {
79
        /*
80
         * Handle requests for our ethernet address.
81
         */
82
        if (!memcmp(arp->target_ip, __local_ip_addr, 4)) {
83
            if (ntohs(arp->opcode) == ARP_REQUEST) {
84
                /* format response. */
85
                arp->opcode = htons(ARP_REPLY);
86
                memcpy(arp->target_ip, arp->sender_ip,
87
                       sizeof(ip_addr_t));
88
                memcpy(arp->target_enet, arp->sender_enet,
89
                       sizeof(enet_addr_t));
90
                memcpy(arp->sender_ip, __local_ip_addr,
91
                       sizeof(ip_addr_t));
92
                memcpy(arp->sender_enet, __local_enet_addr,
93
                       sizeof(enet_addr_t));
94
                pkt->pkt_bytes = sizeof(arp_header_t);
95
                __enet_send(pkt, &arp->target_enet, ETH_TYPE_ARP);
96
 
97
            } else if (ntohs(arp->opcode) == ARP_REPLY && arp_req.waiting) {
98
                if (!memcmp(arp_req.ip, arp->sender_ip, sizeof(ip_addr_t))) {
99
                    memcpy(arp_req.eth, arp->sender_enet, sizeof(enet_addr_t));
100
                    arp_req.waiting = 0;
101
                }
102
            }
103
        }
104
    }
105
    __pktbuf_free(pkt);
106
}
107
 
108
 
109
/*
110
 * Find the ethernet address of the machine with the given
111
 * ip address.
112
 * Return 0 and fills in 'eth_addr' if successful,
113
 *       -1 if unsuccessful.
114
 */
115
int
116
__arp_request(ip_addr_t *ip_addr, enet_addr_t *eth_addr)
117
{
118
    pktbuf_t *pkt;
119
    arp_header_t *arp;
120
    unsigned long retry_start;
121
    enet_addr_t   bcast_addr;
122
    int           retry;
123
 
124
    // Special case request for self
125
    if (!memcmp(ip_addr, __local_ip_addr, 4)) {
126
        memcpy(eth_addr, __local_enet_addr, sizeof(enet_addr_t));
127
        return 0;
128
    }
129
 
130
    /* just fail if can't get a buffer */
131
    if ((pkt = __pktbuf_alloc(ARP_PKT_SIZE)) == NULL)
132
        return -1;
133
 
134
    arp = pkt->arp_hdr;
135
    arp->opcode = htons(ARP_REQUEST);
136
    arp->hw_type = htons(ARP_HW_ETHER);
137
    arp->protocol = htons(0x800);
138
    arp->hw_len = sizeof(enet_addr_t);
139
    arp->proto_len = sizeof(ip_addr_t);
140
 
141
    memcpy(arp->sender_ip, __local_ip_addr, sizeof(ip_addr_t));
142
    memcpy(arp->sender_enet, __local_enet_addr, sizeof(enet_addr_t));
143
    memcpy(arp->target_ip, ip_addr, sizeof(ip_addr_t));
144
 
145
    bcast_addr[0] = 255;
146
    bcast_addr[1] = 255;
147
    bcast_addr[2] = 255;
148
    bcast_addr[3] = 255;
149
    bcast_addr[4] = 255;
150
    bcast_addr[5] = 255;
151
 
152
    arp_req.eth = (char *)eth_addr;
153
    arp_req.ip = (char *)ip_addr;
154
    arp_req.waiting = 1;
155
 
156
    retry = 8;
157
    while (retry-- > 0) {
158
 
159
        /* send the packet */
160
        pkt->pkt_bytes = sizeof(arp_header_t);
161
        __enet_send(pkt, &bcast_addr, ETH_TYPE_ARP);
162
 
163
        retry_start = MS_TICKS();
164
        while ((MS_TICKS_DELAY() - retry_start) < 250) {
165
            __enet_poll();
166
            if (!arp_req.waiting) {
167
                __pktbuf_free(pkt);
168
                return 0;
169
            }
170
        }
171
    }
172
    __pktbuf_free(pkt);
173
    return -1;
174
}
175
 
176
#define NUM_ARP 16
177
static ip_route_t routes[NUM_ARP];
178
 
179
int
180
__arp_lookup(ip_addr_t *host, ip_route_t *rt)
181
{
182
    int i;
183
    static int next_arp = 0;
184
 
185
    for (i = 0;  i < NUM_ARP;  i++) {
186
        if (memcmp(host, &routes[i].ip_addr, sizeof(*host)) == 0) {
187
            // This is a known host
188
            memcpy(rt, &routes[i], sizeof(*rt));
189
            return 0;
190
        }
191
    }
192
    memcpy(&rt->ip_addr, host, sizeof(*host));
193
    if (((*host)[0] == 0xFF) && ((*host)[1] == 0xFF) && ((*host)[2] == 0xFF)) {
194
        memset(&rt->enet_addr, 0xFF, sizeof(&rt->enet_addr));
195
        return 0;
196
#ifdef CYGSEM_REDBOOT_NETWORKING_USE_GATEWAY
197
    } else if (!__ip_addr_local(host)) {
198
        // non-local IP address -- look up Gateway's Ethernet address
199
        host = &__local_ip_gate;
200
#endif
201
    }
202
    if (__arp_request(host, &rt->enet_addr) < 0) {
203
        return -1;
204
    } else {
205
        memcpy(&routes[next_arp], rt, sizeof(*rt));
206
        if (++next_arp == NUM_ARP) next_arp = 0;
207
        return 0;
208
    }
209
}
210
 

powered by: WebSVN 2.1.0

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