• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * lws-api-test-dhcpc
3  *
4  * Written in 2019 by Andy Green <andy@warmcat.com>
5  *
6  * This file is made available under the Creative Commons CC0 1.0
7  * Universal Public Domain Dedication.
8  */
9 
10 #include <libwebsockets.h>
11 #include <signal.h>
12 
13 static int interrupted, ok, fail, exp = 1;
14 struct lws_context *context;
15 const char *nif;
16 
17 static int
lws_dhcpc_cb(void * opaque,int af,uint8_t * ip,int ip_len)18 lws_dhcpc_cb(void *opaque, int af, uint8_t *ip, int ip_len)
19 {
20 	lwsl_user("%s: dhcp set OK\n", __func__);
21 	ok = 1;
22 	interrupted = 1;
23 	return 0;
24 }
25 
sigint_handler(int sig)26 void sigint_handler(int sig)
27 {
28 	interrupted = 1;
29 }
30 
31 int
main(int argc,const char ** argv)32 main(int argc, const char **argv)
33 {
34 	struct lws_context_creation_info info;
35 	const char *p;
36 	int n = 1;
37 
38 	signal(SIGINT, sigint_handler);
39 
40 	memset(&info, 0, sizeof info); /* otherwise uninitialized garbage */
41 	lws_cmdline_option_handle_builtin(argc, argv, &info);
42 	lwsl_user("LWS API selftest: DHCP Client\n");
43 
44 	info.port = CONTEXT_PORT_NO_LISTEN;
45 	info.options = LWS_SERVER_OPTION_DO_SSL_GLOBAL_INIT;
46 
47 	if ((p = lws_cmdline_option(argc, argv, "-i")))
48 		nif = p;
49 
50 	context = lws_create_context(&info);
51 	if (!context) {
52 		lwsl_err("lws init failed\n");
53 		return 1;
54 	}
55 
56 	if (nif) {
57 		lwsl_user("%s: requesting DHCP for %s\n", __func__, nif);
58 		lws_dhcpc_request(context, nif, AF_INET, lws_dhcpc_cb, NULL);
59 	} else {
60 		lwsl_err("%s: use -i <network-interface> to select if\n", __func__);
61 		interrupted = 1;
62 	}
63 
64 	/* the usual lws event loop */
65 
66 	n = 1;
67 	while (n >= 0 && !interrupted)
68 		n = lws_service(context, 0);
69 
70 	lws_context_destroy(context);
71 
72 	if (fail || ok != exp)
73 		lwsl_user("Completed: PASS: %d / %d, FAIL: %d\n", ok, exp,
74 				fail);
75 	else
76 		lwsl_user("Completed: ALL PASS: %d / %d\n", ok, exp);
77 
78 	return !(ok == exp && !fail);
79 }
80