Annotation of previous_trunk/src/slirp/bootp.c, revision 1.1

1.1     ! root        1: /*
        !             2:  * QEMU BOOTP/DHCP server
        !             3:  * 
        !             4:  * Copyright (c) 2004 Fabrice Bellard
        !             5:  * 
        !             6:  * Permission is hereby granted, free of charge, to any person obtaining a copy
        !             7:  * of this software and associated documentation files (the "Software"), to deal
        !             8:  * in the Software without restriction, including without limitation the rights
        !             9:  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        !            10:  * copies of the Software, and to permit persons to whom the Software is
        !            11:  * furnished to do so, subject to the following conditions:
        !            12:  *
        !            13:  * The above copyright notice and this permission notice shall be included in
        !            14:  * all copies or substantial portions of the Software.
        !            15:  *
        !            16:  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        !            17:  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        !            18:  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
        !            19:  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        !            20:  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        !            21:  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
        !            22:  * THE SOFTWARE.
        !            23:  */
        !            24: #include <slirp.h>
        !            25: 
        !            26: /* XXX: only DHCP is supported */
        !            27: 
        !            28: #define NB_ADDR 16
        !            29: 
        !            30: #define START_ADDR 15
        !            31: 
        !            32: #define LEASE_TIME (24 * 3600)
        !            33: 
        !            34: typedef struct {
        !            35:     uint8_t allocated;
        !            36:     uint8_t macaddr[6];
        !            37: } BOOTPClient;
        !            38: 
        !            39: BOOTPClient bootp_clients[NB_ADDR];
        !            40: 
        !            41: static const uint8_t rfc1533_cookie[] = { RFC1533_COOKIE };
        !            42: #if BOOTP_VEND_NEXT
        !            43: static const uint8_t magic_next[] = {'N','e','X','T'};
        !            44: #endif
        !            45: 
        !            46: 
        !            47: static BOOTPClient *get_new_addr(struct in_addr *paddr)
        !            48: {
        !            49:     BOOTPClient *bc;
        !            50:     int i;
        !            51: 
        !            52:     for(i = 0; i < NB_ADDR; i++) {
        !            53:         if (!bootp_clients[i].allocated)
        !            54:             goto found;
        !            55:     }
        !            56:     return NULL;
        !            57:  found:
        !            58:     bc = &bootp_clients[i];
        !            59:     bc->allocated = 1;
        !            60:     paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
        !            61:     return bc;
        !            62: }
        !            63: 
        !            64: static BOOTPClient *find_addr(struct in_addr *paddr, const uint8_t *macaddr)
        !            65: {
        !            66:     BOOTPClient *bc;
        !            67:     int i;
        !            68: 
        !            69:     for(i = 0; i < NB_ADDR; i++) {
        !            70:         if (!memcmp(macaddr, bootp_clients[i].macaddr, 6))
        !            71:             goto found;
        !            72:     }
        !            73:     return NULL;
        !            74:  found:
        !            75:     bc = &bootp_clients[i];
        !            76:     bc->allocated = 1;
        !            77:     paddr->s_addr = htonl(ntohl(special_addr.s_addr) | (i + START_ADDR));
        !            78:     return bc;
        !            79: }
        !            80: 
        !            81: static void dhcp_decode(const uint8_t *buf, int size,
        !            82:                         int *pmsg_type)
        !            83: {
        !            84:     const uint8_t *p, *p_end;
        !            85:     int len, tag;
        !            86: 
        !            87:     *pmsg_type = 0;    
        !            88: 
        !            89:     p = buf;
        !            90:     p_end = buf + size;
        !            91:     if (size < 5)
        !            92:         return;
        !            93:     if (memcmp(p, rfc1533_cookie, 4) != 0)
        !            94:         return;
        !            95:     p += 4;
        !            96:     while (p < p_end) {
        !            97:         tag = p[0];
        !            98:         if (tag == RFC1533_PAD) {
        !            99:             p++; 
        !           100:         } else if (tag == RFC1533_END) {
        !           101:             break;
        !           102:         } else {
        !           103:             p++;
        !           104:             if (p >= p_end)
        !           105:                 break;
        !           106:             len = *p++;
        !           107: 
        !           108:             switch(tag) {
        !           109:             case RFC2132_MSG_TYPE:
        !           110:                 if (len >= 1)
        !           111:                     *pmsg_type = p[0];
        !           112:                 break;
        !           113:             default:
        !           114:                 break;
        !           115:             }
        !           116:             p += len;
        !           117:         }
        !           118:     }
        !           119: }
        !           120: 
        !           121: static void bootp_reply(struct bootp_t *bp)
        !           122: {
        !           123:     BOOTPClient *bc;
        !           124:     struct mbuf *m;
        !           125:     struct bootp_t *rbp;
        !           126:     struct sockaddr_in saddr, daddr;
        !           127: #if BOOTP_VEND_NEXT == 0
        !           128:     struct in_addr dns_addr;
        !           129:     int val;
        !           130: #endif
        !           131:     int dhcp_msg_type;
        !           132:     uint8_t *q;
        !           133: 
        !           134:     /* extract exact DHCP msg type */
        !           135:     dhcp_decode(bp->bp_vend, DHCP_OPT_LEN, &dhcp_msg_type);
        !           136:     
        !           137:     if (dhcp_msg_type == 0)
        !           138:         dhcp_msg_type = DHCPREQUEST; /* Force reply for old BOOTP clients */
        !           139:         
        !           140:     if (dhcp_msg_type != DHCPDISCOVER && 
        !           141:         dhcp_msg_type != DHCPREQUEST)
        !           142:         return;
        !           143:     /* XXX: this is a hack to get the client mac address */
        !           144:     memcpy(client_ethaddr, bp->bp_hwaddr, 6);
        !           145:     
        !           146:     if ((m = m_get()) == NULL)
        !           147:         return;
        !           148:     m->m_data += if_maxlinkhdr;
        !           149:     rbp = (struct bootp_t *)m->m_data;
        !           150:     m->m_data += sizeof(struct udpiphdr);
        !           151:     memset(rbp, 0, sizeof(struct bootp_t));
        !           152: 
        !           153:     if (dhcp_msg_type == DHCPDISCOVER) {
        !           154:     new_addr:
        !           155:         bc = get_new_addr(&daddr.sin_addr);
        !           156:         if (!bc)
        !           157:             return;
        !           158:         memcpy(bc->macaddr, client_ethaddr, 6);
        !           159:     } else {
        !           160:         bc = find_addr(&daddr.sin_addr, bp->bp_hwaddr);
        !           161:         if (!bc) {
        !           162:             /* if never assigned, behaves as if it was already
        !           163:                assigned (windows fix because it remembers its address) */
        !           164:             goto new_addr;
        !           165:         }
        !           166:     }
        !           167: 
        !           168:     saddr.sin_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_ALIAS);
        !           169:     saddr.sin_port = htons(BOOTP_SERVER);
        !           170: 
        !           171:     daddr.sin_port = htons(BOOTP_CLIENT);
        !           172: 
        !           173:     rbp->bp_op = BOOTP_REPLY;
        !           174:     rbp->bp_xid = bp->bp_xid;
        !           175:     rbp->bp_htype = 1;
        !           176:     rbp->bp_hlen = 6;
        !           177:     memcpy(rbp->bp_hwaddr, bp->bp_hwaddr, 6);
        !           178: 
        !           179:     rbp->bp_yiaddr = daddr.sin_addr; /* Client IP address */
        !           180:     rbp->bp_siaddr = saddr.sin_addr; /* Server IP address */
        !           181: 
        !           182:     q = rbp->bp_vend;
        !           183: #if BOOTP_VEND_NEXT
        !           184:     memcpy(q, magic_next, 4);
        !           185:     q += 4;
        !           186:     *q++ = 1;
        !           187:     *q++ = 0;
        !           188:     *q++ = 0;
        !           189:     memset(q, 0, 56);
        !           190:     q += 56;
        !           191:     *q++ = 0;
        !           192: #else
        !           193:     memcpy(q, rfc1533_cookie, 4);
        !           194:     q += 4;
        !           195:     if (dhcp_msg_type == DHCPDISCOVER) {
        !           196:         *q++ = RFC2132_MSG_TYPE;
        !           197:         *q++ = 1;
        !           198:         *q++ = DHCPOFFER;
        !           199:     } else if (dhcp_msg_type == DHCPREQUEST) {
        !           200:         *q++ = RFC2132_MSG_TYPE;
        !           201:         *q++ = 1;
        !           202:         *q++ = DHCPACK;
        !           203:     }
        !           204:         
        !           205:     if (dhcp_msg_type == DHCPDISCOVER ||
        !           206:         dhcp_msg_type == DHCPREQUEST) {
        !           207:         *q++ = RFC2132_SRV_ID;
        !           208:         *q++ = 4;
        !           209:         memcpy(q, &saddr.sin_addr, 4);
        !           210:         q += 4;
        !           211: 
        !           212:         *q++ = RFC1533_NETMASK;
        !           213:         *q++ = 4;
        !           214:         *q++ = 0xff;
        !           215:         *q++ = 0xff;
        !           216:         *q++ = 0xff;
        !           217:         *q++ = 0x00;
        !           218:         
        !           219:         *q++ = RFC1533_GATEWAY;
        !           220:         *q++ = 4;
        !           221:         memcpy(q, &saddr.sin_addr, 4);
        !           222:         q += 4;
        !           223:         
        !           224:         *q++ = RFC1533_DNS;
        !           225:         *q++ = 4;
        !           226:         dns_addr.s_addr = htonl(ntohl(special_addr.s_addr) | CTL_DNS);
        !           227:         memcpy(q, &dns_addr, 4);
        !           228:         q += 4;
        !           229: 
        !           230:         *q++ = RFC2132_LEASE_TIME;
        !           231:         *q++ = 4;
        !           232:         val = htonl(LEASE_TIME);
        !           233:         memcpy(q, &val, 4);
        !           234:         q += 4;
        !           235: 
        !           236:         if (*slirp_hostname) {
        !           237:             val = strlen(slirp_hostname);
        !           238:             *q++ = RFC1533_HOSTNAME;
        !           239:             *q++ = val;
        !           240:             memcpy(q, slirp_hostname, val);
        !           241:             q += val;
        !           242:         }
        !           243:     }
        !           244:     *q++ = RFC1533_END;
        !           245: #endif
        !           246:     m->m_len = sizeof(struct bootp_t) -
        !           247:         sizeof(struct ip) - sizeof(struct udphdr);
        !           248:     udp_output2(NULL, m, &saddr, &daddr, IPTOS_LOWDELAY);
        !           249: }
        !           250: 
        !           251: void bootp_input(struct mbuf *m)
        !           252: {
        !           253:     struct bootp_t *bp = mtod(m, struct bootp_t *);
        !           254: 
        !           255:     if (bp->bp_op == BOOTP_REQUEST) {
        !           256:         bootp_reply(bp);
        !           257:     }
        !           258: }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.