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
30 #include <gdhcp/gdhcp.h>
32 static GMainLoop *main_loop;
34 static void sig_term(int sig)
36 g_main_loop_quit(main_loop);
39 static void handle_error(GDHCPServerError error)
42 case G_DHCP_SERVER_ERROR_NONE:
43 printf("dhcp server ok\n");
45 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
46 printf("Interface unavailable\n");
48 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
49 printf("Interface in use\n");
51 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
52 printf("Interface down\n");
54 case G_DHCP_SERVER_ERROR_NOMEM:
55 printf("No memory\n");
57 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
58 printf("Invalid index\n");
60 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
61 printf("Invalid option\n");
63 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
64 printf("Invalid address\n");
69 static void dhcp_debug(const char *str, void *data)
71 printf("%s: %s\n", (const char *) data, str);
75 int main(int argc, char *argv[])
78 GDHCPServerError error;
79 GDHCPServer *dhcp_server;
83 printf("Usage: dhcp-server-test <interface index>\n");
87 index = atoi(argv[1]);
89 printf("Create DHCP server for interface %d\n", index);
91 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
92 if (dhcp_server == NULL) {
97 g_dhcp_server_set_debug(dhcp_server, dhcp_debug, "DHCP");
99 g_dhcp_server_set_lease_time(dhcp_server, 3600);
100 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, "255.255.0.0");
101 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, "192.168.0.2");
102 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, "192.168.0.3");
103 g_dhcp_server_set_ip_range(dhcp_server, "192.168.0.101",
105 main_loop = g_main_loop_new(NULL, FALSE);
107 printf("Start DHCP Server operation\n");
109 g_dhcp_server_start(dhcp_server);
111 memset(&sa, 0, sizeof(sa));
112 sa.sa_handler = sig_term;
113 sigaction(SIGINT, &sa, NULL);
114 sigaction(SIGTERM, &sa, NULL);
116 g_main_loop_run(main_loop);
118 g_dhcp_server_unref(dhcp_server);
120 g_main_loop_unref(main_loop);