Update Files
This commit is contained in:
535
Kinc/Sources/kinc/libs/core/dhcpclient/dhcpc4.c
Normal file
535
Kinc/Sources/kinc/libs/core/dhcpclient/dhcpc4.c
Normal file
@ -0,0 +1,535 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*
|
||||
* The protocol part of dhcp4 client
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-system-dhcpclient.h"
|
||||
|
||||
#define LDHC_OP_BOOTREQUEST 1
|
||||
#define LDHC_OP_BOOTREPLY 2
|
||||
|
||||
/*
|
||||
* IPv4... max total 576
|
||||
*
|
||||
* 0 1 2 3
|
||||
* 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
|
||||
* +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
|
||||
* | op (1) | htype (1) | hlen (1) | hops (1) |
|
||||
* +---------------+---------------+---------------+---------------+
|
||||
* | +04 xid (4) |
|
||||
* +-------------------------------+-------------------------------+
|
||||
* | +08 secs (2) | +0a flags (2) |
|
||||
* +-------------------------------+-------------------------------+
|
||||
* | +0C ciaddr (4) client IP |
|
||||
* +---------------------------------------------------------------+
|
||||
* | +10 yiaddr (4) your IP |
|
||||
* +---------------------------------------------------------------+
|
||||
* | +14 siaddr (4) server IP |
|
||||
* +---------------------------------------------------------------+
|
||||
* | +18 giaddr (4) gateway IP |
|
||||
* +---------------------------------------------------------------+
|
||||
* | |
|
||||
* | +1C chaddr (16) client HWADDR |
|
||||
* +---------------------------------------------------------------+
|
||||
* | |
|
||||
* | +2C sname (64) |
|
||||
* +---------------------------------------------------------------+
|
||||
* | |
|
||||
* | +6C file (128) |
|
||||
* +---------------------------------------------------------------+
|
||||
* | |
|
||||
* | +EC options (variable) |
|
||||
* +---------------------------------------------------------------+
|
||||
*/
|
||||
|
||||
static const uint8_t rawdisc4[] = {
|
||||
0x45, 0x00, 0, 0, 0, 0, 0x40, 0, 0x2e, IPPROTO_UDP,
|
||||
0, 0, 0, 0, 0, 0, 0xff, 0xff, 0xff, 0xff,
|
||||
0, 68, 0, 67, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
static const uint32_t botable2[] = { 1500, 1750, 5000 /* in case dog slow */ };
|
||||
static const lws_retry_bo_t bo2 = {
|
||||
botable2, LWS_ARRAY_SIZE(botable2), LWS_RETRY_CONCEAL_ALWAYS, 0, 0, 20 };
|
||||
|
||||
static int
|
||||
lws_dhcpc4_prep(uint8_t *start, unsigned int bufsiz, lws_dhcpc_req_t *r, int op)
|
||||
{
|
||||
uint8_t *p = start;
|
||||
|
||||
memset(start, 0, bufsiz);
|
||||
|
||||
*p++ = 1;
|
||||
*p++ = 1;
|
||||
*p++ = 6; /* sizeof ethernet MAC */
|
||||
|
||||
memcpy(p + 1, r->xid, 4);
|
||||
|
||||
// p[7] = 0x80; /* broadcast flag */
|
||||
|
||||
p += 0x1c - 3;
|
||||
|
||||
if (lws_plat_ifname_to_hwaddr(r->wsi_raw->desc.sockfd,
|
||||
(const char *)&r[1], r->is.mac, 6) < 0)
|
||||
return -1;
|
||||
|
||||
memcpy(p, r->is.mac, 6);
|
||||
|
||||
p += 16 + 64 + 128;
|
||||
|
||||
*p++ = 0x63; /* RFC2132 Magic Cookie indicates start of options */
|
||||
*p++ = 0x82;
|
||||
*p++ = 0x53;
|
||||
*p++ = 0x63;
|
||||
|
||||
*p++ = LWSDHC4POPT_MESSAGE_TYPE;
|
||||
*p++ = 1; /* length */
|
||||
*p++ = (uint8_t)op;
|
||||
|
||||
switch (op) {
|
||||
case LWSDHC4PDISCOVER:
|
||||
*p++ = LWSDHC4POPT_PARAM_REQ_LIST;
|
||||
*p++ = 4; /* length */
|
||||
*p++ = LWSDHC4POPT_SUBNET_MASK;
|
||||
*p++ = LWSDHC4POPT_ROUTER;
|
||||
*p++ = LWSDHC4POPT_DNSERVER;
|
||||
*p++ = LWSDHC4POPT_DOMAIN_NAME;
|
||||
break;
|
||||
|
||||
case LWSDHC4PREQUEST:
|
||||
if (r->is.sa46[LWSDH_SA46_IP].sa4.sin_family != AF_INET)
|
||||
break;
|
||||
*p++ = LWSDHC4POPT_REQUESTED_ADS;
|
||||
*p++ = 4; /* length */
|
||||
lws_ser_wu32be(p, r->is.sa46[LWSDH_SA46_IP].sa4.sin_addr.s_addr);
|
||||
p += 4;
|
||||
*p++ = LWSDHC4POPT_SERVER_ID;
|
||||
*p++ = 4; /* length */
|
||||
lws_ser_wu32be(p, r->is.sa46[LWSDH_SA46_DHCP_SERVER].sa4.sin_addr.s_addr);
|
||||
p += 4;
|
||||
break;
|
||||
}
|
||||
|
||||
*p++ = LWSDHC4POPT_END_OPTIONS;
|
||||
|
||||
return lws_ptr_diff(p, start);
|
||||
}
|
||||
|
||||
static int
|
||||
callback_dhcpc4(struct lws *wsi, enum lws_callback_reasons reason, void *user,
|
||||
void *in, size_t len)
|
||||
{
|
||||
lws_dhcpc_req_t *r = (lws_dhcpc_req_t *)user;
|
||||
uint8_t pkt[LWS_PRE + 576], *p = pkt + LWS_PRE;
|
||||
int n, m;
|
||||
|
||||
switch (reason) {
|
||||
|
||||
case LWS_CALLBACK_RAW_ADOPT:
|
||||
lwsl_debug("%s: LWS_CALLBACK_RAW_ADOPT\n", __func__);
|
||||
lws_callback_on_writable(wsi);
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_CLIENT_CONNECTION_ERROR:
|
||||
lwsl_err("%s: udp conn failed\n", __func__);
|
||||
|
||||
/* fallthru */
|
||||
case LWS_CALLBACK_RAW_CLOSE:
|
||||
lwsl_debug("%s: LWS_CALLBACK_RAW_CLOSE\n", __func__);
|
||||
if (!r)
|
||||
break;
|
||||
r->wsi_raw = NULL;
|
||||
lws_sul_cancel(&r->sul_write);
|
||||
if (r->state != LDHC_BOUND) {
|
||||
r->state = LDHC_INIT;
|
||||
lws_retry_sul_schedule(r->context, 0, &r->sul_conn,
|
||||
&bo2, lws_dhcpc4_retry_conn,
|
||||
&r->retry_count_conn);
|
||||
}
|
||||
break;
|
||||
|
||||
case LWS_CALLBACK_RAW_RX:
|
||||
|
||||
if (lws_dhcpc4_parse(r, in, len))
|
||||
break;
|
||||
|
||||
/*
|
||||
* that's it... commit to the configuration
|
||||
*/
|
||||
|
||||
/* set up our network interface as offered */
|
||||
|
||||
if (lws_plat_ifconfig(r->wsi_raw->desc.sockfd, &r->is))
|
||||
/*
|
||||
* Problem setting the IP... maybe something
|
||||
* transient like racing with NetworkManager?
|
||||
* Since the sul retries are still around it
|
||||
* will retry
|
||||
*/
|
||||
return -1;
|
||||
|
||||
/* clear timeouts related to the broadcast socket */
|
||||
|
||||
lws_sul_cancel(&r->sul_write);
|
||||
lws_sul_cancel(&r->sul_conn);
|
||||
|
||||
lwsl_notice("%s: DHCP configured %s\n", __func__,
|
||||
(const char *)&r[1]);
|
||||
r->state = LDHC_BOUND;
|
||||
|
||||
lws_state_transition_steps(&wsi->a.context->mgr_system,
|
||||
LWS_SYSTATE_OPERATIONAL);
|
||||
|
||||
r->cb(r->opaque, &r->is);
|
||||
|
||||
r->wsi_raw = NULL;
|
||||
|
||||
return -1; /* close the broadcast wsi */
|
||||
|
||||
case LWS_CALLBACK_RAW_WRITEABLE:
|
||||
|
||||
if (!r)
|
||||
break;
|
||||
|
||||
/*
|
||||
* UDP is not reliable, it can be locally dropped, or dropped
|
||||
* by any intermediary or the remote peer. So even though we
|
||||
* will do the write in a moment, we schedule another request
|
||||
* for rewrite according to the wsi retry policy.
|
||||
*
|
||||
* If the result came before, we'll cancel it in the close flow.
|
||||
*
|
||||
* If we have already reached the end of our concealed retries
|
||||
* in the policy, just close without another write.
|
||||
*/
|
||||
if (lws_dll2_is_detached(&r->sul_write.list) &&
|
||||
lws_retry_sul_schedule_retry_wsi(wsi, &r->sul_write,
|
||||
lws_dhcpc_retry_write,
|
||||
&r->retry_count_write)) {
|
||||
/* we have reached the end of our concealed retries */
|
||||
lwsl_warn("%s: concealed retries done, failing\n",
|
||||
__func__);
|
||||
goto retry_conn;
|
||||
}
|
||||
|
||||
switch (r->state) {
|
||||
case LDHC_INIT:
|
||||
n = LWSDHC4PDISCOVER;
|
||||
goto bcast;
|
||||
|
||||
case LDHC_REQUESTING:
|
||||
n = LWSDHC4PREQUEST;
|
||||
|
||||
/* fallthru */
|
||||
bcast:
|
||||
n = lws_dhcpc4_prep(p + 28, (unsigned int)
|
||||
(sizeof(pkt) - LWS_PRE - 28), r, n);
|
||||
if (n < 0) {
|
||||
lwsl_err("%s: failed to prep\n", __func__);
|
||||
break;
|
||||
}
|
||||
|
||||
m = lws_plat_rawudp_broadcast(p, rawdisc4,
|
||||
LWS_ARRAY_SIZE(rawdisc4),
|
||||
(size_t)(n + 28),
|
||||
r->wsi_raw->desc.sockfd,
|
||||
(const char *)&r[1]);
|
||||
if (m < 0)
|
||||
lwsl_err("%s: Failed to write dhcp client req: "
|
||||
"%d %d, errno %d\n", __func__,
|
||||
n, m, LWS_ERRNO);
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
retry_conn:
|
||||
lws_retry_sul_schedule(wsi->a.context, 0, &r->sul_conn, &bo2,
|
||||
lws_dhcpc4_retry_conn,
|
||||
&r->retry_count_conn);
|
||||
|
||||
return -1;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct lws_protocols lws_system_protocol_dhcpc4 =
|
||||
{ "lws-dhcp4client", callback_dhcpc4, 0, 128, 0, NULL, 0 };
|
||||
|
||||
void
|
||||
lws_dhcpc4_retry_conn(struct lws_sorted_usec_list *sul)
|
||||
{
|
||||
lws_dhcpc_req_t *r = lws_container_of(sul, lws_dhcpc_req_t, sul_conn);
|
||||
|
||||
if (r->wsi_raw || !lws_dll2_is_detached(&r->sul_conn.list))
|
||||
return;
|
||||
|
||||
/* create the UDP socket aimed at the server */
|
||||
|
||||
r->retry_count_write = 0;
|
||||
r->wsi_raw = lws_create_adopt_udp(r->context->vhost_system, "0.0.0.0",
|
||||
68, LWS_CAUDP_PF_PACKET |
|
||||
LWS_CAUDP_BROADCAST,
|
||||
"lws-dhcp4client", (const char *)&r[1],
|
||||
NULL, NULL, &bo2, "dhcpc");
|
||||
lwsl_debug("%s: created wsi_raw: %s\n", __func__, lws_wsi_tag(r->wsi_raw));
|
||||
if (!r->wsi_raw) {
|
||||
lwsl_err("%s: unable to create udp skt\n", __func__);
|
||||
|
||||
lws_retry_sul_schedule(r->context, 0, &r->sul_conn, &bo2,
|
||||
lws_dhcpc4_retry_conn,
|
||||
&r->retry_count_conn);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
/* force the network if up */
|
||||
lws_plat_if_up((const char *)&r[1], r->wsi_raw->desc.sockfd, 0);
|
||||
lws_plat_if_up((const char *)&r[1], r->wsi_raw->desc.sockfd, 1);
|
||||
|
||||
r->wsi_raw->user_space = r;
|
||||
r->wsi_raw->user_space_externally_allocated = 1;
|
||||
|
||||
lws_get_random(r->wsi_raw->a.context, r->xid, 4);
|
||||
}
|
||||
|
||||
static void
|
||||
lws_sa46_set_ipv4(lws_dhcpc_req_t *r, unsigned int which, uint8_t *p)
|
||||
{
|
||||
r->is.sa46[which].sa4.sin_family = AF_INET;
|
||||
r->is.sa46[which].sa4.sin_addr.s_addr = ntohl(lws_ser_ru32be(p));
|
||||
}
|
||||
|
||||
int
|
||||
lws_dhcpc4_parse(lws_dhcpc_req_t *r, void *in, size_t len)
|
||||
{
|
||||
uint8_t pkt[LWS_PRE + 576], *p = pkt + LWS_PRE, *end;
|
||||
int n, m;
|
||||
|
||||
switch (r->state) {
|
||||
case LDHC_INIT: /* expect DHCPOFFER */
|
||||
case LDHC_REQUESTING: /* expect DHCPACK */
|
||||
/*
|
||||
* We should check carefully if we like what we were
|
||||
* sent... anything can spam us with crafted replies
|
||||
*/
|
||||
if (len < 0x100)
|
||||
break;
|
||||
|
||||
p = (uint8_t *)in + 28; /* skip to UDP payload */
|
||||
if (p[0] != 2 || p[1] != 1 || p[2] != 6)
|
||||
break;
|
||||
|
||||
if (memcmp(&p[4], r->xid, 4)) /* must be our xid */
|
||||
break;
|
||||
|
||||
if (memcmp(&p[0x1c], r->is.mac, 6)) /* our netif mac? */
|
||||
break;
|
||||
|
||||
/* the DHCP magic cookie must be in place */
|
||||
if (lws_ser_ru32be(&p[0xec]) != 0x63825363)
|
||||
break;
|
||||
|
||||
/* "your" client IP address */
|
||||
lws_sa46_set_ipv4(r, LWSDH_SA46_IP, p + 0x10);
|
||||
/* IP of next server used in bootstrap */
|
||||
lws_sa46_set_ipv4(r, LWSDH_SA46_DHCP_SERVER, p + 0x14);
|
||||
|
||||
/* it looks legit so far... look at the options */
|
||||
|
||||
end = (uint8_t *)in + len;
|
||||
p += 0xec + 4;
|
||||
while (p < end) {
|
||||
uint8_t c = *p++;
|
||||
uint8_t l = 0;
|
||||
|
||||
if (c && c != 0xff) {
|
||||
/* pad 0 and EOT 0xff have no length */
|
||||
l = *p++;
|
||||
if (!l) {
|
||||
lwsl_err("%s: zero length\n",
|
||||
__func__);
|
||||
goto broken;
|
||||
}
|
||||
if (p + l > end) {
|
||||
/* ...nice try... */
|
||||
lwsl_err("%s: bad len\n",
|
||||
__func__);
|
||||
goto broken;
|
||||
}
|
||||
}
|
||||
|
||||
if (c == 0xff) /* end of options */
|
||||
break;
|
||||
|
||||
m = 0;
|
||||
switch (c) {
|
||||
case LWSDHC4POPT_SUBNET_MASK:
|
||||
n = LWSDH_IPV4_SUBNET_MASK;
|
||||
goto get_ipv4;
|
||||
|
||||
case LWSDHC4POPT_ROUTER:
|
||||
lws_sa46_set_ipv4(r, LWSDH_SA46_IPV4_ROUTER, p);
|
||||
break;
|
||||
|
||||
case LWSDHC4POPT_TIME_SERVER:
|
||||
lws_sa46_set_ipv4(r, LWSDH_SA46_NTP_SERVER, p);
|
||||
break;
|
||||
|
||||
case LWSDHC4POPT_BROADCAST_ADS:
|
||||
n = LWSDH_IPV4_BROADCAST;
|
||||
goto get_ipv4;
|
||||
|
||||
case LWSDHC4POPT_LEASE_TIME:
|
||||
n = LWSDH_LEASE_SECS;
|
||||
goto get_ipv4;
|
||||
|
||||
case LWSDHC4POPT_RENEWAL_TIME: /* AKA T1 */
|
||||
n = LWSDH_RENEWAL_SECS;
|
||||
goto get_ipv4;
|
||||
|
||||
case LWSDHC4POPT_REBINDING_TIME: /* AKA T2 */
|
||||
n = LWSDH_REBINDING_SECS;
|
||||
goto get_ipv4;
|
||||
|
||||
case LWSDHC4POPT_DNSERVER:
|
||||
if (l & 3)
|
||||
break;
|
||||
m = LWSDH_SA46_DNS_SRV_1;
|
||||
while (l && m - LWSDH_SA46_DNS_SRV_1 < 4) {
|
||||
lws_sa46_set_ipv4(r, (unsigned int)m, p);
|
||||
lws_async_dns_server_add(r->context,
|
||||
&r->is.sa46[m]);
|
||||
l = (uint8_t)(l - 4);
|
||||
p += 4;
|
||||
m++;
|
||||
}
|
||||
break;
|
||||
|
||||
case LWSDHC4POPT_DOMAIN_NAME:
|
||||
m = l;
|
||||
if (m > (int)sizeof(r->is.domain) - 1)
|
||||
m = sizeof(r->is.domain) - 1;
|
||||
lws_strnncpy(r->is.domain, (const char *)p,
|
||||
(unsigned int)m, sizeof(r->is.domain));
|
||||
break;
|
||||
|
||||
case LWSDHC4POPT_MESSAGE_TYPE:
|
||||
/*
|
||||
* Confirm this is the right message
|
||||
* for the state of the negotiation
|
||||
*/
|
||||
if (r->state == LDHC_INIT && *p != LWSDHC4POFFER)
|
||||
goto broken;
|
||||
if (r->state == LDHC_REQUESTING &&
|
||||
*p != LWSDHC4PACK)
|
||||
goto broken;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
p += l;
|
||||
continue;
|
||||
get_ipv4:
|
||||
if (l >= 4)
|
||||
r->is.nums[n] = ntohl(lws_ser_ru32be(p));
|
||||
p += l;
|
||||
continue;
|
||||
broken:
|
||||
memset(r->is.sa46, 0, sizeof(r->is.sa46));
|
||||
break;
|
||||
}
|
||||
|
||||
#if defined(_DEBUG)
|
||||
/* dump what we have parsed out */
|
||||
|
||||
for (n = 0; n < (int)_LWSDH_NUMS_COUNT; n++) {
|
||||
m = (int)ntohl(r->is.nums[n]);
|
||||
lwsl_info("%s: %d: 0x%x\n", __func__, n, m);
|
||||
}
|
||||
|
||||
for (n = 0; n < (int)_LWSDH_SA46_COUNT; n++) {
|
||||
lws_sa46_write_numeric_address(&r->is.sa46[n],
|
||||
(char *)pkt, 48);
|
||||
lwsl_info("%s: %d: %s\n", __func__, n, pkt);
|
||||
}
|
||||
#endif
|
||||
|
||||
/*
|
||||
* Having seen everything in there... do we really feel
|
||||
* we could use it? Everything critical is there?
|
||||
*/
|
||||
|
||||
if (!r->is.sa46[LWSDH_SA46_IP].sa4.sin_family ||
|
||||
!r->is.sa46[LWSDH_SA46_DHCP_SERVER].sa4.sin_family ||
|
||||
!r->is.sa46[LWSDH_SA46_IPV4_ROUTER].sa4.sin_family ||
|
||||
!r->is.nums[LWSDH_IPV4_SUBNET_MASK] ||
|
||||
!r->is.nums[LWSDH_LEASE_SECS] ||
|
||||
!r->is.sa46[LWSDH_SA46_DNS_SRV_1].sa4.sin_family) {
|
||||
lwsl_notice("%s: rejecting on incomplete\n", __func__);
|
||||
memset(r->is.sa46, 0, sizeof(r->is.sa46));
|
||||
break;
|
||||
}
|
||||
|
||||
/*
|
||||
* Network layout has to be internally consistent...
|
||||
* DHCP server has to be reachable by broadcast and
|
||||
* default route has to be on same subnet
|
||||
*/
|
||||
|
||||
if ((r->is.sa46[LWSDH_SA46_IP].sa4.sin_addr.s_addr &
|
||||
r->is.nums[LWSDH_IPV4_SUBNET_MASK]) !=
|
||||
(r->is.sa46[LWSDH_SA46_DHCP_SERVER].sa4.sin_addr.s_addr &
|
||||
r->is.nums[LWSDH_IPV4_SUBNET_MASK])) {
|
||||
lwsl_notice("%s: rejecting on srv %x reachable on mask %x\n",
|
||||
__func__, r->is.sa46[LWSDH_SA46_IP].sa4.sin_addr.s_addr,
|
||||
r->is.nums[LWSDH_IPV4_SUBNET_MASK]);
|
||||
break;
|
||||
}
|
||||
|
||||
if (r->state == LDHC_INIT) {
|
||||
lwsl_info("%s: moving to REQ\n", __func__);
|
||||
r->state = LDHC_REQUESTING;
|
||||
lws_callback_on_writable(r->wsi_raw);
|
||||
//break;
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
156
Kinc/Sources/kinc/libs/core/dhcpclient/dhcpclient.c
Normal file
156
Kinc/Sources/kinc/libs/core/dhcpclient/dhcpclient.c
Normal file
@ -0,0 +1,156 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
#include "private-lib-core.h"
|
||||
#include "private-lib-system-dhcpclient.h"
|
||||
|
||||
void
|
||||
lws_dhcpc_retry_write(struct lws_sorted_usec_list *sul)
|
||||
{
|
||||
lws_dhcpc_req_t *r = lws_container_of(sul, lws_dhcpc_req_t, sul_write);
|
||||
|
||||
lwsl_debug("%s\n", __func__);
|
||||
|
||||
if (r && r->wsi_raw)
|
||||
lws_callback_on_writable(r->wsi_raw);
|
||||
}
|
||||
|
||||
static void
|
||||
lws_dhcpc_destroy(lws_dhcpc_req_t **pr)
|
||||
{
|
||||
lws_dhcpc_req_t *r = *pr;
|
||||
|
||||
lws_sul_cancel(&r->sul_conn);
|
||||
lws_sul_cancel(&r->sul_write);
|
||||
lws_sul_cancel(&r->sul_renew);
|
||||
|
||||
if (r->wsi_raw)
|
||||
lws_set_timeout(r->wsi_raw, 1, LWS_TO_KILL_ASYNC);
|
||||
|
||||
lws_dll2_remove(&r->list);
|
||||
|
||||
lws_free_set_NULL(r);
|
||||
}
|
||||
|
||||
int
|
||||
lws_dhcpc_status(struct lws_context *context, lws_sockaddr46 *sa46)
|
||||
{
|
||||
lws_dhcpc_req_t *r;
|
||||
|
||||
lws_start_foreach_dll(struct lws_dll2 *, p, context->dhcpc_owner.head) {
|
||||
r = (lws_dhcpc_req_t *)p;
|
||||
|
||||
if (r->state == LDHC_BOUND) {
|
||||
if (sa46) {
|
||||
memcpy(sa46, &r->is.sa46[LWSDH_SA46_DNS_SRV_1],
|
||||
sizeof(*sa46));
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
} lws_end_foreach_dll(p);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static lws_dhcpc_req_t *
|
||||
lws_dhcpc_find(struct lws_context *context, const char *iface, int af)
|
||||
{
|
||||
lws_dhcpc_req_t *r;
|
||||
|
||||
/* see if we are already looking after this af / iface combination */
|
||||
|
||||
lws_start_foreach_dll(struct lws_dll2 *, p, context->dhcpc_owner.head) {
|
||||
r = (lws_dhcpc_req_t *)p;
|
||||
|
||||
if (!strcmp((const char *)&r[1], iface) && af == r->af)
|
||||
return r; /* yes... */
|
||||
|
||||
} lws_end_foreach_dll(p);
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
* Create a persistent dhcp client entry for network interface "iface" and AF
|
||||
* type "af"
|
||||
*/
|
||||
|
||||
int
|
||||
lws_dhcpc_request(struct lws_context *context, const char *iface, int af,
|
||||
dhcpc_cb_t cb, void *opaque)
|
||||
{
|
||||
lws_dhcpc_req_t *r = lws_dhcpc_find(context, iface, af);
|
||||
int n;
|
||||
|
||||
/* see if we are already looking after this af / iface combination */
|
||||
|
||||
if (r)
|
||||
return 0;
|
||||
|
||||
/* nope... let's create a request object as he asks */
|
||||
|
||||
n = (int)strlen(iface);
|
||||
r = lws_zalloc(sizeof(*r) + (unsigned int)n + 1u, __func__);
|
||||
if (!r)
|
||||
return 1;
|
||||
|
||||
memcpy(&r[1], iface, (unsigned int)n + 1);
|
||||
r->af = (uint8_t)af;
|
||||
r->cb = cb;
|
||||
r->opaque = opaque;
|
||||
r->context = context;
|
||||
r->state = LDHC_INIT;
|
||||
|
||||
lws_strncpy(r->is.ifname, iface, sizeof(r->is.ifname));
|
||||
|
||||
lws_dll2_add_head(&r->list, &context->dhcpc_owner); /* add him to list */
|
||||
|
||||
lws_dhcpc4_retry_conn(&r->sul_conn);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/*
|
||||
* Destroy every DHCP client object related to interface "iface"
|
||||
*/
|
||||
|
||||
static int
|
||||
_remove_if(struct lws_dll2 *d, void *opaque)
|
||||
{
|
||||
lws_dhcpc_req_t *r = lws_container_of(d, lws_dhcpc_req_t, list);
|
||||
|
||||
if (!opaque || !strcmp((const char *)&r[1], (const char *)opaque))
|
||||
lws_dhcpc_destroy(&r);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
lws_dhcpc_remove(struct lws_context *context, const char *iface)
|
||||
{
|
||||
lws_dll2_foreach_safe(&context->dhcpc_owner, (void *)iface, _remove_if);
|
||||
|
||||
return 0;
|
||||
}
|
@ -0,0 +1,112 @@
|
||||
/*
|
||||
* libwebsockets - small server side websockets and web server implementation
|
||||
*
|
||||
* Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
|
||||
typedef enum {
|
||||
LDHC_INIT_REBOOT,
|
||||
LDHC_REBOOTING, /* jitterwait */
|
||||
LDHC_INIT, /* issue DHCPDISCOVER */
|
||||
LDHC_SELECTING,
|
||||
LDHC_REQUESTING,
|
||||
LDHC_REBINDING,
|
||||
LDHC_BOUND,
|
||||
LDHC_RENEWING
|
||||
} lws_dhcpc_state_t;
|
||||
|
||||
enum {
|
||||
LWSDHC4PDISCOVER = 1,
|
||||
LWSDHC4POFFER,
|
||||
LWSDHC4PREQUEST,
|
||||
LWSDHC4PDECLINE,
|
||||
LWSDHC4PACK,
|
||||
LWSDHC4PNACK,
|
||||
LWSDHC4PRELEASE,
|
||||
|
||||
LWSDHC4POPT_PAD = 0,
|
||||
LWSDHC4POPT_SUBNET_MASK = 1,
|
||||
LWSDHC4POPT_TIME_OFFSET = 2,
|
||||
LWSDHC4POPT_ROUTER = 3,
|
||||
LWSDHC4POPT_TIME_SERVER = 4,
|
||||
LWSDHC4POPT_NAME_SERVER = 5,
|
||||
LWSDHC4POPT_DNSERVER = 6,
|
||||
LWSDHC4POPT_LOG_SERVER = 7,
|
||||
LWSDHC4POPT_COOKIE_SERVER = 8,
|
||||
LWSDHC4POPT_LPR_SERVER = 9,
|
||||
LWSDHC4POPT_IMPRESS_SERVER = 10,
|
||||
LWSDHC4POPT_RESLOC_SERVER = 11,
|
||||
LWSDHC4POPT_HOST_NAME = 12,
|
||||
LWSDHC4POPT_BOOTFILE_SIZE = 13,
|
||||
LWSDHC4POPT_MERIT_DUMP_FILE = 14,
|
||||
LWSDHC4POPT_DOMAIN_NAME = 15,
|
||||
LWSDHC4POPT_SWAP_SERVER = 16,
|
||||
LWSDHC4POPT_ROOT_PATH = 17,
|
||||
LWSDHC4POPT_EXTENSIONS_PATH = 18,
|
||||
LWSDHC4POPT_BROADCAST_ADS = 28,
|
||||
|
||||
LWSDHC4POPT_REQUESTED_ADS = 50,
|
||||
LWSDHC4POPT_LEASE_TIME = 51,
|
||||
LWSDHC4POPT_OPTION_OVERLOAD = 52,
|
||||
LWSDHC4POPT_MESSAGE_TYPE = 53,
|
||||
LWSDHC4POPT_SERVER_ID = 54,
|
||||
LWSDHC4POPT_PARAM_REQ_LIST = 55,
|
||||
LWSDHC4POPT_MESSAGE = 56,
|
||||
LWSDHC4POPT_MAX_DHCP_MSG_SIZE = 57,
|
||||
LWSDHC4POPT_RENEWAL_TIME = 58, /* AKA T1 */
|
||||
LWSDHC4POPT_REBINDING_TIME = 59, /* AKA T2 */
|
||||
LWSDHC4POPT_VENDOR_CLASS_ID = 60,
|
||||
LWSDHC4POPT_CLIENT_ID = 61,
|
||||
|
||||
LWSDHC4POPT_END_OPTIONS = 255
|
||||
};
|
||||
|
||||
typedef struct lws_dhcpc_req {
|
||||
lws_dll2_t list;
|
||||
|
||||
struct lws_context *context;
|
||||
lws_sorted_usec_list_t sul_renew;
|
||||
lws_sorted_usec_list_t sul_conn;
|
||||
lws_sorted_usec_list_t sul_write;
|
||||
dhcpc_cb_t cb; /* cb on completion / failure */
|
||||
void *opaque; /* ignored by lws, give to cb */
|
||||
|
||||
/* these are separated so we can close the bcast one asynchronously */
|
||||
struct lws *wsi_raw; /* for broadcast */
|
||||
lws_dhcpc_state_t state;
|
||||
|
||||
lws_dhcpc_ifstate_t is;
|
||||
|
||||
uint16_t retry_count_conn;
|
||||
uint16_t retry_count_write;
|
||||
uint8_t xid[4];
|
||||
uint8_t af; /* address family */
|
||||
} lws_dhcpc_req_t;
|
||||
/* interface name is overallocated here */
|
||||
|
||||
void
|
||||
lws_dhcpc4_retry_conn(struct lws_sorted_usec_list *sul);
|
||||
|
||||
int
|
||||
lws_dhcpc4_parse(lws_dhcpc_req_t *r, void *in, size_t len);
|
||||
|
||||
void
|
||||
lws_dhcpc_retry_write(struct lws_sorted_usec_list *sul);
|
Reference in New Issue
Block a user