2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <sys/types.h>
20 #include <sys/socket.h>
21 #include <arpa/inet.h>
24 #define LOG_TAG "DhcpClient"
25 #include <cutils/log.h>
26 #include <cutils/properties.h>
28 #include <sysutils/ServiceManager.h>
30 #include "DhcpClient.h"
31 #include "DhcpState.h"
32 #include "DhcpListener.h"
33 #include "IDhcpEventHandlers.h"
34 #include "Controller.h"
37 int ifc_disable(const char *ifname);
38 int ifc_add_host_route(const char *ifname, uint32_t addr);
39 int ifc_remove_host_routes(const char *ifname);
40 int ifc_set_default_route(const char *ifname, uint32_t gateway);
41 int ifc_get_default_route(const char *ifname);
42 int ifc_remove_default_route(const char *ifname);
43 int ifc_reset_connections(const char *ifname);
44 int 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);
46 int dhcp_do_request(const char *ifname,
54 int dhcp_stop(const char *ifname);
55 int dhcp_release_lease(const char *ifname);
56 char *dhcp_get_errmsg();
59 DhcpClient::DhcpClient(IDhcpEventHandlers *handlers) :
60 mState(DhcpState::INIT), mHandlers(handlers) {
61 mServiceManager = new ServiceManager();
63 mListenerSocket = NULL;
66 pthread_mutex_init(&mLock, NULL);
69 DhcpClient::~DhcpClient() {
70 delete mServiceManager;
75 int DhcpClient::start(Controller *c) {
76 LOGD("Starting DHCP service (arp probe = %d)", mDoArpProbe);
77 char svc[PROPERTY_VALUE_MAX];
81 (!mDoArpProbe ? "-A " : ""),
82 c->getBoundInterface());
84 pthread_mutex_lock(&mLock);
87 pthread_mutex_unlock(&mLock);
94 if ((mListenerSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) {
95 LOGE("Failed to create DHCP listener socket");
96 pthread_mutex_unlock(&mLock);
99 memset(&addr, 0, sizeof(addr));
100 addr.sin_family = AF_INET;
101 addr.sin_addr.s_addr = inet_addr("127.0.0.1");
102 addr.sin_port = htons(DhcpClient::STATUS_MONITOR_PORT);
104 if (bind(mListenerSocket, (struct sockaddr *) &addr, sizeof(addr))) {
105 LOGE("Failed to bind DHCP listener socket");
106 close(mListenerSocket);
107 mListenerSocket = -1;
108 pthread_mutex_unlock(&mLock);
112 if (mServiceManager->start(svc)) {
113 LOGE("Failed to start dhcp service");
114 pthread_mutex_unlock(&mLock);
118 mListener = new DhcpListener(mController, mListenerSocket, mHandlers);
119 if (mListener->startListener()) {
120 LOGE("Failed to start listener");
122 mServiceManager->stop("dhcpcd");
127 pthread_mutex_unlock(&mLock);
130 pthread_mutex_unlock(&mLock);
134 int DhcpClient::stop() {
135 pthread_mutex_lock(&mLock);
137 pthread_mutex_unlock(&mLock);
142 mListener->stopListener();
146 close(mListenerSocket);
148 if (mServiceManager->stop("dhcpcd")) {
149 LOGW("Failed to stop DHCP service (%s)", strerror(errno));
150 // XXX: Kill it the hard way.. but its gotta go!
154 pthread_mutex_unlock(&mLock);
158 void DhcpClient::setDoArpProbe(bool probe) {