1 /*
2 * libwebsockets - small server side websockets and web server implementation
3 *
4 * Copyright (C) 2010 - 2021 Andy Green <andy@warmcat.com>
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
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell 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 THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 * IN THE SOFTWARE.
23 */
24
25#include "private-lib-core.h"
26#include "private-lib-system-dhcpclient.h"
27
28void
29lws_dhcpc_retry_write(struct lws_sorted_usec_list *sul)
30{
31	lws_dhcpc_req_t *r = lws_container_of(sul, lws_dhcpc_req_t, sul_write);
32
33	lwsl_debug("%s\n", __func__);
34
35	if (r && r->wsi_raw)
36		lws_callback_on_writable(r->wsi_raw);
37}
38
39static void
40lws_dhcpc_destroy(lws_dhcpc_req_t **pr)
41{
42	lws_dhcpc_req_t *r = *pr;
43
44	lws_sul_cancel(&r->sul_conn);
45	lws_sul_cancel(&r->sul_write);
46	lws_sul_cancel(&r->sul_renew);
47
48	if (r->wsi_raw)
49		lws_set_timeout(r->wsi_raw, 1, LWS_TO_KILL_ASYNC);
50
51	lws_dll2_remove(&r->list);
52
53	lws_free_set_NULL(r);
54}
55
56int
57lws_dhcpc_status(struct lws_context *context, lws_sockaddr46 *sa46)
58{
59	lws_dhcpc_req_t *r;
60
61	lws_start_foreach_dll(struct lws_dll2 *, p, context->dhcpc_owner.head) {
62		r = (lws_dhcpc_req_t *)p;
63
64		if (r->state == LDHC_BOUND) {
65			if (sa46) {
66				memcpy(sa46, &r->is.sa46[LWSDH_SA46_DNS_SRV_1],
67				       sizeof(*sa46));
68			}
69			return 1;
70		}
71
72	} lws_end_foreach_dll(p);
73
74	return 0;
75}
76
77static lws_dhcpc_req_t *
78lws_dhcpc_find(struct lws_context *context, const char *iface, int af)
79{
80	lws_dhcpc_req_t *r;
81
82	/* see if we are already looking after this af / iface combination */
83
84	lws_start_foreach_dll(struct lws_dll2 *, p, context->dhcpc_owner.head) {
85		r = (lws_dhcpc_req_t *)p;
86
87		if (!strcmp((const char *)&r[1], iface) && af == r->af)
88			return r; /* yes...  */
89
90	} lws_end_foreach_dll(p);
91
92	return NULL;
93}
94
95/*
96 * Create a persistent dhcp client entry for network interface "iface" and AF
97 * type "af"
98 */
99
100int
101lws_dhcpc_request(struct lws_context *context, const char *iface, int af,
102		  dhcpc_cb_t cb, void *opaque)
103{
104	lws_dhcpc_req_t *r = lws_dhcpc_find(context, iface, af);
105	int n;
106
107	/* see if we are already looking after this af / iface combination */
108
109	if (r)
110		return 0;
111
112	/* nope... let's create a request object as he asks */
113
114	n = (int)strlen(iface);
115	r = lws_zalloc(sizeof(*r) + (unsigned int)n + 1u, __func__);
116	if (!r)
117		return 1;
118
119	memcpy(&r[1], iface, (unsigned int)n + 1);
120	r->af = (uint8_t)af;
121	r->cb = cb;
122	r->opaque = opaque;
123	r->context = context;
124	r->state = LDHC_INIT;
125
126	lws_strncpy(r->is.ifname, iface, sizeof(r->is.ifname));
127
128	lws_dll2_add_head(&r->list, &context->dhcpc_owner); /* add him to list */
129
130	lws_dhcpc4_retry_conn(&r->sul_conn);
131
132	return 0;
133}
134
135/*
136 * Destroy every DHCP client object related to interface "iface"
137 */
138
139static int
140_remove_if(struct lws_dll2 *d, void *opaque)
141{
142	lws_dhcpc_req_t *r = lws_container_of(d, lws_dhcpc_req_t, list);
143
144	if (!opaque || !strcmp((const char *)&r[1], (const char *)opaque))
145		lws_dhcpc_destroy(&r);
146
147	return 0;
148}
149
150int
151lws_dhcpc_remove(struct lws_context *context, const char *iface)
152{
153	lws_dll2_foreach_safe(&context->dhcpc_owner, (void *)iface, _remove_if);
154
155	return 0;
156}
157