5 * Copyright (C) 2007-2012 Intel Corporation. All rights reserved.
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.
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.
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
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>
37 #include <gdhcp/gdhcp.h>
39 #include "../src/connman.h"
43 static GMainLoop *main_loop;
45 static void sig_term(int sig)
47 g_main_loop_quit(main_loop);
50 static void print_elapsed(void)
54 elapsed = g_timer_elapsed(timer, NULL);
56 printf("elapsed: %f seconds\n", elapsed);
59 static void handle_error(GDHCPClientError error)
62 case G_DHCP_CLIENT_ERROR_NONE:
63 printf("dhcp client ok\n");
65 case G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE:
66 printf("Interface unavailable\n");
68 case G_DHCP_CLIENT_ERROR_INTERFACE_IN_USE:
69 printf("Interface in use\n");
71 case G_DHCP_CLIENT_ERROR_INTERFACE_DOWN:
72 printf("Interface down\n");
74 case G_DHCP_CLIENT_ERROR_NOMEM:
75 printf("No memory\n");
77 case G_DHCP_CLIENT_ERROR_INVALID_INDEX:
78 printf("Invalid index\n");
80 case G_DHCP_CLIENT_ERROR_INVALID_OPTION:
81 printf("Invalid option\n");
86 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
90 printf("No lease available\n");
92 g_main_loop_quit(main_loop);
95 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
97 GList *list, *option_value = NULL;
102 printf("Lease available\n");
104 address = g_dhcp_client_get_address(dhcp_client);
105 printf("address %s\n", address);
109 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
110 for (list = option_value; list; list = list->next)
111 printf("sub-mask %s\n", (char *) list->data);
113 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
114 for (list = option_value; list; list = list->next)
115 printf("domain-name-servers %s\n", (char *) list->data);
117 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
118 for (list = option_value; list; list = list->next)
119 printf("domain-name %s\n", (char *) list->data);
121 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
122 for (list = option_value; list; list = list->next)
123 printf("routers %s\n", (char *) list->data);
125 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
126 for (list = option_value; list; list = list->next)
127 printf("hostname %s\n", (char *) list->data);
130 int main(int argc, char *argv[])
133 GDHCPClientError error;
134 GDHCPClient *dhcp_client;
138 printf("Usage: dhcp-test <interface index>\n");
142 index = atoi(argv[1]);
144 printf("Create DHCP client for interface %d\n", index);
146 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
152 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
154 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
155 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
156 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
157 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
158 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
159 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
161 g_dhcp_client_register_event(dhcp_client,
162 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
163 lease_available_cb, NULL);
165 g_dhcp_client_register_event(dhcp_client,
166 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
168 main_loop = g_main_loop_new(NULL, FALSE);
170 printf("Start DHCP operation\n");
172 timer = g_timer_new();
174 g_dhcp_client_start(dhcp_client, NULL);
176 memset(&sa, 0, sizeof(sa));
177 sa.sa_handler = sig_term;
178 sigaction(SIGINT, &sa, NULL);
179 sigaction(SIGTERM, &sa, NULL);
181 __connman_util_init();
183 g_main_loop_run(main_loop);
185 __connman_util_cleanup();
187 g_timer_destroy(timer);
189 g_dhcp_client_unref(dhcp_client);
191 g_main_loop_unref(main_loop);