Destroy timer of DHCP test program
[framework/connectivity/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/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_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_INTERFACE_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         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
124         for (list = option_value; list; list = list->next)
125                 printf("hostname %s\n", (char *) list->data);
126
127         print_timestamp();
128 }
129
130 int main(int argc, char *argv[])
131 {
132         struct sigaction sa;
133         GDHCPClientError error;
134         GDHCPClient *dhcp_client;
135         int index;
136
137         if (argc < 2) {
138                 printf("Usage: dhcp-test <interface index>\n");
139                 exit(0);
140         }
141
142         index = atoi(argv[1]);
143
144         printf("Create DHCP client for interface %d\n", index);
145
146         timer = g_timer_new();
147
148         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
149         if (dhcp_client == NULL) {
150                 handle_error(error);
151                 exit(0);
152         }
153
154         g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
155
156         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
157         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
158         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
159         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
160         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
161
162         g_dhcp_client_register_event(dhcp_client,
163                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
164                                                 lease_available_cb, NULL);
165
166         g_dhcp_client_register_event(dhcp_client,
167                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
168
169         main_loop = g_main_loop_new(NULL, FALSE);
170
171         print_timestamp();
172
173         printf("Start DHCP operation\n");
174
175         g_dhcp_client_start(dhcp_client);
176
177         memset(&sa, 0, sizeof(sa));
178         sa.sa_handler = sig_term;
179         sigaction(SIGINT, &sa, NULL);
180         sigaction(SIGTERM, &sa, NULL);
181
182         g_main_loop_run(main_loop);
183
184         g_dhcp_client_unref(dhcp_client);
185
186         g_main_loop_unref(main_loop);
187
188         g_timer_destroy(timer);
189
190         return 0;
191 }