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