Magellan Linux

Diff of /trunk/mkinitrd-magellan/busybox/networking/udhcp/serverpacket.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 532 by niro, Sat Sep 1 22:45:15 2007 UTC revision 816 by niro, Fri Apr 24 18:33:46 2009 UTC
# Line 21  Line 21 
21   */   */
22    
23  #include "common.h"  #include "common.h"
24    #include "dhcpc.h"
25  #include "dhcpd.h"  #include "dhcpd.h"
26  #include "options.h"  #include "options.h"
27    
# Line 30  static int send_packet_to_relay(struct d Line 31  static int send_packet_to_relay(struct d
31  {  {
32   DEBUG("Forwarding packet to relay");   DEBUG("Forwarding packet to relay");
33    
34   return udhcp_kernel_packet(payload, server_config.server, SERVER_PORT,   return udhcp_send_kernel_packet(payload, server_config.server, SERVER_PORT,
35   payload->giaddr, SERVER_PORT);   payload->giaddr, SERVER_PORT);
36  }  }
37    
# Line 38  static int send_packet_to_relay(struct d Line 39  static int send_packet_to_relay(struct d
39  /* send a packet to a specific arp address and ip address by creating our own ip packet */  /* send a packet to a specific arp address and ip address by creating our own ip packet */
40  static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)  static int send_packet_to_client(struct dhcpMessage *payload, int force_broadcast)
41  {  {
42   uint8_t *chaddr;   const uint8_t *chaddr;
43   uint32_t ciaddr;   uint32_t ciaddr;
44    
45   if (force_broadcast) {   if (force_broadcast) {
# Line 49  static int send_packet_to_client(struct Line 50  static int send_packet_to_client(struct
50   DEBUG("unicasting packet to client ciaddr");   DEBUG("unicasting packet to client ciaddr");
51   ciaddr = payload->ciaddr;   ciaddr = payload->ciaddr;
52   chaddr = payload->chaddr;   chaddr = payload->chaddr;
53   } else if (ntohs(payload->flags) & BROADCAST_FLAG) {   } else if (payload->flags & htons(BROADCAST_FLAG)) {
54   DEBUG("broadcasting packet to client (requested)");   DEBUG("broadcasting packet to client (requested)");
55   ciaddr = INADDR_BROADCAST;   ciaddr = INADDR_BROADCAST;
56   chaddr = MAC_BCAST_ADDR;   chaddr = MAC_BCAST_ADDR;
# Line 58  static int send_packet_to_client(struct Line 59  static int send_packet_to_client(struct
59   ciaddr = payload->yiaddr;   ciaddr = payload->yiaddr;
60   chaddr = payload->chaddr;   chaddr = payload->chaddr;
61   }   }
62   return udhcp_raw_packet(payload, server_config.server, SERVER_PORT,   return udhcp_send_raw_packet(payload,
63   ciaddr, CLIENT_PORT, chaddr, server_config.ifindex);   /*src*/ server_config.server, SERVER_PORT,
64     /*dst*/ ciaddr, CLIENT_PORT, chaddr,
65     server_config.ifindex);
66  }  }
67    
68    
69  /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */  /* send a dhcp packet, if force broadcast is set, the packet will be broadcast to the client */
70  static int send_packet(struct dhcpMessage *payload, int force_broadcast)  static int send_packet(struct dhcpMessage *payload, int force_broadcast)
71  {  {
  int ret;  
   
72   if (payload->giaddr)   if (payload->giaddr)
73   ret = send_packet_to_relay(payload);   return send_packet_to_relay(payload);
74   else ret = send_packet_to_client(payload, force_broadcast);   return send_packet_to_client(payload, force_broadcast);
  return ret;  
75  }  }
76    
77    
# Line 99  static void add_bootp_options(struct dhc Line 99  static void add_bootp_options(struct dhc
99    
100    
101  /* send a DHCP OFFER to a DHCP DISCOVER */  /* send a DHCP OFFER to a DHCP DISCOVER */
102  int sendOffer(struct dhcpMessage *oldpacket)  int FAST_FUNC send_offer(struct dhcpMessage *oldpacket)
103  {  {
104   struct dhcpMessage packet;   struct dhcpMessage packet;
105   struct dhcpOfferedAddr *lease = NULL;   struct dhcpOfferedAddr *lease = NULL;
# Line 122  int sendOffer(struct dhcpMessage *oldpac Line 122  int sendOffer(struct dhcpMessage *oldpac
122   if (!lease_expired(lease))   if (!lease_expired(lease))
123   lease_time_align = lease->expires - time(0);   lease_time_align = lease->expires - time(0);
124   packet.yiaddr = lease->yiaddr;   packet.yiaddr = lease->yiaddr;
   
125   /* Or the client has a requested ip */   /* Or the client has a requested ip */
126   } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP))   } else if ((req = get_option(oldpacket, DHCP_REQUESTED_IP))
127   /* Don't look here (ugly hackish thing to do) */   /* Don't look here (ugly hackish thing to do) */
128   && memcpy(&req_align, req, 4)   && memcpy(&req_align, req, 4)
129   /* and the ip is in the lease range */   /* and the ip is in the lease range */
130   && ntohl(req_align) >= ntohl(server_config.start)   && ntohl(req_align) >= server_config.start_ip
131   && ntohl(req_align) <= ntohl(server_config.end)   && ntohl(req_align) <= server_config.end_ip
132   && !static_lease_ip /* Check that its not a static lease */   && !static_lease_ip /* Check that its not a static lease */
133   /* and is not already taken/offered */   /* and is not already taken/offered */
134   && (!(lease = find_lease_by_yiaddr(req_align))   && (!(lease = find_lease_by_yiaddr(req_align))
135   /* or its taken, but expired */ /* ADDME: or maybe in here */   /* or its taken, but expired */ /* ADDME: or maybe in here */
136   || lease_expired(lease))   || lease_expired(lease))
137   ) {   ) {
138   packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */   packet.yiaddr = req_align; /* FIXME: oh my, is there a host using this IP? */
139   /* otherwise, find a free IP */   /* otherwise, find a free IP */
# Line 142  int sendOffer(struct dhcpMessage *oldpac Line 141  int sendOffer(struct dhcpMessage *oldpac
141   /* Is it a static lease? (No, because find_address skips static lease) */   /* Is it a static lease? (No, because find_address skips static lease) */
142   packet.yiaddr = find_address(0);   packet.yiaddr = find_address(0);
143   /* try for an expired lease */   /* try for an expired lease */
144   if (!packet.yiaddr) packet.yiaddr = find_address(1);   if (!packet.yiaddr)
145     packet.yiaddr = find_address(1);
146   }   }
147    
148   if (!packet.yiaddr) {   if (!packet.yiaddr) {
# Line 187  int sendOffer(struct dhcpMessage *oldpac Line 187  int sendOffer(struct dhcpMessage *oldpac
187  }  }
188    
189    
190  int sendNAK(struct dhcpMessage *oldpacket)  int FAST_FUNC send_NAK(struct dhcpMessage *oldpacket)
191  {  {
192   struct dhcpMessage packet;   struct dhcpMessage packet;
193    
# Line 198  int sendNAK(struct dhcpMessage *oldpacke Line 198  int sendNAK(struct dhcpMessage *oldpacke
198  }  }
199    
200    
201  int sendACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)  int FAST_FUNC send_ACK(struct dhcpMessage *oldpacket, uint32_t yiaddr)
202  {  {
203   struct dhcpMessage packet;   struct dhcpMessage packet;
204   struct option_set *curr;   struct option_set *curr;
# Line 209  int sendACK(struct dhcpMessage *oldpacke Line 209  int sendACK(struct dhcpMessage *oldpacke
209   init_packet(&packet, oldpacket, DHCPACK);   init_packet(&packet, oldpacket, DHCPACK);
210   packet.yiaddr = yiaddr;   packet.yiaddr = yiaddr;
211    
212   if ((lease_time = get_option(oldpacket, DHCP_LEASE_TIME))) {   lease_time = get_option(oldpacket, DHCP_LEASE_TIME);
213     if (lease_time) {
214   memcpy(&lease_time_align, lease_time, 4);   memcpy(&lease_time_align, lease_time, 4);
215   lease_time_align = ntohl(lease_time_align);   lease_time_align = ntohl(lease_time_align);
216   if (lease_time_align > server_config.lease)   if (lease_time_align > server_config.lease)
# Line 236  int sendACK(struct dhcpMessage *oldpacke Line 237  int sendACK(struct dhcpMessage *oldpacke
237   return -1;   return -1;
238    
239   add_lease(packet.chaddr, packet.yiaddr, lease_time_align);   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     }
244    
245   return 0;   return 0;
246  }  }
247    
248    
249  int send_inform(struct dhcpMessage *oldpacket)  int FAST_FUNC send_inform(struct dhcpMessage *oldpacket)
250  {  {
251   struct dhcpMessage packet;   struct dhcpMessage packet;
252   struct option_set *curr;   struct option_set *curr;

Legend:
Removed from v.532  
changed lines
  Added in v.816