blob: 2bd9c68f15d5c9914f380ccbad9f177fd88750a7 [file] [log] [blame]
San Mehat54962e02009-06-22 10:37:54 -07001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <errno.h>
19#include <sys/types.h>
20#include <arpa/inet.h>
21
22#define LOG_TAG "DhcpClient"
23#include <cutils/log.h>
24#include <cutils/properties.h>
25
26#include <sysutils/ServiceManager.h>
27
28#include "DhcpClient.h"
29#include "DhcpState.h"
30#include "DhcpListener.h"
31#include "IDhcpEventHandlers.h"
32
33extern "C" {
34int ifc_disable(const char *ifname);
35int ifc_add_host_route(const char *ifname, uint32_t addr);
36int ifc_remove_host_routes(const char *ifname);
37int ifc_set_default_route(const char *ifname, uint32_t gateway);
38int ifc_get_default_route(const char *ifname);
39int ifc_remove_default_route(const char *ifname);
40int ifc_reset_connections(const char *ifname);
41int ifc_configure(const char *ifname, in_addr_t ipaddr, in_addr_t netmask, in_addr_t gateway, in_addr_t dns1, in_addr_t dns2);
42
43int dhcp_do_request(const char *ifname,
44 in_addr_t *ipaddr,
45 in_addr_t *gateway,
46 in_addr_t *mask,
47 in_addr_t *dns1,
48 in_addr_t *dns2,
49 in_addr_t *server,
50 uint32_t *lease);
51int dhcp_stop(const char *ifname);
52int dhcp_release_lease(const char *ifname);
53char *dhcp_get_errmsg();
54}
55
56DhcpClient::DhcpClient(IDhcpEventHandlers *handlers) :
57 mState(DhcpState::STOPPED), mHandlers(handlers) {
58 mServiceManager = new ServiceManager();
59 mListener = NULL;
60}
61
62DhcpClient::~DhcpClient() {
63 delete mServiceManager;
64 if (mListener)
65 delete mListener;
66}
67
68int DhcpClient::start(const char *interface) {
69
70 char svc[PROPERTY_VALUE_MAX];
71 snprintf(svc, sizeof(svc), "dhcpcd_ng:%s", interface);
72
73 if (mServiceManager->start(svc)) {
74 LOGE("Failed to start dhcp service");
75 return -1;
76 }
77
78 mListener = new DhcpListener(mHandlers);
79 if (mListener->startListener()) {
80 LOGE("Failed to start listener");
81#if 0
82 mServiceManager->stop("dhcpcd_ng");
83 return -1;
84#endif
85 delete mListener;
86 mListener = NULL;
87 }
88
89 mState = DhcpState::STARTED;
90
91 return 0;
92}
93
94int DhcpClient::stop() {
95 if (mListener) {
96 mListener->stopListener();
97 delete mListener;
98 mListener = NULL;
99 }
100
101 if (mServiceManager->stop("dhcpcd_ng"))
102 LOGW("Failed to stop DHCP service (%s)", strerror(errno));
103 mState = DhcpState::STOPPED;
104 return 0;
105}