[66ec63a] | 1 | /*
|
---|
| 2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - 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 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup dhcp
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /**
|
---|
| 33 | * @file
|
---|
| 34 | * @brief DHCP client
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <bitops.h>
|
---|
[fab2746] | 38 | #include <errno.h>
|
---|
[66ec63a] | 39 | #include <inet/addr.h>
|
---|
| 40 | #include <inet/dnsr.h>
|
---|
| 41 | #include <inet/inetcfg.h>
|
---|
| 42 | #include <io/log.h>
|
---|
| 43 | #include <loc.h>
|
---|
| 44 | #include <stdio.h>
|
---|
| 45 | #include <stdlib.h>
|
---|
| 46 |
|
---|
| 47 | #include "dhcp.h"
|
---|
| 48 | #include "dhcp_std.h"
|
---|
| 49 | #include "transport.h"
|
---|
| 50 |
|
---|
| 51 | #define MAX_MSG_SIZE 1024
|
---|
| 52 | static uint8_t msgbuf[MAX_MSG_SIZE];
|
---|
| 53 |
|
---|
| 54 | typedef struct {
|
---|
| 55 | /** Message type */
|
---|
| 56 | enum dhcp_msg_type msg_type;
|
---|
| 57 | /** Offered address */
|
---|
| 58 | inet_naddr_t oaddr;
|
---|
| 59 | /** Server address */
|
---|
| 60 | inet_addr_t srv_addr;
|
---|
| 61 | /** Router address */
|
---|
| 62 | inet_addr_t router;
|
---|
| 63 | /** DNS server */
|
---|
| 64 | inet_addr_t dns_server;
|
---|
| 65 | } dhcp_offer_t;
|
---|
| 66 |
|
---|
[fab2746] | 67 | static void dhcp_recv_msg(udp_assoc_t *, udp_rmsg_t *);
|
---|
| 68 | static void dhcp_recv_err(udp_assoc_t *, udp_rerr_t *);
|
---|
| 69 | static void dhcp_link_state(udp_assoc_t *, udp_link_state_t);
|
---|
| 70 |
|
---|
| 71 | static udp_cb_t dhcp_transport_cb = {
|
---|
| 72 | .recv_msg = dhcp_recv_msg,
|
---|
| 73 | .recv_err = dhcp_recv_err,
|
---|
| 74 | .link_state = dhcp_link_state
|
---|
| 75 | };
|
---|
[66ec63a] | 76 |
|
---|
[b7fd2a0] | 77 | errno_t dhcp_send(dhcp_transport_t *dt, void *msg, size_t size)
|
---|
[66ec63a] | 78 | {
|
---|
[fab2746] | 79 | inet_ep_t ep;
|
---|
[b7fd2a0] | 80 | errno_t rc;
|
---|
[66ec63a] | 81 |
|
---|
[fab2746] | 82 | inet_ep_init(&ep);
|
---|
| 83 | ep.port = dhcp_server_port;
|
---|
| 84 | inet_addr_set(addr32_broadcast_all_hosts, &ep.addr);
|
---|
[66ec63a] | 85 |
|
---|
[fab2746] | 86 | rc = udp_assoc_send_msg(dt->assoc, &ep, msg, size);
|
---|
[66ec63a] | 87 | if (rc != EOK) {
|
---|
[fab2746] | 88 | log_msg(LOG_DEFAULT, LVL_ERROR, "Failed sending message");
|
---|
[66ec63a] | 89 | return rc;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
| 92 | return EOK;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
[fab2746] | 95 | static void dhcp_recv_msg(udp_assoc_t *assoc, udp_rmsg_t *rmsg)
|
---|
[66ec63a] | 96 | {
|
---|
[fab2746] | 97 | dhcp_transport_t *dt;
|
---|
| 98 | size_t s;
|
---|
[b7fd2a0] | 99 | errno_t rc;
|
---|
[66ec63a] | 100 |
|
---|
[fab2746] | 101 | log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg()");
|
---|
| 102 |
|
---|
| 103 | dt = (dhcp_transport_t *)udp_assoc_userptr(assoc);
|
---|
| 104 | s = udp_rmsg_size(rmsg);
|
---|
| 105 | if (s > MAX_MSG_SIZE)
|
---|
| 106 | s = MAX_MSG_SIZE; /* XXX */
|
---|
| 107 |
|
---|
| 108 | rc = udp_rmsg_read(rmsg, 0, msgbuf, s);
|
---|
| 109 | if (rc != EOK) {
|
---|
| 110 | log_msg(LOG_DEFAULT, LVL_ERROR, "Error receiving message.");
|
---|
| 111 | return;
|
---|
[66ec63a] | 112 | }
|
---|
| 113 |
|
---|
[fab2746] | 114 | log_msg(LOG_DEFAULT, LVL_NOTE, "dhcp_recv_msg() - call recv_cb");
|
---|
| 115 | dt->recv_cb(dt->cb_arg, msgbuf, s);
|
---|
| 116 | }
|
---|
[66ec63a] | 117 |
|
---|
[fab2746] | 118 | static void dhcp_recv_err(udp_assoc_t *assoc, udp_rerr_t *rerr)
|
---|
| 119 | {
|
---|
| 120 | log_msg(LOG_DEFAULT, LVL_WARN, "Ignoring ICMP error");
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | static void dhcp_link_state(udp_assoc_t *assoc, udp_link_state_t ls)
|
---|
| 124 | {
|
---|
| 125 | log_msg(LOG_DEFAULT, LVL_NOTE, "Link state change");
|
---|
[66ec63a] | 126 | }
|
---|
| 127 |
|
---|
[b7fd2a0] | 128 | errno_t dhcp_transport_init(dhcp_transport_t *dt, service_id_t link_id,
|
---|
[66ec63a] | 129 | dhcp_recv_cb_t recv_cb, void *arg)
|
---|
| 130 | {
|
---|
[fab2746] | 131 | udp_t *udp = NULL;
|
---|
| 132 | udp_assoc_t *assoc = NULL;
|
---|
| 133 | inet_ep2_t epp;
|
---|
[b7fd2a0] | 134 | errno_t rc;
|
---|
[66ec63a] | 135 |
|
---|
[fab2746] | 136 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcp_transport_init()");
|
---|
[66ec63a] | 137 |
|
---|
[fab2746] | 138 | inet_ep2_init(&epp);
|
---|
| 139 | epp.local.addr.version = ip_v4;
|
---|
| 140 | epp.local.port = dhcp_client_port;
|
---|
| 141 | epp.local_link = link_id;
|
---|
[66ec63a] | 142 |
|
---|
[fab2746] | 143 | rc = udp_create(&udp);
|
---|
[66ec63a] | 144 | if (rc != EOK) {
|
---|
| 145 | rc = EIO;
|
---|
| 146 | goto error;
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[fab2746] | 149 | rc = udp_assoc_create(udp, &epp, &dhcp_transport_cb, dt, &assoc);
|
---|
[66ec63a] | 150 | if (rc != EOK) {
|
---|
| 151 | rc = EIO;
|
---|
| 152 | goto error;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
[58e8646] | 155 | rc = udp_assoc_set_nolocal(assoc);
|
---|
| 156 | if (rc != EOK) {
|
---|
| 157 | rc = EIO;
|
---|
| 158 | goto error;
|
---|
| 159 | }
|
---|
| 160 |
|
---|
[fab2746] | 161 | dt->udp = udp;
|
---|
| 162 | dt->assoc = assoc;
|
---|
[66ec63a] | 163 | dt->recv_cb = recv_cb;
|
---|
| 164 | dt->cb_arg = arg;
|
---|
| 165 |
|
---|
| 166 | return EOK;
|
---|
| 167 | error:
|
---|
[fab2746] | 168 | udp_assoc_destroy(assoc);
|
---|
| 169 | udp_destroy(udp);
|
---|
[66ec63a] | 170 | return rc;
|
---|
| 171 | }
|
---|
| 172 |
|
---|
| 173 | void dhcp_transport_fini(dhcp_transport_t *dt)
|
---|
| 174 | {
|
---|
[fab2746] | 175 | udp_assoc_destroy(dt->assoc);
|
---|
| 176 | udp_destroy(dt->udp);
|
---|
[66ec63a] | 177 | }
|
---|
| 178 |
|
---|
| 179 | /** @}
|
---|
| 180 | */
|
---|