San Mehat | 54962e0 | 2009-06-22 10:37:54 -0700 | [diff] [blame] | 1 | /* |
| 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 Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 17 | #include <errno.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <netinet/in.h> |
| 20 | #include <arpa/inet.h> |
| 21 | |
San Mehat | 54962e0 | 2009-06-22 10:37:54 -0700 | [diff] [blame] | 22 | #define LOG_TAG "DhcpListener" |
| 23 | #include <cutils/log.h> |
| 24 | |
| 25 | #include <DhcpListener.h> |
| 26 | #include "IDhcpEventHandlers.h" |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 27 | #include "DhcpState.h" |
| 28 | #include "DhcpEvent.h" |
| 29 | #include "Controller.h" |
San Mehat | 54962e0 | 2009-06-22 10:37:54 -0700 | [diff] [blame] | 30 | |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 31 | DhcpListener::DhcpListener(Controller *c, int socket, IDhcpEventHandlers *handlers) : |
| 32 | SocketListener(socket, false) { |
San Mehat | 54962e0 | 2009-06-22 10:37:54 -0700 | [diff] [blame] | 33 | mHandlers = handlers; |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 34 | mController = c; |
San Mehat | 54962e0 | 2009-06-22 10:37:54 -0700 | [diff] [blame] | 35 | } |
| 36 | |
| 37 | DhcpListener::~DhcpListener() { |
| 38 | } |
| 39 | |
| 40 | bool DhcpListener::onDataAvailable(SocketClient *cli) { |
San Mehat | c4a895b | 2009-06-23 21:10:57 -0700 | [diff] [blame] | 41 | char buffer[255]; |
| 42 | int rc; |
| 43 | |
| 44 | if ((rc = read(cli->getSocket(), buffer, sizeof(buffer))) < 0) { |
| 45 | LOGW("Error reading dhcp status msg (%s)", strerror(errno)); |
| 46 | 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, ":"))) { |
| 56 | LOGW("Error parsing state '%s'", buffer); |
| 57 | 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)) { |
| 68 | LOGW("Malformatted IP specified"); |
| 69 | } |
| 70 | if (!inet_aton(strsep(&next, ":"), &netmask)) { |
| 71 | LOGW("Malformatted netmask specified"); |
| 72 | } |
| 73 | if (!inet_aton(strsep(&next, ":"), &broadcast)) { |
| 74 | LOGW("Malformatted broadcast specified"); |
| 75 | } |
| 76 | if (!inet_aton(strsep(&next, ":"), &gateway)) { |
| 77 | LOGW("Malformatted gateway specified"); |
| 78 | } |
| 79 | if (!inet_aton(strsep(&next, ":"), &dns1)) { |
| 80 | LOGW("Malformatted dns1 specified"); |
| 81 | } |
| 82 | if (!inet_aton(strsep(&next, ":"), &dns2)) { |
| 83 | LOGW("Malformatted dns2 specified"); |
| 84 | } |
| 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, ":"))) { |
| 95 | LOGW("Error parsing event '%s'", buffer); |
| 96 | return true; |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | int ev = DhcpEvent::parseString(tmp); |
| 101 | mHandlers->onDhcpEvent(mController, ev); |
| 102 | |
| 103 | } else { |
| 104 | LOGW("Unknown DHCP monitor msg '%s'", buffer); |
| 105 | } |
| 106 | |
San Mehat | 54962e0 | 2009-06-22 10:37:54 -0700 | [diff] [blame] | 107 | return true; |
| 108 | } |