blob: 16c369b81430982f8913021535cb8f2e37b3aac0 [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
San Mehatc4a895b2009-06-23 21:10:57 -070017#include <errno.h>
18#include <sys/socket.h>
19#include <netinet/in.h>
20#include <arpa/inet.h>
21
San Mehat54962e02009-06-22 10:37:54 -070022#define LOG_TAG "DhcpListener"
23#include <cutils/log.h>
24
25#include <DhcpListener.h>
26#include "IDhcpEventHandlers.h"
San Mehatc4a895b2009-06-23 21:10:57 -070027#include "DhcpState.h"
28#include "DhcpEvent.h"
29#include "Controller.h"
San Mehat54962e02009-06-22 10:37:54 -070030
San Mehatc4a895b2009-06-23 21:10:57 -070031DhcpListener::DhcpListener(Controller *c, int socket, IDhcpEventHandlers *handlers) :
32 SocketListener(socket, false) {
San Mehat54962e02009-06-22 10:37:54 -070033 mHandlers = handlers;
San Mehatc4a895b2009-06-23 21:10:57 -070034 mController = c;
San Mehat54962e02009-06-22 10:37:54 -070035}
36
37DhcpListener::~DhcpListener() {
38}
39
40bool DhcpListener::onDataAvailable(SocketClient *cli) {
San Mehatc4a895b2009-06-23 21:10:57 -070041 char buffer[255];
42 int rc;
43
44 if ((rc = read(cli->getSocket(), buffer, sizeof(buffer))) < 0) {
Steve Blockae8b56c2012-01-05 22:25:38 +000045 ALOGW("Error reading dhcp status msg (%s)", strerror(errno));
San Mehatc4a895b2009-06-23 21:10:57 -070046 return true;
47 }
48
49 if (!strncmp(buffer, "STATE:", 6)) {
50 char *next = buffer;
51 char *tmp;
52 int i;
53
54 for (i = 0; i < 2; i++) {
55 if (!(tmp = strsep(&next, ":"))) {
Steve Blockae8b56c2012-01-05 22:25:38 +000056 ALOGW("Error parsing state '%s'", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -070057 return true;
58 }
59 }
60
61 int st = DhcpState::parseString(tmp);
62 mHandlers->onDhcpStateChanged(mController, st);
63 } else if (!strncmp(buffer, "ADDRINFO:", 9)) {
64 char *next = buffer + 9;
65 struct in_addr ipaddr, netmask, gateway, broadcast, dns1, dns2;
66
67 if (!inet_aton(strsep(&next, ":"), &ipaddr)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000068 ALOGW("Malformatted IP specified");
San Mehatc4a895b2009-06-23 21:10:57 -070069 }
70 if (!inet_aton(strsep(&next, ":"), &netmask)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000071 ALOGW("Malformatted netmask specified");
San Mehatc4a895b2009-06-23 21:10:57 -070072 }
73 if (!inet_aton(strsep(&next, ":"), &broadcast)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000074 ALOGW("Malformatted broadcast specified");
San Mehatc4a895b2009-06-23 21:10:57 -070075 }
76 if (!inet_aton(strsep(&next, ":"), &gateway)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000077 ALOGW("Malformatted gateway specified");
San Mehatc4a895b2009-06-23 21:10:57 -070078 }
79 if (!inet_aton(strsep(&next, ":"), &dns1)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000080 ALOGW("Malformatted dns1 specified");
San Mehatc4a895b2009-06-23 21:10:57 -070081 }
82 if (!inet_aton(strsep(&next, ":"), &dns2)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000083 ALOGW("Malformatted dns2 specified");
San Mehatc4a895b2009-06-23 21:10:57 -070084 }
85 mHandlers->onDhcpLeaseUpdated(mController, &ipaddr, &netmask,
86 &broadcast, &gateway, &dns1, &dns2);
87
88 } else if (!strncmp(buffer, "EVENT:", 6)) {
89 char *next = buffer;
90 char *tmp;
91 int i;
92
93 for (i = 0; i < 2; i++) {
94 if (!(tmp = strsep(&next, ":"))) {
Steve Blockae8b56c2012-01-05 22:25:38 +000095 ALOGW("Error parsing event '%s'", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -070096 return true;
97 }
98 }
99
100 int ev = DhcpEvent::parseString(tmp);
101 mHandlers->onDhcpEvent(mController, ev);
102
103 } else {
Steve Blockae8b56c2012-01-05 22:25:38 +0000104 ALOGW("Unknown DHCP monitor msg '%s'", buffer);
San Mehatc4a895b2009-06-23 21:10:57 -0700105 }
106
San Mehat54962e02009-06-22 10:37:54 -0700107 return true;
108}