wifi: Add support for autoscan request
[framework/connectivity/connman.git] / tools / dhcp-server-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2012  Intel Corporation. All rights reserved.
6  *
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.
10  *
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.
15  *
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
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29
30 #include <gdhcp/gdhcp.h>
31
32 static GMainLoop *main_loop;
33
34 static void sig_term(int sig)
35 {
36         g_main_loop_quit(main_loop);
37 }
38
39 static void handle_error(GDHCPServerError error)
40 {
41         switch (error) {
42         case G_DHCP_SERVER_ERROR_NONE:
43                 printf("dhcp server ok\n");
44                 break;
45         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
46                 printf("Interface unavailable\n");
47                 break;
48         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
49                 printf("Interface in use\n");
50                 break;
51         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
52                 printf("Interface down\n");
53                 break;
54         case G_DHCP_SERVER_ERROR_NOMEM:
55                 printf("No memory\n");
56                 break;
57         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
58                 printf("Invalid index\n");
59                 break;
60         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
61                 printf("Invalid option\n");
62                 break;
63         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
64                 printf("Invalid address\n");
65                 break;
66         }
67 }
68
69 static void dhcp_debug(const char *str, void *data)
70 {
71         printf("%s: %s\n", (const char *) data, str);
72 }
73
74
75 int main(int argc, char *argv[])
76 {
77         struct sigaction sa;
78         GDHCPServerError error;
79         GDHCPServer *dhcp_server;
80         int index;
81
82         if (argc < 2) {
83                 printf("Usage: dhcp-server-test <interface index>\n");
84                 exit(0);
85         }
86
87         index = atoi(argv[1]);
88
89         printf("Create DHCP server for interface %d\n", index);
90
91         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
92         if (dhcp_server == NULL) {
93                 handle_error(error);
94                 exit(0);
95         }
96
97         g_dhcp_server_set_debug(dhcp_server, dhcp_debug, "DHCP");
98
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",
104                                                         "192.168.0.102");
105         main_loop = g_main_loop_new(NULL, FALSE);
106
107         printf("Start DHCP Server operation\n");
108
109         g_dhcp_server_start(dhcp_server);
110
111         memset(&sa, 0, sizeof(sa));
112         sa.sa_handler = sig_term;
113         sigaction(SIGINT, &sa, NULL);
114         sigaction(SIGTERM, &sa, NULL);
115
116         g_main_loop_run(main_loop);
117
118         g_dhcp_server_unref(dhcp_server);
119
120         g_main_loop_unref(main_loop);
121
122         return 0;
123 }