]> git.proxmox.com Git - qemu.git/blobdiff - slirp/bootp.c
slirp: Factor out internal state structure
[qemu.git] / slirp / bootp.c
index fec52c0a84a98565eac415ef917629d634700ed0..911ec712c5da2651ff985cfd61f7a4e25d3b452f 100644 (file)
 
 /* XXX: only DHCP is supported */
 
-#define NB_ADDR 16
-
 #define LEASE_TIME (24 * 3600)
 
-typedef struct {
-    uint8_t allocated;
-    uint8_t macaddr[6];
-} BOOTPClient;
-
-static BOOTPClient bootp_clients[NB_ADDR];
-
-char *bootp_filename;
-
 static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
 
 #ifdef DEBUG
@@ -47,35 +36,35 @@ do if (slirp_debug & DBG_CALL) { fprintf(dfd, fmt, ##  __VA_ARGS__); fflush(dfd)
 #define dprintf(fmt, ...)
 #endif
 
-static BOOTPClient *get_new_addr(struct in_addr *paddr,
+static BOOTPClient *get_new_addr(Slirp *slirp, struct in_addr *paddr,
                                  const uint8_t *macaddr)
 {
     BOOTPClient *bc;
     int i;
 
-    for(i = 0; i < NB_ADDR; i++) {
-        bc = &bootp_clients[i];
+    for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
+        bc = &slirp->bootp_clients[i];
         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6))
             goto found;
     }
     return NULL;
  found:
-    bc = &bootp_clients[i];
+    bc = &slirp->bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
+    paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
     return bc;
 }
 
-static BOOTPClient *request_addr(const struct in_addr *paddr,
+static BOOTPClient *request_addr(Slirp *slirp, const struct in_addr *paddr,
                                  const uint8_t *macaddr)
 {
     uint32_t req_addr = ntohl(paddr->s_addr);
-    uint32_t dhcp_addr = ntohl(vdhcp_startaddr.s_addr);
+    uint32_t dhcp_addr = ntohl(slirp->vdhcp_startaddr.s_addr);
     BOOTPClient *bc;
 
     if (req_addr >= dhcp_addr &&
-        req_addr < (dhcp_addr + NB_ADDR)) {
-        bc = &bootp_clients[req_addr - dhcp_addr];
+        req_addr < (dhcp_addr + NB_BOOTP_CLIENTS)) {
+        bc = &slirp->bootp_clients[req_addr - dhcp_addr];
         if (!bc->allocated || !memcmp(macaddr, bc->macaddr, 6)) {
             bc->allocated = 1;
             return bc;
@@ -84,20 +73,21 @@ static BOOTPClient *request_addr(const struct in_addr *paddr,
     return NULL;
 }
 
-static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
+static BOOTPClient *find_addr(Slirp *slirp, 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))
+    for(i = 0; i < NB_BOOTP_CLIENTS; i++) {
+        if (!memcmp(macaddr, slirp->bootp_clients[i].macaddr, 6))
             goto found;
     }
     return NULL;
  found:
-    bc = &bootp_clients[i];
+    bc = &slirp->bootp_clients[i];
     bc->allocated = 1;
-    paddr->s_addr = vdhcp_startaddr.s_addr + htonl(i);
+    paddr->s_addr = slirp->vdhcp_startaddr.s_addr + htonl(i);
     return bc;
 }
 
@@ -148,7 +138,7 @@ static void dhcp_decode(const struct bootp_t *bp, int *pmsg_type,
     }
 }
 
-static void bootp_reply(const struct bootp_t *bp)
+static void bootp_reply(Slirp *slirp, const struct bootp_t *bp)
 {
     BOOTPClient *bc = NULL;
     struct mbuf *m;
@@ -173,10 +163,12 @@ static void bootp_reply(const struct bootp_t *bp)
         dhcp_msg_type != DHCPREQUEST)
         return;
     /* XXX: this is a hack to get the client mac address */
-    memcpy(client_ethaddr, bp->bp_hwaddr, 6);
+    memcpy(slirp->client_ethaddr, bp->bp_hwaddr, 6);
 
-    if ((m = m_get()) == NULL)
+    m = m_get(slirp);
+    if (!m) {
         return;
+    }
     m->m_data += IF_MAXLINKHDR;
     rbp = (struct bootp_t *)m->m_data;
     m->m_data += sizeof(struct udpiphdr);
@@ -184,30 +176,30 @@ static void bootp_reply(const struct bootp_t *bp)
 
     if (dhcp_msg_type == DHCPDISCOVER) {
         if (preq_addr) {
-            bc = request_addr(preq_addr, client_ethaddr);
+            bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
             if (bc) {
                 daddr.sin_addr = *preq_addr;
             }
         }
         if (!bc) {
          new_addr:
-            bc = get_new_addr(&daddr.sin_addr, client_ethaddr);
+            bc = get_new_addr(slirp, &daddr.sin_addr, slirp->client_ethaddr);
             if (!bc) {
                 dprintf("no address left\n");
                 return;
             }
         }
-        memcpy(bc->macaddr, client_ethaddr, 6);
+        memcpy(bc->macaddr, slirp->client_ethaddr, 6);
     } else if (preq_addr) {
-        bc = request_addr(preq_addr, client_ethaddr);
+        bc = request_addr(slirp, preq_addr, slirp->client_ethaddr);
         if (bc) {
             daddr.sin_addr = *preq_addr;
-            memcpy(bc->macaddr, client_ethaddr, 6);
+            memcpy(bc->macaddr, slirp->client_ethaddr, 6);
         } else {
             daddr.sin_addr.s_addr = 0;
         }
     } else {
-        bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
+        bc = find_addr(slirp, &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) */
@@ -215,7 +207,7 @@ static void bootp_reply(const struct bootp_t *bp)
         }
     }
 
-    saddr.sin_addr = vhost_addr;
+    saddr.sin_addr = slirp->vhost_addr;
     saddr.sin_port = htons(BOOTP_SERVER);
 
     daddr.sin_port = htons(BOOTP_CLIENT);
@@ -248,9 +240,9 @@ static void bootp_reply(const struct bootp_t *bp)
             *q++ = DHCPACK;
         }
 
-        if (bootp_filename)
+        if (slirp->bootp_filename)
             snprintf((char *)rbp->bp_file, sizeof(rbp->bp_file), "%s",
-                     bootp_filename);
+                     slirp->bootp_filename);
 
         *q++ = RFC2132_SRV_ID;
         *q++ = 4;
@@ -259,10 +251,10 @@ static void bootp_reply(const struct bootp_t *bp)
 
         *q++ = RFC1533_NETMASK;
         *q++ = 4;
-        memcpy(q, &vnetwork_mask, 4);
+        memcpy(q, &slirp->vnetwork_mask, 4);
         q += 4;
 
-        if (!slirp_restrict) {
+        if (!slirp->restricted) {
             *q++ = RFC1533_GATEWAY;
             *q++ = 4;
             memcpy(q, &saddr.sin_addr, 4);
@@ -270,7 +262,7 @@ static void bootp_reply(const struct bootp_t *bp)
 
             *q++ = RFC1533_DNS;
             *q++ = 4;
-            memcpy(q, &vnameserver_addr, 4);
+            memcpy(q, &slirp->vnameserver_addr, 4);
             q += 4;
         }
 
@@ -280,11 +272,11 @@ static void bootp_reply(const struct bootp_t *bp)
         memcpy(q, &val, 4);
         q += 4;
 
-        if (*slirp_hostname) {
-            val = strlen(slirp_hostname);
+        if (*slirp->client_hostname) {
+            val = strlen(slirp->client_hostname);
             *q++ = RFC1533_HOSTNAME;
             *q++ = val;
-            memcpy(q, slirp_hostname, val);
+            memcpy(q, slirp->client_hostname, val);
             q += val;
         }
     } else {
@@ -315,6 +307,6 @@ void bootp_input(struct mbuf *m)
     struct bootp_t *bp = mtod(m, struct bootp_t *);
 
     if (bp->bp_op == BOOTP_REQUEST) {
-        bootp_reply(bp);
+        bootp_reply(m->slirp, bp);
     }
 }