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>
38 #include <gdhcp/gdhcp.h>
42 static GMainLoop *main_loop;
44 static void sig_term(int sig)
46 g_main_loop_quit(main_loop);
49 static void print_elapsed(void)
53 elapsed = g_timer_elapsed(timer, NULL);
55 printf("elapsed: %f seconds\n", elapsed);
58 static void handle_error(GDHCPClientError error)
61 case G_DHCP_CLIENT_ERROR_NONE:
62 printf("dhcp client ok\n");
64 case G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE:
65 printf("Interface unavailable\n");
67 case G_DHCP_CLIENT_ERROR_INTERFACE_IN_USE:
68 printf("Interface in use\n");
70 case G_DHCP_CLIENT_ERROR_INTERFACE_DOWN:
71 printf("Interface down\n");
73 case G_DHCP_CLIENT_ERROR_NOMEM:
74 printf("No memory\n");
76 case G_DHCP_CLIENT_ERROR_INVALID_INDEX:
77 printf("Invalid index\n");
79 case G_DHCP_CLIENT_ERROR_INVALID_OPTION:
80 printf("Invalid option\n");
85 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
89 printf("No lease available\n");
91 g_main_loop_quit(main_loop);
94 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
96 GList *list, *option_value = NULL;
101 printf("Lease available\n");
103 address = g_dhcp_client_get_address(dhcp_client);
104 printf("address %s\n", address);
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);
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);
116 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
117 for (list = option_value; list; list = list->next)
118 printf("domain-name %s\n", (char *) list->data);
120 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
121 for (list = option_value; list; list = list->next)
122 printf("routers %s\n", (char *) list->data);
124 option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
125 for (list = option_value; list; list = list->next)
126 printf("hostname %s\n", (char *) list->data);
129 int main(int argc, char *argv[])
132 GDHCPClientError error;
133 GDHCPClient *dhcp_client;
137 printf("Usage: dhcp-test <interface index>\n");
141 index = atoi(argv[1]);
143 printf("Create DHCP client for interface %d\n", index);
145 dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
146 if (dhcp_client == NULL) {
151 g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
153 g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
154 g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
155 g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
156 g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
157 g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
158 g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
160 g_dhcp_client_register_event(dhcp_client,
161 G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
162 lease_available_cb, NULL);
164 g_dhcp_client_register_event(dhcp_client,
165 G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
167 main_loop = g_main_loop_new(NULL, FALSE);
169 printf("Start DHCP operation\n");
171 timer = g_timer_new();
173 g_dhcp_client_start(dhcp_client, NULL);
175 memset(&sa, 0, sizeof(sa));
176 sa.sa_handler = sig_term;
177 sigaction(SIGINT, &sa, NULL);
178 sigaction(SIGTERM, &sa, NULL);
180 g_main_loop_run(main_loop);
182 g_timer_destroy(timer);
184 g_dhcp_client_unref(dhcp_client);
186 g_main_loop_unref(main_loop);