Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/networking/udhcp/serverpacket.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/networking/udhcp/serverpacket.c
r821 r1770 1 /* vi: set sw=4 ts=4: */ 1 2 /* serverpacket.c 2 3 * … … 20 21 */ 21 22 22 #include <sys/socket.h>23 #include <netinet/in.h>24 #include <arpa/inet.h>25 #include <string.h>26 #include <time.h>27 28 23 #include "common.h" 29 #include "serverpacket.h"30 24 #include "dhcpd.h" 31 25 #include "options.h" 32 #include "static_leases.h" 26 33 27 34 28 /* send a packet to giaddr using the kernel ip stack */ 35 29 static int send_packet_to_relay(struct dhcpMessage *payload) 36 30 { 37 DEBUG( LOG_INFO,"Forwarding packet to relay");31 DEBUG("Forwarding packet to relay"); 38 32 39 33 return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT, … … 45 39 static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast) 46 40 { 47 uint8_t *chaddr;41 const uint8_t *chaddr; 48 42 uint32_t ciaddr; 49 43 50 44 if (force_broadcast) { 51 DEBUG( LOG_INFO,"broadcasting packet to client (NAK)");45 DEBUG("broadcasting packet to client (NAK)"); 52 46 ciaddr = INADDR_BROADCAST; 53 47 chaddr = MAC_BCAST_ADDR; 54 48 } else if (payload->ciaddr) { 55 DEBUG( LOG_INFO,"unicasting packet to client ciaddr");49 DEBUG("unicasting packet to client ciaddr"); 56 50 ciaddr = payload->ciaddr; 57 51 chaddr = payload->chaddr; 58 52 } else if (ntohs(payload->flags) & BROADCAST_FLAG) { 59 DEBUG( LOG_INFO,"broadcasting packet to client (requested)");53 DEBUG("broadcasting packet to client (requested)"); 60 54 ciaddr = INADDR_BROADCAST; 61 55 chaddr = MAC_BCAST_ADDR; 62 56 } else { 63 DEBUG( LOG_INFO,"unicasting packet to client yiaddr");57 DEBUG("unicasting packet to client yiaddr"); 64 58 ciaddr = payload->yiaddr; 65 59 chaddr = payload->chaddr; … … 122 116 123 117 /* ADDME: if static, short circuit */ 124 if(!static_lease_ip) 125 { 126 /* the client is in our lease/offered table */ 127 if ((lease = find_lease_by_chaddr(oldpacket->chaddr))) { 128 if (!lease_expired(lease)) 129 lease_time_align = lease->expires - time(0); 130 packet.yiaddr = lease->yiaddr; 131 132 /* Or the client has a requested ip */ 133 } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) && 134 135 /* Don't look here (ugly hackish thing to do) */ 136 memcpy(&req_align, req, 4) && 137 138 /* and the ip is in the lease range */ 139 ntohl(req_align) >= ntohl(server_config.start) && 140 ntohl(req_align) <= ntohl(server_config.end) && 141 142 !static_lease_ip && /* Check that its not a static lease */ 143 /* and is not already taken/offered */ 144 ((!(lease = find_lease_by_yiaddr(req_align)) || 145 146 /* or its taken, but expired */ /* ADDME: or maybe in here */ 147 lease_expired(lease)))) { 148 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */ 149 118 if (!static_lease_ip) { 119 /* the client is in our lease/offered table */ 120 lease = find_lease_by_chaddr(oldpacket->chaddr); 121 if (lease) { 122 if (!lease_expired(lease)) 123 lease_time_align = lease->expires - time(0); 124 packet.yiaddr = lease->yiaddr; 125 /* Or the client has a requested ip */ 126 } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP)) 127 /* Don't look here (ugly hackish thing to do) */ 128 && memcpy(&req_align, req, 4) 129 /* and the ip is in the lease range */ 130 && ntohl(req_align) >= server_config.start_ip 131 && ntohl(req_align) <= server_config.end_ip 132 && !static_lease_ip /* Check that its not a static lease */ 133 /* and is not already taken/offered */ 134 && (!(lease = find_lease_by_yiaddr(req_align)) 135 /* or its taken, but expired */ /* ADDME: or maybe in here */ 136 || lease_expired(lease)) 137 ) { 138 packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */ 150 139 /* otherwise, find a free IP */ 140 } else { 141 /* Is it a static lease? (No, because find_address skips static lease) */ 142 packet.yiaddr = find_address(0); 143 /* try for an expired lease */ 144 if (!packet.yiaddr) 145 packet.yiaddr = find_address(1); 146 } 147 148 if (!packet.yiaddr) { 149 bb_error_msg("no IP addresses to give - OFFER abandoned"); 150 return -1; 151 } 152 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) { 153 bb_error_msg("lease pool is full - OFFER abandoned"); 154 return -1; 155 } 156 lease_time = get_option(oldpacket, DHCP_LEASE_TIME); 157 if (lease_time) { 158 memcpy(&lease_time_align, lease_time, 4); 159 lease_time_align = ntohl(lease_time_align); 160 if (lease_time_align > server_config.lease) 161 lease_time_align = server_config.lease; 162 } 163 164 /* Make sure we aren't just using the lease time from the previous offer */ 165 if (lease_time_align < server_config.min_lease) 166 lease_time_align = server_config.lease; 167 /* ADDME: end of short circuit */ 151 168 } else { 152 /* Is it a static lease? (No, because find_address skips static lease) */153 packet.yiaddr = find_address(0);154 155 /* try for an expired lease */156 if (!packet.yiaddr) packet.yiaddr = find_address(1);157 }158 159 if(!packet.yiaddr) {160 LOG(LOG_WARNING, "no IP addresses to give -- OFFER abandoned");161 return -1;162 }163 164 if (!add_lease(packet.chaddr, packet.yiaddr, server_config.offer_time)) {165 LOG(LOG_WARNING, "lease pool is full -- OFFER abandoned");166 return -1;167 }168 169 if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {170 memcpy(&lease_time_align, lease_time, 4);171 lease_time_align = ntohl(lease_time_align);172 if (lease_time_align > server_config.lease)173 lease_time_align = server_config.lease;174 }175 176 /* Make sure we aren't just using the lease time from the previous offer */177 if (lease_time_align < server_config.min_lease)178 lease_time_align = server_config.lease;179 }180 /* ADDME: end of short circuit */181 else182 {183 169 /* It is a static lease... use it */ 184 170 packet.yiaddr = static_lease_ip; … … 197 183 198 184 addr.s_addr = packet.yiaddr; 199 LOG(LOG_INFO, "sending OFFER of %s", inet_ntoa(addr));185 bb_info_msg("Sending OFFER of %s", inet_ntoa(addr)); 200 186 return send_packet(&packet, 0); 201 187 } … … 208 194 init_packet(&packet, oldpacket, DHCPNAK); 209 195 210 DEBUG( LOG_INFO, "sending NAK");196 DEBUG("Sending NAK"); 211 197 return send_packet(&packet, 1); 212 198 } … … 224 210 packet.yiaddr = yiaddr; 225 211 226 if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) { 212 lease_time = get_option(oldpacket, DHCP_LEASE_TIME); 213 if (lease_time) { 227 214 memcpy(&lease_time_align, lease_time, 4); 228 215 lease_time_align = ntohl(lease_time_align); … … 245 232 246 233 addr.s_addr = packet.yiaddr; 247 LOG(LOG_INFO, "sending ACK to %s", inet_ntoa(addr));234 bb_info_msg("Sending ACK to %s", inet_ntoa(addr)); 248 235 249 236 if (send_packet(&packet, 0) < 0) … … 251 238 252 239 add_lease(packet.chaddr, packet.yiaddr, lease_time_align); 240 if (ENABLE_FEATURE_UDHCPD_WRITE_LEASES_EARLY) { 241 /* rewrite the file with leases at every new acceptance */ 242 write_leases(); 243 } 253 244 254 245 return 0;
Note:
See TracChangeset
for help on using the changeset viewer.