Add initial version of DHCP client test tool
[platform/upstream/connman.git] / tools / dhcp-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  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.h>
39
40 static GTimer *timer;
41
42 static GMainLoop *main_loop = NULL;
43
44 static void sig_term(int sig)
45 {
46         g_main_loop_quit(main_loop);
47 }
48
49 static void print_timestamp(void)
50 {
51         GTimeVal timestamp;
52         gdouble elapsed;
53         char *str;
54
55         g_get_current_time(&timestamp);
56         str = g_time_val_to_iso8601(&timestamp);
57         printf("=== %s ===\n", str);
58         g_free(str);
59
60         elapsed = g_timer_elapsed(timer, NULL);
61
62         printf("elapsed: %f seconds\n", elapsed);
63 }
64
65 static void handle_error(GDHCPClientError error)
66 {
67         switch (error) {
68         case G_DHCP_CLIENT_ERROR_NONE:
69                 printf("dhcp client ok\n");
70                 break;
71         case G_DHCP_CLIENT_ERROR_INERFACE_UNAVAILABLE:
72                 printf("Interface unavailable\n");
73                 break;
74         case G_DHCP_CLIENT_ERROR_INTERFACE_IN_USE:
75                 printf("Interface in use\n");
76                 break;
77         case G_DHCP_CLIENT_ERROR_INTERFACE_DOWN:
78                 printf("Interface down\n");
79                 break;
80         case G_DHCP_CLIENT_ERROR_NOMEM:
81                 printf("No memory\n");
82                 break;
83         case G_DHCP_CLIENT_ERROR_INVALID_INDEX:
84                 printf("Invalid index\n");
85                 break;
86         case G_DHCP_CLIENT_ERROR_INVALID_OPTION:
87                 printf("Invalid option\n");
88                 break;
89         }
90 }
91
92 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
93 {
94         printf("No Lease Available!\n");
95
96         print_timestamp();
97
98         g_main_loop_quit(main_loop);
99 }
100
101 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
102 {
103         GList *list, *option_value = NULL;
104         char *address;
105
106         address = g_dhcp_client_get_address(dhcp_client);
107         printf("address %s\n", address);
108         if (address == NULL)
109                 return;
110
111         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
112         for (list = option_value; list; list = list->next)
113                 printf("sub-mask %s\n", (char *) list->data);
114
115         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
116         for (list = option_value; list; list = list->next)
117                 printf("domain-name-servers %s\n", (char *) list->data);
118
119         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
120         for (list = option_value; list; list = list->next)
121                 printf("routers %s\n", (char *) list->data);
122
123         print_timestamp();
124 }
125
126 int main(int argc, char *argv[])
127 {
128         struct sigaction sa;
129         GDHCPClientError error;
130         GDHCPClient *dhcp_client;
131         int index;
132
133         if (argc < 2) {
134                 printf("Usage: dhcp-test <interface index>\n");
135                 exit(0);
136         }
137
138         index = atoi(argv[1]);
139
140         printf("Create DHCP client for interface %d\n", index);
141
142         timer = g_timer_new();
143
144         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
145         if (dhcp_client == NULL) {
146                 handle_error(error);
147                 exit(0);
148         }
149
150         g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
151
152         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
153         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
154         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
155         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
156         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
157
158         g_dhcp_client_register_event(dhcp_client,
159                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
160                                                 lease_available_cb, NULL);
161
162         g_dhcp_client_register_event(dhcp_client,
163                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
164
165         main_loop = g_main_loop_new(NULL, FALSE);
166
167         print_timestamp();
168
169         printf("Start DHCP operation\n");
170
171         g_dhcp_client_start(dhcp_client);
172
173         memset(&sa, 0, sizeof(sa));
174         sa.sa_handler = sig_term;
175         sigaction(SIGINT, &sa, NULL);
176         sigaction(SIGTERM, &sa, NULL);
177
178         g_main_loop_run(main_loop);
179
180         g_dhcp_client_unref(dhcp_client);
181
182         g_main_loop_unref(main_loop);
183
184         return 0;
185 }