5 * Copyright (C) 2007-2010 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>
36 #include <linux/if_arp.h>
42 static GMainLoop *main_loop = NULL;
44 static void sig_term(int sig)
46 g_main_loop_quit(main_loop);
49 static void print_timestamp(void)
55 g_get_current_time(×tamp);
56 str = g_time_val_to_iso8601(×tamp);
57 printf("=== %s ===\n", str);
60 elapsed = g_timer_elapsed(timer, NULL);
62 printf("elapsed: %f seconds\n", elapsed);
65 static void handle_error(GDHCPClientError error)
68 case G_DHCP_CLIENT_ERROR_NONE:
69 printf("dhcp client ok\n");
71 case G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE:
72 printf("Interface unavailable\n");
74 case G_DHCP_CLIENT_ERROR_INTERFACE_IN_USE:
75 printf("Interface in use\n");
77 case G_DHCP_CLIENT_ERROR_INTERFACE_DOWN:
78 printf("Interface down\n");
80 case G_DHCP_CLIENT_ERROR_NOMEM:
81 printf("No memory\n");
83 case G_DHCP_CLIENT_ERROR_INVALID_INDEX:
84 printf("Invalid index\n");
86 case G_DHCP_CLIENT_ERROR_INVALID_OPTION:
87 printf("Invalid option\n");
92 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
94 printf("No Lease Available!\n");
98 g_main_loop_quit(main_loop);
101 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
103 GList *list, *option_value = NULL;
106 address = g_dhcp_client_get_address(dhcp_client);
107 printf("address %s\n", address);
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);
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);
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);
126 int main(int argc, char *argv[])
129 GDHCPClientError error;
130 GDHCPClient *dhcp_client;
134 printf("Usage: dhcp-test <interface index>\n");
138 index = atoi(argv[1]);
140 printf("Create DHCP client for interface %d\n", index);
142 timer = g_timer_new();
144 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
145 if (dhcp_client == NULL) {
150 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
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);
158 g_dhcp_client_register_event(dhcp_client,
159 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
160 lease_available_cb, NULL);
162 g_dhcp_client_register_event(dhcp_client,
163 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
165 main_loop = g_main_loop_new(NULL, FALSE);
169 printf("Start DHCP operation\n");
171 g_dhcp_client_start(dhcp_client);
173 memset(&sa, 0, sizeof(sa));
174 sa.sa_handler = sig_term;
175 sigaction(SIGINT, &sa, NULL);
176 sigaction(SIGTERM, &sa, NULL);
178 g_main_loop_run(main_loop);
180 g_dhcp_client_unref(dhcp_client);
182 g_main_loop_unref(main_loop);