comparison packages/redboot/current/src/net/bootp.c @ 115:6ed91473a1cd ecos-sw-2000-08-21

Merge from eCos master repository on 2000-08-21-22:40:54-BST
author jlarmour
date Fri, 25 Aug 2000 17:32:38 +0000
parents
children 25e238959bae
comparison
equal deleted inserted replaced
114:5ad2b71d525e 115:6ed91473a1cd
1 //==========================================================================
2 //
3 // net/bootp.c
4 //
5 // Stand-alone minimal BOOTP support for RedBoot
6 //
7 //==========================================================================
8 //####COPYRIGHTBEGIN####
9 //
10 // -------------------------------------------
11 // The contents of this file are subject to the Red Hat eCos Public License
12 // Version 1.1 (the "License"); you may not use this file except in
13 // compliance with the License. You may obtain a copy of the License at
14 // http://www.redhat.com/
15 //
16 // Software distributed under the License is distributed on an "AS IS"
17 // basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
18 // License for the specific language governing rights and limitations under
19 // the License.
20 //
21 // The Original Code is eCos - Embedded Configurable Operating System,
22 // released September 30, 1998.
23 //
24 // The Initial Developer of the Original Code is Red Hat.
25 // Portions created by Red Hat are
26 // Copyright (C) 1998, 1999, 2000 Red Hat, Inc.
27 // All Rights Reserved.
28 // -------------------------------------------
29 //
30 //####COPYRIGHTEND####
31 //==========================================================================
32 //#####DESCRIPTIONBEGIN####
33 //
34 // Author(s): gthomas
35 // Contributors: gthomas
36 // Date: 2000-07-14
37 // Purpose:
38 // Description:
39 //
40 // This code is part of RedBoot (tm).
41 //
42 //####DESCRIPTIONEND####
43 //
44 //==========================================================================
45
46 #include <net/net.h>
47 #include <net/bootp.h>
48
49 extern int net_debug;
50
51 #define SHOULD_BE_RANDOM 0x12345555
52
53 /* How many milliseconds to wait before retrying the request */
54 #define RETRY_TIME 2000
55
56 static bootp_header_t *bp_info;
57
58 static void
59 bootp_handler(udp_socket_t *skt, char *buf, int len,
60 ip_route_t *src_route, word src_port)
61 {
62 bootp_header_t *b;
63
64 b = (bootp_header_t *)buf;
65 if (bp_info) {
66 memcpy(bp_info, b, len);
67 }
68
69 if (b->bp_op == BOOTREPLY &&
70 !memcmp(b->bp_chaddr, __local_enet_addr, 6)) {
71 memcpy(__local_ip_addr, &b->bp_yiaddr, 4);
72 }
73 }
74
75
76 /*
77 * Find our IP address and copy to __local_ip_addr.
78 * Return zero if successful, -1 if not.
79 */
80 int
81 __bootp_find_local_ip(bootp_header_t *info)
82 {
83 udp_socket_t udp_skt;
84 bootp_header_t b;
85 ip_route_t r;
86 int retry;
87 unsigned long start;
88
89 bp_info = info;
90
91 memset(&b, 0, sizeof(b));
92
93 b.bp_op = BOOTREQUEST;
94 b.bp_htype = HTYPE_ETHERNET;
95 b.bp_hlen = 6;
96 b.bp_xid = SHOULD_BE_RANDOM;
97
98 __local_ip_addr[0] = 0;
99 __local_ip_addr[1] = 0;
100 __local_ip_addr[2] = 0;
101 __local_ip_addr[3] = 0;
102
103 memcpy(b.bp_chaddr, __local_enet_addr, 6);
104
105 /* fill out route for a broadcast */
106 r.ip_addr[0] = 255;
107 r.ip_addr[1] = 255;
108 r.ip_addr[2] = 255;
109 r.ip_addr[3] = 255;
110 r.enet_addr[0] = 255;
111 r.enet_addr[1] = 255;
112 r.enet_addr[2] = 255;
113 r.enet_addr[3] = 255;
114 r.enet_addr[4] = 255;
115 r.enet_addr[5] = 255;
116
117 /* setup a socket listener for bootp replies */
118 __udp_install_listener(&udp_skt, IPPORT_BOOTPC, bootp_handler);
119
120 retry = 3;
121 while (retry-- > 0) {
122 start = MS_TICKS();
123
124 __udp_send((char *)&b, sizeof(b), &r, IPPORT_BOOTPS, IPPORT_BOOTPC);
125
126 do {
127 __enet_poll();
128 if (__local_ip_addr[0] || __local_ip_addr[1] ||
129 __local_ip_addr[2] || __local_ip_addr[3]) {
130 /* success */
131 __udp_remove_listener(IPPORT_BOOTPC);
132 return 0;
133 }
134 } while ((MS_TICKS() - start) < RETRY_TIME);
135 }
136
137 /* timed out */
138 __udp_remove_listener(IPPORT_BOOTPC);
139 net_debug = 0;
140 return -1;
141 }
142
143