File: main.c

package info (click to toggle)
libwebsockets 4.3.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,288 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (103 lines) | stat: -rw-r--r-- 2,204 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*
 * lws-api-test-dhcpc
 *
 * Written in 2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 */

#include <libwebsockets.h>
#include <signal.h>

static int interrupted, ok, fail, exp = 1;
struct lws_context *context;
const char *nif;

static const char * const sa46_names[] = {
	"LWSDH_SA46_IP",
	"LWSDH_SA46_DNS_SRV_1",
	"LWSDH_SA46_DNS_SRV_2",
	"LWSDH_SA46_DNS_SRV_3",
	"LWSDH_SA46_DNS_SRV_4",
	"LWSDH_SA46_IPV4_ROUTER",
	"LWSDH_SA46_NTP_SERVER",
	"LWSDH_SA46_DHCP_SERVER",
};

static int
lws_dhcpc_cb(void *opaque, lws_dhcpc_ifstate_t *is)
{
	unsigned int n;
	char buf[64];

	lwsl_user("%s: dhcp set OK\n", __func__);

	for (n = 0; n < LWS_ARRAY_SIZE(sa46_names); n++) {
		lws_sa46_write_numeric_address(&is->sa46[n], buf, sizeof(buf));
		lwsl_notice("%s: %s: %s\n", __func__, sa46_names[n], buf);
	}

	ok = 1;
	interrupted = 1;
	return 0;
}

void sigint_handler(int sig)
{
	interrupted = 1;
}

int
main(int argc, const char **argv)
{
	struct lws_context_creation_info info;
#if !defined(__COVERITY__)
	const char *p;
#endif
	int n = 1;

	signal(SIGINT, sigint_handler);

	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
	lws_cmdline_option_handle_builtin(argc, argv, &info);
	lwsl_user("LWS API selftest: DHCP Client\n");

	info.port = CONTEXT_PORT_NO_LISTEN;
	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;

#if !defined(__COVERITY__)
	if ((p = lws_cmdline_option(argc, argv, "-i")))
		nif = p;
#endif

	context = lws_create_context(&info);
	if (!context) {
		lwsl_err("lws init failed\n");
		return 1;
	}

	if (nif) {
		lwsl_user("%s: requesting DHCP for %s\n", __func__, nif);
		lws_dhcpc_request(context, nif, AF_INET, lws_dhcpc_cb, NULL);
	} else {
		lwsl_err("%s: use -i <network-interface> to select if\n", __func__);
		interrupted = 1;
	}

	/* the usual lws event loop */

	n = 1;
	while (n >= 0 && !interrupted)
		n = lws_service(context, 0);

	lws_context_destroy(context);

	if (fail || ok != exp)
		lwsl_user("Completed: PASS: %d / %d, FAIL: %d\n", ok, exp,
				fail);
	else
		lwsl_user("Completed: ALL PASS: %d / %d\n", ok, exp);

	return !(ok == exp && !fail);
}