BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file dhcpclient_test.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. 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 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29  
30 #include <stddef.h>
31  
32 #include <misc/debug.h>
33 #include <base/DebugObject.h>
34 #include <base/BLog.h>
35 #include <system/BReactor.h>
36 #include <system/BSignal.h>
37 #include <system/BTime.h>
38 #include <system/BNetwork.h>
39 #include <dhcpclient/BDHCPClient.h>
40  
41 BReactor reactor;
42 BRandom2 random2;
43 BDHCPClient dhcp;
44  
45 static void signal_handler (void *user);
46 static void dhcp_handler (void *unused, int event);
47  
48 int main (int argc, char **argv)
49 {
50 if (argc <= 0) {
51 return 1;
52 }
53  
54 if (argc != 2) {
55 printf("Usage: %s <interface>\n", argv[0]);
56 goto fail0;
57 }
58  
59 char *ifname = argv[1];
60  
61 BTime_Init();
62  
63 BLog_InitStdout();
64  
65 if (!BNetwork_GlobalInit()) {
66 DEBUG("BNetwork_GlobalInit failed");
67 goto fail1;
68 }
69  
70 if (!BReactor_Init(&reactor)) {
71 DEBUG("BReactor_Init failed");
72 goto fail1;
73 }
74  
75 if (!BRandom2_Init(&random2, 0)) {
76 DEBUG("BRandom2_Init failed");
77 goto fail1a;
78 }
79  
80 if (!BSignal_Init(&reactor, signal_handler, NULL)) {
81 DEBUG("BSignal_Init failed");
82 goto fail2;
83 }
84  
85 struct BDHCPClient_opts opts = {};
86  
87 if (!BDHCPClient_Init(&dhcp, ifname, opts, &reactor, &random2, dhcp_handler, NULL)) {
88 DEBUG("BDHCPClient_Init failed");
89 goto fail3;
90 }
91  
92 BReactor_Exec(&reactor);
93  
94 BDHCPClient_Free(&dhcp);
95 fail3:
96 BSignal_Finish();
97 fail2:
98 BRandom2_Free(&random2);
99 fail1a:
100 BReactor_Free(&reactor);
101 fail1:
102 BLog_Free();
103 fail0:
104 DebugObjectGlobal_Finish();
105  
106 return 1;
107 }
108  
109 void signal_handler (void *user)
110 {
111 DEBUG("termination requested");
112  
113 BReactor_Quit(&reactor, 0);
114 }
115  
116 void dhcp_handler (void *unused, int event)
117 {
118 switch (event) {
119 case BDHCPCLIENT_EVENT_UP: {
120 printf("DHCP: up");
121  
122 uint32_t ip;
123 uint8_t *ipb = (void *)&ip;
124  
125 BDHCPClient_GetClientIP(&dhcp, &ip);
126 printf(" IP=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
127  
128 BDHCPClient_GetClientMask(&dhcp, &ip);
129 printf(" Mask=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
130  
131 if (BDHCPClient_GetRouter(&dhcp, &ip)) {
132 printf(" Router=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
133 }
134  
135 uint32_t dns[BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS];
136 int num = BDHCPClient_GetDNS(&dhcp, dns, BDHCPCLIENT_MAX_DOMAIN_NAME_SERVERS);
137 for (int i = 0; i < num; i++) {
138 ip=dns[i];
139 printf(" DNS=%"PRIu8".%"PRIu8".%"PRIu8".%"PRIu8, ipb[0], ipb[1], ipb[2], ipb[3]);
140 }
141  
142 printf("\n");
143 } break;
144  
145 case BDHCPCLIENT_EVENT_DOWN: {
146 printf("DHCP: down\n");
147 } break;
148  
149 case BDHCPCLIENT_EVENT_ERROR: {
150 printf("DHCP: error\n");
151  
152 // exit reactor
153 BReactor_Quit(&reactor, 0);
154 } break;
155  
156 default:
157 ASSERT(0);
158 }
159 }