ppc fixes - gcc 3.4 compile fix (initial patch by Jocelyn Mayer)
[qemu] / slirp / bootp.c
index 7e4b5ba..56caf70 100644 (file)
@@ -33,6 +33,7 @@
 
 typedef struct {
     uint8_t allocated;
+    uint8_t macaddr[6];
 } BOOTPClient;
 
 BOOTPClient bootp_clients[NB_ADDR];
@@ -63,6 +64,23 @@ static BOOTPClient *get_new_addr(struct in_addr *paddr)
     return bc;
 }
 
+static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
+{
+    BOOTPClient *bc;
+    int i;
+
+    for(i = 0; i < NB_ADDR; i++) {
+        if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
+            goto found;
+    }
+    return NULL;
+ found:
+    bc = &bootp_clients[i];
+    bc->allocated = 1;
+    paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
+    return bc;
+}
+
 static void dhcp_decode(const uint8_t *buf, int size,
                         int *pmsg_type)
 {
@@ -118,6 +136,9 @@ static void bootp_reply(struct bootp_t *bp)
     dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
     dprintf("bootp packet op=%d msgtype=%d\n", bp->bp_op, dhcp_msg_type);
     
+    if (dhcp_msg_type == 0)
+        dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
+        
     if (dhcp_msg_type != DHCPDISCOVER && 
         dhcp_msg_type != DHCPREQUEST)
         return;
@@ -131,10 +152,21 @@ static void bootp_reply(struct bootp_t *bp)
     m->m_data += sizeof(struct udpiphdr);
     memset(rbp, 0, sizeof(struct bootp_t));
 
-    bc = get_new_addr(&daddr.sin_addr);
-    if (!bc) {
-        dprintf("no address left\n");
-        return;
+    if (dhcp_msg_type == DHCPDISCOVER) {
+    new_addr:
+        bc = get_new_addr(&daddr.sin_addr);
+        if (!bc) {
+            dprintf("no address left\n");
+            return;
+        }
+        memcpy(bc->macaddr, client_ethaddr, 6);
+    } else {
+        bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
+        if (!bc) {
+            /* if never assigned, behaves as if it was already
+               assigned (windows fix because it remembers its address) */
+            goto new_addr;
+        }
     }
     dprintf("offered addr=%08x\n", ntohl(daddr.sin_addr.s_addr));
 
@@ -149,7 +181,8 @@ static void bootp_reply(struct bootp_t *bp)
     rbp->bp_hlen = 6;
     memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
 
-    rbp->bp_yiaddr = daddr.sin_addr; /* IP address */
+    rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
+    rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
 
     q = rbp->bp_vend;
     memcpy(q, rfc1533_cookie, 4);
@@ -198,7 +231,8 @@ static void bootp_reply(struct bootp_t *bp)
     }
     *q++ = RFC1533_END;
     
-    m->m_len = sizeof(struct bootp_t);
+    m->m_len = sizeof(struct bootp_t) - 
+        sizeof(struct ip) - sizeof(struct udphdr);
     udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
 }