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
30 #include <gdhcp/gdhcp.h>
32 #include "../src/connman.h"
34 static GMainLoop *main_loop;
36 static void sig_term(int sig)
38 g_main_loop_quit(main_loop);
41 static void handle_error(GDHCPServerError error)
44 case G_DHCP_SERVER_ERROR_NONE:
45 printf("dhcp server ok\n");
47 case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
48 printf("Interface unavailable\n");
50 case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
51 printf("Interface in use\n");
53 case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
54 printf("Interface down\n");
56 case G_DHCP_SERVER_ERROR_NOMEM:
57 printf("No memory\n");
59 case G_DHCP_SERVER_ERROR_INVALID_INDEX:
60 printf("Invalid index\n");
62 case G_DHCP_SERVER_ERROR_INVALID_OPTION:
63 printf("Invalid option\n");
65 case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
66 printf("Invalid address\n");
71 static void dhcp_debug(const char *str, void *data)
73 printf("%s: %s\n", (const char *) data, str);
77 int main(int argc, char *argv[])
80 GDHCPServerError error;
81 GDHCPServer *dhcp_server;
85 printf("Usage: dhcp-server-test <interface index>\n");
89 index = atoi(argv[1]);
91 printf("Create DHCP server for interface %d\n", index);
93 dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
99 g_dhcp_server_set_debug(dhcp_server, dhcp_debug, "DHCP");
101 g_dhcp_server_set_lease_time(dhcp_server, 3600);
102 g_dhcp_server_set_option(dhcp_server, G_DHCP_SUBNET, "255.255.0.0");
103 g_dhcp_server_set_option(dhcp_server, G_DHCP_ROUTER, "192.168.0.2");
104 g_dhcp_server_set_option(dhcp_server, G_DHCP_DNS_SERVER, "192.168.0.3");
105 g_dhcp_server_set_ip_range(dhcp_server, "192.168.0.101",
107 main_loop = g_main_loop_new(NULL, FALSE);
109 printf("Start DHCP Server operation\n");
111 g_dhcp_server_start(dhcp_server);
113 memset(&sa, 0, sizeof(sa));
114 sa.sa_handler = sig_term;
115 sigaction(SIGINT, &sa, NULL);
116 sigaction(SIGTERM, &sa, NULL);
118 __connman_util_init();
120 g_main_loop_run(main_loop);
122 __connman_util_cleanup();
124 g_dhcp_server_unref(dhcp_server);
126 g_main_loop_unref(main_loop);