These are the examples provided for [[/users/frank26080115/libraries/LPC1700CMSIS_Lib/]] Note, the entire "program" is not compilable!
EMAC/uIP/apps/dhcpc/dhcpc.c@0:bf7b9fba3924, 2011-03-20 (annotated)
- Committer:
- frank26080115
- Date:
- Sun Mar 20 05:38:56 2011 +0000
- Revision:
- 0:bf7b9fba3924
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
frank26080115 | 0:bf7b9fba3924 | 1 | /* |
frank26080115 | 0:bf7b9fba3924 | 2 | * Copyright (c) 2005, Swedish Institute of Computer Science |
frank26080115 | 0:bf7b9fba3924 | 3 | * All rights reserved. |
frank26080115 | 0:bf7b9fba3924 | 4 | * |
frank26080115 | 0:bf7b9fba3924 | 5 | * Redistribution and use in source and binary forms, with or without |
frank26080115 | 0:bf7b9fba3924 | 6 | * modification, are permitted provided that the following conditions |
frank26080115 | 0:bf7b9fba3924 | 7 | * are met: |
frank26080115 | 0:bf7b9fba3924 | 8 | * 1. Redistributions of source code must retain the above copyright |
frank26080115 | 0:bf7b9fba3924 | 9 | * notice, this list of conditions and the following disclaimer. |
frank26080115 | 0:bf7b9fba3924 | 10 | * 2. Redistributions in binary form must reproduce the above copyright |
frank26080115 | 0:bf7b9fba3924 | 11 | * notice, this list of conditions and the following disclaimer in the |
frank26080115 | 0:bf7b9fba3924 | 12 | * documentation and/or other materials provided with the distribution. |
frank26080115 | 0:bf7b9fba3924 | 13 | * 3. Neither the name of the Institute nor the names of its contributors |
frank26080115 | 0:bf7b9fba3924 | 14 | * may be used to endorse or promote products derived from this software |
frank26080115 | 0:bf7b9fba3924 | 15 | * without specific prior written permission. |
frank26080115 | 0:bf7b9fba3924 | 16 | * |
frank26080115 | 0:bf7b9fba3924 | 17 | * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND |
frank26080115 | 0:bf7b9fba3924 | 18 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
frank26080115 | 0:bf7b9fba3924 | 19 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
frank26080115 | 0:bf7b9fba3924 | 20 | * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE |
frank26080115 | 0:bf7b9fba3924 | 21 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
frank26080115 | 0:bf7b9fba3924 | 22 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
frank26080115 | 0:bf7b9fba3924 | 23 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
frank26080115 | 0:bf7b9fba3924 | 24 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
frank26080115 | 0:bf7b9fba3924 | 25 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
frank26080115 | 0:bf7b9fba3924 | 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
frank26080115 | 0:bf7b9fba3924 | 27 | * SUCH DAMAGE. |
frank26080115 | 0:bf7b9fba3924 | 28 | * |
frank26080115 | 0:bf7b9fba3924 | 29 | * This file is part of the uIP TCP/IP stack |
frank26080115 | 0:bf7b9fba3924 | 30 | * |
frank26080115 | 0:bf7b9fba3924 | 31 | * @(#)$Id: dhcpc.c,v 1.2 2006/06/11 21:46:37 adam Exp $ |
frank26080115 | 0:bf7b9fba3924 | 32 | */ |
frank26080115 | 0:bf7b9fba3924 | 33 | |
frank26080115 | 0:bf7b9fba3924 | 34 | #include <stdio.h> |
frank26080115 | 0:bf7b9fba3924 | 35 | #include <string.h> |
frank26080115 | 0:bf7b9fba3924 | 36 | |
frank26080115 | 0:bf7b9fba3924 | 37 | #include "uip.h" |
frank26080115 | 0:bf7b9fba3924 | 38 | #include "dhcpc.h" |
frank26080115 | 0:bf7b9fba3924 | 39 | #include "timer.h" |
frank26080115 | 0:bf7b9fba3924 | 40 | #include "pt.h" |
frank26080115 | 0:bf7b9fba3924 | 41 | |
frank26080115 | 0:bf7b9fba3924 | 42 | #define STATE_INITIAL 0 |
frank26080115 | 0:bf7b9fba3924 | 43 | #define STATE_SENDING 1 |
frank26080115 | 0:bf7b9fba3924 | 44 | #define STATE_OFFER_RECEIVED 2 |
frank26080115 | 0:bf7b9fba3924 | 45 | #define STATE_CONFIG_RECEIVED 3 |
frank26080115 | 0:bf7b9fba3924 | 46 | |
frank26080115 | 0:bf7b9fba3924 | 47 | static struct dhcpc_state s; |
frank26080115 | 0:bf7b9fba3924 | 48 | |
frank26080115 | 0:bf7b9fba3924 | 49 | struct dhcp_msg { |
frank26080115 | 0:bf7b9fba3924 | 50 | u8_t op, htype, hlen, hops; |
frank26080115 | 0:bf7b9fba3924 | 51 | u8_t xid[4]; |
frank26080115 | 0:bf7b9fba3924 | 52 | u16_t secs, flags; |
frank26080115 | 0:bf7b9fba3924 | 53 | u8_t ciaddr[4]; |
frank26080115 | 0:bf7b9fba3924 | 54 | u8_t yiaddr[4]; |
frank26080115 | 0:bf7b9fba3924 | 55 | u8_t siaddr[4]; |
frank26080115 | 0:bf7b9fba3924 | 56 | u8_t giaddr[4]; |
frank26080115 | 0:bf7b9fba3924 | 57 | u8_t chaddr[16]; |
frank26080115 | 0:bf7b9fba3924 | 58 | #ifndef UIP_CONF_DHCP_LIGHT |
frank26080115 | 0:bf7b9fba3924 | 59 | u8_t sname[64]; |
frank26080115 | 0:bf7b9fba3924 | 60 | u8_t file[128]; |
frank26080115 | 0:bf7b9fba3924 | 61 | #endif |
frank26080115 | 0:bf7b9fba3924 | 62 | u8_t options[312]; |
frank26080115 | 0:bf7b9fba3924 | 63 | }; |
frank26080115 | 0:bf7b9fba3924 | 64 | |
frank26080115 | 0:bf7b9fba3924 | 65 | #define BOOTP_BROADCAST 0x8000 |
frank26080115 | 0:bf7b9fba3924 | 66 | |
frank26080115 | 0:bf7b9fba3924 | 67 | #define DHCP_REQUEST 1 |
frank26080115 | 0:bf7b9fba3924 | 68 | #define DHCP_REPLY 2 |
frank26080115 | 0:bf7b9fba3924 | 69 | #define DHCP_HTYPE_ETHERNET 1 |
frank26080115 | 0:bf7b9fba3924 | 70 | #define DHCP_HLEN_ETHERNET 6 |
frank26080115 | 0:bf7b9fba3924 | 71 | #define DHCP_MSG_LEN 236 |
frank26080115 | 0:bf7b9fba3924 | 72 | |
frank26080115 | 0:bf7b9fba3924 | 73 | #define DHCPC_SERVER_PORT 67 |
frank26080115 | 0:bf7b9fba3924 | 74 | #define DHCPC_CLIENT_PORT 68 |
frank26080115 | 0:bf7b9fba3924 | 75 | |
frank26080115 | 0:bf7b9fba3924 | 76 | #define DHCPDISCOVER 1 |
frank26080115 | 0:bf7b9fba3924 | 77 | #define DHCPOFFER 2 |
frank26080115 | 0:bf7b9fba3924 | 78 | #define DHCPREQUEST 3 |
frank26080115 | 0:bf7b9fba3924 | 79 | #define DHCPDECLINE 4 |
frank26080115 | 0:bf7b9fba3924 | 80 | #define DHCPACK 5 |
frank26080115 | 0:bf7b9fba3924 | 81 | #define DHCPNAK 6 |
frank26080115 | 0:bf7b9fba3924 | 82 | #define DHCPRELEASE 7 |
frank26080115 | 0:bf7b9fba3924 | 83 | |
frank26080115 | 0:bf7b9fba3924 | 84 | #define DHCP_OPTION_SUBNET_MASK 1 |
frank26080115 | 0:bf7b9fba3924 | 85 | #define DHCP_OPTION_ROUTER 3 |
frank26080115 | 0:bf7b9fba3924 | 86 | #define DHCP_OPTION_DNS_SERVER 6 |
frank26080115 | 0:bf7b9fba3924 | 87 | #define DHCP_OPTION_REQ_IPADDR 50 |
frank26080115 | 0:bf7b9fba3924 | 88 | #define DHCP_OPTION_LEASE_TIME 51 |
frank26080115 | 0:bf7b9fba3924 | 89 | #define DHCP_OPTION_MSG_TYPE 53 |
frank26080115 | 0:bf7b9fba3924 | 90 | #define DHCP_OPTION_SERVER_ID 54 |
frank26080115 | 0:bf7b9fba3924 | 91 | #define DHCP_OPTION_REQ_LIST 55 |
frank26080115 | 0:bf7b9fba3924 | 92 | #define DHCP_OPTION_END 255 |
frank26080115 | 0:bf7b9fba3924 | 93 | |
frank26080115 | 0:bf7b9fba3924 | 94 | static const u8_t xid[4] = {0xad, 0xde, 0x12, 0x23}; |
frank26080115 | 0:bf7b9fba3924 | 95 | static const u8_t magic_cookie[4] = {99, 130, 83, 99}; |
frank26080115 | 0:bf7b9fba3924 | 96 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 97 | static u8_t * |
frank26080115 | 0:bf7b9fba3924 | 98 | add_msg_type(u8_t *optptr, u8_t type) |
frank26080115 | 0:bf7b9fba3924 | 99 | { |
frank26080115 | 0:bf7b9fba3924 | 100 | *optptr++ = DHCP_OPTION_MSG_TYPE; |
frank26080115 | 0:bf7b9fba3924 | 101 | *optptr++ = 1; |
frank26080115 | 0:bf7b9fba3924 | 102 | *optptr++ = type; |
frank26080115 | 0:bf7b9fba3924 | 103 | return optptr; |
frank26080115 | 0:bf7b9fba3924 | 104 | } |
frank26080115 | 0:bf7b9fba3924 | 105 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 106 | static u8_t * |
frank26080115 | 0:bf7b9fba3924 | 107 | add_server_id(u8_t *optptr) |
frank26080115 | 0:bf7b9fba3924 | 108 | { |
frank26080115 | 0:bf7b9fba3924 | 109 | *optptr++ = DHCP_OPTION_SERVER_ID; |
frank26080115 | 0:bf7b9fba3924 | 110 | *optptr++ = 4; |
frank26080115 | 0:bf7b9fba3924 | 111 | memcpy(optptr, s.serverid, 4); |
frank26080115 | 0:bf7b9fba3924 | 112 | return optptr + 4; |
frank26080115 | 0:bf7b9fba3924 | 113 | } |
frank26080115 | 0:bf7b9fba3924 | 114 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 115 | static u8_t * |
frank26080115 | 0:bf7b9fba3924 | 116 | add_req_ipaddr(u8_t *optptr) |
frank26080115 | 0:bf7b9fba3924 | 117 | { |
frank26080115 | 0:bf7b9fba3924 | 118 | *optptr++ = DHCP_OPTION_REQ_IPADDR; |
frank26080115 | 0:bf7b9fba3924 | 119 | *optptr++ = 4; |
frank26080115 | 0:bf7b9fba3924 | 120 | memcpy(optptr, s.ipaddr, 4); |
frank26080115 | 0:bf7b9fba3924 | 121 | return optptr + 4; |
frank26080115 | 0:bf7b9fba3924 | 122 | } |
frank26080115 | 0:bf7b9fba3924 | 123 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 124 | static u8_t * |
frank26080115 | 0:bf7b9fba3924 | 125 | add_req_options(u8_t *optptr) |
frank26080115 | 0:bf7b9fba3924 | 126 | { |
frank26080115 | 0:bf7b9fba3924 | 127 | *optptr++ = DHCP_OPTION_REQ_LIST; |
frank26080115 | 0:bf7b9fba3924 | 128 | *optptr++ = 3; |
frank26080115 | 0:bf7b9fba3924 | 129 | *optptr++ = DHCP_OPTION_SUBNET_MASK; |
frank26080115 | 0:bf7b9fba3924 | 130 | *optptr++ = DHCP_OPTION_ROUTER; |
frank26080115 | 0:bf7b9fba3924 | 131 | *optptr++ = DHCP_OPTION_DNS_SERVER; |
frank26080115 | 0:bf7b9fba3924 | 132 | return optptr; |
frank26080115 | 0:bf7b9fba3924 | 133 | } |
frank26080115 | 0:bf7b9fba3924 | 134 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 135 | static u8_t * |
frank26080115 | 0:bf7b9fba3924 | 136 | add_end(u8_t *optptr) |
frank26080115 | 0:bf7b9fba3924 | 137 | { |
frank26080115 | 0:bf7b9fba3924 | 138 | *optptr++ = DHCP_OPTION_END; |
frank26080115 | 0:bf7b9fba3924 | 139 | return optptr; |
frank26080115 | 0:bf7b9fba3924 | 140 | } |
frank26080115 | 0:bf7b9fba3924 | 141 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 142 | static void |
frank26080115 | 0:bf7b9fba3924 | 143 | create_msg(register struct dhcp_msg *m) |
frank26080115 | 0:bf7b9fba3924 | 144 | { |
frank26080115 | 0:bf7b9fba3924 | 145 | m->op = DHCP_REQUEST; |
frank26080115 | 0:bf7b9fba3924 | 146 | m->htype = DHCP_HTYPE_ETHERNET; |
frank26080115 | 0:bf7b9fba3924 | 147 | m->hlen = s.mac_len; |
frank26080115 | 0:bf7b9fba3924 | 148 | m->hops = 0; |
frank26080115 | 0:bf7b9fba3924 | 149 | memcpy(m->xid, xid, sizeof(m->xid)); |
frank26080115 | 0:bf7b9fba3924 | 150 | m->secs = 0; |
frank26080115 | 0:bf7b9fba3924 | 151 | m->flags = HTONS(BOOTP_BROADCAST); /* Broadcast bit. */ |
frank26080115 | 0:bf7b9fba3924 | 152 | /* uip_ipaddr_copy(m->ciaddr, uip_hostaddr);*/ |
frank26080115 | 0:bf7b9fba3924 | 153 | memcpy(m->ciaddr, uip_hostaddr, sizeof(m->ciaddr)); |
frank26080115 | 0:bf7b9fba3924 | 154 | memset(m->yiaddr, 0, sizeof(m->yiaddr)); |
frank26080115 | 0:bf7b9fba3924 | 155 | memset(m->siaddr, 0, sizeof(m->siaddr)); |
frank26080115 | 0:bf7b9fba3924 | 156 | memset(m->giaddr, 0, sizeof(m->giaddr)); |
frank26080115 | 0:bf7b9fba3924 | 157 | memcpy(m->chaddr, s.mac_addr, s.mac_len); |
frank26080115 | 0:bf7b9fba3924 | 158 | memset(&m->chaddr[s.mac_len], 0, sizeof(m->chaddr) - s.mac_len); |
frank26080115 | 0:bf7b9fba3924 | 159 | #ifndef UIP_CONF_DHCP_LIGHT |
frank26080115 | 0:bf7b9fba3924 | 160 | memset(m->sname, 0, sizeof(m->sname)); |
frank26080115 | 0:bf7b9fba3924 | 161 | memset(m->file, 0, sizeof(m->file)); |
frank26080115 | 0:bf7b9fba3924 | 162 | #endif |
frank26080115 | 0:bf7b9fba3924 | 163 | |
frank26080115 | 0:bf7b9fba3924 | 164 | memcpy(m->options, magic_cookie, sizeof(magic_cookie)); |
frank26080115 | 0:bf7b9fba3924 | 165 | } |
frank26080115 | 0:bf7b9fba3924 | 166 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 167 | static void |
frank26080115 | 0:bf7b9fba3924 | 168 | send_discover(void) |
frank26080115 | 0:bf7b9fba3924 | 169 | { |
frank26080115 | 0:bf7b9fba3924 | 170 | u8_t *end; |
frank26080115 | 0:bf7b9fba3924 | 171 | struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; |
frank26080115 | 0:bf7b9fba3924 | 172 | |
frank26080115 | 0:bf7b9fba3924 | 173 | create_msg(m); |
frank26080115 | 0:bf7b9fba3924 | 174 | |
frank26080115 | 0:bf7b9fba3924 | 175 | end = add_msg_type(&m->options[4], DHCPDISCOVER); |
frank26080115 | 0:bf7b9fba3924 | 176 | end = add_req_options(end); |
frank26080115 | 0:bf7b9fba3924 | 177 | end = add_end(end); |
frank26080115 | 0:bf7b9fba3924 | 178 | |
frank26080115 | 0:bf7b9fba3924 | 179 | uip_send(uip_appdata, end - (u8_t *)uip_appdata); |
frank26080115 | 0:bf7b9fba3924 | 180 | } |
frank26080115 | 0:bf7b9fba3924 | 181 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 182 | static void |
frank26080115 | 0:bf7b9fba3924 | 183 | send_request(void) |
frank26080115 | 0:bf7b9fba3924 | 184 | { |
frank26080115 | 0:bf7b9fba3924 | 185 | u8_t *end; |
frank26080115 | 0:bf7b9fba3924 | 186 | struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; |
frank26080115 | 0:bf7b9fba3924 | 187 | |
frank26080115 | 0:bf7b9fba3924 | 188 | create_msg(m); |
frank26080115 | 0:bf7b9fba3924 | 189 | |
frank26080115 | 0:bf7b9fba3924 | 190 | end = add_msg_type(&m->options[4], DHCPREQUEST); |
frank26080115 | 0:bf7b9fba3924 | 191 | end = add_server_id(end); |
frank26080115 | 0:bf7b9fba3924 | 192 | end = add_req_ipaddr(end); |
frank26080115 | 0:bf7b9fba3924 | 193 | end = add_end(end); |
frank26080115 | 0:bf7b9fba3924 | 194 | |
frank26080115 | 0:bf7b9fba3924 | 195 | uip_send(uip_appdata, end - (u8_t *)uip_appdata); |
frank26080115 | 0:bf7b9fba3924 | 196 | } |
frank26080115 | 0:bf7b9fba3924 | 197 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 198 | static u8_t |
frank26080115 | 0:bf7b9fba3924 | 199 | parse_options(u8_t *optptr, int len) |
frank26080115 | 0:bf7b9fba3924 | 200 | { |
frank26080115 | 0:bf7b9fba3924 | 201 | u8_t *end = optptr + len; |
frank26080115 | 0:bf7b9fba3924 | 202 | u8_t type = 0; |
frank26080115 | 0:bf7b9fba3924 | 203 | |
frank26080115 | 0:bf7b9fba3924 | 204 | while(optptr < end) { |
frank26080115 | 0:bf7b9fba3924 | 205 | switch(*optptr) { |
frank26080115 | 0:bf7b9fba3924 | 206 | case DHCP_OPTION_SUBNET_MASK: |
frank26080115 | 0:bf7b9fba3924 | 207 | memcpy(s.netmask, optptr + 2, 4); |
frank26080115 | 0:bf7b9fba3924 | 208 | break; |
frank26080115 | 0:bf7b9fba3924 | 209 | case DHCP_OPTION_ROUTER: |
frank26080115 | 0:bf7b9fba3924 | 210 | memcpy(s.default_router, optptr + 2, 4); |
frank26080115 | 0:bf7b9fba3924 | 211 | break; |
frank26080115 | 0:bf7b9fba3924 | 212 | case DHCP_OPTION_DNS_SERVER: |
frank26080115 | 0:bf7b9fba3924 | 213 | memcpy(s.dnsaddr, optptr + 2, 4); |
frank26080115 | 0:bf7b9fba3924 | 214 | break; |
frank26080115 | 0:bf7b9fba3924 | 215 | case DHCP_OPTION_MSG_TYPE: |
frank26080115 | 0:bf7b9fba3924 | 216 | type = *(optptr + 2); |
frank26080115 | 0:bf7b9fba3924 | 217 | break; |
frank26080115 | 0:bf7b9fba3924 | 218 | case DHCP_OPTION_SERVER_ID: |
frank26080115 | 0:bf7b9fba3924 | 219 | memcpy(s.serverid, optptr + 2, 4); |
frank26080115 | 0:bf7b9fba3924 | 220 | break; |
frank26080115 | 0:bf7b9fba3924 | 221 | case DHCP_OPTION_LEASE_TIME: |
frank26080115 | 0:bf7b9fba3924 | 222 | memcpy(s.lease_time, optptr + 2, 4); |
frank26080115 | 0:bf7b9fba3924 | 223 | break; |
frank26080115 | 0:bf7b9fba3924 | 224 | case DHCP_OPTION_END: |
frank26080115 | 0:bf7b9fba3924 | 225 | return type; |
frank26080115 | 0:bf7b9fba3924 | 226 | } |
frank26080115 | 0:bf7b9fba3924 | 227 | |
frank26080115 | 0:bf7b9fba3924 | 228 | optptr += optptr[1] + 2; |
frank26080115 | 0:bf7b9fba3924 | 229 | } |
frank26080115 | 0:bf7b9fba3924 | 230 | return type; |
frank26080115 | 0:bf7b9fba3924 | 231 | } |
frank26080115 | 0:bf7b9fba3924 | 232 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 233 | static u8_t |
frank26080115 | 0:bf7b9fba3924 | 234 | parse_msg(void) |
frank26080115 | 0:bf7b9fba3924 | 235 | { |
frank26080115 | 0:bf7b9fba3924 | 236 | struct dhcp_msg *m = (struct dhcp_msg *)uip_appdata; |
frank26080115 | 0:bf7b9fba3924 | 237 | |
frank26080115 | 0:bf7b9fba3924 | 238 | if(m->op == DHCP_REPLY && |
frank26080115 | 0:bf7b9fba3924 | 239 | memcmp(m->xid, xid, sizeof(xid)) == 0 && |
frank26080115 | 0:bf7b9fba3924 | 240 | memcmp(m->chaddr, s.mac_addr, s.mac_len) == 0) { |
frank26080115 | 0:bf7b9fba3924 | 241 | memcpy(s.ipaddr, m->yiaddr, 4); |
frank26080115 | 0:bf7b9fba3924 | 242 | return parse_options(&m->options[4], uip_datalen()); |
frank26080115 | 0:bf7b9fba3924 | 243 | } |
frank26080115 | 0:bf7b9fba3924 | 244 | return 0; |
frank26080115 | 0:bf7b9fba3924 | 245 | } |
frank26080115 | 0:bf7b9fba3924 | 246 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 247 | static |
frank26080115 | 0:bf7b9fba3924 | 248 | PT_THREAD(handle_dhcp(void)) |
frank26080115 | 0:bf7b9fba3924 | 249 | { |
frank26080115 | 0:bf7b9fba3924 | 250 | PT_BEGIN(&s.pt); |
frank26080115 | 0:bf7b9fba3924 | 251 | |
frank26080115 | 0:bf7b9fba3924 | 252 | /* try_again:*/ |
frank26080115 | 0:bf7b9fba3924 | 253 | s.state = STATE_SENDING; |
frank26080115 | 0:bf7b9fba3924 | 254 | s.ticks = CLOCK_SECOND; |
frank26080115 | 0:bf7b9fba3924 | 255 | |
frank26080115 | 0:bf7b9fba3924 | 256 | do { |
frank26080115 | 0:bf7b9fba3924 | 257 | send_discover(); |
frank26080115 | 0:bf7b9fba3924 | 258 | timer_set(&s.timer, s.ticks); |
frank26080115 | 0:bf7b9fba3924 | 259 | PT_WAIT_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer)); |
frank26080115 | 0:bf7b9fba3924 | 260 | |
frank26080115 | 0:bf7b9fba3924 | 261 | if(uip_newdata() && parse_msg() == DHCPOFFER) { |
frank26080115 | 0:bf7b9fba3924 | 262 | s.state = STATE_OFFER_RECEIVED; |
frank26080115 | 0:bf7b9fba3924 | 263 | break; |
frank26080115 | 0:bf7b9fba3924 | 264 | } |
frank26080115 | 0:bf7b9fba3924 | 265 | |
frank26080115 | 0:bf7b9fba3924 | 266 | if(s.ticks < CLOCK_SECOND * 60) { |
frank26080115 | 0:bf7b9fba3924 | 267 | s.ticks *= 2; |
frank26080115 | 0:bf7b9fba3924 | 268 | } |
frank26080115 | 0:bf7b9fba3924 | 269 | } while(s.state != STATE_OFFER_RECEIVED); |
frank26080115 | 0:bf7b9fba3924 | 270 | |
frank26080115 | 0:bf7b9fba3924 | 271 | s.ticks = CLOCK_SECOND; |
frank26080115 | 0:bf7b9fba3924 | 272 | |
frank26080115 | 0:bf7b9fba3924 | 273 | do { |
frank26080115 | 0:bf7b9fba3924 | 274 | send_request(); |
frank26080115 | 0:bf7b9fba3924 | 275 | timer_set(&s.timer, s.ticks); |
frank26080115 | 0:bf7b9fba3924 | 276 | PT_WAIT_UNTIL(&s.pt, uip_newdata() || timer_expired(&s.timer)); |
frank26080115 | 0:bf7b9fba3924 | 277 | |
frank26080115 | 0:bf7b9fba3924 | 278 | if(uip_newdata() && parse_msg() == DHCPACK) { |
frank26080115 | 0:bf7b9fba3924 | 279 | s.state = STATE_CONFIG_RECEIVED; |
frank26080115 | 0:bf7b9fba3924 | 280 | break; |
frank26080115 | 0:bf7b9fba3924 | 281 | } |
frank26080115 | 0:bf7b9fba3924 | 282 | |
frank26080115 | 0:bf7b9fba3924 | 283 | if(s.ticks <= CLOCK_SECOND * 10) { |
frank26080115 | 0:bf7b9fba3924 | 284 | s.ticks += CLOCK_SECOND; |
frank26080115 | 0:bf7b9fba3924 | 285 | } else { |
frank26080115 | 0:bf7b9fba3924 | 286 | PT_RESTART(&s.pt); |
frank26080115 | 0:bf7b9fba3924 | 287 | } |
frank26080115 | 0:bf7b9fba3924 | 288 | } while(s.state != STATE_CONFIG_RECEIVED); |
frank26080115 | 0:bf7b9fba3924 | 289 | |
frank26080115 | 0:bf7b9fba3924 | 290 | #if 0 |
frank26080115 | 0:bf7b9fba3924 | 291 | printf("Got IP address %d.%d.%d.%d\n", |
frank26080115 | 0:bf7b9fba3924 | 292 | uip_ipaddr1(s.ipaddr), uip_ipaddr2(s.ipaddr), |
frank26080115 | 0:bf7b9fba3924 | 293 | uip_ipaddr3(s.ipaddr), uip_ipaddr4(s.ipaddr)); |
frank26080115 | 0:bf7b9fba3924 | 294 | printf("Got netmask %d.%d.%d.%d\n", |
frank26080115 | 0:bf7b9fba3924 | 295 | uip_ipaddr1(s.netmask), uip_ipaddr2(s.netmask), |
frank26080115 | 0:bf7b9fba3924 | 296 | uip_ipaddr3(s.netmask), uip_ipaddr4(s.netmask)); |
frank26080115 | 0:bf7b9fba3924 | 297 | printf("Got DNS server %d.%d.%d.%d\n", |
frank26080115 | 0:bf7b9fba3924 | 298 | uip_ipaddr1(s.dnsaddr), uip_ipaddr2(s.dnsaddr), |
frank26080115 | 0:bf7b9fba3924 | 299 | uip_ipaddr3(s.dnsaddr), uip_ipaddr4(s.dnsaddr)); |
frank26080115 | 0:bf7b9fba3924 | 300 | printf("Got default router %d.%d.%d.%d\n", |
frank26080115 | 0:bf7b9fba3924 | 301 | uip_ipaddr1(s.default_router), uip_ipaddr2(s.default_router), |
frank26080115 | 0:bf7b9fba3924 | 302 | uip_ipaddr3(s.default_router), uip_ipaddr4(s.default_router)); |
frank26080115 | 0:bf7b9fba3924 | 303 | printf("Lease expires in %ld seconds\n", |
frank26080115 | 0:bf7b9fba3924 | 304 | ntohs(s.lease_time[0])*65536ul + ntohs(s.lease_time[1])); |
frank26080115 | 0:bf7b9fba3924 | 305 | #endif |
frank26080115 | 0:bf7b9fba3924 | 306 | |
frank26080115 | 0:bf7b9fba3924 | 307 | dhcpc_configured(&s); |
frank26080115 | 0:bf7b9fba3924 | 308 | |
frank26080115 | 0:bf7b9fba3924 | 309 | /* timer_stop(&s.timer);*/ |
frank26080115 | 0:bf7b9fba3924 | 310 | |
frank26080115 | 0:bf7b9fba3924 | 311 | /* |
frank26080115 | 0:bf7b9fba3924 | 312 | * PT_END restarts the thread so we do this instead. Eventually we |
frank26080115 | 0:bf7b9fba3924 | 313 | * should reacquire expired leases here. |
frank26080115 | 0:bf7b9fba3924 | 314 | */ |
frank26080115 | 0:bf7b9fba3924 | 315 | while(1) { |
frank26080115 | 0:bf7b9fba3924 | 316 | PT_YIELD(&s.pt); |
frank26080115 | 0:bf7b9fba3924 | 317 | } |
frank26080115 | 0:bf7b9fba3924 | 318 | |
frank26080115 | 0:bf7b9fba3924 | 319 | PT_END(&s.pt); |
frank26080115 | 0:bf7b9fba3924 | 320 | } |
frank26080115 | 0:bf7b9fba3924 | 321 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 322 | void |
frank26080115 | 0:bf7b9fba3924 | 323 | dhcpc_init(const void *mac_addr, int mac_len) |
frank26080115 | 0:bf7b9fba3924 | 324 | { |
frank26080115 | 0:bf7b9fba3924 | 325 | uip_ipaddr_t addr; |
frank26080115 | 0:bf7b9fba3924 | 326 | |
frank26080115 | 0:bf7b9fba3924 | 327 | s.mac_addr = mac_addr; |
frank26080115 | 0:bf7b9fba3924 | 328 | s.mac_len = mac_len; |
frank26080115 | 0:bf7b9fba3924 | 329 | |
frank26080115 | 0:bf7b9fba3924 | 330 | s.state = STATE_INITIAL; |
frank26080115 | 0:bf7b9fba3924 | 331 | uip_ipaddr(addr, 255,255,255,255); |
frank26080115 | 0:bf7b9fba3924 | 332 | s.conn = uip_udp_new(&addr, HTONS(DHCPC_SERVER_PORT)); |
frank26080115 | 0:bf7b9fba3924 | 333 | if(s.conn != NULL) { |
frank26080115 | 0:bf7b9fba3924 | 334 | uip_udp_bind(s.conn, HTONS(DHCPC_CLIENT_PORT)); |
frank26080115 | 0:bf7b9fba3924 | 335 | } |
frank26080115 | 0:bf7b9fba3924 | 336 | PT_INIT(&s.pt); |
frank26080115 | 0:bf7b9fba3924 | 337 | } |
frank26080115 | 0:bf7b9fba3924 | 338 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 339 | void |
frank26080115 | 0:bf7b9fba3924 | 340 | dhcpc_appcall(void) |
frank26080115 | 0:bf7b9fba3924 | 341 | { |
frank26080115 | 0:bf7b9fba3924 | 342 | handle_dhcp(); |
frank26080115 | 0:bf7b9fba3924 | 343 | } |
frank26080115 | 0:bf7b9fba3924 | 344 | /*---------------------------------------------------------------------------*/ |
frank26080115 | 0:bf7b9fba3924 | 345 | void |
frank26080115 | 0:bf7b9fba3924 | 346 | dhcpc_request(void) |
frank26080115 | 0:bf7b9fba3924 | 347 | { |
frank26080115 | 0:bf7b9fba3924 | 348 | u16_t ipaddr[2]; |
frank26080115 | 0:bf7b9fba3924 | 349 | |
frank26080115 | 0:bf7b9fba3924 | 350 | if(s.state == STATE_INITIAL) { |
frank26080115 | 0:bf7b9fba3924 | 351 | uip_ipaddr(ipaddr, 0,0,0,0); |
frank26080115 | 0:bf7b9fba3924 | 352 | uip_sethostaddr(ipaddr); |
frank26080115 | 0:bf7b9fba3924 | 353 | /* handle_dhcp(PROCESS_EVENT_NONE, NULL);*/ |
frank26080115 | 0:bf7b9fba3924 | 354 | } |
frank26080115 | 0:bf7b9fba3924 | 355 | } |
frank26080115 | 0:bf7b9fba3924 | 356 | /*---------------------------------------------------------------------------*/ |