device: Combine two if statements with identical outcome
[framework/connectivity/connman.git] / tools / dhcp-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/ioctl.h>
32 #include <sys/socket.h>
33 #include <arpa/inet.h>
34 #include <net/route.h>
35 #include <net/ethernet.h>
36 #include <linux/if_arp.h>
37
38 #include <gdhcp/gdhcp.h>
39
40 static GTimer *timer;
41
42 static GMainLoop *main_loop;
43
44 static void sig_term(int sig)
45 {
46         g_main_loop_quit(main_loop);
47 }
48
49 static void print_elapsed(void)
50 {
51         gdouble elapsed;
52
53         elapsed = g_timer_elapsed(timer, NULL);
54
55         printf("elapsed: %f seconds\n", elapsed);
56 }
57
58 static void handle_error(GDHCPClientError error)
59 {
60         switch (error) {
61         case G_DHCP_CLIENT_ERROR_NONE:
62                 printf("dhcp client ok\n");
63                 break;
64         case G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE:
65                 printf("Interface unavailable\n");
66                 break;
67         case G_DHCP_CLIENT_ERROR_INTERFACE_IN_USE:
68                 printf("Interface in use\n");
69                 break;
70         case G_DHCP_CLIENT_ERROR_INTERFACE_DOWN:
71                 printf("Interface down\n");
72                 break;
73         case G_DHCP_CLIENT_ERROR_NOMEM:
74                 printf("No memory\n");
75                 break;
76         case G_DHCP_CLIENT_ERROR_INVALID_INDEX:
77                 printf("Invalid index\n");
78                 break;
79         case G_DHCP_CLIENT_ERROR_INVALID_OPTION:
80                 printf("Invalid option\n");
81                 break;
82         }
83 }
84
85 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
86 {
87         print_elapsed();
88
89         printf("No lease available\n");
90
91         g_main_loop_quit(main_loop);
92 }
93
94 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
95 {
96         GList *list, *option_value = NULL;
97         char *address;
98
99         print_elapsed();
100
101         printf("Lease available\n");
102
103         address = g_dhcp_client_get_address(dhcp_client);
104         printf("address %s\n", address);
105         if (address == NULL)
106                 return;
107
108         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
109         for (list = option_value; list; list = list->next)
110                 printf("sub-mask %s\n", (char *) list->data);
111
112         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
113         for (list = option_value; list; list = list->next)
114                 printf("domain-name-servers %s\n", (char *) list->data);
115
116         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
117         for (list = option_value; list; list = list->next)
118                 printf("domain-name %s\n", (char *) list->data);
119
120         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
121         for (list = option_value; list; list = list->next)
122                 printf("routers %s\n", (char *) list->data);
123
124         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
125         for (list = option_value; list; list = list->next)
126                 printf("hostname %s\n", (char *) list->data);
127 }
128
129 int main(int argc, char *argv[])
130 {
131         struct sigaction sa;
132         GDHCPClientError error;
133         GDHCPClient *dhcp_client;
134         int index;
135
136         if (argc < 2) {
137                 printf("Usage: dhcp-test <interface index>\n");
138                 exit(0);
139         }
140
141         index = atoi(argv[1]);
142
143         printf("Create DHCP client for interface %d\n", index);
144
145         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
146         if (dhcp_client == NULL) {
147                 handle_error(error);
148                 exit(0);
149         }
150
151         g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
152
153         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
154         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
155         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
156         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
157         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
158         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
159
160         g_dhcp_client_register_event(dhcp_client,
161                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
162                                                 lease_available_cb, NULL);
163
164         g_dhcp_client_register_event(dhcp_client,
165                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
166
167         main_loop = g_main_loop_new(NULL, FALSE);
168
169         printf("Start DHCP operation\n");
170
171         timer = g_timer_new();
172
173         g_dhcp_client_start(dhcp_client, NULL);
174
175         memset(&sa, 0, sizeof(sa));
176         sa.sa_handler = sig_term;
177         sigaction(SIGINT, &sa, NULL);
178         sigaction(SIGTERM, &sa, NULL);
179
180         g_main_loop_run(main_loop);
181
182         g_timer_destroy(timer);
183
184         g_dhcp_client_unref(dhcp_client);
185
186         g_main_loop_unref(main_loop);
187
188         return 0;
189 }