diff options
author | Denys Vlasenko <vda.linux@googlemail.com> | 2009-06-17 13:24:03 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2009-06-17 13:24:03 +0200 |
commit | 6947d2c7e194e3be31eed1c75260e15fca1a2568 (patch) | |
tree | ff4d67b49e0e7e02e29ecc090db619a39b24498d /networking/udhcp/serverpacket.c | |
parent | 2b0e95780863da44f6a9244699ece8620a599e19 (diff) | |
download | busybox-w32-6947d2c7e194e3be31eed1c75260e15fca1a2568.tar.gz busybox-w32-6947d2c7e194e3be31eed1c75260e15fca1a2568.tar.bz2 busybox-w32-6947d2c7e194e3be31eed1c75260e15fca1a2568.zip |
udhcp: logging improvements, field and variable renames
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'networking/udhcp/serverpacket.c')
-rw-r--r-- | networking/udhcp/serverpacket.c | 73 |
1 files changed, 35 insertions, 38 deletions
diff --git a/networking/udhcp/serverpacket.c b/networking/udhcp/serverpacket.c index 209d3c8f3..c3724e0e2 100644 --- a/networking/udhcp/serverpacket.c +++ b/networking/udhcp/serverpacket.c | |||
@@ -107,14 +107,30 @@ static void add_bootp_options(struct dhcp_packet *packet) | |||
107 | } | 107 | } |
108 | 108 | ||
109 | 109 | ||
110 | static uint32_t select_lease_time(struct dhcp_packet *packet) | ||
111 | { | ||
112 | uint32_t lease_time_sec = server_config.max_lease_sec; | ||
113 | uint8_t *lease_time_opt = get_option(packet, DHCP_LEASE_TIME); | ||
114 | if (lease_time_opt) { | ||
115 | move_from_unaligned32(lease_time_sec, lease_time_opt); | ||
116 | lease_time_sec = ntohl(lease_time_sec); | ||
117 | if (lease_time_sec > server_config.max_lease_sec) | ||
118 | lease_time_sec = server_config.max_lease_sec; | ||
119 | if (lease_time_sec < server_config.min_lease_sec) | ||
120 | lease_time_sec = server_config.min_lease_sec; | ||
121 | } | ||
122 | return lease_time_sec; | ||
123 | } | ||
124 | |||
125 | |||
110 | /* send a DHCP OFFER to a DHCP DISCOVER */ | 126 | /* send a DHCP OFFER to a DHCP DISCOVER */ |
111 | int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) | 127 | int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) |
112 | { | 128 | { |
113 | struct dhcp_packet packet; | 129 | struct dhcp_packet packet; |
114 | uint32_t req_align; | 130 | uint32_t req_nip; |
115 | uint32_t lease_time_aligned = server_config.lease; | 131 | uint32_t lease_time_sec = server_config.max_lease_sec; |
116 | uint32_t static_lease_ip; | 132 | uint32_t static_lease_ip; |
117 | uint8_t *req, *lease_time, *p_host_name; | 133 | uint8_t *req_ip_opt, *p_host_name; |
118 | struct option_set *curr; | 134 | struct option_set *curr; |
119 | struct in_addr addr; | 135 | struct in_addr addr; |
120 | 136 | ||
@@ -126,31 +142,31 @@ int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) | |||
126 | if (!static_lease_ip) { | 142 | if (!static_lease_ip) { |
127 | struct dyn_lease *lease; | 143 | struct dyn_lease *lease; |
128 | 144 | ||
129 | lease = find_lease_by_chaddr(oldpacket->chaddr); | 145 | lease = find_lease_by_mac(oldpacket->chaddr); |
130 | /* The client is in our lease/offered table */ | 146 | /* The client is in our lease/offered table */ |
131 | if (lease) { | 147 | if (lease) { |
132 | signed_leasetime_t tmp = lease->expires - time(NULL); | 148 | signed_leasetime_t tmp = lease->expires - time(NULL); |
133 | if (tmp >= 0) | 149 | if (tmp >= 0) |
134 | lease_time_aligned = tmp; | 150 | lease_time_sec = tmp; |
135 | packet.yiaddr = lease->lease_nip; | 151 | packet.yiaddr = lease->lease_nip; |
136 | } | 152 | } |
137 | /* Or the client has requested an IP */ | 153 | /* Or the client has requested an IP */ |
138 | else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL | 154 | else if ((req_ip_opt = get_option(oldpacket, DHCP_REQUESTED_IP)) != NULL |
139 | /* (read IP) */ | 155 | /* (read IP) */ |
140 | && (move_from_unaligned32(req_align, req), 1) | 156 | && (move_from_unaligned32(req_nip, req_ip_opt), 1) |
141 | /* and the IP is in the lease range */ | 157 | /* and the IP is in the lease range */ |
142 | && ntohl(req_align) >= server_config.start_ip | 158 | && ntohl(req_nip) >= server_config.start_ip |
143 | && ntohl(req_align) <= server_config.end_ip | 159 | && ntohl(req_nip) <= server_config.end_ip |
144 | /* and is not already taken/offered */ | 160 | /* and is not already taken/offered */ |
145 | && (!(lease = find_lease_by_yiaddr(req_align)) | 161 | && (!(lease = find_lease_by_nip(req_nip)) |
146 | /* or its taken, but expired */ | 162 | /* or its taken, but expired */ |
147 | || lease_expired(lease)) | 163 | || is_expired_lease(lease)) |
148 | ) { | 164 | ) { |
149 | packet.yiaddr = req_align; | 165 | packet.yiaddr = req_nip; |
150 | } | 166 | } |
151 | /* Otherwise, find a free IP */ | 167 | /* Otherwise, find a free IP */ |
152 | else { | 168 | else { |
153 | packet.yiaddr = find_free_or_expired_address(oldpacket->chaddr); | 169 | packet.yiaddr = find_free_or_expired_nip(oldpacket->chaddr); |
154 | } | 170 | } |
155 | 171 | ||
156 | if (!packet.yiaddr) { | 172 | if (!packet.yiaddr) { |
@@ -162,23 +178,13 @@ int FAST_FUNC send_offer(struct dhcp_packet *oldpacket) | |||
162 | bb_error_msg("lease pool is full - OFFER abandoned"); | 178 | bb_error_msg("lease pool is full - OFFER abandoned"); |
163 | return -1; | 179 | return -1; |
164 | } | 180 | } |
165 | lease_time = get_option(oldpacket, DHCP_LEASE_TIME); | 181 | lease_time_sec = select_lease_time(oldpacket); |
166 | if (lease_time) { | ||
167 | move_from_unaligned32(lease_time_aligned, lease_time); | ||
168 | lease_time_aligned = ntohl(lease_time_aligned); | ||
169 | if (lease_time_aligned > server_config.lease) | ||
170 | lease_time_aligned = server_config.lease; | ||
171 | } | ||
172 | |||
173 | /* Make sure we aren't just using the lease time from the previous offer */ | ||
174 | if (lease_time_aligned < server_config.min_lease) | ||
175 | lease_time_aligned = server_config.min_lease; | ||
176 | } else { | 182 | } else { |
177 | /* It is a static lease... use it */ | 183 | /* It is a static lease... use it */ |
178 | packet.yiaddr = static_lease_ip; | 184 | packet.yiaddr = static_lease_ip; |
179 | } | 185 | } |
180 | 186 | ||
181 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned)); | 187 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
182 | 188 | ||
183 | curr = server_config.options; | 189 | curr = server_config.options; |
184 | while (curr) { | 190 | while (curr) { |
@@ -210,25 +216,16 @@ int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | |||
210 | { | 216 | { |
211 | struct dhcp_packet packet; | 217 | struct dhcp_packet packet; |
212 | struct option_set *curr; | 218 | struct option_set *curr; |
213 | uint8_t *lease_time; | 219 | uint32_t lease_time_sec; |
214 | uint32_t lease_time_aligned = server_config.lease; | ||
215 | struct in_addr addr; | 220 | struct in_addr addr; |
216 | uint8_t *p_host_name; | 221 | uint8_t *p_host_name; |
217 | 222 | ||
218 | init_packet(&packet, oldpacket, DHCPACK); | 223 | init_packet(&packet, oldpacket, DHCPACK); |
219 | packet.yiaddr = yiaddr; | 224 | packet.yiaddr = yiaddr; |
220 | 225 | ||
221 | lease_time = get_option(oldpacket, DHCP_LEASE_TIME); | 226 | lease_time_sec = select_lease_time(oldpacket); |
222 | if (lease_time) { | ||
223 | move_from_unaligned32(lease_time_aligned, lease_time); | ||
224 | lease_time_aligned = ntohl(lease_time_aligned); | ||
225 | if (lease_time_aligned > server_config.lease) | ||
226 | lease_time_aligned = server_config.lease; | ||
227 | else if (lease_time_aligned < server_config.min_lease) | ||
228 | lease_time_aligned = server_config.min_lease; | ||
229 | } | ||
230 | 227 | ||
231 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_aligned)); | 228 | add_simple_option(packet.options, DHCP_LEASE_TIME, htonl(lease_time_sec)); |
232 | 229 | ||
233 | curr = server_config.options; | 230 | curr = server_config.options; |
234 | while (curr) { | 231 | while (curr) { |
@@ -246,7 +243,7 @@ int FAST_FUNC send_ACK(struct dhcp_packet *oldpacket, uint32_t yiaddr) | |||
246 | return -1; | 243 | return -1; |
247 | 244 | ||
248 | p_host_name = get_option(oldpacket, DHCP_HOST_NAME); | 245 | p_host_name = get_option(oldpacket, DHCP_HOST_NAME); |
249 | add_lease(packet.chaddr, packet.yiaddr, lease_time_aligned, p_host_name); | 246 | add_lease(packet.chaddr, packet.yiaddr, lease_time_sec, p_host_name); |
250 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { | 247 | if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { |
251 | /* rewrite the file with leases at every new acceptance */ | 248 | /* rewrite the file with leases at every new acceptance */ |
252 | write_leases(); | 249 | write_leases(); |