DA: Skip initializing failed_bssids list when eapol failure case
[platform/upstream/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 #include "../src/connman.h"
33
34 static GMainLoop *main_loop;
35
36 static void sig_term(int sig)
37 {
38         g_main_loop_quit(main_loop);
39 }
40
41 static void handle_error(GDHCPServerError error)
42 {
43         switch (error) {
44         case G_DHCP_SERVER_ERROR_NONE:
45                 printf("dhcp server ok\n");
46                 break;
47         case G_DHCP_SERVER_ERROR_INTERFACE_UNAVAILABLE:
48                 printf("Interface unavailable\n");
49                 break;
50         case G_DHCP_SERVER_ERROR_INTERFACE_IN_USE:
51                 printf("Interface in use\n");
52                 break;
53         case G_DHCP_SERVER_ERROR_INTERFACE_DOWN:
54                 printf("Interface down\n");
55                 break;
56         case G_DHCP_SERVER_ERROR_NOMEM:
57                 printf("No memory\n");
58                 break;
59         case G_DHCP_SERVER_ERROR_INVALID_INDEX:
60                 printf("Invalid index\n");
61                 break;
62         case G_DHCP_SERVER_ERROR_INVALID_OPTION:
63                 printf("Invalid option\n");
64                 break;
65         case G_DHCP_SERVER_ERROR_IP_ADDRESS_INVALID:
66                 printf("Invalid address\n");
67                 break;
68         }
69 }
70
71 static void dhcp_debug(const char *str, void *data)
72 {
73         printf("%s: %s\n", (const char *) data, str);
74 }
75
76
77 int main(int argc, char *argv[])
78 {
79         struct sigaction sa;
80         GDHCPServerError error;
81         GDHCPServer *dhcp_server;
82         int index;
83
84         if (argc < 2) {
85                 printf("Usage: dhcp-server-test <interface index>\n");
86                 exit(0);
87         }
88
89         index = atoi(argv[1]);
90
91         printf("Create DHCP server for interface %d\n", index);
92
93         dhcp_server = g_dhcp_server_new(G_DHCP_IPV4, index, &error);
94         if (!dhcp_server) {
95                 handle_error(error);
96                 exit(0);
97         }
98
99         g_dhcp_server_set_debug(dhcp_server, dhcp_debug, "DHCP");
100
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",
106                                                         "192.168.0.102");
107         main_loop = g_main_loop_new(NULL, FALSE);
108
109         printf("Start DHCP Server operation\n");
110
111         g_dhcp_server_start(dhcp_server);
112
113         memset(&sa, 0, sizeof(sa));
114         sa.sa_handler = sig_term;
115         sigaction(SIGINT, &sa, NULL);
116         sigaction(SIGTERM, &sa, NULL);
117
118         __connman_util_init();
119
120         g_main_loop_run(main_loop);
121
122         __connman_util_cleanup();
123
124         g_dhcp_server_unref(dhcp_server);
125
126         g_main_loop_unref(main_loop);
127
128         return 0;
129 }