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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [redboot/] [current/] [src/] [net/] [arp.c] - Blame information for rev 786

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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, 2003 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY 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        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):    gthomas
43
// Contributors: gthomas
44
// Date:         2000-07-14
45
// Purpose:      
46
// Description:  
47
//              
48
// This code is part of RedBoot (tm).
49
//
50
//####DESCRIPTIONEND####
51
//
52
//==========================================================================
53
 
54
#include <net/net.h>
55
 
56
static struct {
57
    int      waiting;
58
    char     *eth;
59
    char     *ip;
60
} arp_req;
61
 
62
/*
63
 * Handle incoming ARP packets.
64
 */
65
void
66
__arp_handler(pktbuf_t *pkt)
67
{
68
    arp_header_t *arp = pkt->arp_hdr;
69
    int hw_type, protocol;
70
 
71
    /*
72
     * Only handle ethernet hardware and IP protocol.
73
     */
74
    protocol = ntohs(arp->protocol);
75
    hw_type = ntohs(arp->hw_type);
76
    if ((hw_type == ARP_HW_ETHER) && (protocol == ETH_TYPE_IP)) {
77
        /*
78
         * Handle requests for our ethernet address.
79
         */
80
        if (!memcmp(arp->target_ip, __local_ip_addr, 4)) {
81
            if (ntohs(arp->opcode) == ARP_REQUEST) {
82
                /* format response. */
83
                arp->opcode = htons(ARP_REPLY);
84
                memcpy(arp->target_ip, arp->sender_ip,
85
                       sizeof(ip_addr_t));
86
                memcpy(arp->target_enet, arp->sender_enet,
87
                       sizeof(enet_addr_t));
88
                memcpy(arp->sender_ip, __local_ip_addr,
89
                       sizeof(ip_addr_t));
90
                memcpy(arp->sender_enet, __local_enet_addr,
91
                       sizeof(enet_addr_t));
92
                pkt->pkt_bytes = sizeof(arp_header_t);
93
                __enet_send(pkt, &arp->target_enet, ETH_TYPE_ARP);
94
 
95
            } else if (ntohs(arp->opcode) == ARP_REPLY && arp_req.waiting) {
96
                if (!memcmp(arp_req.ip, arp->sender_ip, sizeof(ip_addr_t))) {
97
                    memcpy(arp_req.eth, arp->sender_enet, sizeof(enet_addr_t));
98
                    arp_req.waiting = 0;
99
                }
100
            }
101
        }
102
    }
103
    __pktbuf_free(pkt);
104
}
105
 
106
 
107
/*
108
 * Find the ethernet address of the machine with the given
109
 * ip address.
110
 * Return 0 and fills in 'eth_addr' if successful,
111
 *       -1 if unsuccessful.
112
 */
113
int
114
__arp_request(ip_addr_t *ip_addr, enet_addr_t *eth_addr, int allow_self)
115
{
116
    pktbuf_t *pkt;
117
    arp_header_t *arp;
118
    unsigned long retry_start;
119
    enet_addr_t   bcast_addr;
120
    int           retry;
121
 
122
    if (!allow_self) {
123
        // Special case request for self
124
        if (!memcmp(ip_addr, __local_ip_addr, 4)) {
125
            memcpy(eth_addr, __local_enet_addr, sizeof(enet_addr_t));
126
            return 0;
127
        }
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) < 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.