Remove wrong rules from connman dbus conf
[platform/upstream/connman.git] / tools / dhcp-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 <errno.h>
28 #include <unistd.h>
29 #include <stdlib.h>
30 #include <string.h>
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
37 #include <gdhcp/gdhcp.h>
38
39 #include "../src/connman.h"
40
41 static GTimer *timer;
42
43 static GMainLoop *main_loop;
44
45 static void sig_term(int sig)
46 {
47         g_main_loop_quit(main_loop);
48 }
49
50 static void print_elapsed(void)
51 {
52         gdouble elapsed;
53
54         elapsed = g_timer_elapsed(timer, NULL);
55
56         printf("elapsed: %f seconds\n", elapsed);
57 }
58
59 static void handle_error(GDHCPClientError error)
60 {
61         switch (error) {
62         case G_DHCP_CLIENT_ERROR_NONE:
63                 printf("dhcp client ok\n");
64                 break;
65         case G_DHCP_CLIENT_ERROR_INTERFACE_UNAVAILABLE:
66                 printf("Interface unavailable\n");
67                 break;
68         case G_DHCP_CLIENT_ERROR_INTERFACE_IN_USE:
69                 printf("Interface in use\n");
70                 break;
71         case G_DHCP_CLIENT_ERROR_INTERFACE_DOWN:
72                 printf("Interface down\n");
73                 break;
74         case G_DHCP_CLIENT_ERROR_NOMEM:
75                 printf("No memory\n");
76                 break;
77         case G_DHCP_CLIENT_ERROR_INVALID_INDEX:
78                 printf("Invalid index\n");
79                 break;
80         case G_DHCP_CLIENT_ERROR_INVALID_OPTION:
81                 printf("Invalid option\n");
82                 break;
83         }
84 }
85
86 static void no_lease_cb(GDHCPClient *dhcp_client, gpointer user_data)
87 {
88         print_elapsed();
89
90         printf("No lease available\n");
91
92         g_main_loop_quit(main_loop);
93 }
94
95 static void lease_available_cb(GDHCPClient *dhcp_client, gpointer user_data)
96 {
97         GList *list, *option_value = NULL;
98         char *address;
99
100         print_elapsed();
101
102         printf("Lease available\n");
103
104         address = g_dhcp_client_get_address(dhcp_client);
105         printf("address %s\n", address);
106         if (!address)
107                 return;
108
109         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_SUBNET);
110         for (list = option_value; list; list = list->next)
111                 printf("sub-mask %s\n", (char *) list->data);
112
113         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DNS_SERVER);
114         for (list = option_value; list; list = list->next)
115                 printf("domain-name-servers %s\n", (char *) list->data);
116
117         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_DOMAIN_NAME);
118         for (list = option_value; list; list = list->next)
119                 printf("domain-name %s\n", (char *) list->data);
120
121         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_ROUTER);
122         for (list = option_value; list; list = list->next)
123                 printf("routers %s\n", (char *) list->data);
124
125         option_value = g_dhcp_client_get_option(dhcp_client, G_DHCP_HOST_NAME);
126         for (list = option_value; list; list = list->next)
127                 printf("hostname %s\n", (char *) list->data);
128 }
129
130 int main(int argc, char *argv[])
131 {
132         struct sigaction sa;
133         GDHCPClientError error;
134         GDHCPClient *dhcp_client;
135         int index;
136
137         if (argc < 2) {
138                 printf("Usage: dhcp-test <interface index>\n");
139                 exit(0);
140         }
141
142         index = atoi(argv[1]);
143
144         printf("Create DHCP client for interface %d\n", index);
145
146         dhcp_client = g_dhcp_client_new(G_DHCP_IPV4, index, &error);
147         if (!dhcp_client) {
148                 handle_error(error);
149                 exit(0);
150         }
151
152         g_dhcp_client_set_send(dhcp_client, G_DHCP_HOST_NAME, "<hostname>");
153
154         g_dhcp_client_set_request(dhcp_client, G_DHCP_HOST_NAME);
155         g_dhcp_client_set_request(dhcp_client, G_DHCP_SUBNET);
156         g_dhcp_client_set_request(dhcp_client, G_DHCP_DNS_SERVER);
157         g_dhcp_client_set_request(dhcp_client, G_DHCP_DOMAIN_NAME);
158         g_dhcp_client_set_request(dhcp_client, G_DHCP_NTP_SERVER);
159         g_dhcp_client_set_request(dhcp_client, G_DHCP_ROUTER);
160
161         g_dhcp_client_register_event(dhcp_client,
162                         G_DHCP_CLIENT_EVENT_LEASE_AVAILABLE,
163                                                 lease_available_cb, NULL);
164
165         g_dhcp_client_register_event(dhcp_client,
166                         G_DHCP_CLIENT_EVENT_NO_LEASE, no_lease_cb, NULL);
167
168         main_loop = g_main_loop_new(NULL, FALSE);
169
170         printf("Start DHCP operation\n");
171
172         timer = g_timer_new();
173
174         g_dhcp_client_start(dhcp_client, NULL);
175
176         memset(&sa, 0, sizeof(sa));
177         sa.sa_handler = sig_term;
178         sigaction(SIGINT, &sa, NULL);
179         sigaction(SIGTERM, &sa, NULL);
180
181         __connman_util_init();
182
183         g_main_loop_run(main_loop);
184
185         __connman_util_cleanup();
186
187         g_timer_destroy(timer);
188
189         g_dhcp_client_unref(dhcp_client);
190
191         g_main_loop_unref(main_loop);
192
193         return 0;
194 }