gonzui


Format: Advanced Search

t2ex/bsd_source/kernel/sysmain/src/network_sample/dhcp.hbare sourcepermlink (0.03 seconds)

Search this content:

    1: /*
    2:  * dhcpcd - DHCP client daemon
    3:  * Copyright (c) 2006-2008 Roy Marples <roy@marples.name>
    4:  * All rights reserved
    5: 
    6:  * Redistribution and use in source and binary forms, with or without
    7:  * modification, are permitted provided that the following conditions
    8:  * are met:
    9:  * 1. Redistributions of source code must retain the above copyright
   10:  *    notice, this list of conditions and the following disclaimer.
   11:  * 2. Redistributions in binary form must reproduce the above copyright
   12:  *    notice, this list of conditions and the following disclaimer in the
   13:  *    documentation and/or other materials provided with the distribution.
   14:  *
   15:  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   16:  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   17:  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   18:  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   19:  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   20:  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   21:  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   22:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   23:  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   24:  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   25:  * SUCH DAMAGE.
   26:  */
   27: 
   28: #ifndef DHCP_H
   29: #define DHCP_H
   30: 
   31: #ifndef T2EX
   32: #define T2EX
   33: #endif
   34: 
   35: #ifndef T2EX
   36: #include <arpa/inet.h>
   37: #include <netinet/in.h>
   38: 
   39: #include <stdint.h>
   40: 
   41: #include "common.h"
   42: #else
   43: #include <t2ex/socket.h>
   44: #endif
   45: 
   46: /* Max MTU - defines dhcp option length */
   47: #define MTU_MAX             1500
   48: #define MTU_MIN             576
   49: 
   50: /* UDP port numbers for DHCP */
   51: #define DHCP_SERVER_PORT    67
   52: #define DHCP_CLIENT_PORT    68
   53: 
   54: #define MAGIC_COOKIE        0x63825363
   55: #define BROADCAST_FLAG      0x8000
   56: 
   57: /* DHCP message OP code */
   58: #define DHCP_BOOTREQUEST    1
   59: #define DHCP_BOOTREPLY      2
   60: 
   61: /* DHCP message type */
   62: #define DHCP_DISCOVER       1
   63: #define DHCP_OFFER          2
   64: #define DHCP_REQUEST        3
   65: #define DHCP_DECLINE        4
   66: #define DHCP_ACK            5
   67: #define DHCP_NAK            6
   68: #define DHCP_RELEASE        7
   69: #define DHCP_INFORM         8
   70: 
   71: /* Constants taken from RFC 2131. */
   72: #define T1                      0.5
   73: #define T2                      0.875
   74: #define DHCP_BASE               4
   75: #define DHCP_MAX                64
   76: #define DHCP_RAND_MIN           -1
   77: #define DHCP_RAND_MAX           1
   78: #define DHCP_ARP_FAIL           10
   79: 
   80: /* number of usecs in a second. */
   81: #define USECS_SECOND            1000000
   82: /* As we use timevals, we should use the usec part for
   83:  * greater randomisation. */
   84: #define DHCP_RAND_MIN_U         DHCP_RAND_MIN * USECS_SECOND
   85: #define DHCP_RAND_MAX_U         DHCP_RAND_MAX * USECS_SECOND
   86: #define PROBE_MIN_U             PROBE_MIN * USECS_SECOND
   87: #define PROBE_MAX_U             PROBE_MAX * USECS_SECOND
   88: 
   89: /* DHCP options */
   90: enum DHO {
   91:         DHO_PAD                    = 0,
   92:         DHO_SUBNETMASK             = 1,
   93:         DHO_ROUTER                 = 3,
   94:         DHO_DNSSERVER              = 6,
   95:         DHO_HOSTNAME               = 12,
   96:         DHO_DNSDOMAIN              = 15,
   97:         DHO_MTU                    = 26,
   98:         DHO_BROADCAST              = 28,
   99:         DHO_STATICROUTE            = 33,
  100:         DHO_NISDOMAIN              = 40,
  101:         DHO_NISSERVER              = 41,
  102:         DHO_NTPSERVER              = 42,
  103:         DHO_VENDOR                 = 43,
  104:         DHO_IPADDRESS              = 50,
  105:         DHO_LEASETIME              = 51,
  106:         DHO_OPTIONSOVERLOADED      = 52,
  107:         DHO_MESSAGETYPE            = 53,
  108:         DHO_SERVERID               = 54,
  109:         DHO_PARAMETERREQUESTLIST   = 55,
  110:         DHO_MESSAGE                = 56,
  111:         DHO_MAXMESSAGESIZE         = 57,
  112:         DHO_RENEWALTIME            = 58,
  113:         DHO_REBINDTIME             = 59,
  114:         DHO_VENDORCLASSID          = 60,
  115:         DHO_CLIENTID               = 61,
  116:         DHO_USERCLASS              = 77,  /* RFC 3004 */
  117:         DHO_FQDN                   = 81,
  118:         DHO_DNSSEARCH              = 119, /* RFC 3397 */
  119:         DHO_CSR                    = 121, /* RFC 3442 */
  120:         DHO_MSCSR                  = 249, /* MS code for RFC 3442 */
  121:         DHO_END                    = 255
  122: };
  123: 
  124: /* FQDN values - lsnybble used in flags
  125:  * hsnybble to create order
  126:  * and to allow 0x00 to mean disable
  127:  */
  128: enum FQDN {
  129:         FQDN_DISABLE    = 0x00,
  130:         FQDN_NONE       = 0x18,
  131:         FQDN_PTR        = 0x20,
  132:         FQDN_BOTH       = 0x31
  133: };
  134: 
  135: /* Sizes for DHCP options */
  136: #define DHCP_CHADDR_LEN         16
  137: #define SERVERNAME_LEN          64
  138: #define BOOTFILE_LEN            128
  139: #define DHCP_UDP_LEN            (14 + 20 + 8)
  140: #define DHCP_FIXED_LEN          (DHCP_UDP_LEN + 226)
  141: #define DHCP_OPTION_LEN         (MTU_MAX - DHCP_FIXED_LEN)
  142: 
  143: /* Some crappy DHCP servers require the BOOTP minimum length */
  144: #define BOOTP_MESSAGE_LENTH_MIN 300
  145: 
  146: struct dhcp_message {
  147:         uint8_t op;           /* message type */
  148:         uint8_t hwtype;       /* hardware address type */
  149:         uint8_t hwlen;        /* hardware address length */
  150:         uint8_t hwopcount;    /* should be zero in client message */
  151:         uint32_t xid;            /* transaction id */
  152:         uint16_t secs;           /* elapsed time in sec. from boot */
  153:         uint16_t flags;
  154:         uint32_t ciaddr;         /* (previously allocated) client IP */
  155:         uint32_t yiaddr;         /* 'your' client IP address */
  156:         uint32_t siaddr;         /* should be zero in client's messages */
  157:         uint32_t giaddr;         /* should be zero in client's messages */
  158:         uint8_t chaddr[DHCP_CHADDR_LEN];  /* client's hardware address */
  159:         uint8_t servername[SERVERNAME_LEN];    /* server host name */
  160:         uint8_t bootfile[BOOTFILE_LEN];    /* boot file name */
  161:         uint32_t cookie;
  162:         uint8_t options[DHCP_OPTION_LEN]; /* message options - cookie */
  163: } _packed;
  164: 
  165: struct dhcp_lease {
  166:         struct in_addr addr;
  167:         struct in_addr net;
  168:         struct in_addr brd;
  169:         uint32_t leasetime;
  170:         uint32_t renewaltime;
  171:         uint32_t rebindtime;
  172:         struct in_addr server;
  173:         time_t leasedfrom;
  174:         struct timeval boundtime;
  175:         uint8_t frominfo;
  176: };
  177: 
  178: #ifndef T2EX
  179: #include "dhcpcd.h"
  180: #include "if-options.h"
  181: #include "net.h"
  182: 
  183: #define add_option_mask(var, val) (var[val >> 3] |= 1 << (val & 7))
  184: #define del_option_mask(var, val) (var[val >> 3] &= ~(1 << (val & 7)))
  185: #define has_option_mask(var, val) (var[val >> 3] & (1 << (val & 7)))
  186: int make_option_mask(uint8_t *, const char *, int);
  187: void print_options(void);
  188: char *get_option_string(const struct dhcp_message *, uint8_t);
  189: int get_option_addr(struct in_addr *, const struct dhcp_message *, uint8_t);
  190: int get_option_uint32(uint32_t *, const struct dhcp_message *, uint8_t);
  191: int get_option_uint16(uint16_t *, const struct dhcp_message *, uint8_t);
  192: int get_option_uint8(uint8_t *, const struct dhcp_message *, uint8_t);
  193: #define is_bootp(m) (m &&                                               \
  194:             !IN_LINKLOCAL(htonl((m)->yiaddr)) &&                       \
  195:             get_option_uint8(NULL, m, DHO_MESSAGETYPE) == -1)
  196: struct rt *get_option_routes(const char *, const struct dhcp_message *);
  197: ssize_t configure_env(char **, const char *, const struct dhcp_message *,
  198:     const struct if_options *);
  199: 
  200: int dhcp_message_add_addr(struct dhcp_message *, uint8_t, struct in_addr);
  201: ssize_t make_message(struct dhcp_message **, const struct interface *,
  202:     uint8_t);
  203: int valid_dhcp_packet(unsigned char *);
  204: 
  205: ssize_t write_lease(const struct interface *, const struct dhcp_message *);
  206: struct dhcp_message *read_lease(const struct interface *);
  207: void get_lease(struct dhcp_lease *, const struct dhcp_message *);
  208: #endif
  209: 
  210: #endif